作者:阿拉丁神丢
转自:http://blog.sina.com.cn/s/blog_6035432c0100y51q.html
void Timer_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 0x1F3; //499,自动重装载寄存器,当计数器到达这个寄存器 锁存的数值时,溢出产生事件 500 *2us
TIM_TimeBaseStructure.TIM_Prescaler = 0x47; //71,TIM时钟频率 36/72 = 0.5MHZ
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//设置计数模式,向上计数
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);//初始化定时器
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_Cmd(TIM3, DISABLE);//失能TIM外设
TIM_Cmd(TIM4, DISABLE);
TIM_PrescalerConfig(TIM3, 0x47, TIM_PSCReloadMode_Immediate);//马上装载预分频值
TIM_PrescalerConfig(TIM4, 0x47, TIM_PSCReloadMode_Immediate);
TIM_ClearFlag(TIM3, TIM_FLAG_Update);//清除TIM更新标志
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
TIM_ITConfig(TIM3, TIM_IT_Update,ENABLE );//失能TIM更新中断
TIM_ITConfig(TIM4, TIM_IT_Update,ENABLE);
}
中断处理函数
void TIM3_IRQHandler(void)
{
/* Clear TIM3 update interrupt
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
do_();
}
void TIM4_IRQHandler(void)
{
/* Clear TIM4 update interrupt
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
do_();
}