vu32 TimeDisplay = 0;
void RTC_init(u32 y,u32 m,u32 d,u32 h,u32 mm,u32 s)
{
if(BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
{
USART1_PutString("RTC 不曾配置....");
RTC_Configuration();
USART1_PutString("RTC 开始配置....");
Time_Adjust( y, m, d, h, mm, s);
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
}
else
{
if(RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
{
USART1_PutString("\r\n电源复位....");
}
else if(RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
{
USART1_PutString("\r\n External Reset occurred....");
}
USART1_PutString("\r\n 不须要配置 RTC....");
RTC_WaitForSynchro();
RTC_ITConfig(RTC_IT_SEC, ENABLE);
RTC_WaitForLastTask();
}
#ifdef RTCClockOutput_Enable
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
BKP_TamperPinCmd(DISABLE);
BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
#endif
RCC_ClearFlag();
}
void RTC_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
BKP_DeInit();
RCC_LSEConfig(RCC_LSE_ON);
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{}
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForSynchro();
RTC_WaitForLastTask();
RTC_ITConfig(RTC_IT_SEC, ENABLE);
RTC_WaitForLastTask();
RTC_SetPrescaler(32767);
RTC_WaitForLastTask();
}
u32 Month_Days_Accu_C[13] = {0,31,59,90,120,151,181,212,243,273,304,334,365};
u32 Month_Days_Accu_L[13] = {0,31,60,91,121,152,182,213,244,274,305,335,366};
#define SecsPerDay (3600*24)
u32 Time_Regulate(u32 y,u32 m,u32 d,u32 h,u32 mm,u32 s)
{
char data[20];
//#if 1
u32 Tmp_Year=0xFFFF, Tmp_Month=0xFF, Tmp_Date=0xFF;
u32 LeapY, ComY, TotSeconds, TotDays;
//#endif
u32 Tmp_HH = 0xFF, Tmp_MM = 0xFF, Tmp_SS = 0xFF;
USART1_PutString("\r\n==============Time Settings=====================================");
//#if 1
USART1_PutString("\r\nSet Year");
while(Tmp_Year == 0xFFFF)
{
Tmp_Year =y;
}
sprintf1(data,": %d", Tmp_Year);
USART1_PutString(data);
while(Tmp_Month == 0xFF)
{
Tmp_Month = m;
}
sprintf1(data,": %d", Tmp_Month);
USART1_PutString(data);
while(Tmp_Date == 0xFF)
{
Tmp_Date =d;
}
sprintf1(data,": %d", Tmp_Date);
USART1_PutString(data);
//#endif
while(Tmp_HH == 0xFF)
{
Tmp_HH = h;
}
sprintf1(data,": %d", Tmp_HH);
USART1_PutString(data);
USART1_PutString("\r\nSet Minutes");
while(Tmp_MM == 0xFF)
{
Tmp_MM = mm;
}
sprintf1(data,": %d", Tmp_MM);;
USART1_PutString(data);
while(Tmp_SS == 0xFF)
{
Tmp_SS = s;
}
sprintf1(data,": %d", Tmp_SS);
USART1_PutString(data);
//#if 1
{
if(Tmp_Year==2000)
LeapY = 0;
else
LeapY = (Tmp_Year - 2000 -1)/4 +1;
ComY = (Tmp_Year - 2000)-(LeapY);
if (Tmp_Year%4)
//common year
TotDays = LeapY*366 + ComY*365 + Month_Days_Accu_C[Tmp_Month-1] + (Tmp_Date-1);
else
//leap year
TotDays = LeapY*366 + ComY*365 + Month_Days_Accu_L[Tmp_Month-1] + (Tmp_Date-1);
TotSeconds = TotDays*SecsPerDay + (Tmp_HH*3600 + Tmp_MM*60 + Tmp_SS);
}
//#endif
return TotSeconds;
}
void Time_Adjust(u32 y,u32 m,u32 d,u32 h,u32 mm,u32 s)
{
RTC_WaitForLastTask();
RTC_SetCounter(Time_Regulate(y,m,d,h,mm,s));
RTC_WaitForLastTask();
}
#define SecsPerComYear 3153600//(365*3600*24)
#define SecsPerLeapYear 31622400//(366*3600*24)
#define SecsPerFourYear 126230400//((365*3600*24)*3+(366*3600*24))
#define SecsPerDay (3600*24)
s32 Year_Secs_Accu[5]={0,
31622400,
63158400,
94694400,
126230400};
s32 Month_Secs_Accu_C[13] = { 0,
2678400,
5097600,
7776000,
10368000,
13046400,
15638400,
18316800,
20995200,
23587200,