赞
踩
系统运行后先设置中断优先级分组,即
在主函数调用函数:void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup);
整个系统执行过程中,只设置一次中断分组。
//配置过程
void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口1中断,此处可改为任何有中断功能的模块定时器,外部中断等,
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;// 抢占优先级为1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;// 子优先级位2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据上面指定的参数初始化NVIC寄存器
如果需要挂起/解挂,查看中断当前激活状态,分别调用相关函数即可。
优先级分组只是给抢占优先级与响应优先级在中断优先级寄存器中高四位分配各个优先级数字所占的位数,在程序中只能设定一次。
(1)void NVIC_PriorityGroup();//写在主程序中;
(2)NVIC_Init(); //写在初始化配置中
默认情况下,如何不设置中断优先级分组,则中断优先级分组默认为0,即0位抢占优先级,4位响应优先级,则无任何抢占优先级,所以无论中断的优先级设多少,都没有抢占中断的功能,所以,如果想要一个中断打断另一个中断,必须设置中断优先级分组为1,2,3,4.
如设置优先级分组为2:NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
{
假设NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1)则
// 使能EXTI0中断
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 指定抢占式优先级别1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 指定响应优先级别0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// 使能EXTI9_5中断
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // 指定抢占式优先级别0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // 指定响应优先级别1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//TIM3中断
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //响应优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
TIM_Cmd(TIM3, ENABLE); //使能TIMx外设
}
在上例子中,优先抢占优先级,其数字越低,优先级越高。相同下比较响应优先级,其数字越低,优先级越高
**Tips:**代码中抢占优先级0,1等,响应优先级(子优先级)0,1,3等,都是由自己设置的,但是必须在抢占优先级或者响应优先级设定的范围内。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。