| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- /*********************************************************************************************************
- * Module : Timer.c
- * Abstract : Timer modules, this module is used to improve Timing service!
- * Version : 1.0.0
- * Author : Leopul(COPYRIGHT 2025 - 2027 Leopul. All rights reserved.)
- * Complete : 2025-02-16
- * Content : None
- * Note : None
- *********************************************************************************************************/
- #include "Timer.h"
- #include "gd32e230x_conf.h"
- #include "main.h"
- static unsigned char s_i100msFlag = 0;
- static unsigned char s_i2msFlag = 0;
- static unsigned char s_i1secFlag = 0;
- volatile unsigned long int g_del_ms =0;
- static void ConfigTimer14(unsigned short arr, unsigned short psc);
- static void ConfigTimer15(unsigned short arr, unsigned short psc);
- static void ConfigTimer16(unsigned short arr, unsigned short psc);
- static sts_data_t *data = 0;
- static void ConfigTimer14(unsigned short arr, unsigned short psc)
- {
- 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);
- }
- static void ConfigTimer15(unsigned short arr, unsigned short psc)
- {
- timer_parameter_struct timer_initpara;
- rcu_periph_clock_enable(RCU_TIMER15);
- timer_deinit(TIMER15);
- 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(TIMER15, &timer_initpara);
- timer_interrupt_enable(TIMER15, TIMER_INT_UP);
- nvic_irq_enable(TIMER15_IRQn, 3);
- timer_enable(TIMER15);
- }
- static void ConfigTimer16(unsigned short arr,unsigned short psc)
- {
- timer_parameter_struct timer_initpara;
- rcu_periph_clock_enable(RCU_TIMER16);
- timer_deinit(TIMER16);
- 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(TIMER16, &timer_initpara);
- timer_interrupt_enable(TIMER16, TIMER_INT_UP);
- nvic_irq_enable(TIMER16_IRQn, 3);
- timer_enable(TIMER16);
- }
- void TIMER14_IRQHandler(void)
- {
- //static unsigned short s_iCnt2 = 0;
- static uint16_t cnt_1000 = 0;
- static uint8_t cnt_100 = 0;
- static uint8_t cnt_5 = 0;
- if(timer_interrupt_flag_get(TIMER14, TIMER_INT_FLAG_UP) == SET)
- {
- timer_interrupt_flag_clear(TIMER14, TIMER_INT_FLAG_UP);
- }
- cnt_1000++;
- cnt_100++;
- if(cnt_1000 >=1000)
- {
- data->flag_1000ms = 1;
- cnt_1000 = 0;
- }
- if(cnt_100 >= 100)
- {
- data->flag_100ms = 1;
- cnt_100 = 0;
- }
- }
- void TIMER15_IRQHandler(void)
- {
- static unsigned short s_iCnt100 = 0;
- if(timer_interrupt_flag_get(TIMER15, TIMER_INT_FLAG_UP) == SET)
- {
- timer_interrupt_flag_clear(TIMER15, TIMER_INT_FLAG_UP);
- }
- s_iCnt100++;
-
- if(s_iCnt100 >= 100)
- {
- s_iCnt100 = 0;
- s_i100msFlag = 1;
- }
- }
- extern uint8_t read_kWh_flag;
- void TIMER16_IRQHandler(void)
- {
- static short s_iCnt1000 = 0;
- static uint32_t cnt = 0;
- if (timer_interrupt_flag_get(TIMER16, TIMER_INT_FLAG_UP) == SET)
- {
- timer_interrupt_flag_clear(TIMER16, TIMER_INT_FLAG_UP);
- }
-
- }
- void InitTimer(void)
- {
- ConfigTimer14(999, 71); //72MHz/(71+1)=1MHz,由0计数到999为1ms
- // ConfigTimer15(999, 71); //72MHz/(71+1)=1MHz,由0计数到999为1ms
- // ConfigTimer16(999, 71); //72MHz/(71+1)=1MHz,由0计数到999为1ms
- data = get_sts();
- }
- unsigned char Get2msFlag(void)
- {
- return(s_i2msFlag);
- }
- void Clr2msFlag(void)
- {
- s_i2msFlag = 0;
- }
- unsigned char Get100msFlag(void)
- {
- return(s_i100msFlag);
- }
- void Clr100msFlag(void)
- {
- s_i100msFlag = 0;
- }
- unsigned char Get1SecFlag(void)
- {
- return(s_i1secFlag);
- }
- void Clr1SecFlag(void)
- {
- s_i1secFlag = 0;
- }
|