关闭

STM32之NVIC理解

378人阅读 评论(0) 收藏 举报
STM32有43个channel的settable的中断源;AIRC(Application Interrupt and Reset Register)寄存器中有用于指定优先级的4 bits。这4个bits用于分配preemption优先级和sub优先级,在STM32的固件库中定义如下
#define NVIC_PriorityGroup_0 ((u32)0x700)
#define NVIC_PriorityGroup_1 ((u32)0x600)
#define NVIC_PriorityGroup_2 ((u32)0x500)
#define NVIC_PriorityGroup_3 ((u32)0x400)
#define NVIC_PriorityGroup_4 ((u32)0x300)

每个工程只能有一种分组方式:

NVIC_PriorityGroup_0来分组:抢占优先级为1个,子优先级为16

NVIC_PriorityGroup_1来分组:抢占优先级为2,子优先级为8

NVIC_PriorityGroup_2来分组:抢占优先级为4,子优先级为4

NVIC_PriorityGroup_3来分组:抢占优先级为8,子优先级为2

NVIC_PriorityGroup_0来分组:抢占优先级为16,子优先级为1

抢占优先级高的中断可以打断抢占优先级低的中断;抢占优先级相同时比较响应优先级(子优先级)。

先设定分组方式

例:NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);    //NVIC_PriorityGroup_4来分组

此时,有一中断EXTI0_IRQChannel,你指定他为抢占优先级8,则
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 8; // 指定抢占式优先级别,可取0-15

又有1中断SPI1_IRQChannel,设定如下
NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // 指定抢占式优先级别,可取0-15

SPI1_IRQChannel的抢占优先级高,EXTI0_IRQChannel做事的时候可以打断(嵌套)


又:若分组方式为:NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);    //NVIC_PriorityGroup_3来分组

此时就有8个抢占优先级,2个子优先级;

EXTI0_IRQChannel中断,你指定他为抢占优先级3,则:
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; // 指定抢占式优先级别,可取0-7
还需要指定他的阶层:
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 指定响应优先级别,可取0-1

又有TIM2_IRQn中断,设定如下:

NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; 
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;// 指定抢占式优先级别,可取0-7

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; // 指定响应优先级别,可取0-1

由于抢占优先级相同,响应优先级EXTI0_IRQChannel中断高于TIM2_IRQn中断,所以EXTI0_IRQChannel可以打断TIM2_IRQn;

还有一USB_LP_CAN1_RX0_IRQn,设定如下:

NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;   //CAN1 RX0中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;   //响应优先级别0,可取0-1

USB_LP_CAN1_RX0_IRQn优先级最高,前两中断发生时,它都可以打断。

附:

void NVIC_Config(void)

{       

NVIC_InitTypeDef NVIC_InitStructure;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);//一个工程中只能使用一种分组方式
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; //CAN1 RX0中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;// 指定抢占式优先级别,可取0-7
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; // 指定响应优先级别,可取0-1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; // 指定抢占式优先级别,可取0-7
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 指定响应优先级别,可取0-1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1408次
    • 积分:25
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档