Timer.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*********************************************************************************************************
  2. * Module : Timer.c
  3. * Abstract : Timer modules, this module is used to improve Timing service!
  4. * Version : 1.0.0
  5. * Author : Leopul(COPYRIGHT 2025 - 2027 Leopul. All rights reserved.)
  6. * Complete : 2025-02-16
  7. * Content : None
  8. * Note : None
  9. *********************************************************************************************************/
  10. #include "Timer.h"
  11. #include "gd32e230x_conf.h"
  12. #include "main.h"
  13. static unsigned char s_i100msFlag = 0;
  14. static unsigned char s_i2msFlag = 0;
  15. static unsigned char s_i1secFlag = 0;
  16. volatile unsigned long int g_del_ms =0;
  17. static void ConfigTimer14(unsigned short arr, unsigned short psc);
  18. static void ConfigTimer15(unsigned short arr, unsigned short psc);
  19. static void ConfigTimer16(unsigned short arr, unsigned short psc);
  20. static sts_data_t *data = 0;
  21. static void ConfigTimer14(unsigned short arr, unsigned short psc)
  22. {
  23. timer_parameter_struct timer_initpara;
  24. rcu_periph_clock_enable(RCU_TIMER14);
  25. timer_deinit(TIMER14);
  26. timer_struct_para_init(&timer_initpara);
  27. timer_initpara.prescaler = psc;
  28. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  29. timer_initpara.period = arr;
  30. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  31. timer_init(TIMER14, &timer_initpara);
  32. timer_interrupt_enable(TIMER14, TIMER_INT_UP);
  33. nvic_irq_enable(TIMER14_IRQn, 2);
  34. timer_enable(TIMER14);
  35. }
  36. static void ConfigTimer15(unsigned short arr, unsigned short psc)
  37. {
  38. timer_parameter_struct timer_initpara;
  39. rcu_periph_clock_enable(RCU_TIMER15);
  40. timer_deinit(TIMER15);
  41. timer_struct_para_init(&timer_initpara);
  42. timer_initpara.prescaler = psc;
  43. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  44. timer_initpara.period = arr;
  45. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  46. timer_init(TIMER15, &timer_initpara);
  47. timer_interrupt_enable(TIMER15, TIMER_INT_UP);
  48. nvic_irq_enable(TIMER15_IRQn, 3);
  49. timer_enable(TIMER15);
  50. }
  51. static void ConfigTimer16(unsigned short arr,unsigned short psc)
  52. {
  53. timer_parameter_struct timer_initpara;
  54. rcu_periph_clock_enable(RCU_TIMER16);
  55. timer_deinit(TIMER16);
  56. timer_struct_para_init(&timer_initpara);
  57. timer_initpara.prescaler = psc;
  58. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  59. timer_initpara.period = arr;
  60. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  61. timer_init(TIMER16, &timer_initpara);
  62. timer_interrupt_enable(TIMER16, TIMER_INT_UP);
  63. nvic_irq_enable(TIMER16_IRQn, 3);
  64. timer_enable(TIMER16);
  65. }
  66. void TIMER14_IRQHandler(void)
  67. {
  68. //static unsigned short s_iCnt2 = 0;
  69. static uint16_t cnt_1000 = 0;
  70. static uint8_t cnt_100 = 0;
  71. static uint8_t cnt_5 = 0;
  72. if(timer_interrupt_flag_get(TIMER14, TIMER_INT_FLAG_UP) == SET)
  73. {
  74. timer_interrupt_flag_clear(TIMER14, TIMER_INT_FLAG_UP);
  75. }
  76. cnt_1000++;
  77. cnt_100++;
  78. if(cnt_1000 >=1000)
  79. {
  80. data->flag_1000ms = 1;
  81. cnt_1000 = 0;
  82. }
  83. if(cnt_100 >= 100)
  84. {
  85. data->flag_100ms = 1;
  86. cnt_100 = 0;
  87. }
  88. }
  89. void TIMER15_IRQHandler(void)
  90. {
  91. static unsigned short s_iCnt100 = 0;
  92. if(timer_interrupt_flag_get(TIMER15, TIMER_INT_FLAG_UP) == SET)
  93. {
  94. timer_interrupt_flag_clear(TIMER15, TIMER_INT_FLAG_UP);
  95. }
  96. s_iCnt100++;
  97. if(s_iCnt100 >= 100)
  98. {
  99. s_iCnt100 = 0;
  100. s_i100msFlag = 1;
  101. }
  102. }
  103. extern uint8_t read_kWh_flag;
  104. void TIMER16_IRQHandler(void)
  105. {
  106. static short s_iCnt1000 = 0;
  107. static uint32_t cnt = 0;
  108. if (timer_interrupt_flag_get(TIMER16, TIMER_INT_FLAG_UP) == SET)
  109. {
  110. timer_interrupt_flag_clear(TIMER16, TIMER_INT_FLAG_UP);
  111. }
  112. }
  113. void InitTimer(void)
  114. {
  115. ConfigTimer14(999, 71); //72MHz/(71+1)=1MHz,由0计数到999为1ms
  116. // ConfigTimer15(999, 71); //72MHz/(71+1)=1MHz,由0计数到999为1ms
  117. // ConfigTimer16(999, 71); //72MHz/(71+1)=1MHz,由0计数到999为1ms
  118. data = get_sts();
  119. }
  120. unsigned char Get2msFlag(void)
  121. {
  122. return(s_i2msFlag);
  123. }
  124. void Clr2msFlag(void)
  125. {
  126. s_i2msFlag = 0;
  127. }
  128. unsigned char Get100msFlag(void)
  129. {
  130. return(s_i100msFlag);
  131. }
  132. void Clr100msFlag(void)
  133. {
  134. s_i100msFlag = 0;
  135. }
  136. unsigned char Get1SecFlag(void)
  137. {
  138. return(s_i1secFlag);
  139. }
  140. void Clr1SecFlag(void)
  141. {
  142. s_i1secFlag = 0;
  143. }