① 初始化IO口为输入。
GPIO_Init();
② 开启IO口复用时钟。
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
③ 设置IO口与中断线的映射关系。
void GPIO_EXTILineConfig();
④ 初始化线上中断,设置触发条件等。
EXTI_Init();
⑤ 配置中断分组(NVIC),并使能中断。
NVIC_Init();
⑥ 编写中断服务函数。
EXTIx_IRQHandler();
⑦ 清除中断标志位
EXTI_ClearITPendingBit();
源码:
1 void EXTIX_Init(void) 2 { 3 EXTI_InitTypeDef EXTI_InitStructure; 4 NVIC_InitTypeDef NVIC_InitStructure; 5 6 KEY_Init(); // °´¼ü¶Ë¿Ú³õʼ»¯ 7 8 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); 9 10 //GPIOE.3 ÖжÏÏßÒÔ¼°Öжϳõʼ»¯ÅäÖà ϽµÑØ´¥·¢ //KEY1 11 GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource3); 12 EXTI_InitStructure.EXTI_Line=EXTI_Line3; 13 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; 14 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; 15 EXTI_Init(&EXTI_InitStructure); //¸ù¾ÝEXTI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèEXTI¼Ä´æÆ÷ 16 17 //GPIOE.4 ÖжÏÏßÒÔ¼°Öжϳõʼ»¯ÅäÖà ϽµÑØ´¥·¢ //KEY0 18 GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource4); 19 EXTI_InitStructure.EXTI_Line=EXTI_Line4; 20 EXTI_Init(&EXTI_InitStructure); //¸ù¾ÝEXTI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèEXTI¼Ä´æÆ÷ 21 22 23 //GPIOA.0 ÖжÏÏßÒÔ¼°Öжϳõʼ»¯ÅäÖà ÉÏÉýÑØ´¥·¢ PA0 WK_UP 24 GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0); 25 26 EXTI_InitStructure.EXTI_Line=EXTI_Line0; 27 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; 28 EXTI_Init(&EXTI_InitStructure); //¸ù¾ÝEXTI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèEXTI¼Ä´æÆ÷ 29 30 31 NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //ʹÄÜ°´¼üWK_UPËùÔÚµÄÍⲿÖжÏͨµÀ 32 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //ÇÀÕ¼ÓÅÏȼ¶2£¬ 33 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03; //×ÓÓÅÏȼ¶3 34 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÍⲿÖжÏͨµÀ 35 NVIC_Init(&NVIC_InitStructure); 36 37 NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; //ʹÄÜ°´¼üKEY1ËùÔÚµÄÍⲿÖжÏͨµÀ 38 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //ÇÀÕ¼ÓÅÏȼ¶2 39 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; //×ÓÓÅÏȼ¶1 40 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÍⲿÖжÏͨµÀ 41 NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝNVIC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèNVIC¼Ä´æÆ÷ 42 43 NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn; //ʹÄÜ°´¼üKEY0ËùÔÚµÄÍⲿÖжÏͨµÀ 44 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //ÇÀÕ¼ÓÅÏȼ¶2 45 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; //×ÓÓÅÏȼ¶0 46 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÍⲿÖжÏͨµÀ 47 NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝNVIC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèNVIC¼Ä´æÆ÷ 48 49 } 50 51 //ÍⲿÖжÏ0·þÎñ³ÌÐò 52 void EXTI0_IRQHandler(void) 53 { 54 delay_ms(10);//Ïû¶¶ 55 if(WK_UP==1) //WK_UP°´¼ü 56 { 57 BEEP=!BEEP; 58 } 59 EXTI_ClearITPendingBit(EXTI_Line0); //Çå³ýLINE0ÉϵÄÖжϱê־λ 60 } 61 62 63 //ÍⲿÖжÏ3·þÎñ³ÌÐò 64 void EXTI3_IRQHandler(void) 65 { 66 delay_ms(10);//Ïû¶¶ 67 if(KEY1==0) //°´¼üKEY1 68 { 69 LED1=!LED1; 70 } 71 EXTI_ClearITPendingBit(EXTI_Line3); //Çå³ýLINE3ÉϵÄÖжϱê־λ 72 } 73 74 void EXTI4_IRQHandler(void) 75 { 76 delay_ms(10);//Ïû¶¶ 77 if(KEY0==0) //°´¼üKEY0 78 { 79 LED0=!LED0; 80 LED1=!LED1; 81 } 82 EXTI_ClearITPendingBit(EXTI_Line4); //Çå³ýLINE4ÉϵÄÖжϱê־λ 83 }