-
Notifications
You must be signed in to change notification settings - Fork 1
/
ds3231test.c
60 lines (52 loc) · 1.42 KB
/
ds3231test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <bcm2835.h>
#include <stdio.h>
#include <unistd.h>
//regaddr,seconds,minutes,hours,weekdays,days,months,yeas
char buf[]={0x00,0x00,0x43,0x15,0x05,0x01,0x03,0x19};
char *str[] ={"SUN","Mon","Tues","Wed","Thur","Fri","Sat"};
void pcf8563SetTime(uint8_t * buf)
{
bcm2835_i2c_write(buf,8);
}
void pcf8563ReadTime(uint8_t * buf)
{
buf[0] = 0x00;
bcm2835_i2c_write_read_rs(buf ,1, buf,7);
}
int main(int argc, char **argv)
{
if (!bcm2835_init())
{
printf("bcm2835_init failed. Are you running as root??\n");
return 1;
}
if (!bcm2835_i2c_begin(2))
{
printf("bcm2835_i2c_begin failed. Are you running as root??\n");
return 1;
}
bcm2835_i2c_setSlaveAddress(0x68);
bcm2835_i2c_set_baudrate(10000);
printf("DS3231 Test Program ...\n\n");
//pcf8563SetTime(buf);
while(1)
{
pcf8563ReadTime(buf);
buf[0] = buf[0]&0x7F; //sec
buf[1] = buf[1]&0x7F; //min
buf[2] = buf[2]&0x3F; //hour
buf[3] = buf[3]&0x07; //week
buf[4] = buf[4]&0x3F; //day
buf[5] = buf[5]&0x1F; //mouth
//year/month/day
printf("20%02x-%02x-%02x ",buf[6],buf[5],buf[4]);
////hour:minute/second
printf("%02x:%02x:%02x ",buf[2],buf[1],buf[0]);
////weekday
printf("%s\n",str[(unsigned char)buf[3]]);
bcm2835_delay(1000);
}
bcm2835_i2c_end(2);
bcm2835_close();
return 0;
}