2025-06-27 00:32:57 +08:00
|
|
|
|
#include "base.h"
|
|
|
|
|
#include "timer.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static TIMER_UserStruct g_timer[9]={0};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2025-07-05 19:47:28 +08:00
|
|
|
|
@<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ͨ<EFBFBD>ö<EFBFBD>ʱ<EFBFBD><EFBFBD>
|
2025-06-27 00:32:57 +08:00
|
|
|
|
@TIM2\TIM3\TIM4\TIM5\TIM6\TIM7\TIM12\TIM13\TIM14
|
|
|
|
|
*/
|
|
|
|
|
int TIMER_InitNormal (TIMER_InitStruct *init)
|
|
|
|
|
{
|
|
|
|
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
|
|
|
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|
|
|
|
u16 arr;u16 psc;
|
|
|
|
|
if (init==0) return -1;
|
2025-07-05 19:47:28 +08:00
|
|
|
|
if (init->Cycle>60000)//<2F><>ʱʱ<CAB1><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>60<36><30><EFBFBD><EFBFBD>
|
2025-06-27 00:32:57 +08:00
|
|
|
|
{
|
2025-07-05 19:47:28 +08:00
|
|
|
|
//psc=84*500-1; //ÿ100us<75><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1
|
2025-06-27 00:32:57 +08:00
|
|
|
|
psc=90*100-1;
|
|
|
|
|
arr=init->Cycle/100-1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//u16 t=60000/(init->Cycle*84)+1;
|
|
|
|
|
//psc=84/t-1;
|
|
|
|
|
u16 t=60000/(init->Cycle*90)+1;
|
|
|
|
|
psc=90/t-1;
|
|
|
|
|
arr=t*init->Cycle-1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u32 timer=(u32)(init->Tim-TIM2)/(TIM3-TIM2);
|
|
|
|
|
if (init->UpdataCall)
|
|
|
|
|
{
|
|
|
|
|
g_timer[timer].UpdataCall=init->UpdataCall;
|
|
|
|
|
g_timer[timer].Inited=1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RCC_APB1PeriphClockCmd(1<<timer,ENABLE);
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
TIM_TimeBaseInitStructure.TIM_Period = arr; //<2F>Զ<EFBFBD><D4B6><EFBFBD>װ<EFBFBD><D7B0>ֵ
|
|
|
|
|
TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //<2F><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ƶ
|
|
|
|
|
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //<2F><><EFBFBD>ϼ<EFBFBD><CFBC><EFBFBD>ģʽ
|
2025-06-27 00:32:57 +08:00
|
|
|
|
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
TIM_TimeBaseInit(init->Tim,&TIM_TimeBaseInitStructure);//<2F><>ʼ<EFBFBD><CABC>TIM
|
2025-06-27 00:32:57 +08:00
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
TIM_ITConfig(init->Tim,TIM_IT_Update,ENABLE); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|
|
|
|
TIM_Cmd(init->Tim,ENABLE); //ʹ<>ܶ<EFBFBD>ʱ<EFBFBD><CAB1>3
|
2025-06-27 00:32:57 +08:00
|
|
|
|
|
|
|
|
|
u8 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannel=irq_num; //<2F><>ʱ<EFBFBD><CAB1><EFBFBD>ж<EFBFBD>
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //<2F><>ռ<EFBFBD><D5BC><EFBFBD>ȼ<EFBFBD>1
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //<2F><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>3
|
2025-06-27 00:32:57 +08:00
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
|
|
|
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TIMER_DeInit (TIM_TypeDef* TIMx)
|
|
|
|
|
{
|
|
|
|
|
TIM_DeInit (TIMx);
|
|
|
|
|
u32 timer=(u32)(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 u8 timer_num=0;
|
2025-07-05 19:47:28 +08:00
|
|
|
|
if(TIM_GetITStatus((TIM_TypeDef *)timer,TIM_IT_Update)==SET) //<2F><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
2025-06-27 00:32:57 +08:00
|
|
|
|
{
|
|
|
|
|
if (g_timer[timer_num].Inited==1)
|
|
|
|
|
{
|
|
|
|
|
g_timer[timer_num].UpdataCall();
|
|
|
|
|
}
|
2025-07-05 19:47:28 +08:00
|
|
|
|
TIM_ClearITPendingBit((TIM_TypeDef *)timer,TIM_IT_Update); //<2F><><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־λ
|
2025-06-27 00:32:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
void TIM3_IRQHandler (void)
|
|
|
|
|
{
|
|
|
|
|
const TIM_TypeDef *timer=TIM3;
|
|
|
|
|
const u8 timer_num=1;
|
2025-07-05 19:47:28 +08:00
|
|
|
|
if(TIM_GetITStatus((TIM_TypeDef *)timer,TIM_IT_Update)==SET) //<2F><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
2025-06-27 00:32:57 +08:00
|
|
|
|
{
|
|
|
|
|
if (g_timer[timer_num].Inited==1)
|
|
|
|
|
{
|
|
|
|
|
g_timer[timer_num].UpdataCall();
|
|
|
|
|
}
|
2025-07-05 19:47:28 +08:00
|
|
|
|
TIM_ClearITPendingBit((TIM_TypeDef *)timer,TIM_IT_Update); //<2F><><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־λ
|
2025-06-27 00:32:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void TIM4_IRQHandler (void)
|
|
|
|
|
{
|
|
|
|
|
const TIM_TypeDef *timer=TIM4;
|
|
|
|
|
const u8 timer_num=2;
|
2025-07-05 19:47:28 +08:00
|
|
|
|
if(TIM_GetITStatus((TIM_TypeDef *)timer,TIM_IT_Update)==SET) //<2F><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
2025-06-27 00:32:57 +08:00
|
|
|
|
{
|
|
|
|
|
if (g_timer[timer_num].Inited==1)
|
|
|
|
|
{
|
|
|
|
|
g_timer[timer_num].UpdataCall();
|
|
|
|
|
}
|
2025-07-05 19:47:28 +08:00
|
|
|
|
TIM_ClearITPendingBit((TIM_TypeDef *)timer,TIM_IT_Update); //<2F><><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־λ
|
2025-06-27 00:32:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void TIM5_IRQHandler (void)
|
|
|
|
|
{
|
|
|
|
|
const TIM_TypeDef *timer=TIM5;
|
|
|
|
|
const u8 timer_num=3;
|
2025-07-05 19:47:28 +08:00
|
|
|
|
if(TIM_GetITStatus((TIM_TypeDef *)timer,TIM_IT_Update)==SET) //<2F><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
2025-06-27 00:32:57 +08:00
|
|
|
|
{
|
|
|
|
|
if (g_timer[timer_num].Inited==1)
|
|
|
|
|
{
|
|
|
|
|
g_timer[timer_num].UpdataCall();
|
|
|
|
|
}
|
2025-07-05 19:47:28 +08:00
|
|
|
|
TIM_ClearITPendingBit((TIM_TypeDef *)timer,TIM_IT_Update); //<2F><><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־λ
|
2025-06-27 00:32:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void TIM7_IRQHandler (void)
|
|
|
|
|
{
|
|
|
|
|
const TIM_TypeDef *timer=TIM7;
|
|
|
|
|
const u8 timer_num=5;
|
2025-07-05 19:47:28 +08:00
|
|
|
|
if(TIM_GetITStatus((TIM_TypeDef *)timer,TIM_IT_Update)==SET) //<2F><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
2025-06-27 00:32:57 +08:00
|
|
|
|
{
|
|
|
|
|
if (g_timer[timer_num].Inited==1)
|
|
|
|
|
{
|
|
|
|
|
g_timer[timer_num].UpdataCall();
|
|
|
|
|
}
|
2025-07-05 19:47:28 +08:00
|
|
|
|
TIM_ClearITPendingBit((TIM_TypeDef *)timer,TIM_IT_Update); //<2F><><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־λ
|
2025-06-27 00:32:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|