//TIM2时钟配置
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//定时器TIM2初始化
TIM_TimeBaseStructure.TIM_Period = 499; //装载值(499+1)=500us
TIM_TimeBaseStructure.TIM_Prescaler = 63; //预分频(时钟分频)64M/(63+1)=1M Hz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); //使能指定的TIM2中断,允许更新中断
TIM_Cmd(TIM2, ENABLE); //使能TIMx
}
定时时间计算公式:
计算:T=((1+499)*(1+63))/(64*1000)=0.5ms
注意:
1.系统时钟默认72MHz,我这里配置的64Mhz;
2.TIM_TimeBaseStructure.TIM_RepetitionCounter=0; //重复计数,重复溢出0次后溢出中断
(1)配置为0,计数器每溢出一次,中断一次。
(2)非0时,需配置重复速率TIMx_RCR寄存器。