📄 ds1307.c
字号:
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>#define DS1307_RAM_ADDR_START 0x08#define DS1307_RAM_ADDR_END 0x3F#define DS1307_RAM_SIZE 0x40#define WRITE_COM 1#define READ_COM 2#define BCD_TO_BIN(val) (((val)&0x0F) + ((val)>>4)*10)#define BIN_TO_BCD(val) ((((val)/10)<<4) + (val)%10)#define TWELVE_HOUR_MODE(n) (((n)>>6)&1)#define HOURS_AP(n) (((n)>>5)&1)#define HOURS_12(n) BCD_TO_BIN((n)&0x1F)#define HOURS_24(n) BCD_TO_BIN((n)&0x3F)static char *ds1307_mon2str( unsigned int mon){ char *mon2str[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; if( mon > 11) return "error"; else return mon2str[ mon];}static char *ds1307_week2str( unsigned int week){ char *week2str[7] = { "Monday", "Tuesday", "Wednesday", "Tursday", "Friday", "Saturday","Sunday" }; if( week > 6) return "error"; else return week2str[ week];}struct time_Des{ char second; char minute; char hour; char week; char date; char month; char year;};static struct time_Des ds1307time;/* Physical interface */struct ds1307_local{ char addr; /* SPI chip-select number */ char* pTime; /* number of bytes per page */ int len;};static struct ds1307_local ds1307Frame;int main(void) { int file; char val; int year,month,date,week,hour,minute,second; if ((file = open("/dev/i2c/0",O_RDWR)) < 0) { printf("open /dev/i2c/0 is failed\n"); exit(1); } printf("Start test DS1307"); printf("\n\rDo you want to Reset Time(Y/N)"); scanf("%c",&val);if(val=='Y'||val=='y'){ printf("\n\ryear(00-99):"); scanf("%d",&year); printf("month(1-12):"); scanf("%d",&month); printf("date(1-31):"); scanf("%d",&date); printf("week(1-7):"); scanf("%d",&week); printf("hour(0-23):"); scanf("%d",&hour); printf("minute(0-59):"); scanf("%d",&minute); printf("second(0-59):"); scanf("%d",&second); //set time printf("\nYour input string is =%d %d %d %d %d %d %d\n\r\n\r",year,month,date,week,hour,minute,second); //set time/* ds1307time.year =year;
ds1307time.month =month;
ds1307time.date =date;
ds1307time.week =week;
ds1307time.hour =hour;
ds1307time.minute =minute;
ds1307time.second =second;*/
ds1307time.year =BIN_TO_BCD(year);
ds1307time.month =BIN_TO_BCD(month);
ds1307time.date =BIN_TO_BCD(date);
ds1307time.week =BIN_TO_BCD(week);
ds1307time.hour =BIN_TO_BCD(hour);
ds1307time.minute =BIN_TO_BCD(minute);
ds1307time.second =BIN_TO_BCD(second);
ds1307Frame.len = 7; ds1307Frame.addr = 0x00; ds1307Frame.pTime = (char*)&ds1307time; ioctl(file, WRITE_COM, &ds1307Frame);} ds1307Frame.pTime = (char*)&ds1307time; while (1) // Set sequential part here
{ ds1307Frame.len = 7; ds1307Frame.addr = 0x00;// ds1307Frame.pTime = (char*)&ds1307time; ioctl(file, READ_COM, &ds1307Frame);
sleep(1);
printf("Now is = ' %d-%s-200%d %2d:%2d:%2d %s '\n",\ BCD_TO_BIN(ds1307time.date),\ ds1307_mon2str(BCD_TO_BIN(ds1307time.month)-1),\ BCD_TO_BIN(ds1307time.year),\ BCD_TO_BIN(ds1307time.hour),\ BCD_TO_BIN(ds1307time.minute),\ BCD_TO_BIN(ds1307time.second),\ ds1307_week2str(BCD_TO_BIN(ds1307time.week)-1));
} close(file); exit(0); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -