为什么外部中断实验“卡住了”

2019-07-21 01:51发布

使用一个按键(连接PD3)做外部中断实验,期望的现象是按住它时全部灯亮,松开时4个LED灯继续流水灯现象。但实际现象是按一下它流水灯就停止了,当前亮着的灯亮着,其它灯灭了,无法恢复流水灯,除非系统复位。
[mw_shl_code=c,true]#include "stm32f10x.h" #include "delay.h" #define RCC_USER2 RCC_APB2Periph_GPIOD #define USER2_PORT GPIOD #define USER2_Pin GPIO_Pin_3 #define RCC_LED RCC_APB2Periph_GPIOF #define LED_PORT GPIOF #define DS1 GPIO_Pin_6 #define DS2 GPIO_Pin_7 #define DS3 GPIO_Pin_8 #define DS4 GPIO_Pin_9 void Key_Init(void); void LED_Init(void); void NVIC_GroupConfig(void); void EXTIX_Init(void); int main() { LED_Init(); Key_Init(); NVIC_GroupConfig(); EXTIX_Init(); GPIO_SetBits(LED_PORT,DS1|DS2|DS3|DS4); while(1) { GPIO_SetBits(LED_PORT,DS2|DS3|DS4); GPIO_ResetBits(LED_PORT,DS1); Delay_ms(500); GPIO_SetBits(LED_PORT,DS1|DS3|DS4); GPIO_ResetBits(LED_PORT,DS2); Delay_ms(500); GPIO_SetBits(LED_PORT,DS2|DS1|DS4); GPIO_ResetBits(LED_PORT,DS3); Delay_ms(500); GPIO_SetBits(LED_PORT,DS2|DS3|DS1); GPIO_ResetBits(LED_PORT,DS4); Delay_ms(500); } } void Key_Init() { GPIO_InitTypeDef GPIO_StructureInit; RCC_APB2PeriphClockCmd(RCC_USER2,ENABLE); GPIO_StructureInit.GPIO_Pin=USER2_Pin; GPIO_StructureInit.GPIO_Mode=GPIO_Mode_IPU; GPIO_Init(USER2_PORT,&GPIO_StructureInit); } void LED_Init() { GPIO_InitTypeDef GPIO_StructureInit; RCC_APB2PeriphClockCmd(RCC_LED,ENABLE); GPIO_StructureInit.GPIO_Pin=DS1|DS2|DS3|DS4; GPIO_StructureInit.GPIO_Speed=GPIO_Speed_2MHz; GPIO_StructureInit.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_Init(LED_PORT,&GPIO_StructureInit); } void NVIC_GroupConfig() { NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); } void EXTIX_Init() { EXTI_InitTypeDef EXTI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource3); EXTI_InitStructure.EXTI_Line = EXTI_Line3; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); NVIC_InitStructure.NVIC_IRQChannel=EXTI3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0X02; NVIC_InitStructure.NVIC_IRQChannelSubPriority=0X03; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); } void EXTI3_IRQHandler() { if(EXTI_GetITStatus(EXTI_Line3)!=RESET) { Delay_ms(10); if(GPIO_ReadInputDataBit(USER2_PORT,USER2_Pin)==0) GPIO_ResetBits(LED_PORT,DS1|DS2|DS3|DS4); while(GPIO_ReadInputDataBit(USER2_PORT,USER2_Pin)==0); EXTI_ClearITPendingBit(EXTI_Line3); } } [/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。