1. 程式人生 > >[STM32F103]外部中斷

[STM32F103]外部中斷

① 初始化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 }