void TIM3_Configuration(void) //每0.5ms发生一次更新事件(进入中断服务程序).
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_DeInit(TIM3); //重新将Timer设置为缺省值
TIM_InternalClockConfig(TIM3); //采用内部时钟给TIM3提供时钟源
TIM_ARRPreloadConfig(TIM3, DISABLE); //禁止ARR预装载缓冲器 预装载寄存器的内容被立即传送到影子寄存器
TIM_TimeBaseStructure.TIM_Prescaler = 24-1; //预分频系数为24-1,这样计数器计数频率为48MHz/24 = 2MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //设置计数器模式为向上计数模式
TIM_TimeBaseStructure.TIM_Period = 1000; //设置计数溢出大小,每计1000个数就产生一个更新事件
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //将配置应用到TIM3中
TIM_ClearFlag(TIM3, TIM_FLAG_Update); //清除溢出中断标志
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //使能TIM3的更新中断
TIM_Cmd(TIM3, ENABLE); //开启定时器3
//Enable the TIM3 Interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//---------------------------------0.5ms中断一次---------------------------------------
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3 TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
//添加代码
}
}