频率计算公式为,使用48M晶振:48000000/48/100=10000HZ
可得:频率为10K HZ
中断时间为:1/10000=100us
void TIM3_Init(void)//0.1ms
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //tim3时钟使能,APB时钟48M
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //分频系数为1
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInitStructure.TIM_Period = 100 - 1;
TIM_TimeBaseInitStructure.TIM_Prescaler = 48 - 1; //定时100us
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //允许定时器3更新中断
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //定时器3中断
NVIC_InitStructure.NVIC_IRQChannelPriority = 0; //优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM3, ENABLE); //使能定时器3
}
void TIM3_IRQHandler(void)//100us
{
if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //溢出中断
{
cnt10ms++;
cnt20ms++;
cnt1ms++;
cnt1s++;
TIM_ClearITPendingBit(TIM3, TIM_IT_Update); //清除中断标志位
}
}