2017/03/21

Raspberry Pi:RTC即時時鐘PCF8523

簡單記錄一下設定作業:Adafruit的RTC即時時鐘PCF8523電路板模組。其實這篇的內容,幾乎都是來自Adafruit的文件

PCF8523屬於便宜的RTC,若想要更好的,請選用DS1307(一般)或DS3231(很好)。

我的環境:Raspberry Pi 3,Raspbian  2017-03-02。

硬體線路,參看https://pinout.xyz/的腳位功能圖:

  • 實體腳位1(3.3V),接到PCF8523模組的VCC。
  • 實體腳位6(GND),接到PCF8523模組的GND。
  • 實體腳位3(SDA),接到PCF8523模組的SDA。
  • 實體腳位5(SCL),接到PCF8523模組的SCL。
照片。
然後執行raspi-config,進入選單5 Interfacing Options - P5 I2C,開啟I2C介面。
安裝所需模組:
$ sudo apt-get install python-smbus i2c-tools

執行i2cdetect測試,PCF8523模組的I2C位址是0x68。
$ 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: -- -- -- -- -- -- -- --

編輯 /boot/config,加入底下這行:
dtoverlay=i2c-rtc,pcf8523

重開機,再次執行,此時不會看到68,因為被系統拿去了。
$ 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: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

移除原本的虛擬硬體時鐘套件,註:之後我不知道怎麼恢復。
$ sudo apt-get -y remove fake-hwclock
$ sudo update-rc.d -f fake-hwclock remove

修改/lib/udev/hwclock-set,註解掉底下這三行:
#if [ -e /run/systemd/system ] ; then
# exit 0
#fi

執行raspi-config,進入選單4 Localisation Options - I2 Change Timezone,設定時區。
Current default time zone: 'Asia/Taipei'
Local time is now:      Sun Mar 19 11:15:30 CST 2017.
Universal Time is now:  Sun Mar 19 03:15:30 UTC 2017.

執行hwclock,讀取即時時鐘的資訊:
$ sudo hwclock -D -r
hwclock from util-linux 2.25.2
Using the /dev interface to the clock.
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
/dev/rtc does not have interrupt functions. Waiting in loop for time from /dev/rtc to change
...got clock tick
Time read from Hardware Clock: 2017/03/19 03:13:31
Hw clock time : 2017/03/19 03:13:31 = 1489893211 seconds since 1969
Sun 19 Mar 2017 11:13:31 AM CST  -0.431869 seconds

rpi應會上網抓取時間日期,下指令date看看是否正確。
$ date
Sun Mar 19 11:16:04 CST 2017

把日期時間、寫入即時時鐘。
$ sudo hwclock -w

看看對不對。
$ sudo hwclock -r
Sun 19 Mar 2017 11:17:38 AM CST  -0.588629 seconds

之後重開機,就會從即時時鐘抓日期時間。


參考資料:

No comments:

Post a Comment