好了,我们再来看下NVIC,STM32 NVIC的使用可以说用很多好处,在以后的应用中可能就有所体会了
我们先来看一下Reference Datasheet中是如何说的:
SCB->AIRCR中目前只用到4位,也就是最高能有16级中断嵌套,如果全使用的话可以达到256级
选用优先级分组(实际就是选几位用于主优先级几位用于辅优先级)
注意,我们在一个工程中只能用一种分组方式
The table below gives the allowed values of the pre-emption priority and subpriority according
to the Priority Grouping configuration performed by NVIC_PriorityGroupConfig function
===========================================================================
NVIC_PriorityGroup | NVIC_IRQChannelPreemptionPriority | NVIC_IRQChannelSubPriority | Description
===========================================================================
NVIC_PriorityGroup_0 | 0 | 0-15 | 0 bits for pre-emption priority
| | | 4 bits for subpriority
----------------------------------------------------------------------------------------------------------------------------
NVIC_PriorityGroup_1 | 0-1 | 0-7 | 1 bits for pre-emption priority
| | | 3 bits for subpriority
----------------------------------------------------------------------------------------------------------------------------
NVIC_PriorityGroup_2 | 0-3 | 0-3 | 2 bits for pre-emption priority
| | | 2 bits for subpriority
----------------------------------------------------------------------------------------------------------------------------
NVIC_PriorityGroup_3 | 0-7 | 0-1 | 3 bits for pre-emption priority
| | | 1 bits for subpriority
----------------------------------------------------------------------------------------------------------------------------
NVIC_PriorityGroup_4 | 0-15 | 0 | 4 bits for pre-emption priority
| | | 0 bits for subpriority
以上是我提取库中misc.h中的说明便于大家理解
如果要使用中断那就得把中断向量表先存储到存储器,我们先来一段程序看一下吧
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
#endif
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
这里边包含一个条件编译,如果我们要把中断向量表存储在RAM或者FLASH就要定义相关的宏
后边选用优先级分组的0组,也就是0位用于主优先级,4位用于辅优先级
主优先级可以中断嵌套(可以抢占)
辅优先级不可以中断嵌套,只能当前中断完成之后再相应优先级最高的
如果辅优先级相同则相应中断向量表靠前的
NVIC_IRQChannel 是说明用的哪种中断,包括所有的中断与异常(NVIC都管理,包括内核的)
讲到这里我们要提一下不可屏蔽中断,这个中断是不能屏蔽的NMI是不能屏蔽的,其他中断都是可以屏蔽的