2025-06-27 00:32:57 +08:00
|
|
|
|
#include "base.h"
|
2025-10-18 13:58:40 +08:00
|
|
|
|
#include "stm32f4xx.h"
|
2025-06-27 00:32:57 +08:00
|
|
|
|
#include "timer.h"
|
|
|
|
|
|
2025-10-18 13:58:40 +08:00
|
|
|
|
static TIMER_UserStruct g_timer[9] = {0};
|
2025-06-27 00:32:57 +08:00
|
|
|
|
|
|
|
|
|
/*
|
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
|
|
|
|
|
*/
|
2025-10-18 13:58:40 +08:00
|
|
|
|
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) // <20><>ʱʱ<CAB1><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>60<36><30><EFBFBD><EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
// psc=84*500-1; //ÿ100us<75><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>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);
|
|
|
|
|
// <20>Զ<EFBFBD><D4B6><EFBFBD>װ<EFBFBD><D7B0>ֵ
|
|
|
|
|
TIM_TimeBaseInitStructure.TIM_Period = arr;
|
|
|
|
|
// <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ƶ
|
|
|
|
|
TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
|
|
|
|
|
// <20><><EFBFBD>ϼ<EFBFBD><CFBC><EFBFBD>ģʽ
|
|
|
|
|
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
|
|
|
|
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
|
|
|
|
|
// <20><>ʼ<EFBFBD><CABC>TIM
|
|
|
|
|
TIM_TimeBaseInit(init->Tim, &TIM_TimeBaseInitStructure);
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|
|
|
|
TIM_ITConfig(init->Tim, TIM_IT_Update, ENABLE);
|
|
|
|
|
// ʹ<>ܶ<EFBFBD>ʱ<EFBFBD><CAB1>
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><>ʱ<EFBFBD><CAB1><EFBFBD>ж<EFBFBD>
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = irq_num;
|
|
|
|
|
// <20><>ռ<EFBFBD><D5BC><EFBFBD>ȼ<EFBFBD>1
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>3
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03;
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
|
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
|
return 0;
|
2025-06-27 00:32:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-10-18 13:58:40 +08:00
|
|
|
|
void TIMER_DeInit(TIM_TypeDef *TIMx) {
|
|
|
|
|
TIM_DeInit(TIMx);
|
|
|
|
|
uint32_t timer = (uint32_t)(TIMx - TIM2) / (TIM3 - TIM2);
|
2025-06-27 00:32:57 +08:00
|
|
|
|
|
2025-10-18 13:58:40 +08:00
|
|
|
|
RCC_APB1PeriphClockCmd(1 << timer, DISABLE);
|
|
|
|
|
g_timer[timer].UpdataCall = 0;
|
|
|
|
|
g_timer[timer].Inited = 0;
|
2025-06-27 00:32:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-10-18 13:58:40 +08:00
|
|
|
|
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) // <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
if (g_timer[timer_num].Inited == 1) {
|
|
|
|
|
g_timer[timer_num].UpdataCall();
|
|
|
|
|
}
|
|
|
|
|
TIM_ClearITPendingBit((TIM_TypeDef *)timer, TIM_IT_Update); // <20><><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־λ
|
|
|
|
|
}
|
2025-06-27 00:32:57 +08:00
|
|
|
|
}
|
2025-10-18 13:58:40 +08:00
|
|
|
|
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) // <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
if (g_timer[timer_num].Inited == 1) {
|
|
|
|
|
g_timer[timer_num].UpdataCall();
|
|
|
|
|
}
|
|
|
|
|
TIM_ClearITPendingBit((TIM_TypeDef *)timer, TIM_IT_Update); // <20><><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־λ
|
|
|
|
|
}
|
2025-06-27 00:32:57 +08:00
|
|
|
|
}
|
2025-10-18 13:58:40 +08:00
|
|
|
|
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) // <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
if (g_timer[timer_num].Inited == 1) {
|
|
|
|
|
g_timer[timer_num].UpdataCall();
|
|
|
|
|
}
|
|
|
|
|
TIM_ClearITPendingBit((TIM_TypeDef *)timer, TIM_IT_Update); // <20><><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־λ
|
|
|
|
|
}
|
2025-06-27 00:32:57 +08:00
|
|
|
|
}
|
2025-10-18 13:58:40 +08:00
|
|
|
|
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) // <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
if (g_timer[timer_num].Inited == 1) {
|
|
|
|
|
g_timer[timer_num].UpdataCall();
|
|
|
|
|
}
|
|
|
|
|
TIM_ClearITPendingBit((TIM_TypeDef *)timer, TIM_IT_Update); // <20><><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־λ
|
|
|
|
|
}
|
2025-06-27 00:32:57 +08:00
|
|
|
|
}
|
2025-10-18 13:58:40 +08:00
|
|
|
|
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) // <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
if (g_timer[timer_num].Inited == 1) {
|
|
|
|
|
g_timer[timer_num].UpdataCall();
|
|
|
|
|
}
|
|
|
|
|
TIM_ClearITPendingBit((TIM_TypeDef *)timer, TIM_IT_Update); // <20><><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־λ
|
|
|
|
|
}
|
2025-06-27 00:32:57 +08:00
|
|
|
|
}
|