RTC(i2c)

[rpi+rtc](http://yehnan.blogspot.tw/2014/01/raspberry-pirtc.html)

[Adding a Real Time Clock to your Raspberry Pi](http://thepihut.com/blogs/raspberry-pi-tutorials/17209332-adding-a-real-time-clock-to-your-raspberry-pi)


安裝i2c工具程式

pi@raspberrypi:~ $ sudo apt-get install python-smbus i2c-tools

開啟I2C介面:開始程式集/Preferences/Raspberry Pi Configuration

測試是否偵測得到i2c晶片 (先確認I2C介面是否已開啟)

pi@raspberrypi:~ $ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --

手動插入驅動程式

pi@raspberrypi:~ $ sudo modprobe i2c-bcm2708
pi@raspberrypi:~ $ sudo modprobe i2c-dev
pi@raspberrypi:~ $ sudo modprobe rtc-ds1307

提升權限並寫入設定值

pi@raspberrypi:~ $ sudo bash
root@raspberrypi:/home/pi# echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
root@raspberrypi:/home/pi# exit
exit

寫入RTC

pi@raspberrypi:~ $ sudo hwclock -r
Sat 01 Jan 2000 08:25:02 CST  -0.664135 seconds
pi@raspberrypi:~ $ date
Wed 19 Apr 14:57:02 CST 2017
pi@raspberrypi:~ $ sudo hwclock -w
pi@raspberrypi:~ $ sudo hwclock -r
Wed 19 Apr 2017 14:57:19 CST  -0.300283 seconds

同場加映

手動修改系統時間

pi@raspberrypi:~ $ date
Wed 19 Apr 15:27:04 CST 2017
pi@raspberrypi:~ $ sudo date -s "2011-01-01 01:01:01"
Sat  1 Jan 01:01:01 CST 2011
pi@raspberrypi:~ $ date
Sat  1 Jan 01:01:03 CST 2011

無法設定RTC解法:重新插入rtc-ds1307模組並提升權限並設定0x68。

pi@raspberrypi:~ $ sudo hwclock -r
hwclock: Cannot access the Hardware Clock via any known method.
hwclock: Use the --debug option to see the details of our search for an access method.

檢查驅動,重新設定

pi@raspberrypi:~ $ lsmod | grep i2c
i2c_bcm2708             4834  0 
i2c_dev                 5859  0 
pi@raspberrypi:~ $ lsmod | grep rtc
rtc_ds1307              9521  0 
pi@raspberrypi:~ $ sudo bash 
root@raspberrypi:/home/pi# echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
root@raspberrypi:/home/pi# exit
exit
pi@raspberrypi:~ $ sudo hwclock -r
Sat 01 Jan 2011 01:34:36 CST  -0.952313 seconds
pi@raspberrypi:~ $ sudo hwclock -w
pi@raspberrypi:~ $ sudo hwclock -r
Wed 19 Apr 2017 16:05:32 CST  -0.467660 seconds

猜測為開機時未加入ds1307裝置,修改rc.local為開機時自動加入ds1307

加入以下兩行:

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
sudo hwclock -s

如下:

pi@raspberrypi:~ $ sudo cat /etc/rc.local 
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
sudo hwclock -s

exit 0

results matching ""

    No results matching ""