#include "base.h" #include "stm32f4xx.h" #include "timer.h" static TIMER_UserStruct g_timer[9] = {0}; /* @初始化通用定时器 @TIM2\TIM3\TIM4\TIM5\TIM6\TIM7\TIM12\TIM13\TIM14 */ int TIMER_InitNormal(TIMER_InitStruct *init) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; NVIC_InitTypeDef NVIC_InitStructure; uint16_t arr; uint16_t psc; if (init == 0) return -1; if (init->Cycle > 60000) // 定时时间大于60毫秒 { // psc=84*500-1; //每100us计数器加1 psc = 90 * 100 - 1; arr = init->Cycle / 100 - 1; } else { // uint16_t t=60000/(init->Cycle*84)+1; // psc=84/t-1; uint16_t t = 60000 / (init->Cycle * 90) + 1; psc = 90 / t - 1; arr = t * init->Cycle - 1; } uint32_t timer = (uint32_t)(init->Tim - TIM2) / (TIM3 - TIM2); if (init->UpdataCall) { g_timer[timer].UpdataCall = init->UpdataCall; g_timer[timer].Inited = 1; } RCC_APB1PeriphClockCmd(1 << timer, ENABLE); // 自动重装载值 TIM_TimeBaseInitStructure.TIM_Period = arr; // 定时器分频 TIM_TimeBaseInitStructure.TIM_Prescaler = psc; // 向上计数模式 TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; // 初始化TIM TIM_TimeBaseInit(init->Tim, &TIM_TimeBaseInitStructure); // 允许定时器更新中断 TIM_ITConfig(init->Tim, TIM_IT_Update, ENABLE); // 使能定时器 TIM_Cmd(init->Tim, ENABLE); uint8_t irq_num = 0; if (init->Tim == TIM2) { irq_num = TIM2_IRQn; } else if (init->Tim == TIM3) { irq_num = TIM3_IRQn; } else if (init->Tim == TIM4) { irq_num = TIM4_IRQn; } else if (init->Tim == TIM5) { irq_num = TIM5_IRQn; } else if (init->Tim == TIM6) { // irq_num=TIM6_IRQn; } else if (init->Tim == TIM7) { irq_num = TIM7_IRQn; } else if (init->Tim == TIM12) { // irq_num=TIM12_IRQn; } else if (init->Tim == TIM13) { // irq_num=TIM13_IRQn; } else if (init->Tim == TIM14) { // irq_num=TIM14_IRQn; } // 定时器中断 NVIC_InitStructure.NVIC_IRQChannel = irq_num; // 抢占优先级1 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01; // 子优先级3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); return 0; } void TIMER_DeInit(TIM_TypeDef *TIMx) { TIM_DeInit(TIMx); uint32_t timer = (uint32_t)(TIMx - TIM2) / (TIM3 - TIM2); RCC_APB1PeriphClockCmd(1 << timer, DISABLE); g_timer[timer].UpdataCall = 0; g_timer[timer].Inited = 0; } void TIM2_IRQHandler(void) { const TIM_TypeDef *timer = TIM2; const uint8_t timer_num = 0; if (TIM_GetITStatus((TIM_TypeDef *)timer, TIM_IT_Update) == SET) // 溢出中断 { if (g_timer[timer_num].Inited == 1) { g_timer[timer_num].UpdataCall(); } TIM_ClearITPendingBit((TIM_TypeDef *)timer, TIM_IT_Update); // 清除中断标志位 } } void TIM3_IRQHandler(void) { const TIM_TypeDef *timer = TIM3; const uint8_t timer_num = 1; if (TIM_GetITStatus((TIM_TypeDef *)timer, TIM_IT_Update) == SET) // 溢出中断 { if (g_timer[timer_num].Inited == 1) { g_timer[timer_num].UpdataCall(); } TIM_ClearITPendingBit((TIM_TypeDef *)timer, TIM_IT_Update); // 清除中断标志位 } } void TIM4_IRQHandler(void) { const TIM_TypeDef *timer = TIM4; const uint8_t timer_num = 2; if (TIM_GetITStatus((TIM_TypeDef *)timer, TIM_IT_Update) == SET) // 溢出中断 { if (g_timer[timer_num].Inited == 1) { g_timer[timer_num].UpdataCall(); } TIM_ClearITPendingBit((TIM_TypeDef *)timer, TIM_IT_Update); // 清除中断标志位 } } void TIM5_IRQHandler(void) { const TIM_TypeDef *timer = TIM5; const uint8_t timer_num = 3; if (TIM_GetITStatus((TIM_TypeDef *)timer, TIM_IT_Update) == SET) // 溢出中断 { if (g_timer[timer_num].Inited == 1) { g_timer[timer_num].UpdataCall(); } TIM_ClearITPendingBit((TIM_TypeDef *)timer, TIM_IT_Update); // 清除中断标志位 } } void TIM7_IRQHandler(void) { const TIM_TypeDef *timer = TIM7; const uint8_t timer_num = 5; if (TIM_GetITStatus((TIM_TypeDef *)timer, TIM_IT_Update) == SET) // 溢出中断 { if (g_timer[timer_num].Inited == 1) { g_timer[timer_num].UpdataCall(); } TIM_ClearITPendingBit((TIM_TypeDef *)timer, TIM_IT_Update); // 清除中断标志位 } }