首先了解下中断的优先级,分为抢占优先级和响应优先级,抢占优先级的级别高于响应优先级。
中断的示例模板见下:
void NVIC_ini() //中断配置示例函数
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置分组
//设置中断优先级
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //中断通道
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //响应优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //抢占优先级
NVIC_Init(&NVIC_InitStructure); //中断初始化
}