1、NVIC_PriorityGroupConfig有5个选项,选择优先级使用哪一组,他们在不同组的取值范围是:
NVIC_PriorityGroup_0 | PreemptionPriority=0(只看SubPriority) | SubPriority=0-15 |
NVIC_PriorityGroup_1 | PreemptionPriority=0-1 | SubPriority=0-7 |
NVIC_PriorityGroup_2 | PreemptionPriority=0-3 | SubPriority=0-3 |
NVIC_PriorityGroup_3 | PreemptionPriority=0-7 | SubPriority=0-1 |
NVIC_PriorityGroup_4 | PreemptionPriority=0-15 | SubPriority=0(只看PreemptionPriority) |
2、1组内相同的PreemptionPriority不能抢占,SubPriority低的优先执行,例如:
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3); // 设为第三组,PreemptionPriority=0-7
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; // 指定抢占式优先级别2,可取0-7
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // 指定响应优先级别1
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; // 跟上面一样,但不能抢占对方
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 指定响应优先级别0,那么比USART1_IRQChannel优先执行,他两不能互相中断
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 指定抢占式优先级别1,可以抢占上面2个
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 没影响,只跟同组PreemptionPriority的才有效
3、可见,如果系统中中断比较多,又要抢占的话,选NVIC_PriorityGroup_4即可。
4、敲黑板,注意了,一个系统只能调用一次NVIC_PriorityGroupConfig(),设置按最后调用的为准,那些多次调用来说优先级的,都他妈是扯淡的!