为树莓派 添加一个 实时时钟DS1302
1、在系统中安装wiringPi(这个就不多说了很简单)2、连接ds1302模块和树莓派引脚
连接前需要在ds1302的DTA引脚上添加一个10K的上拉电阻,
VCC—3.3V—-
GND—0V—-
CLK—SCLK—-23
DTA—SDA0—-24
RST—CE0—–27
3、修改程序
ds1302.c的mian函数里有一个ds1302setup()函数 ,用来设置引脚,修改如图:
int main(int argc, char *argv[])
{
int i;
int clock;
wiringPiSetup();
//ds1308setup(0,1,2);
ds1302setup(14,10,30);
if(argx == 2)
{
/**/if(strcmp(argv,"-slc") == 0)
return setLinuxClocl();
else if(strcmp(argv,"-sdsc") == 0)
return setDSclock();
else if(strcmp(argv,"-rtest") == 0)
return ramTest();
else
{
printf("Usage:ds1302[-slc|-sdsc|-rtest]\n");
return EXIT_FAILURE;
}
}
}这里的14,10,30引脚号是wPi的编号不要弄错了
在wiringPi/examples目录下有ds1302.c文件 在wiringPi/examples目录下执行make ds1302即可生产可执行文件ds1302
4、测试
4.1 执行sudo ./ds1302 -rtest
显示:
说明一切正常,否者请检查连接是否正确
4.2 sudo ./ds1302 -sdsc 即可将系统时间写进ds1302模块中去
4.3 从DS模块读取时间
执行sudo ./ds1302
4.4 从DS模块读取时间来设置系统的时间
sudo ./ds1302 -slc
5、获取当地时间 (以上获取的是UTC(Coordinated Universal Time)时间到当前时刻的秒数,要想从该秒数转换成本地时间需要用localtime()函数struct tm *localtime(const time_t *clock),)
修改ds1302.c中的setDSclock()函数
如下:
static int setDSclock (void)
{
//struct tm t ;
struct tm* t = NULL;
time_t now ;
int clock ;
printf ("Setting the clock in the DS1302 from Linux time... ") ;
now = time (NULL) ;
//gmtime_r (&now, &t) ;
t = localtime(&now);
//clock [ 0] = dToBcd (t.tm_sec) ; // seconds
//clock [ 1] = dToBcd (t.tm_min) ; // mins
//clock [ 2] = dToBcd (t.tm_hour) ; // hours
//clock [ 3] = dToBcd (t.tm_mday) ; // date
//clock [ 4] = dToBcd (t.tm_mon + 1) ; // months 0-11 --> 1-12
//clock [ 5] = dToBcd (t.tm_wday + 1) ; // weekdays (sun 0)
//clock [ 6] = dToBcd (t.tm_year - 100) ; // years
//clock [ 7] = 0 ; // W-Protect off
clock [ 0] = dToBcd (t->tm_sec) ; // seconds
clock [ 1] = dToBcd (t->tm_min) ; // mins
clock [ 2] = dToBcd (t->tm_hour) ; // hours
clock [ 3] = dToBcd (t->tm_mday) ; // date
clock [ 4] = dToBcd (t->tm_mon + 1) ; // months 0-11 --> 1-12
clock [ 5] = dToBcd (t->tm_wday + 1) ; // weekdays (sun 0)
clock [ 6] = dToBcd (t->tm_year - 100) ; // years
clock [ 7] = 0 ; // W-Protect off
ds1302clockWrite (clock) ;
printf ("OK\n %2d:%02d\n %2d:%02d\n",t->tm_hour,t->tm_min,dToBcd (t->tm_hour),dToBcd (t->tm_min) ) ;
之后重新编译make ds1302 重新执行以上指令即可获取当地时间了
相关文章:树莓派4B 更新wiringPi库到2.52的方法 如果出现报错
DS1302 RAM TEST FAILURE. 433 errors.
DS1302 RAM TEST FAILURE. 465 errors.
等错误 那就是你接了三角 并没有接vcc和gnd接地 VCC:模块的电源引脚,接树莓派的1号引脚(3.3V)
GND:模块的地,接树莓派的6号引脚(Grand)
CLK:SCLK 串行时钟,接树莓派wiringPi 0(物理编号:11号)
DAT:I/O 数据线,接树莓派wiringPi 1(物理编号:12号)
RST:复位引脚,接树莓派wiringPi 2(物理编号:13号) 七、开机同步时间将树莓派的时间与网络同步后,就可以写入到DS1302中,这样DS1302具有准确的当前时间,就算掉电也不会丢失,在树莓派离线时,可以在每次开机时将DS1302的时间写入到树莓派中,从而使得树莓派实现了实时时钟的功能。获得可执行文件ds1302的目录pi@raspberrypi:~/wiringPi/examples $ pwd
/home/pi/wiringPi/examples然后修改 /etc/rc.local 文件,在exit 0前面添加时间同步命令,这样在树莓派开机时就会自动将DS1302的时间同步到树莓派上。添加以下命令:sudo /home/pi/wiringPi/examples/ds1302 -slc保存后退出。我们将树莓派断网,然后树莓派关机,过一会开机,使用date命令读取时间,可以看到树莓派的走时和当前时间一致,说明时间自动同步成功!
解决小时无法同步问题
static int setDSclock (void)
{
//struct tm t ;
struct tm* t = NULL;
time_t now ;
int clock ;
printf ("Setting the clock in the DS1302 from Linux time... ") ;
now = time (NULL) ;
//gmtime_r (&now, &t) ;
t = localtime(&now);
//clock [ 0] = dToBcd (t.tm_sec) ; // seconds
//clock [ 1] = dToBcd (t.tm_min) ; // mins
//clock [ 2] = dToBcd (t.tm_hour) ; // hours
//clock [ 3] = dToBcd (t.tm_mday) ; // date
//clock [ 4] = dToBcd (t.tm_mon + 1) ; // months 0-11 --> 1-12
//clock [ 5] = dToBcd (t.tm_wday + 1) ; // weekdays (sun 0)
//clock [ 6] = dToBcd (t.tm_year - 100) ; // years
//clock [ 7] = 0 ; // W-Protect off
clock [ 0] = dToBcd (t->tm_sec) ; // seconds
clock [ 1] = dToBcd (t->tm_min) ; // mins
clock [ 2] = dToBcd (t->tm_hour) ; // hours
clock [ 3] = dToBcd (t->tm_mday) ; // date
clock [ 4] = dToBcd (t->tm_mon + 1) ; // months 0-11 --> 1-12
clock [ 5] = dToBcd (t->tm_wday + 1) ; // weekdays (sun 0)
clock [ 6] = dToBcd (t->tm_year - 100) ; // years
clock [ 7] = 0 ; // W-Protect off
ds1302clockWrite (clock) ;
printf ("OK\n %2d:%02d\n %2d:%02d\n",t->tm_hour,t->tm_min,dToBcd (t->tm_hour),dToBcd (t->tm_min) ) ;
gpio readall 调出接口图
+-----+-----+---------+------+---+---Pi A+--+---+------+---------+-----+-----+
| BCM | wPi | Name| Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| | | 3.3v | | |1 || 2| | | 5v | | |
| 2 | 8 | SDA.1 | IN | 1 |3 || 4| | | 5v | | |
| 3 | 9 | SCL.1 | IN | 1 |5 || 6| | | 0v | | |
| 4 | 7 | GPIO. 7 | IN | 1 |7 || 8| 1 | ALT0 | TxD | 15| 14|
| | | 0v | | |9 || 10 | 1 | ALT0 | RxD | 16| 15|
|17 | 0 | GPIO. 0 | IN | 0 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18|
|27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | |
|22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23|
| | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24|
|10 |12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | |
| 9 |13 | MISO | IN | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25|
|11 |14 | SCLK |OUT | 0 | 23 || 24 | 1 | IN | CE0 | 10| 8 |
| | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11| 7 |
| 0 |30 | SDA.0 |OUT | 0 | 27 || 28 | 1 | IN | SCL.0 | 31| 1 |
| 5 |21 | GPIO.21 | IN | 1 | 29 || 30 | | | 0v | | |
| 6 |22 | GPIO.22 | IN | 1 | 31 || 32 | 0 | IN | GPIO.26 | 26| 12|
|13 |23 | GPIO.23 | IN | 0 | 33 || 34 | | | 0v | | |
|19 |24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27| 16|
|26 |25 | GPIO.25 | IN | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28| 20|
| | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29| 21|
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi | Name| Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+---Pi A+--+---+------+---------+-----+-----+
页:
[1]