NVIC_IRQChannelPreemptionPriority:先占优先级
NVIC_IRQChannelSubPriority:从优先级
高抢占优先级中断可以打断低抢占优先级的中断,即可中断嵌套。抢占优先级号码越小优先级越高当抢占优先级号码相同时,从优先级号码起作用。同抢占优先级下的中断是不能进行嵌套的,只能根据从优先级的号码进行排队,从优先级号码即为排队号码,号码越低越靠前。
如买火车票一样,系统中设置学生、军人的优先级(抢占优先级)高于普通群众,学生、军人可以抢占系统通道,得到优先买票的权力,但学生、军人之间是不能进行抢占的,只能根据排队(从优先级)来决定谁可以先买到票。
先用函数NVIC_PriorityGroupConfig(NVIC_PriorityGroup_x)决定有几个先占优先级和从优先级。NVIC_PriorityGroup_x可取如下:
NVIC_PriorityGroup_0 => 选择第0组
NVIC_PriorityGroup_1 => 选择第1组
NVIC_PriorityGroup_2 => 选择第2组
NVIC_PriorityGroup_3 => 选择第3组
NVIC_PriorityGroup_4 => 选择第4组
举例:
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//配置为1组。
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // 指定抢占式优先级别0,可取0-1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能中断
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 指定抢占式优先级别1,可取0-1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 指定从优先级别0,可取0-7
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // 指定从优先级别1,可取0-7
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
EXTI0_IRQChannel 抢占优先级比SPI1_IRQChannel、USART1_IRQChannel高,所以EXTI0_IRQChannel中断可以打断正在执行的SPI1_IRQChannel/USART1_IRQChannel中断,实现中断嵌套。
SPI1_IRQChannel、USART1_IRQChannel抢占优先级相等,不能相互打断,即在执行SPI1_IRQChannel/USART1_IRQChannel中断时USART1_IRQChannel/SPI1_IRQChannel中断无法打断。同时产生SPI1_IRQChannel、USART1_IRQChannel中断时,要根据从优先级进行排队,SPI1_IRQChannel从优先级比较高,所以先执行SPI1_IRQChannel中断。
|