1、硬件連接
DS3231是高速(400kHz)I2C接口的實時時鐘芯片,0°C至+40°C範圍內精度爲±2ppm,-40°C至+85°C範圍內精度爲±3.5ppm,工作電壓3.3V,提供電池備份,具有輸入低功耗,實時時鐘產生秒、分、時、星期、日期、月和年計時,並提供有效期到2100年的閏年補償。
可以使用CM3計算板上任意的I2C接口連接DS3231,這裏使用GPIO2和GPIO3 I2C1接口連接時鐘芯片。採用CR1220電池對芯片備用供電。硬件連接如下:
2、系統註冊設備
2.1 查看是否正確連接
首先查看i2c 接口是否識別到DS3231設備,使用如下命令查看是否存在id爲68的設備,如果存在則i2c已經正確連接了DS3231:
sudo i2cdetect -y 1
2.2 設置開機註冊時鐘設備並同步
CM3開機後主動註冊DS3231,並同步時鐘時間爲系統時間。在/etc/rc.local中填入如下命令:
sudo modprobe i2c-dev
sudo modprobe i2c-bcm2708
echo ds3231 0x68 | sudo tee /sys/class/i2c-adapter/i2c-1/new_device
sudo hwclock -s
sudo hwclock -w
modprobe命令自動處理可載入模塊,可載入指定的個別模塊,或是載入一組相依的模塊會根據depmod所產生的相依關係,決定要載入哪些模塊。若在載入過程中發生錯誤,在modprobe會卸載整組的模塊。
hwclock命令用法如下:
Usage:
hwclock [function] [option...]
Time clocks utility.
Functions:
-r, --show display the RTC time
--get display drift corrected RTC time
--set set the RTC according to --date
-s, --hctosys set the system time from the RTC
-w, --systohc set the RTC from the system time
--systz send timescale configurations to the kernel
-a, --adjust adjust the RTC to account for systematic drift
--predict predict the drifted RTC time according to --date
Options:
-u, --utc the RTC timescale is UTC
-l, --localtime the RTC timescale is Local
-f, --rtc <file> use an alternate file to /dev/rtc0
--directisa use the ISA bus instead of /dev/rtc0 access
--date <time> date/time input for --set and --predict
--delay <sec> delay used when set new RTC time
--update-drift update the RTC drift factor
--noadjfile do not use /etc/adjtime
--adjfile <file> use an alternate file to /etc/adjtime
--test dry run; implies --verbose
-v, --verbose display more details
-h, --help display this help
-V, --version display version
sudo hwclock -s 用RTC時間更新系統時間
sudo hwclock -w 用系統時間更新RTC時間
2.3 重啓並確認是否註冊成功
重啓後,查看i2c-1 原來68的id已經變成UU,變成UU則說明已經註冊成功。
2.4 查看時間
date 查看系統時間,hwclock -r 讀取硬件時間。