RTC.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /********************************************************************************
  2. * File Name: RTC.c
  3. * Function Describe: 实时时间 万年历 软模拟
  4. * Explain:
  5. * Writer: ShiLiangWen
  6. * Date: 2019-2-17
  7. *******************************************************************************/
  8. #include "includes.h"
  9. SUT_RTC sutRTC={2000,1,1,0,0,0};
  10. /*
  11. 判断当月有多少天
  12. 输入:year 如:2019 month 从1开始
  13. */
  14. int MonthDays(int year, int month)//判断当月共有多少天
  15. {
  16. int month_days;
  17. if(year<2000)year+=2000;
  18. if (month == 2)
  19. {
  20. if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
  21. {
  22. month_days = 29;
  23. }
  24. else
  25. {
  26. month_days = 28;
  27. }
  28. }
  29. else if (month == 4 || month == 6 || month == 9 || month == 11)
  30. {
  31. month_days = 30;
  32. }
  33. else
  34. {
  35. month_days = 31;
  36. }
  37. return month_days;
  38. }
  39. /*
  40. RTC 增加1秒
  41. */
  42. void RTCIncSecond(void)
  43. {
  44. tsk_lock();
  45. if(++sutRTC.Seconds>59){
  46. sutRTC.Seconds=0;
  47. if(++sutRTC.Minute>59){
  48. sutRTC.Minute=0;
  49. if(++sutRTC.Hours>23){
  50. sutRTC.Hours=0;
  51. if(++sutRTC.Days>MonthDays(sutRTC.Years,sutRTC.Months)){
  52. sutRTC.Days=1;
  53. if(++sutRTC.Months>12){
  54. sutRTC.Months=1;
  55. sutRTC.Years++;
  56. }
  57. }
  58. }
  59. }
  60. }
  61. tsk_unlock();
  62. }
  63. void RTC_Process(void)
  64. {
  65. unsigned char BCD[6];
  66. static unsigned long lastTick=100;
  67. unsigned long curTick;
  68. curTick=os_time_get();
  69. if(curTick>lastTick){
  70. lastTick=curTick+100;
  71. RTCIncSecond();
  72. //printf("%d-%d-%d %d:%d:%d\r\n",sutRTC.Years,sutRTC.Months,sutRTC.Days,sutRTC.Hours,sutRTC.Minute,sutRTC.Seconds);
  73. //RTC_Get(BCD);
  74. //TraceData(BCD,6);
  75. }
  76. }
  77. /*
  78. */
  79. void RTC_Set(unsigned char *BCD)
  80. {
  81. tsk_lock();
  82. sutRTC.Years=(int)(BCD[0]>>4)*10;
  83. sutRTC.Years+=(int)BCD[0]&0xF;
  84. sutRTC.Years+=2000;
  85. sutRTC.Months=(int)(BCD[1]>>4)*10;
  86. sutRTC.Months+=(int)BCD[1]&0xF;
  87. sutRTC.Days=(int)(BCD[2]>>4)*10;
  88. sutRTC.Days+=(int)BCD[2]&0xF;
  89. sutRTC.Hours=(int)(BCD[3]>>4)*10;
  90. sutRTC.Hours+=(int)BCD[3]&0xF;
  91. sutRTC.Minute=(int)(BCD[4]>>4)*10;
  92. sutRTC.Minute+=(int)BCD[4]&0xF;
  93. sutRTC.Seconds=(int)(BCD[5]>>4)*10;
  94. sutRTC.Seconds+=(int)BCD[5]&0xF;
  95. tsk_unlock();
  96. }
  97. void RTC_Get(unsigned char *BCD)
  98. {
  99. tsk_lock();
  100. int y=sutRTC.Years-2000;
  101. BCD[0]=((y/10)<<4)| (y%10);
  102. BCD[1]=((sutRTC.Months/10)<<4)|(sutRTC.Months%10);
  103. BCD[2]=((sutRTC.Days/10)<<4)|(sutRTC.Days%10);
  104. BCD[3]=((sutRTC.Hours/10)<<4)|(sutRTC.Hours%10);
  105. BCD[4]=((sutRTC.Minute/10)<<4)|(sutRTC.Minute%10);
  106. BCD[5]=((sutRTC.Seconds/10)<<4)|(sutRTC.Seconds%10);
  107. tsk_unlock();
  108. }
  109. //判断RTC时间是否有效
  110. int RTC_Effective(void)
  111. {
  112. if(sutRTC.Years>2001)return 1;
  113. else return 0;
  114. }