stm32f103系列定时器计算方法
频率计算方法:72000000/72/1000=1000Hz
定时器中断时间:1/1000Hz=0.001s //1ms
TIM4代码如下:
void TIM4_1ms_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel=TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseInitStructure.TIM_Period = 1000-1; //1ms
TIM_TimeBaseInitStructure.TIM_Prescaler=72-1; //72分频=1MHz
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上计数模式
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM4,ENABLE);
}
//定时器中断
void TIM4_IRQHandler(void)//定时器中断 1ms
{
if(TIM_GetITStatus(TIM4,TIM_IT_Update)==SET)
{
time1_cntr++;//根据需要添加
}
TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
}