配置外部中断示例
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
3 映射IO到中断线
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource4);映射PE4到中断线
4 初始化线上中断,设置触发方式
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line=EXTI_Line4; //
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_Init(&EXTI_InitStructure);
5 配置中断分组NVIC,使能中断
//记得在主函数里设置中断分组NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2)
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel=EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;
NVIC_Init();
编写外部中断函数EXTI_IRQHandler()
可能要进行必要的防抖处理
最后要手动清除相关寄存器的中断标志位,否者下次不会进入中断服务函数
EXTI_ClearITPendingBit(EXTI__Line4);