当前位置: 首页> 健康> 养生 > STM32嵌套向量中断控制器—NVIC

STM32嵌套向量中断控制器—NVIC

时间:2025/9/6 14:49:43来源:https://blog.csdn.net/m0_73951316/article/details/141607351 浏览次数:0次

NVIC简介:

        NVIC,即Nested Vectored Interrupt Controller(嵌套向量中断控制器),是STM32中的中断控制器。它负责管理和协调处理器的中断请求,是STM32中处理异步事件的重要机制。
NVIC提供了灵活、高效、可扩展的中断处理机制,支持多级优先级、多向中断、嵌套向量中断等特性。当一个中断请求到达时,NVIC会确定其优先级并决定是否应该中断当前执行的程序,以便及时响应和处理该中断请求。这种设计有助于提高系统的响应速度和可靠性,特别是在需要处理大量中断请求的实时应用程序中。
        NVIC 支持:256个中断(16内核+240外部),支持:256个优先级,允许裁剪。

NVIC工作原理:

中断优先级基本概念

    NVIC可以管理多个中断请求,并按优先级处理它们。在STM32中,中断优先级被划分为抢占式优先级和响应优先级,可以根据具体的应用需求进行配置。不同的优先级分组方式会影响中断的响应和处理顺序。

抢占优先级:

        如果一个中断的抢占优先级高于当前正在执行的中断,那么它可以打断当前中断,优先得到执行。数值越小,优先级越高。

响应优先级:

        如果两个中断同时到达,且它们的抢占优先级相同,那么响应优先级高的中断将首先得到响应。数值越小,优先级越高。

自然优先级:

        自然优先级是由硬件固定并预先设定的,用户无法更改。当抢占优先级和响应优先级都相同时,自然优先级将决定哪个中断先得到处理。

优先级执行顺序:

        当多个中断同时发生时,执行顺序首先由抢占优先级决定。如果抢占优先级相同,则进一步由响应优先级决定。如果响应优先级也相同,则最终由自然优先级决定。
        在中断嵌套的情况下,高抢占优先级的中断可以打断低抢占优先级的中断,但高响应优先级的中断不能打断低响应优先级的中断(当它们具有相同的抢占优先级时)。

优先级分组:

        优先级寄存器 IPR 有 8 位,但实际只使用到高 4 位,用于决定抢占优先级、响应优先级的等级。它们由 AIRCR 寄存器控制。

NVIC寄存器配置常用函数:

 使能中断函数

/*** @brief  Enables a device specific interrupt in the NVIC interrupt controller.* @note   To configure interrupts priority correctly, the NVIC_PriorityGroupConfig()*         function should be called before. * @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 the appropriate CMSIS device file (stm32f10xxx.h))* @retval None*/
void HAL_NVIC_EnableIRQ(IRQn_Type IRQn)
{/* Check the parameters */assert_param(IS_NVIC_DEVICE_IRQ(IRQn));/* Enable interrupt */NVIC_EnableIRQ(IRQn);
}

 点进NVIC_EnableIRQ(IRQn);--->我们可以看到:它是__NVIC_EnableIRQ的宏定义

再点进__NVIC_EnableIRQ ;--->我们可以看到这是对中断使能寄存器(ISER)的操作控制

 失能中断函数

/*** @brief  Disables a device specific interrupt in the NVIC interrupt controller.* @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 the appropriate CMSIS device file (stm32f10xxx.h))  * @retval None*/
void HAL_NVIC_DisableIRQ(IRQn_Type IRQn)
{/* Check the parameters */assert_param(IS_NVIC_DEVICE_IRQ(IRQn));/* Disable interrupt */NVIC_DisableIRQ(IRQn);
}

 点进NVIC_DisableIRQ(IRQn);--->我们可以看到:它是__NVIC_DisableIRQ的宏定义

 再点进__NVIC_DisableIRQ ;--->我们可以看到这是对中断使能寄存器(ICER)的操作控制

 中断优先级函数:void 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 the appropriate CMSIS device file (stm32f10xx.h))* @param  PreemptPriority: The preemption priority for the IRQn channel.*         This parameter can be a value between 0 and 15*         A lower priority value indicates a higher priority * @param  SubPriority: the subpriority level for the IRQ channel.*         This parameter can be a value between 0 and 15*         A lower priority value indicates a higher priority.          * @retval None*/
void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority)
{ uint32_t prioritygroup = 0x00U;/* Check the parameters */assert_param(IS_NVIC_SUB_PRIORITY(SubPriority));assert_param(IS_NVIC_PREEMPTION_PRIORITY(PreemptPriority));prioritygroup = NVIC_GetPriorityGrouping();NVIC_SetPriority(IRQn, NVIC_EncodePriority(prioritygroup, PreemptPriority, SubPriority));
}

  点进NVIC_SetPriority--->我们可以看到:它是__NVIC_SetPriority的宏定义

再点进__NVIC_SetPriority ;--->我们可以看到这是对中断优先级的操作控制(IP和SHP)

中断优先级分组设置函数:void HAL_NVIC_SetPriorityGrouping

/*** @brief  Sets the priority grouping field (preemption priority and subpriority)*         using the required unlock sequence.* @param  PriorityGroup: The priority grouping bits length. *         This parameter can be one of the following values:*         @arg NVIC_PRIORITYGROUP_0: 0 bits for preemption priority*                                    4 bits for subpriority*         @arg NVIC_PRIORITYGROUP_1: 1 bits for preemption priority*                                    3 bits for subpriority*         @arg NVIC_PRIORITYGROUP_2: 2 bits for preemption priority*                                    2 bits for subpriority*         @arg NVIC_PRIORITYGROUP_3: 3 bits for preemption priority*                                    1 bits for subpriority*         @arg NVIC_PRIORITYGROUP_4: 4 bits for preemption priority*                                    0 bits for subpriority* @note   When the NVIC_PriorityGroup_0 is selected, IRQ preemption is no more possible. *         The pending IRQ priority will be managed only by the subpriority. * @retval None*/
void HAL_NVIC_SetPriorityGrouping(uint32_t PriorityGroup)
{/* Check the parameters */assert_param(IS_NVIC_PRIORITY_GROUP(PriorityGroup));/* Set the PRIGROUP[10:8] bits according to the PriorityGroup parameter value */NVIC_SetPriorityGrouping(PriorityGroup);
}

 点进NVIC_SetPriorityGrouping-->我们可以看到:它是__NVIC_SetPriorityGrouping的宏定义

再点进__NVIC_SetPriorityGrouping ;--->我们可以看到这是对中断优先级分组的操作控制(AIRCR )

NVIC配置方法步骤:

设置中断分组 → 设置中断优先级 → 使能中断
设置中断分组一般在 HAL_Init 函数中进行。

 进入HAL_Init();——然后进入分组函数HAL_NVIC_SetPriorityGrouping的NVIC_PRIORITYGROUP_2中,会发现里面有5种分组,但我们常用的是NVIC_PRIORITYGROUP_2

关键字:STM32嵌套向量中断控制器—NVIC

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: