以下我的中断设置:用的是IOB0作中断源,不知道为何一上电就进中断,在程序里却有时进,有时不进中断。 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_SPI1 | RCC_APB2Periph_AFIO, ENABLE);// 使能GPIOB时钟
/*IOB0开放外部中断*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
void EXTI_Configuration(void) { EXTI_InitTypeDef EXTI_InitStructure; EXTI_DeInit(); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0); EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line1; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); // EXTI_GenerateSWInterrupt(EXTI_Line0); }
void EXTI0_IRQHandler(void) { if(EXTI_GetITStatus(EXTI_Line0) != RESET) { /* Clear the EXTI line 9 pending bit */ EXTI_ClearITPendingBit(EXTI_Line0); GPIO_SetBits(GPIOB, GPIO_Pin_8 ); sta=1; // delay(); } }
都是参考例程写的,不知道错在那里,IOB0接一个按键,按下时接GND。 |