123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- #include "includes.h"
- USER_RTC_DEF thisTime;
- static uint32_t DateToSeconds(USER_RTC_DEF time);
- static void GetDate(uint32_t TimeVar,USER_RTC_DEF *time);
- void RTC_Init(void)
- {
- thisTime.year=2000;
- thisTime.month=1;
- thisTime.day=1,
- thisTime.hour=0;
- thisTime.min=0;
- thisTime.second=0;
- }
- void RTC_Update(void)
- {
- #if 1
- if(++thisTime.second>=60){
- thisTime.second=0;
- if(++thisTime.min>=60){
- thisTime.min=0;
- if(++thisTime.hour>=24){
- thisTime.hour=0;
- }
- }
- }
- #else
- USER_RTC_DEF tempTime;
- memcpy((unsigned char *)&tempTime, (unsigned char *)&thisTime, sizeof(USER_RTC_DEF));
- if(++thisTime.second>=60){
- thisTime.second=0;
- if(++thisTime.min>=60){
- thisTime.min=0;
- if(++thisTime.hour>=24){
- thisTime.hour=0;
- GetDate(DateToSeconds(tempTime)+1, &thisTime);
- }
- }
- }
- #endif
- }
- void RTC_UserSetTime(USER_RTC_DEF time)
- {
- #if 1
- thisTime.year=time.year;
- thisTime.month=time.month;
- thisTime.day=time.day;
- thisTime.hour=time.hour;
- thisTime.min=time.min;
- thisTime.second=time.second;
- #else
- GetDate(DateToSeconds(time), &thisTime);
- #endif
- }
- void RTC_UserGetTime(USER_RTC_DEF *time)
- {
- time->year=thisTime.year;
- time->month=thisTime.month;
- time->day=thisTime.day;
- time->hour=thisTime.hour;
- time->min=thisTime.min;
- time->second=thisTime.second;
- }
- //////////////////////////////////////////
- #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)
- const uint32_t Month_Days_Accu_C[13] = {0,31,59,90,120,151,181,212,243,273,304,334,365};
- const uint32_t Month_Days_Accu_L[13] = {0,31,60,91,121,152,182,213,244,274,305,335,366};
- const int32_t Year_Secs_Accu[5]={0,
- 31622400,
- 63158400,
- 94694400,
- 126230400};
- const int32_t Month_Secs_Accu_C[13] = { 0,
- 2678400,
- 5097600,
- 7776000,
- 10368000,
- 13046400,
- 15638400,
- 18316800,
- 20995200,
- 23587200,
- 26265600,
- 28857600,
- 31536000};
- const int32_t Month_Secs_Accu_L[13] = {0,
- 2678400,
- 5184000,
- 7862400,
- 10454400,
- 13132800,
- 15724800,
- 18403200,
- 21081600,
- 23673600,
- 26352000,
- 28944000,
- 31622400};
- //输入年月日时分秒,输出总秒数
- static uint32_t DateToSeconds(USER_RTC_DEF time)
- {
- uint32_t Tmp_Year, Tmp_Month, Tmp_Date;
- uint32_t LeapY, ComY, TotSeconds, TotDays;
-
- uint32_t Tmp_HH, Tmp_MM, Tmp_SS;
- Tmp_Year =time.year;
- Tmp_Month = time.month;
- Tmp_Date =time.day;
- Tmp_HH = time.hour;
- Tmp_MM = time.min;
- Tmp_SS = time.second;
- if(Tmp_Year==2000) LeapY = 0;
- else LeapY = (Tmp_Year - 2000 -1)/4 +1;
-
- ComY = (Tmp_Year - 2000)-(LeapY);
-
- if (Tmp_Year%4) TotDays = LeapY*366 + ComY*365 + Month_Days_Accu_C[Tmp_Month-1] + (Tmp_Date-1);
- else 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);
- return TotSeconds;
- }
- static void GetDate(uint32_t TimeVar,USER_RTC_DEF *time)
- {
- static uint32_t SY=999,SM=999,SD=999;
- uint32_t TY = 0, TM = 1, TD = 0;
- int32_t Num4Y,NumY, OffSec, Off4Y = 0;
- uint32_t i;
- int32_t NumDay;
- u32 THH = 0, TMM = 0, TSS = 0;
- Num4Y = TimeVar/SecsPerFourYear;
- OffSec = TimeVar%SecsPerFourYear;
- i=1;
- while(OffSec > Year_Secs_Accu[i++])
- Off4Y++;
- /* Numer of Complete Year */
- NumY = Num4Y*4 + Off4Y;
- /* 2000,2001,...~2000+NumY-1 complete year before, so this year is 2000+NumY*/
- TY = 2000+NumY;
-
- OffSec = OffSec - Year_Secs_Accu[i-2];
-
- /* Month (TBD with OffSec)*/
- i=0;
- if(TY%4)
- {// common year
- while(OffSec > Month_Secs_Accu_C[i++]);
- TM = i-1;
- OffSec = OffSec - Month_Secs_Accu_C[i-2];
- }
- else
- {// leap year
- while(OffSec > Month_Secs_Accu_L[i++]);
- TM = i-1;
- OffSec = OffSec - Month_Secs_Accu_L[i-2];
- }
-
- /* Date (TBD with OffSec) */
- NumDay = OffSec/SecsPerDay;
- OffSec = OffSec%SecsPerDay;
- TD = NumDay+1;
- /* Compute hours */
- THH = OffSec/3600;
- /* Compute minutes */
- TMM = (OffSec % 3600)/60;
- /* Compute seconds */
- TSS = (OffSec % 3600)% 60;
- time->year=TY;
- time->month=TM;
- time->day=TD,
- time->hour=THH;
- time->min=TMM;
- time->second=TSS;
- }
|