2024-01-20 13:19:09 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "timer.h"
|
|
|
|
#include "nvic.h"
|
|
|
|
|
2024-01-24 22:08:32 +08:00
|
|
|
void Timer_InitBase(TIM_TypeDef *TIMx, uint32_t Time_us)
|
2024-01-20 13:19:09 +08:00
|
|
|
{
|
|
|
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
|
|
|
|
|
|
|
if( TIMx == TIM2){
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE); /* enable clock */
|
|
|
|
TIM2_NVIC_Configuration(); //enable interruptation
|
|
|
|
}
|
|
|
|
else if( TIMx == TIM3){
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 , ENABLE); /* enable clock */
|
|
|
|
TIM3_NVIC_Configuration(); //enable interruptation
|
|
|
|
}
|
|
|
|
else if( TIMx == TIM4){
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 , ENABLE); /* enable clock */
|
|
|
|
TIM4_NVIC_Configuration(); //enable interruptation
|
|
|
|
}
|
|
|
|
else if( TIMx == TIM5){
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5 , ENABLE); /* enable clock */
|
|
|
|
TIM5_NVIC_Configuration(); //enable interruptation
|
|
|
|
}
|
|
|
|
else if( TIMx == TIM6){
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6 , ENABLE); /* enable clock */
|
|
|
|
TIM6_NVIC_Configuration(); //enable interruptation
|
|
|
|
}
|
|
|
|
else if( TIMx == TIM7){
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7 , ENABLE); /* enable clock */
|
|
|
|
TIM7_NVIC_Configuration(); //enable interruptation
|
|
|
|
}
|
|
|
|
TIM_DeInit(TIMx);
|
2024-01-24 22:08:32 +08:00
|
|
|
TIM_TimeBaseStructure.TIM_Period= Time_us-1; /* The value of auto reload register */
|
|
|
|
TIM_TimeBaseStructure.TIM_Prescaler= (72 - 1); /* Prescler : 72M/72 */
|
2024-01-20 13:19:09 +08:00
|
|
|
|
|
|
|
if(TIMx == TIM3 || TIMx == TIM4){ /* ClockDivision*/
|
|
|
|
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV4;
|
|
|
|
}else{
|
|
|
|
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
|
|
|
|
}
|
|
|
|
|
2024-01-24 22:08:32 +08:00
|
|
|
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; /* Count upwards */
|
2024-01-20 13:19:09 +08:00
|
|
|
TIM_TimeBaseInit(TIMx , &TIM_TimeBaseStructure);
|
2024-01-24 22:08:32 +08:00
|
|
|
TIM_ClearFlag(TIMx , TIM_FLAG_Update); /* Clear interrupt flags */
|
2024-01-20 13:19:09 +08:00
|
|
|
TIM_ITConfig(TIMx ,TIM_IT_Update,ENABLE);
|
|
|
|
TIM_Cmd(TIMx , ENABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|