Timer.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 led_cnt = 0;
  70. static uint16_t read_ele_cnt = 0;
  71. if(timer_interrupt_flag_get(TIMER14, TIMER_INT_FLAG_UP) == SET)
  72. {
  73. timer_interrupt_flag_clear(TIMER14, TIMER_INT_FLAG_UP);
  74. }
  75. led_cnt++;
  76. read_ele_cnt++;
  77. if(led_cnt >=1000)
  78. {
  79. data->led_flag = 1;
  80. led_cnt = 0;
  81. }
  82. if(read_ele_cnt >= 100)
  83. {
  84. data->read_ele_flag = 1;
  85. read_ele_cnt = 0;
  86. }
  87. }
  88. void TIMER15_IRQHandler(void)
  89. {
  90. static unsigned short s_iCnt100 = 0;
  91. if(timer_interrupt_flag_get(TIMER15, TIMER_INT_FLAG_UP) == SET)
  92. {
  93. timer_interrupt_flag_clear(TIMER15, TIMER_INT_FLAG_UP);
  94. }
  95. s_iCnt100++;
  96. if(s_iCnt100 >= 100)
  97. {
  98. s_iCnt100 = 0;
  99. s_i100msFlag = 1;
  100. }
  101. }
  102. extern uint8_t read_kWh_flag;
  103. void TIMER16_IRQHandler(void)
  104. {
  105. static short s_iCnt1000 = 0;
  106. static uint32_t cnt = 0;
  107. if (timer_interrupt_flag_get(TIMER16, TIMER_INT_FLAG_UP) == SET)
  108. {
  109. timer_interrupt_flag_clear(TIMER16, TIMER_INT_FLAG_UP);
  110. }
  111. }
  112. void InitTimer(void)
  113. {
  114. ConfigTimer14(999, 71); //72MHz/(71+1)=1MHz,由0计数到999为1ms
  115. // ConfigTimer15(999, 71); //72MHz/(71+1)=1MHz,由0计数到999为1ms
  116. // ConfigTimer16(999, 71); //72MHz/(71+1)=1MHz,由0计数到999为1ms
  117. data = get_sts();
  118. }
  119. unsigned char Get2msFlag(void)
  120. {
  121. return(s_i2msFlag);
  122. }
  123. void Clr2msFlag(void)
  124. {
  125. s_i2msFlag = 0;
  126. }
  127. unsigned char Get100msFlag(void)
  128. {
  129. return(s_i100msFlag);
  130. }
  131. void Clr100msFlag(void)
  132. {
  133. s_i100msFlag = 0;
  134. }
  135. unsigned char Get1SecFlag(void)
  136. {
  137. return(s_i1secFlag);
  138. }
  139. void Clr1SecFlag(void)
  140. {
  141. s_i1secFlag = 0;
  142. }