树莓派新系统DS1307设置为系统时间(转自adafruit)

Raspbian Jessie (Systemd)

You can add support for the RTC by adding a device tree overlay. Run
编辑config.txt文件,在windows下也可以

sudo nano /boot/config.txt

to edit the pi configuration and add whichever matches your RTC chip:
添加以下内容:

dtoverlay=i2c-rtc,ds1307

to the end of the file
如下图
raspberry_pi_dtoverlay.png
Save it and run sudo reboot to start again. Log in and run sudo i2cdetect -y 1 to see the UU show up where 0x68 should be
重启,登录后运行sudo i2cdetect -y 1 可以看到如下信息:
raspberry_pi_UU.png
Disable the “fake hwclock” which interferes with the ‘real’ hwclock
关闭改动时间功能

sudo apt-get -y remove fake-hwclock
sudo update-rc.d -f fake-hwclock remove

raspberry_pi_removehwclock.png
Now with the fake-hw clock off, you can start the original ‘hardware clock’ script.
Run sudo nano /lib/udev/hwclock-set and comment out these three lines:
运行sudo nano /lib/udev/hwclock-set 注释掉以下3行如下图:

if [ -e /run/systemd/system ] ; then
 exit 0
fi

raspberry_pi_hwclock-set.png

Sync time from Pi to RTC

When you first plug in the RTC module, it’s going to have the wrong time because it has to be set once. You can always read the time directly from the RTC with sudo hwclock -D -r
运行 sudo hwclock -D -r 查看当前硬件时间
raspberry_pi_invalidclok.png
You can see, the date at first is invalid! You can set the correct time easily. First run date to verify the time is correct. Plug in Ethernet or WiFi to let the Pi sync the right time from the Internet. Once that’s done, run sudo hwclock -w to write the time, and another sudo hwclock -r to read the time
然后设置时间并同步写到DS1307硬件上

sudo hwclock -w

查看时间

sudo hwclock -r

raspberry_pi_setrtc.png
Once the time is set, make sure the coin cell battery is inserted so that the time is saved. You only have to set the time once
That’s it! Next time you boot the time will automatically be synced from the RTC module
设置完后就ok了, 以后重启断电,只要RTC上的电池有电在运行着,树莓派的时间都会同步RTC的。
哦,发现时间不对刚好慢了几小时的话记得改时区哦,具体方法请自行百(gu)度(ge)。


原文地址:https://learn.adafruit.com/adding-a-real-time-clock-to-raspberry-pi/set-rtc-time?view=all

发表评论

邮箱地址不会被公开。 必填项已用*标注