EXTI外部中断触发:神舟IV
先设置两个中断输入口 PA6/PA7 浮空输入 PA6下降沿,PA7上升沿
EXTI_Config
1 void EXTI_Config(void) 2 { 3 GPIO_InitTypeDef GPIO_InitStructure; 4 EXTI_InitTypeDef EXTI_InitStructure; 5 NVIC_InitTypeDef NVIC_InitStructure; 6 7 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); 8 9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; 10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 11 GPIO_Init(GPIOA, &GPIO_InitStructure); 12 GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource6); 13 GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource7); 14 15 // Configure EXTI Line6 to generate an interrupt on falling edge 16 EXTI_InitStructure.EXTI_Line = EXTI_Line6; 17 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; 18 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; 19 EXTI_InitStructure.EXTI_LineCmd = ENABLE; 20 EXTI_Init(&EXTI_InitStructure); 21 22 EXTI_InitStructure.EXTI_Line = EXTI_Line7; 23 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; 24 EXTI_Init(&EXTI_InitStructure); 25 26 /* Enable the EXTI15_10 Interrupt */ 27 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 28 NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //EXTI15_10_IRQChannel; 29 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; 30 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 31 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 32 NVIC_Init(&NVIC_InitStructure); 33 }
设置两个pin PB0/PB1(推挽输出) 为中断输入PA6/PA7提供中断源
EXTI_Input_Config
1 void EXTI_Input_Config(void) 2 { 3 /* Config PB0 for PA6 and PB1 for PA7 */ 4 GPIO_InitTypeDef GPIO_InitStructure; 5 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO , ENABLE); //RCC_APB2Periph_AFIO 6 7 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; 8 /* 推挽输出 */ 9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; 10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 11 GPIO_Init(GPIOB, &GPIO_InitStructure); 12 13 }
按键4按下后检测PB0状态,然后位取反,分别接入PA6,PA7,串口显示OK
EXTI0_IRQHandler
1 void EXTI0_IRQHandler(void) 2 { 3 u8 i; 4 /* Key4 */ 5 if(EXTI_GetITStatus(EXTI_Line0) != RESET) 6 { 7 Led_Turn_off_all(); 8 Led_Turn_on_4(); 9 10 printf("\n\r Key 4 interrupt \n\r"); 11 if(GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_0)) 12 { 13 printf("\n\r PB0 Set \n\r"); 14 GPIO_ResetBits(GPIOB, GPIO_Pin_0); 15 for(i=0;i<100;i++); 16 } 17 else 18 { 19 printf("\n\r PB0 Reset \n\r"); 20 GPIO_SetBits(GPIOB, GPIO_Pin_0); 21 for(i=0;i<100;i++); 22 } 23 24 if(GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_0)) 25 { 26 printf("\n\r PB0 Set \n\r"); 27 } 28 else 29 { 30 printf("\n\r PB0 Reset \n\r"); 31 } 32 33 /* Clear the EXTI Line 0 */ 34 EXTI_ClearITPendingBit(EXTI_Line0); 35 } 36 }
PA6/7触发后中断处理函数
EXTI9_5_IRQHandler
1 void EXTI9_5_IRQHandler(void) 2 { 3 if(EXTI_GetITStatus(EXTI_Line6) != RESET) 4 { 5 printf("\n\r PA_6 interrupt \n\r"); 6 7 /* Clear the EXTI Line 6 */ 8 EXTI_ClearITPendingBit(EXTI_Line6); 9 } 10 if(EXTI_GetITStatus(EXTI_Line7) != RESET) 11 { 12 printf("\n\r PA_7 interrupt \n\r"); 13 14 /* Clear the EXTI Line 7 */ 15 EXTI_ClearITPendingBit(EXTI_Line7); 16 } 17 18 }