CM3计算板安装硬件时钟DS3231

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电池对芯片备用供电。硬件连接如下:

CM3计算板安装硬件时钟DS3231_第1张图片

CM3计算板安装硬件时钟DS3231_第2张图片

2、系统注册设备

2.1 查看是否正确连接

首先查看i2c 接口是否识别到DS3231设备,使用如下命令查看是否存在id为68的设备,如果存在则i2c已经正确连接了DS3231:

sudo i2cdetect -y 1

CM3计算板安装硬件时钟DS3231_第3张图片

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      use an alternate file to /dev/rtc0
     --directisa      use the ISA bus instead of /dev/rtc0 access
     --date 

sudo hwclock -s  用RTC时间更新系统时间
sudo hwclock -w 用系统时间更新RTC时间

2.3 重启并确认是否注册成功

重启后,查看i2c-1 原来68的id已经变成UU,变成UU则说明已经注册成功。

CM3计算板安装硬件时钟DS3231_第4张图片

2.4 查看时间

date 查看系统时间,hwclock -r 读取硬件时间。

你可能感兴趣的:(Raspberry)