DS1302 电子时钟

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u011041241/article/details/45242707

1203 是BCD码输入

故用按键的时候要转换

 

Write1302(DS1302_HOUR,(CurrentTime.Hour/10)<<4|(CurrentTime.Hour%10));
Write1302(DS1302_MINUTE,(CurrentTime.Minute/10)<<4|(CurrentTime.Minute%10));

 

1302  结构体

 

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[9];
 unsigned char TimeString[9];
}SYSTEMTIME; //定义的时间类型

 

 

 

 

void DS1302_GetTime(SYSTEMTIME *Time)
{
 unsigned char 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)
{
 Time->DateString[0] = Time->Year/10 + '0';
 Time->DateString[1] = Time->Year%10 + '0';
 Time->DateString[2] = '-';
 Time->DateString[3] = Time->Month/10 + '0';
 Time->DateString[4] = Time->Month%10 + '0';
 Time->DateString[5] = '-';
 Time->DateString[6] = Time->Day/10 + '0';
 Time->DateString[7] = Time->Day%10 + '0';
 Time->DateString[8] = '\0';
}

void TimeToStr(SYSTEMTIME *Time)
{
 Time->TimeString[0] = Time->Hour/10 + '0';
 Time->TimeString[1] = Time->Hour%10 + '0';
 Time->TimeString[2] = ':';
 Time->TimeString[3] = Time->Minute/10 + '0';
 Time->TimeString[4] = Time->Minute%10 + '0';
 Time->TimeString[5] = ':';
 Time->TimeString[6] = Time->Second/10 + '0';
 Time->TimeString[7] = Time->Second%10 + '0';
 Time->DateString[8] = '\0';
}

 

SYSTEMTIME CurrentTime;

定义

 DS1302_GetTime(&CurrentTime);

获得时间

 

 

展开阅读全文

没有更多推荐了,返回首页