1.NVIC基本结构
NVIC主要处理分配中断先后顺序的任务
2.NVIC优先级分组
分组方式 | 抢占优先级 | 响应优先级 |
分组0 | 0位,取值为0 | 4位,取值为0-15 |
分组1 | 1位,取值为0-1 | 3位,取值为0-7 |
分组2 | 2位,取值为0-3 | 2位,取值为0-3 |
分组3 | 4位,取值为0-15 | 0位,取值为0 |
数字越小,等级越高;
抢占优先级高的可以中断嵌套,响应优先级高的可以优先排队;
抢占优先级和响应优先级均相同的按中断号排序
3.EXTI基本介绍
EXTI可以监测指定GPIO口的电平信号,当其指定的GPIO口产生电平变化时,EXTI将立即向NVIC发出中断申请,经过NVIC裁决后即可中断CPU主程序,使CPU执行EXTI对应的中断程序
支持的触发方式:上升沿/下降沿/双边沿/软件触发
支持所有的GPIO口,但相同的Pin不能同时触发(如PA0和PB0)
通道数:16个GPIO_Pin,外加PVD输出、RTC闹钟、USB唤醒、以太网唤醒
触发响应方式:中断响应/事件响应
4.相关标准库函数解释
void GPIO_AFIODeInit(void);
//调用该函数,AFIO外设的配置就会全部清除
void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);
//AFIO外部中断引脚选择
//如选择PB14
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource14);
//调用该函数就会把EXTI的配置全部清除
void EXTI_DeInit(void);
//赋默认值
void EXTI_StructInit(EXTI_InitTypeDef* EXTI_InitStruct);
//软件触发外部中断
void EXTI_GenerateSWInterrupt(uint32_t EXTI_Line);
//在主程序中使用,查看标志位是否被置1
FlagStatus EXTI_GetFlagStatus(uint32_t EXTI_Line);
//在主程序中使用,对置1的标志位清0
void EXTI_ClearFlag(uint32_t EXTI_Line);
//在中断函数中使用,看中断标志位是否被置1
ITStatus EXTI_GetITStatus(uint32_t EXTI_Line);
//在中断函数中使用,清除中断挂起标志位
void EXTI_ClearITPendingBit(uint32_t EXTI_Line);
//初始化EXTI外设
void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct);
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line14;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
//EXTI_Mode_Interrupt, 中断响应
//EXTI_Mode_Event 事件响应
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_Init(&EXTI_InitStructure);
//设置中断分组,整个芯片只有一个分组 最好放在主函数 否则每个模块分组要相同
void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup);
//初始化NVIC
void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级
NVIC_Init(&NVIC_InitStructure);
5.案例
#include "stm32f10x.h" // Device header
uint16_t CountSensor_Count;
void CountSensor_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource14);
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line14;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发中断
EXTI_Init(&EXTI_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
}
uint16_t CountSensor_Get(void)
{
return CountSensor_Count;
}
void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line14) == SET) //判断中断标志位是否置1,即是否进中断
{
CountSensor_Count++;
EXTI_ClearITPendingBit(EXTI_Line14); //清除中断标志位
}
}