Timer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. uint8_t recode_normal_flag = 0;
  43. uint8_t recode_channel = 0;
  44. void TIMER2_IRQHandler(void)
  45. {
  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(data->chiose_flag)
  63. // {
  64. // data->chiose_counter++;
  65. // if(data->chiose_counter >= data->chiose_count_max)
  66. // {
  67. // data->chiose_counter = 0;
  68. // //data->beep();
  69. // }
  70. // }
  71. if(data->exit_init_flag)
  72. {
  73. if(data->get_input1_level()==0)
  74. {
  75. level_cnt1++;
  76. if(level_cnt1 >= 3)
  77. {
  78. level_cnt1 = 0;
  79. data->m_data.m_r.r_data.input_status.input_status[INPUT_CHANNEL_1] = 0;
  80. if(data->m_data.m_r.r_data.input_status.input_status[INPUT_CHANNEL_2])
  81. {
  82. if(data->m_data.m_rw.rw_data.chiose_ch != INPUT_CHANNEL_2)
  83. {
  84. recode_normal_channl = 1;
  85. data->choise_input(INPUT_CHANNEL_2);
  86. }
  87. }
  88. }
  89. }else
  90. {
  91. level_cnt1 = 0;
  92. data->m_data.m_r.r_data.input_status.input_status[INPUT_CHANNEL_1] = 1;
  93. if(recode_normal_channl == 1)
  94. {
  95. recode_normal_channl = 0;
  96. recode_normal_flag = 1;
  97. recode_channel = INPUT_CHANNEL_1;
  98. data->choise_input(INPUT_CHANNEL_1);
  99. }
  100. }
  101. if(data->get_input2_level()==0)
  102. {
  103. level_cnt2++;
  104. if(level_cnt2 >= 2)
  105. {
  106. level_cnt2 = 0;
  107. data->m_data.m_r.r_data.input_status.input_status[INPUT_CHANNEL_2] = 0;
  108. if(data->m_data.m_r.r_data.input_status.input_status[INPUT_CHANNEL_1])
  109. {
  110. if(data->m_data.m_rw.rw_data.chiose_ch != INPUT_CHANNEL_1)
  111. {
  112. recode_normal_channl = 2;
  113. data->choise_input(INPUT_CHANNEL_1);
  114. }
  115. }
  116. }
  117. }else
  118. {
  119. level_cnt2 = 0;
  120. data->m_data.m_r.r_data.input_status.input_status[INPUT_CHANNEL_2] = 1;
  121. if(recode_normal_channl == 2)
  122. {
  123. recode_normal_channl = 0;
  124. recode_normal_flag = 1;
  125. data->choise_input(INPUT_CHANNEL_2);
  126. }
  127. }
  128. }
  129. }
  130. void InitTimer(void)
  131. {
  132. data = get_sts();
  133. ConfigTimer14(999, 71); //72MHz/(71+1)=1MHz£¬ÓÉ0¼ÆÊýµ½999Ϊ1ms
  134. }