修改程序,将TIM2设为高优先级,TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // 预分频器 TIM_TimeBaseStructure.TIM_Period = 0xffff; //设定计数器自动重装值 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//选择时钟分频:不分频 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
时间: 2024-01-18 22:06:43 浏览: 136
您可以使用以下代码将TIM2设置为高优先级:
```
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //设置抢占优先级为0,最高优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //设置子优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // 预分频器
TIM_TimeBaseStructure.TIM_Period = 0xffff; //设定计数器自动重装值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//选择时钟分频:不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ClearITPendingBit(TIM2, TIM_IT_Update); //清除中断标志位
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //使能更新中断
```
其中,前面的部分是将TIM2设置为高优先级的代码,后面的部分是配置TIM2定时器的代码。在配置中,将TIM2的抢占优先级设置为0,这样可以确保TIM2中断在系统中的优先级最高。同时,将TIM2的更新中断使能,并清除中断标志位,以防止出现误触发。
阅读全文