userRtc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include "includes.h"
  2. USER_RTC_DEF thisTime;
  3. static uint32_t DateToSeconds(USER_RTC_DEF time);
  4. static void GetDate(uint32_t TimeVar,USER_RTC_DEF *time);
  5. void RTC_Init(void)
  6. {
  7. thisTime.year=2000;
  8. thisTime.month=1;
  9. thisTime.day=1,
  10. thisTime.hour=0;
  11. thisTime.min=0;
  12. thisTime.second=0;
  13. }
  14. void RTC_Update(void)
  15. {
  16. #if 1
  17. if(++thisTime.second>=60){
  18. thisTime.second=0;
  19. if(++thisTime.min>=60){
  20. thisTime.min=0;
  21. if(++thisTime.hour>=24){
  22. thisTime.hour=0;
  23. }
  24. }
  25. }
  26. #else
  27. USER_RTC_DEF tempTime;
  28. memcpy((unsigned char *)&tempTime, (unsigned char *)&thisTime, sizeof(USER_RTC_DEF));
  29. if(++thisTime.second>=60){
  30. thisTime.second=0;
  31. if(++thisTime.min>=60){
  32. thisTime.min=0;
  33. if(++thisTime.hour>=24){
  34. thisTime.hour=0;
  35. GetDate(DateToSeconds(tempTime)+1, &thisTime);
  36. }
  37. }
  38. }
  39. #endif
  40. }
  41. void RTC_UserSetTime(USER_RTC_DEF time)
  42. {
  43. #if 1
  44. thisTime.year=time.year;
  45. thisTime.month=time.month;
  46. thisTime.day=time.day;
  47. thisTime.hour=time.hour;
  48. thisTime.min=time.min;
  49. thisTime.second=time.second;
  50. #else
  51. GetDate(DateToSeconds(time), &thisTime);
  52. #endif
  53. }
  54. void RTC_UserGetTime(USER_RTC_DEF *time)
  55. {
  56. time->year=thisTime.year;
  57. time->month=thisTime.month;
  58. time->day=thisTime.day;
  59. time->hour=thisTime.hour;
  60. time->min=thisTime.min;
  61. time->second=thisTime.second;
  62. }
  63. //////////////////////////////////////////
  64. #define SecsPerComYear 3153600//(365*3600*24)
  65. #define SecsPerLeapYear 31622400//(366*3600*24)
  66. #define SecsPerFourYear 126230400//((365*3600*24)*3+(366*3600*24))
  67. #define SecsPerDay (3600*24)
  68. const uint32_t Month_Days_Accu_C[13] = {0,31,59,90,120,151,181,212,243,273,304,334,365};
  69. const uint32_t Month_Days_Accu_L[13] = {0,31,60,91,121,152,182,213,244,274,305,335,366};
  70. const int32_t Year_Secs_Accu[5]={0,
  71. 31622400,
  72. 63158400,
  73. 94694400,
  74. 126230400};
  75. const int32_t Month_Secs_Accu_C[13] = { 0,
  76. 2678400,
  77. 5097600,
  78. 7776000,
  79. 10368000,
  80. 13046400,
  81. 15638400,
  82. 18316800,
  83. 20995200,
  84. 23587200,
  85. 26265600,
  86. 28857600,
  87. 31536000};
  88. const int32_t Month_Secs_Accu_L[13] = {0,
  89. 2678400,
  90. 5184000,
  91. 7862400,
  92. 10454400,
  93. 13132800,
  94. 15724800,
  95. 18403200,
  96. 21081600,
  97. 23673600,
  98. 26352000,
  99. 28944000,
  100. 31622400};
  101. //输入年月日时分秒,输出总秒数
  102. static uint32_t DateToSeconds(USER_RTC_DEF time)
  103. {
  104. uint32_t Tmp_Year, Tmp_Month, Tmp_Date;
  105. uint32_t LeapY, ComY, TotSeconds, TotDays;
  106. uint32_t Tmp_HH, Tmp_MM, Tmp_SS;
  107. Tmp_Year =time.year;
  108. Tmp_Month = time.month;
  109. Tmp_Date =time.day;
  110. Tmp_HH = time.hour;
  111. Tmp_MM = time.min;
  112. Tmp_SS = time.second;
  113. if(Tmp_Year==2000) LeapY = 0;
  114. else LeapY = (Tmp_Year - 2000 -1)/4 +1;
  115. ComY = (Tmp_Year - 2000)-(LeapY);
  116. if (Tmp_Year%4) TotDays = LeapY*366 + ComY*365 + Month_Days_Accu_C[Tmp_Month-1] + (Tmp_Date-1);
  117. else TotDays = LeapY*366 + ComY*365 + Month_Days_Accu_L[Tmp_Month-1] + (Tmp_Date-1);
  118. TotSeconds = TotDays*SecsPerDay + (Tmp_HH*3600 + Tmp_MM*60 + Tmp_SS);
  119. return TotSeconds;
  120. }
  121. static void GetDate(uint32_t TimeVar,USER_RTC_DEF *time)
  122. {
  123. static uint32_t SY=999,SM=999,SD=999;
  124. uint32_t TY = 0, TM = 1, TD = 0;
  125. int32_t Num4Y,NumY, OffSec, Off4Y = 0;
  126. uint32_t i;
  127. int32_t NumDay;
  128. u32 THH = 0, TMM = 0, TSS = 0;
  129. Num4Y = TimeVar/SecsPerFourYear;
  130. OffSec = TimeVar%SecsPerFourYear;
  131. i=1;
  132. while(OffSec > Year_Secs_Accu[i++])
  133. Off4Y++;
  134. /* Numer of Complete Year */
  135. NumY = Num4Y*4 + Off4Y;
  136. /* 2000,2001,...~2000+NumY-1 complete year before, so this year is 2000+NumY*/
  137. TY = 2000+NumY;
  138. OffSec = OffSec - Year_Secs_Accu[i-2];
  139. /* Month (TBD with OffSec)*/
  140. i=0;
  141. if(TY%4)
  142. {// common year
  143. while(OffSec > Month_Secs_Accu_C[i++]);
  144. TM = i-1;
  145. OffSec = OffSec - Month_Secs_Accu_C[i-2];
  146. }
  147. else
  148. {// leap year
  149. while(OffSec > Month_Secs_Accu_L[i++]);
  150. TM = i-1;
  151. OffSec = OffSec - Month_Secs_Accu_L[i-2];
  152. }
  153. /* Date (TBD with OffSec) */
  154. NumDay = OffSec/SecsPerDay;
  155. OffSec = OffSec%SecsPerDay;
  156. TD = NumDay+1;
  157. /* Compute hours */
  158. THH = OffSec/3600;
  159. /* Compute minutes */
  160. TMM = (OffSec % 3600)/60;
  161. /* Compute seconds */
  162. TSS = (OffSec % 3600)% 60;
  163. time->year=TY;
  164. time->month=TM;
  165. time->day=TD,
  166. time->hour=THH;
  167. time->min=TMM;
  168. time->second=TSS;
  169. }