Timer.c 4.5 KB

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