Timer.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 "gd32f30x_libopt.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_TIMER2);
  25. timer_deinit(TIMER2);
  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(TIMER2, &timer_initpara);
  32. timer_interrupt_enable(TIMER2, TIMER_INT_UP);
  33. nvic_irq_enable(TIMER2_IRQn,0,3);
  34. timer_enable(TIMER2);
  35. }
  36. static uint8_t cnt_1 = 0;
  37. static uint8_t cnt_2 = 0;
  38. static uint8_t level_cnt1 = 0;
  39. static uint8_t level_cnt2 = 0;
  40. static uint32_t beep_cnt = 0;
  41. uint8_t recode_normal_channl = 0;
  42. void TIMER2_IRQHandler(void)
  43. {
  44. //static unsigned short s_iCnt2 = 0;
  45. static uint16_t cnt_1000 = 0;
  46. static uint16_t cnt_100 = 0;
  47. if(timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_UP) == SET)
  48. {
  49. timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP);
  50. }
  51. if(data->beep_flag || data->i_beep_flag[0].beep_waning || data->i_beep_flag[1].beep_waning || data->o_beep_flag.beep_waning)
  52. {
  53. beep_cnt++;
  54. if(beep_cnt >= data->beep_count)
  55. {
  56. beep_cnt = 0;
  57. data->beep();
  58. }
  59. }else
  60. {
  61. beep_cnt = 0;
  62. }
  63. if(data->chiose_flag)
  64. {
  65. data->chiose_counter++;
  66. if(data->chiose_counter >= data->chiose_count_max)
  67. {
  68. data->chiose_counter = 0;
  69. data->beep();
  70. }
  71. }
  72. #if 0
  73. if(data->input_1_up_flag)
  74. {
  75. level_cnt1 = 0;
  76. if(!data->input_1_down_flag)
  77. {
  78. cnt_1++;
  79. if(cnt_1 >= 3)
  80. {
  81. cnt_1 = data->input_1_up_flag = data->input_1_down_flag = 0;
  82. data->m_data.input_status.input_status[INPUT_CHANNEL_1] = 0;
  83. if(data->m_data.input_status.input_status[INPUT_CHANNEL_2]) data->choise_input(INPUT_CHANNEL_2);
  84. }
  85. }else
  86. {
  87. cnt_1 = data->input_1_up_flag = data->input_1_down_flag = 0;
  88. }
  89. }
  90. #endif
  91. if(data->exit_init_flag)
  92. {
  93. if(data->get_input1_level()==0)
  94. {
  95. level_cnt1++;
  96. if(level_cnt1 >= 3)
  97. {
  98. level_cnt1 = 0;
  99. data->m_data.input_status.input_status[INPUT_CHANNEL_1] = 0;
  100. if(data->m_data.input_status.input_status[INPUT_CHANNEL_2])
  101. {
  102. if(data->m_data.md_choise.choise_ch != INPUT_CHANNEL_2)
  103. {
  104. recode_normal_channl = 1;
  105. data->choise_input(INPUT_CHANNEL_2);
  106. }
  107. }
  108. }
  109. }else
  110. {
  111. level_cnt1 = 0;
  112. data->m_data.input_status.input_status[INPUT_CHANNEL_1] = 1;
  113. if(recode_normal_channl == 1)
  114. {
  115. recode_normal_channl = 0;
  116. data->choise_input(INPUT_CHANNEL_1);
  117. }
  118. }
  119. }
  120. #if 0
  121. if(data->input_2_up_flag)
  122. {
  123. if(!data->input_2_down_flag)
  124. {
  125. cnt_2++;
  126. if(cnt_2 >= 3)
  127. {
  128. cnt_2 = data->input_2_up_flag = data->input_2_down_flag = 0;
  129. data->m_data.input_status.input_status[INPUT_CHANNEL_2] = 0;
  130. if(data->m_data.input_status.input_status[INPUT_CHANNEL_1]) data->choise_input(INPUT_CHANNEL_1);
  131. }
  132. }else
  133. {
  134. cnt_2 = data->input_2_up_flag = data->input_2_down_flag = 0;
  135. }
  136. }
  137. #endif
  138. if(data->exit_init_flag)
  139. {
  140. if(data->get_input2_level()==0)
  141. {
  142. level_cnt2++;
  143. if(level_cnt2 >= 2)
  144. {
  145. level_cnt2 = 0;
  146. data->m_data.input_status.input_status[INPUT_CHANNEL_2] = 0;
  147. if(data->m_data.input_status.input_status[INPUT_CHANNEL_1])
  148. {
  149. if(data->m_data.md_choise.choise_ch != INPUT_CHANNEL_1)
  150. {
  151. recode_normal_channl = 2;
  152. data->choise_input(INPUT_CHANNEL_1);
  153. }
  154. }
  155. }
  156. }else
  157. {
  158. level_cnt2 = 0;
  159. data->m_data.input_status.input_status[INPUT_CHANNEL_2] = 1;
  160. if(recode_normal_channl == 2)
  161. {
  162. recode_normal_channl = 0;
  163. data->choise_input(INPUT_CHANNEL_2);
  164. }
  165. }
  166. }
  167. cnt_1000++;
  168. cnt_100++;
  169. if(cnt_1000 >= 1000)
  170. {
  171. data->flag_1000ms = 1;
  172. cnt_1000 = 0;
  173. }
  174. if(cnt_100 >= 100)
  175. {
  176. data->flag_100ms = 1;
  177. cnt_100 = 0;
  178. }
  179. }
  180. void InitTimer(void)
  181. {
  182. data = get_sts();
  183. ConfigTimer14(999, 71); //72MHz/(71+1)=1MHz£¬ÓÉ0¼ÆÊýµ½999Ϊ1ms
  184. }