timer.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 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. uint16_t rl_delay_cnt[RelaySlaveChaNum] = {0};
  20. uint16_t rl_delay_zero_cnt[ZERO_CTRL_NUM] = {0};
  21. void time_init(void)
  22. {
  23. timer14_config(9999,71);
  24. }
  25. static 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. board->detection_1_count ++;
  51. if(board->detection_1_count >= 100)
  52. {
  53. #if (POWER_TYPE != POWER_DC)
  54. board->detection_1_value = 0;
  55. #endif
  56. board->detection_1_count = 0;
  57. }
  58. #if SURPPORT_2_PT
  59. board->detection_2_count ++;
  60. if(board->detection_2_count >= 100)
  61. {
  62. #if (POWER_TYPE != POWER_DC)
  63. board->detection_2_value = 0;
  64. #endif
  65. board->detection_2_count = 0;
  66. }
  67. #endif
  68. for(int i = 0 ; i < RelaySlaveChaNum;i++)
  69. {
  70. if(IS_RL_COUNTER_BEGINE_FLAG_EN(board->relay_staging_staus[i]))
  71. {
  72. if(rl_delay_cnt[i] < board->relay_limit_counter[i])
  73. {
  74. rl_delay_cnt[i]++;
  75. }else
  76. {
  77. rl_delay_cnt[i] = 0;
  78. CLEAR_RL_COUNTER_BEGIN_FLAG(board->relay_staging_staus[i]);
  79. SET_RL_COUNTER_END_FLAG(board->relay_staging_staus[i]);
  80. }
  81. }
  82. }
  83. for(int i = 0 ; i < ZERO_CTRL_NUM;i++)
  84. {
  85. if(IS_RL_COUNTER_BEGINE_FLAG_EN(board->relay_zero_staging_status[i]))
  86. {
  87. if(rl_delay_zero_cnt[i] < board->relay_zero_limit_counter[i])
  88. {
  89. rl_delay_zero_cnt[i]++;
  90. }else
  91. {
  92. rl_delay_zero_cnt[i] = 0;
  93. CLEAR_RL_COUNTER_BEGIN_FLAG(board->relay_zero_staging_status[i]);
  94. SET_RL_COUNTER_END_FLAG(board->relay_zero_staging_status[i]);
  95. }
  96. }
  97. }
  98. if(count_1s == 100)
  99. {
  100. board->flag_1s = 1;
  101. count_1s = 0;
  102. count_10s ++;
  103. if(count_10s == 10)
  104. {
  105. board->flag_10s = 1;
  106. count_10s = 0;
  107. }
  108. }
  109. }
  110. }