第0组:所有4位用来配置响应优先级:16种中断向量具有都不相同的响应优先级。 第1组:最高1位用来配置抢占优先级,低3位用来配置响应优先级。表示有2种级别的抢占优先级(0级,1级),有8种响应优先级:有8种中断,其抢占优先级都为0级,响应优先级分别为0~7;其余8种中断向量的抢占优先级则都为1级,响应优先级别分别为0~7。 第2组:2位用来配置抢占优先级,2位用来配置响应优先级:4种抢占优先级,4 种响应优先级。 第3组:高3位用来配置抢占优先级,最低1位用来配置响应优先级:8种抢占优先级,2种响应2优先级。 第4组:所有4位用来配置抢占优先级:16种中断向量都是只有抢占属性,没有响应属性。
要配置这些优先级组,可以采用库函数NVIC_PriorityGroupConfig(),可输入的参数为NVIC_PriorityGroup_0~NVIC_PriorityGroup_4,分别为以上5种分配组。
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寄存器
void NVIC_PriorityGroup();//写在主程序中;
NVIC_Init(); //写在初始化配置中
{
//使能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);
TIM_Cmd(TIM3, ENABLE); //使能TIMx外设
}