完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
#include #include typedef unsigned char uint8; typedef unsigned int uint16; typedef struct __SYSTEMtiME__ { unsigned char Second; unsigned char Minute; unsigned char Hour; unsigned char Week; unsigned char Day; unsigned char Month; unsigned char Year; unsigned char DateString[11]; unsigned char TimeString[9]; unsigned char week_value[2]; }SYSTEMTIME; //定义的时间类型 SYSTEMTIME CurrentTime; ***it SCK=P3^5;//时钟 ***it SDA=P3^4;//数据 ***it RST=P1^7;//复位 ***it RS=P1^0; ***it RW=P1^1; ***it EN=P1^5; ***it busy=P0^7; #define AM(X) X #define PM(X) (X+12) // 转成24小时制 #define DS1302_SECOND 0x80 //时钟芯片的寄存器位置,存放时间 #define DS1302_MINUTE 0x82 #define DS1302_HOUR 0x84 #define DS1302_WEEK 0x8A #define DS1302_DAY 0x86 #define DS1302_MONTH 0x88 #define DS1302_YEAR 0x8C void DS1302WriteByte(uint8 dat) //时钟写入一个字节 { uint8 i; SCK=0; for(i=0;i<8;i++) { SDA=dat & 0x01; SCK=1; dat>>=1; SCK=0; } } uint8 DS1302ReadByte() //时钟读取一个字节 { uint8 i,dat; for(i=0;i<8;i++) { dat>>=1; if(SDA==1) { dat |= 0x80; } SCK=1; SCK=0; } return dat; } void Write1302(uint8 Addr,uint8 dat) //Addr:1302的地址 dat:要写的数据 { RST=0; SCK=0; RST=1; DS1302WriteByte(Addr); //命令,地址 DS1302WriteByte(dat); //写1Byte数据 SCK=1; RST=0; } uint8 Read1302(uint8 Addr) //读取1302某地址数据 { uint8 dat; RST=0; SCK=0; RST=1; DS1302WriteByte(Addr | 0x01); //命令,地址 dat=DS1302ReadByte(); //读1Byte数据 SCK=1; RST=0; return dat; } void DS1302_GetTime(SYSTEMTIME *Time) //获取时钟芯片的时钟数据到自定义的结构型数组 { uint8 ReadValue; ReadValue=Read1302(DS1302_SECOND); Time->Second=((ReadValue & 0x70)>>4)*10+(ReadValue & 0x0F); ReadValue=Read1302(DS1302_MINUTE); Time->Minute=((ReadValue & 0x70)>>4)*10+(ReadValue & 0x0F); ReadValue=Read1302(DS1302_HOUR); Time->Hour=((ReadValue & 0x70)>>4)*10+(ReadValue & 0x0F); ReadValue=Read1302(DS1302_DAY); Time->Day=((ReadValue & 0x70)>>4)*10+(ReadValue & 0x0F); ReadValue=Read1302(DS1302_WEEK); Time->Week=((ReadValue & 0x70)>>4)*10+(ReadValue & 0x0F); ReadValue=Read1302(DS1302_MONTH); Time->Month=((ReadValue & 0x70)>>4)*10+(ReadValue & 0x0F); ReadValue=Read1302(DS1302_YEAR); Time->Year=((ReadValue & 0x70)>>4)*10+(ReadValue & 0x0F); } void DateToStr(SYSTEMTIME *Time) //将时间年,月,日,星期数据转换成液晶显示字符串,放到数组里DateString[] { Time->DateString[0] = '2'; Time->DateString[1] = '0'; Time->DateString[2] = Time->Year/10 + '0'; Time->DateString[3] = Time->Year%10 + '0'; Time->DateString[4]='/'; Time->DateString[5]=Time->Month/10+'0'; Time->DateString[6]=Time->Month%10+'0'; Time->DateString[7]='/'; Time->DateString[8]=Time->Day/10+'0'; Time->DateString[9]=Time->Day%10+'0'; Time->week_value[0] = Time->Week%10 + '0'; //星期的数据另外放到 week_value[]数组里,跟年,月,日的分开存放,因为等一下要在最后显示 Time->week_value[1] = ' |