一、中断优先级分组函数:
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技术参考手册(广州周立功单片机发展有限公司著);