STM32篇五:嵌套中断向量控制器NVIC

1、中断和事件区别

外部激励信号来看,中断和事件没有区别。从处理来看,中断会向CPU发出请求,需要CPU响应中断程序。事件则是对其他模块发出的脉冲信号,具体其他设备如何响应,就由这个模块决定。

2、配置

1、设置中断优先级分组

主要设置SCB->AIRCR[10:8],中断优先级分组见下图。
在这里插入图片描述

  • 初始化函数说明如下:
/**
  * @brief  Configures the priority grouping: pre-emption priority and subpriority.
  *         配置优先级分组:抢占优先级和子优先级
  * @param  NVIC_PriorityGroup: specifies the priority grouping bits length. 指定优先级分组位的长度
  *   This parameter can be one of the following values:
  *     @arg NVIC_PriorityGroup_0: 0 bits for pre-emption priority
  *                                4 bits for subpriority
  *     @arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority
  *                                3 bits for subpriority
  *     @arg NVIC_PriorityGroup_2: 2 bits for pre-emption priority
  *                                2 bits for subpriority
  *     @arg NVIC_PriorityGroup_3: 3 bits for pre-emption priority
  *                                1 bits for subpriority
  *     @arg NVIC_PriorityGroup_4: 4 bits for pre-emption priority
  *                                0 bits for subpriority
  * @retval None
  */
void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
{
  /* Check the parameters */
  assert_param(IS_NVIC_PRIORITY_GROUP(NVIC_PriorityGroup));
  
  /* Set the PRIGROUP[10:8] bits according to NVIC_PriorityGroup value */
  SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup;
}

2、配置中断参数

主要配置中断优先级寄存器(NVIC_IPRx)和中断开启寄存器((NVIC_ISERx),以及中断关闭寄存器(NVIC_ICERx)。

  • 配置函数说明如下:
/**
  * @brief  Initializes the NVIC peripheral according to the specified
  *         parameters in the NVIC_InitStruct.
  *         根据NVIC_InitStruct中的指定参数初始化NVIC外设。
  * @param  NVIC_InitStruct: pointer to a NVIC_InitTypeDef structure that contains
  *         the configuration information for the specified NVIC peripheral.
  * @retval None
  */
void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)
{
  uint32_t tmppriority = 0x00, tmppre = 0x00, tmpsub = 0x0F;
  
  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(NVIC_InitStruct->NVIC_IRQChannelCmd));
  assert_param(IS_NVIC_PREEMPTION_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority));  
  assert_param(IS_NVIC_SUB_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelSubPriority));
    
  if (NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE)
  {
    /* Compute the Corresponding IRQ Priority --------------------------------*/
    /* 计算相应的IRQ优先级 --------------------------------*/
    tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700))>> 0x08;
    tmppre = (0x4 - tmppriority);
    tmpsub = tmpsub >> tmppriority;

    tmppriority = (uint32_t)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre;
    tmppriority |=  NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub;
    tmppriority = tmppriority << 0x04;
        
    NVIC->IP[NVIC_InitStruct->NVIC_IRQChannel] = tmppriority;
    
    /* Enable the Selected IRQ Channels --------------------------------------*/
    NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
      (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
  }
  else
  {
    /* Disable the Selected IRQ Channels -------------------------------------*/
    NVIC->ICER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
      (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
  }
}
  • NVIC_InitTypeDef结构说明如下:
typedef struct
{
  // 指定要启用或禁用的IRQ通道。此参数可以是@ref IRQn_Type的值(有关完整的STM32设备IRQ通道列表,请参阅stm32f10x.h文件)
  uint8_t NVIC_IRQChannel;               

  // 指定NVIC_IRQChannel中指定的IRQ通道的抢占优先级。 如表@ref NVIC_Priority_Table中所述,此参数可以是0到15之间的值。
  uint8_t NVIC_IRQChannelPreemptionPriority;  

  // 为NVIC_IRQChannel中指定的IRQ通道指定子优先级。 如表@ref NVIC_Priority_Table中所述,此参数可以是0到15之间的值。
  uint8_t NVIC_IRQChannelSubPriority;
    
  // 指定是启用还是禁用NVIC_IRQChannel中定义的IRQ通道。此参数可以设置为ENABLE或DISABLE
  FunctionalState NVIC_IRQChannelCmd;         
    
} NVIC_InitTypeDef;

3、中断优先级配置表

在这里插入图片描述

评论 1 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

lqonlylove

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值