timer.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*********************************************************************************************************
  2. * Module : timer
  3. * Abstract : defined data structure!
  4. * Version : 1.0.0
  5. * Author : Leopul(COPYRIGHT 2025 ~ 2027 Leopul. All rights reserved.)
  6. * Complete : 2025-04-09
  7. * Content : None
  8. * Note : None
  9. *********************************************************************************************************/
  10. #include "timer.h"
  11. #include "gd32e23x.h"
  12. #include "board_cfg.h"
  13. #include "common.h"
  14. static board_t *board = 0;
  15. static void timer14_config(uint16_t arr,uint16_t psc);
  16. static void timer15_config(uint16_t arr,uint16_t psc);
  17. static void relay_remember(void);
  18. static uint16_t count_1s = 0;
  19. static uint16_t count_10s = 0;
  20. static uint16_t count_100ms = 0;
  21. uint16_t rl_delay_cnt[RelaySlaveChaNum] = {0};
  22. uint16_t rl_delay_flag[RelaySlaveChaNum] = {0};
  23. uint8_t rl_open_delay_flag[RelaySlaveChaNum] = {0};
  24. uint8_t rl_open_delay_count[RelaySlaveChaNum] = {0};
  25. uint8_t rl_pwm_flag[RelaySlaveChaNum] = {0};
  26. void time_init(void)
  27. {
  28. timer14_config(9999,71);;
  29. timer15_config(24,71);
  30. }
  31. static void timer14_config(uint16_t arr,uint16_t psc)
  32. {
  33. board = get_board();
  34. timer_parameter_struct timer_initpara;
  35. rcu_periph_clock_enable(RCU_TIMER14);
  36. timer_deinit(TIMER14);
  37. timer_struct_para_init(&timer_initpara);
  38. timer_initpara.prescaler = psc;
  39. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  40. timer_initpara.period = arr;
  41. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  42. timer_init(TIMER14, &timer_initpara);
  43. timer_interrupt_enable(TIMER14, TIMER_INT_UP);
  44. nvic_irq_enable(TIMER14_IRQn, 2);
  45. timer_enable(TIMER14);
  46. }
  47. extern uint8_t relay_counter_flag[RelaySlaveChaNum];
  48. void TIMER14_IRQHandler(void)
  49. {
  50. if(timer_interrupt_flag_get(TIMER14, TIMER_INT_FLAG_UP) == SET)
  51. {
  52. timer_interrupt_flag_clear(TIMER14, TIMER_INT_FLAG_UP);
  53. }
  54. if(board)
  55. {
  56. count_1s ++;
  57. for(int i = 0 ; i < RelaySlaveChaNum;i++)
  58. {
  59. if(IS_RL_COUNTER_BEGINE_FLAG_EN(board->relay_staging_staus[i]))
  60. {
  61. if(rl_delay_cnt[i] < board->relay_limit_counter[i])
  62. {
  63. rl_delay_cnt[i]++;
  64. }else
  65. {
  66. rl_delay_cnt[i] = 0;
  67. CLEAR_RL_COUNTER_BEGIN_FLAG(board->relay_staging_staus[i]);
  68. SET_RL_COUNTER_END_FLAG(board->relay_staging_staus[i]);
  69. }
  70. }
  71. if(rl_open_delay_flag[i])
  72. {
  73. if(rl_open_delay_count[i] < 3)
  74. {
  75. rl_open_delay_count[i]++;
  76. }else
  77. {
  78. rl_pwm_flag[i] = 1;
  79. rl_open_delay_count[i] = 0;
  80. rl_open_delay_flag[i] = 0;
  81. }
  82. }
  83. }
  84. if(count_1s == 100)
  85. {
  86. board->flag_1s = 1;
  87. count_1s = 0;
  88. count_10s ++;
  89. if(count_10s == 10)
  90. {
  91. board->flag_10s = 1;
  92. count_10s = 0;
  93. }
  94. }
  95. count_100ms++;
  96. if(count_100ms == 10)
  97. {
  98. count_100ms = 0;
  99. board->flag_100ms = 1;
  100. }
  101. board->detection_1_count++;
  102. if(board->detection_1_count >= 100)
  103. {
  104. board->detection_1_count = 0;
  105. board->detection_1_value = 0;
  106. }
  107. #if SURPPORT_2_PT
  108. board->detection_2_count++;
  109. if(board->detection_2_count >= 100)
  110. {
  111. board->detection_2_count = 0;
  112. board->detection_2_value = 0;
  113. }
  114. #endif
  115. }
  116. }
  117. static void timer15_config(uint16_t arr,uint16_t psc)
  118. {
  119. board = get_board();
  120. timer_parameter_struct timer_initpara;
  121. rcu_periph_clock_enable(RCU_TIMER15);
  122. timer_deinit(TIMER15);
  123. timer_struct_para_init(&timer_initpara);
  124. timer_initpara.prescaler = psc;
  125. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  126. timer_initpara.period = arr;
  127. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  128. timer_init(TIMER15, &timer_initpara);
  129. timer_interrupt_enable(TIMER15, TIMER_INT_UP);
  130. nvic_irq_enable(TIMER15_IRQn, 2);
  131. timer_enable(TIMER15);
  132. }
  133. void TIMER15_IRQHandler(void)
  134. {
  135. if(timer_interrupt_flag_get(TIMER15, TIMER_INT_FLAG_UP) == SET)
  136. {
  137. timer_interrupt_flag_clear(TIMER15, TIMER_INT_FLAG_UP);
  138. }
  139. board = get_board();
  140. for(int i = 0; i < RelaySlaveChaNum;i++)
  141. {
  142. if(board->md_data.r_data.state.state[i].state == 1 && rl_pwm_flag[i])
  143. {
  144. board->tog(i);
  145. }
  146. }
  147. }