timer.c 2.6 KB

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