STM32学习笔记-NVIC中断知识点

时间:2022-08-15 14:37:32

STM32学习笔记-NVIC中断知识点总结

中断优先级设置步骤

1. 系统运行后先设置中断优先级分组

函数:void NVIC_PriorityGroupConfig(uint32_tNVIC_PriorityGroup)

分组配置寄存器SCB->AIRCR

AIRCR[10:8] IP bit[7:4]分配情况 分配结果
0 111 0:4 0位抢占优先级,4位
1 110 1:3 1位抢占优先级,3位响应优先级
2 101 2:2 2位抢占优先级,2位
3 100 3:1 3位抢占优先级,1位响应优先级
4 100 4:0 4位抢占优先级,0位响应优先级

抢占优先级&响应优先级区别:

  • 首先判断抢占优先级,抢占优先级越高越先响应,高抢占优先级可以打断低响应优先级
  • 抢占优先级相同的情况下当同时发生时响应优先级越高的先发生,高响应优先级不能打算低响应优先级
  • 都相同的情况下哪个先来哪个先发生

2. 针对每个中断设置对应的抢占优先级和响应优先级

函数:void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)

typedef struct
{
uint8_t NVIC_IRQChannel; //设置中断通道
uint8_t NVIC_IRQChannelPreemptionPriority; //设置响应优先级
uint8_t NVIC_IRQChannelSubPriority; //设置抢占优先级
FunctionalState NVIC_IRQChannelCmd; //通道使能
} NVIC_InitTypeDef; //例如开启串口一中断
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寄存器

3. 如需挂起/解挂,查看当前激活状态,分别调用相关函数即可

函数:

  • **static _INLINE void NVIC_SetPendingIRQ(IRQn_Type IRQn) **
  • **static _INLINE uint32_t NVIC_GetPendingIRQ(IRQn_Type IRQn) **
  • **static _INLINE void NVIC_ClearPendingIRQ(IRQn_Type IRQn) **