一、介绍
一般使用多个中断时,就会考虑中断的重要性及其之间的优先级关系。HAL库中可以通过HAL_NVIC_SetPriority函数来设置中断的优先级,决定中断是否能够被抢占。
开发环境:
MCU:STM32L071CBT6
IDE:KEIL5、STM32CubeMX
二、函数的讲解
HAL_NVIC_SetPriority函数的定义如下:
/** * @brief Sets the priority of an interrupt. * @param IRQn External interrupt number . * This parameter can be an enumerator of IRQn_Type enumeration * (For the complete STM32 Devices IRQ Channels list, please refer to stm32l0xx.h file) * @param PreemptPriority The pre-emption priority for the IRQn channel. * This parameter can be a value between 0 and 3. * A lower priority value indicates a higher priority * @param SubPriority the subpriority level for the IRQ channel. * with stm32l0xx devices, this parameter is a dummy value and it is ignored, because * no subpriority supported in Cortex M0+ based products. * @retval None */ void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority) { /* Check the parameters */ assert_param(IS_NVIC_PREEMPTION_PRIORITY(PreemptPriority)); NVIC_SetPriority(IRQn,PreemptPriority); }第一个参数为要设置的中断号,第二个参数为抢占优先级,有着0~3四个等级,值越小表示的优先级越高。即抢占优先级0的中断的优先级高于抢占优先级1的中断。第三个参数为响应优先级,从底层代码注释可以看出对于Cortex M0+的产品不支持该参数,该参数不用设置。
三、配置
这里有两种方法修改抢占优先级。
1)通过CubeMx配置,如下图:
2)在代码初始化函数里直接修改
打开stm32l0xx_hal_msp.c文件,查看各个外设的MspInit函数里找到HAL_NVIC_SetPriority去修改抢占优先级。
两种方法最终都是在这边修改优先级。