NVIC_PriorityGroupConfig 中断优先级设置,用最短代码讲解

1、NVIC_PriorityGroupConfig有5个选项,选择优先级使用哪一组,他们在不同组的取值范围是:



NVIC_PriorityGroup_0PreemptionPriority=0(只看SubPriority)SubPriority=0-15
NVIC_PriorityGroup_1PreemptionPriority=0-1SubPriority=0-7
NVIC_PriorityGroup_2PreemptionPriority=0-3SubPriority=0-3
NVIC_PriorityGroup_3PreemptionPriority=0-7SubPriority=0-1
NVIC_PriorityGroup_4PreemptionPriority=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(),设置按最后调用的为准,那些多次调用来说优先级的,都他妈是扯淡的!


本文为3YL原创,转载无需联系,但请注明来自labisart.com。

原创文章不易,如果觉得有帮助,可打赏或点击右侧广告支持:

查看打赏记录

发表评论请遵守党国法律!后台审核后方可显示!
  • 最新评论
  • 总共0条评论
  • Blog v1.1© 2024 labisart.com 版权所有 | 联系:labartwork@163.com