timer.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 relay_remember(void);
  17. static uint16_t count_1s = 0;
  18. static uint16_t count_10s = 0;
  19. static uint16_t count_100ms = 0;
  20. static uint32_t read_count = 0;
  21. uint16_t rl_delay_cnt[RelaySlaveChaNum] = {0};
  22. uint16_t rl_delay_zero_cnt = 0;
  23. void time_init(void)
  24. {
  25. timer14_config(9999,71);
  26. }
  27. static void timer14_config(uint16_t arr,uint16_t psc)
  28. {
  29. board = get_board();
  30. timer_parameter_struct timer_initpara;
  31. rcu_periph_clock_enable(RCU_TIMER14);
  32. timer_deinit(TIMER14);
  33. timer_struct_para_init(&timer_initpara);
  34. timer_initpara.prescaler = psc;
  35. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  36. timer_initpara.period = arr;
  37. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  38. timer_init(TIMER14, &timer_initpara);
  39. timer_interrupt_enable(TIMER14, TIMER_INT_UP);
  40. nvic_irq_enable(TIMER14_IRQn, 2);
  41. timer_enable(TIMER14);
  42. }
  43. void TIMER14_IRQHandler(void)
  44. {
  45. if(timer_interrupt_flag_get(TIMER14, TIMER_INT_FLAG_UP) == SET)
  46. {
  47. timer_interrupt_flag_clear(TIMER14, TIMER_INT_FLAG_UP);
  48. }
  49. if(board)
  50. {
  51. count_1s ++;
  52. for(int i = 0 ; i < RelaySlaveChaNum;i++)
  53. {
  54. if(IS_RL_COUNTER_BEGINE_FLAG_EN(board->relay_staging_staus[i]))
  55. {
  56. if(rl_delay_cnt[i] < board->relay_limit_counter[i])
  57. {
  58. rl_delay_cnt[i]++;
  59. }else
  60. {
  61. rl_delay_cnt[i] = 0;
  62. CLEAR_RL_COUNTER_BEGIN_FLAG(board->relay_staging_staus[i]);
  63. SET_RL_COUNTER_END_FLAG(board->relay_staging_staus[i]);
  64. }
  65. }
  66. }
  67. #if SUPPORT_ZERO_CRTL
  68. if(IS_RL_COUNTER_BEGINE_FLAG_EN(board->relay_zero_staging_status))
  69. {
  70. if(rl_delay_zero_cnt < board->relay_zero_limit_counter)
  71. {
  72. rl_delay_zero_cnt++;
  73. }else
  74. {
  75. rl_delay_zero_cnt = 0;
  76. CLEAR_RL_COUNTER_BEGIN_FLAG(board->relay_zero_staging_status);
  77. SET_RL_COUNTER_END_FLAG(board->relay_zero_staging_status);
  78. }
  79. }
  80. #endif
  81. if(count_1s == 100)
  82. {
  83. board->flag_1s = 1;
  84. count_1s = 0;
  85. count_10s ++;
  86. if(count_10s == 10 )
  87. {
  88. board->flag_10s = 1;
  89. count_10s = 0;
  90. }
  91. }
  92. count_100ms++;
  93. if(count_100ms == 10)
  94. {
  95. count_100ms = 0;
  96. board->flag_100ms = 1;
  97. }
  98. board->detection_1_count++;
  99. if(board->detection_1_count >= 100)
  100. {
  101. board->detection_1_count = 0;
  102. board->detection_1_value = 0;
  103. }
  104. #if SURPPORT_2_PT
  105. board->detection_2_count++;
  106. if(board->detection_2_count >= 100)
  107. {
  108. board->detection_2_count = 0;
  109. board->detection_2_value = 0;
  110. }
  111. #endif
  112. }
  113. }