STM32 NVIC相关库函数解析

一、中断优先级分组函数:
1)相关寄存器
在这里插入图片描述
实际上,STM32的中断优先级只用到高4位,如下图所示:
在这里插入图片描述

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; // 对寄存器进行写操作时要求在 VECTKEY域中写入 0x5FA。否则写入值被忽略。 设置中断优先级分组
}

二、中断初始化函数
1.中断优先级寄存器
在这里插入图片描述
2.中断使能设置寄存器
在这里插入图片描述
2.中断使能清除寄存器
在这里插入图片描述

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 --------------------------------*/    
    tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700))>> 0x08; // 求出抢占优先级位数 例如:设置为6代表1位抢占和7位响应 7-6=1即1位抢占
    tmppre = (0x4 - tmppriority); // 得到抢占优先级在搞4位中的哪一位
    tmpsub = tmpsub >> tmppriority; // 将抢占位置设为0

    tmppriority = (uint32_t)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre; // 将抢占优先级移到对应位置
    tmppriority |=  NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub; // 获取响应优先级数值
    tmppriority = tmppriority << 0x04; // 将设定值移到高4位
        
    NVIC->IP[NVIC_InitStruct->NVIC_IRQChannel] = tmppriority; // 使用对应寄存器设置优先级
    
    /* Enable the Selected IRQ Channels --------------------------------------*/ // 右移5位得到组号 &0x1F得到位号
    NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
      (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
  }
  else
  {
    /* Disable the Selected IRQ Channels -------------------------------------*/ // 右移5位得到组号 &0x1F得到位号
    NVIC->ICER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
      (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
  }
}

参考资料:
1.CorTex-M3权威指南;
2.CM3技术参考手册(广州周立功单片机发展有限公司著);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值