/********************************************************************************************************* * Module : timer * Abstract : defined data structure! * Version : 1.0.0 * Author : Leopul(COPYRIGHT 2025 ~ 2027 Leopul. All rights reserved.) * Complete : 2025-04-09 * Content : None * Note : None *********************************************************************************************************/ #include "timer.h" #include "gd32e23x.h" #include "board_cfg.h" #include "common.h" static board_t *board = 0; static void timer14_config(uint16_t arr,uint16_t psc); static void relay_remember(void); static uint16_t count_1s = 0; static uint16_t count_10s = 0; static uint16_t count_100ms = 0; static uint32_t read_count = 0; uint16_t rl_delay_cnt[RelaySlaveChaNum] = {0}; uint16_t rl_delay_zero_cnt = 0; void time_init(void) { timer14_config(9999,71); } static void timer14_config(uint16_t arr,uint16_t psc) { board = get_board(); timer_parameter_struct timer_initpara; rcu_periph_clock_enable(RCU_TIMER14); timer_deinit(TIMER14); timer_struct_para_init(&timer_initpara); timer_initpara.prescaler = psc; timer_initpara.counterdirection = TIMER_COUNTER_UP; timer_initpara.period = arr; timer_initpara.clockdivision = TIMER_CKDIV_DIV1; timer_init(TIMER14, &timer_initpara); timer_interrupt_enable(TIMER14, TIMER_INT_UP); nvic_irq_enable(TIMER14_IRQn, 2); timer_enable(TIMER14); } void TIMER14_IRQHandler(void) { if(timer_interrupt_flag_get(TIMER14, TIMER_INT_FLAG_UP) == SET) { timer_interrupt_flag_clear(TIMER14, TIMER_INT_FLAG_UP); } if(board) { count_1s ++; for(int i = 0 ; i < RelaySlaveChaNum;i++) { if(IS_RL_COUNTER_BEGINE_FLAG_EN(board->relay_staging_staus[i])) { if(rl_delay_cnt[i] < board->relay_limit_counter[i]) { rl_delay_cnt[i]++; }else { rl_delay_cnt[i] = 0; CLEAR_RL_COUNTER_BEGIN_FLAG(board->relay_staging_staus[i]); SET_RL_COUNTER_END_FLAG(board->relay_staging_staus[i]); } } } #if SUPPORT_ZERO_CRTL if(IS_RL_COUNTER_BEGINE_FLAG_EN(board->relay_zero_staging_status)) { if(rl_delay_zero_cnt < board->relay_zero_limit_counter) { rl_delay_zero_cnt++; }else { rl_delay_zero_cnt = 0; CLEAR_RL_COUNTER_BEGIN_FLAG(board->relay_zero_staging_status); SET_RL_COUNTER_END_FLAG(board->relay_zero_staging_status); } } #endif if(count_1s == 100) { board->flag_1s = 1; count_1s = 0; count_10s ++; if(count_10s == 10 ) { board->flag_10s = 1; count_10s = 0; } } count_100ms++; if(count_100ms == 10) { count_100ms = 0; board->flag_100ms = 1; } board->detection_1_count++; if(board->detection_1_count >= 100) { board->detection_1_count = 0; board->detection_1_value = 0; } #if SURPPORT_2_PT board->detection_2_count++; if(board->detection_2_count >= 100) { board->detection_2_count = 0; board->detection_2_value = 0; } #endif } }