树莓派加挂实时时钟芯片PCF8563模块

发布时间 2023-09-08 16:44:29作者: Mike Von

硬件:树莓派4B, PCF8563模块

树莓派系统版本:

:~ $ lsb_release -a
No LSB modules are available.
Distributor ID:    Raspbian
Description:    Raspbian GNU/Linux 10 (buster)
Release:    10
Codename:    buster

 

在开始之前需要确认自己手里的芯片是PCF8563还是PCF8583,确保后面不会用错驱动程序。

第一步:硬件连接

排针1  <-> 树莓派Pin1
排针2  <-> 树莓派Pin9
排针3  <-> 树莓派Pin3
排针4  <-> 树莓派Pin5

 

 第二步:系统配置

1. 安装i2c-tools

sudo apt-get install i2c-tools

2. 打开i2c配置

sudo nano /boot/config.txt

取消注释 dtparam=i2c_arm=on,或者直接添加这一行

3. 查看cpu版本:

:~ $ cat /proc/cpuinfo
......

Hardware    : BCM2835
Revision    : b03112
Serial        : 1000000023fda7d8
Model        : Raspberry Pi 4 Model B Rev 1.2

4. 添加树莓派启动参数 bcm2835.vc_i2c_override=1

sudo nano /boot/cmdline.txt 

 

 

 5. 添加树莓派需要加载的模块

sudo nano /etc/modules

i2c-dev
i2c-bcm2835
rtc-8563

 6. 用sudo reboot 重启后,查看该芯片的i2c设备地址sudo i2cdetect -y 1 或者  sudo i2cdetect -y 0

:~ $ 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: -- 51 -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --

从上面的结果看设备地址是0x51

7. 初始化时钟芯片的时间为当前系统时间

sudo bash
echo pcf8563 0x51 > /sys/class/i2c-adapter/i2c-1/new_device
hwclock --systohc --noadjfile --utc
exit

8. 配置开机自启设定当前系统时间

sudo nano /etc/rc.local

添加脚本:

sudo bash -c "echo pcf8563 0x51 > /sys/class/i2c-adapter/i2c-1/new_device"
sudo hwclock --hctosys

  

9. sudo reboot 完成。