一、EXTI配置步骤
//1.配置RCC时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOx,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);// 开启AFIO时钟,AFIO:GPIO复用/重映射功能
//2.配置EXTI GPIO端口及工作模式(输入模式)
//3.配置EXTI 中断线、模式(上升沿、下降沿、双边沿)、响应方式
GPIO_EXTILineConfig(GPIO_PortSourceGPIOx,GPIO_PinSourcex);//将EXTI挂载到GPIO引脚上
EXTI_Line必须与GPIO引脚保持一致,PA0-Px0连接在EXTI0,PA1-Px1连接在EXTI1,以此类推
EXTI_Mode_Interrupt:中断模式;
EXTI_Mode_Event:事件模式,该模式不会立即触发中断,只是在寄存器上把相应的事件标志位置1,应用这个模式需要不停查询相应寄存器;
//4.配置NVIC
二、编写中断函数
void EXTIx_IRQHandler();
//读取中断函数状态
EXTI_ClearITPendingBit();
//清除中断函数标志
EXTI_ClearITPendingBit();
三、代码
void EXTI_Config(){
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource3);
EXTI_InitStructure.EXTI_Line=EXTI_Line3;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=EXTI3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI3_IRQHandler(){
if(EXTI_GetITStatus(EXTI_Line3)!=RESET){
EXTI_ClearITPendingBit(EXTI_Line3);
PCout(13)^=1;
}
}
标签:NVIC,ENABLE,中断,STM32,InitStructure,GPIO,RCC,EXTI,小记
From: https://www.cnblogs.com/au-up/p/18125066