STM32之中斷與事件一個(gè)使用GPIO作為外部中斷的示例
1.GPIO的正確設(shè)置
GPIO_InitTypeDef GPIO_InitStructure;/* Enable GPIOD clock */RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);/* Configure PD.03, PC.04, as input floating */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 ;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;GPIO_Init(GPIOD, &GPIO_InitStructure);GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource3); //PD3作為外部中斷引腳
其中GPIO_EXITLineConfig函數(shù)的聲明如下:
/* @brief Selects the GPIO pin used as EXTI Line.* @param GPIO_PortSource: selects the GPIO port to be used as source for EXTI lines.* This parameter can be GPIO_PortSourceGPIOx where x can be (A..G).* @param GPIO_PinSource: specifies the EXTI line to be configured.* This parameter can be GPIO_PinSourcex where x can be (0..15).* @retval None*/void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource)
2.中斷初始化
EXTI_InitTypeDef EXTI_InitStructure;// Configure EXTI Line3 to generate an interrupt on falling edgeEXTI_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);GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource4);// Configure EXTI Line4 to generate an interrupt on falling edgeEXTI_InitStructure.EXTI_Line = EXTI_Line4;EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;EXTI_InitStructure.EXTI_LineCmd = ENABLE;EXTI_Init(&EXTI_InitStructure);
3.在NVIC_Configuration 中Configures the NVIC and Vector Table base address.
/* Configure the Priority Group to 2 bits */NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);/* enabling interrupt */NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQChannel;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);/* Enable the EXTI3 Interrupt on PD.3 */NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQChannel;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);/* Enable the EXTI4 Interrupt on PD.4 */NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQChannel;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);
4.在stm32f0x_it.c 中寫入 中斷處理函數(shù)
/** Function Name : EXTI3_IRQHandler* Description : This function handles External interrupt Line 3 request.* Input : None* Output : None* Return : None*/void EXTI3_IRQHandler(void){if(EXTI_GetITStatus(EXTI_Line3) != RESET){// Led_RW_ON();GPIO_SetBits(GPIOC, GPIO_Pin_4| GPIO_Pin_5 );/* Clear the EXTI line 9 pending bit */EXTI_ClearITPendingBit(EXTI_Line3);}}/** Function Name : EXTI4_IRQHandler* Description : This function handles External interrupt Line 4 request.* Input : None* Output : None* Return : None*/void EXTI4_IRQHandler(void){if(EXTI_GetITStatus(EXTI_Line4) != RESET){//Led_RW_OFF();GPIO_ResetBits(GPIOC, GPIO_Pin_4 | GPIO_Pin_5);/* Clear the EXTI line 9 pending bit */EXTI_ClearITPendingBit(EXTI_Line4);}}
注:本文參考:http://www.61ic.com/Technology/embed/201103/31045.html
另一個(gè)示例:
配置如下:
void vcm_airbag_exti_set(void){EXTI_InitTypeDef EXTI_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;GPIO_InitTypeDef GPIO_InitStructure;/* Enable key GPIO clock */RCC_AHB1PeriphClockCmd(AIRBAG_GPIO_RCC, ENABLE);GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;/* Configure pin(PD_15): airbag gpio */GPIO_InitStructure.GPIO_Pin = AIRBAG_GPIO_PIN;GPIO_Init(AIRBAG_GPIO_PORT, &GPIO_InitStructure);RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); //´ò¿ªEXTIʱÖÓSYSCFG_EXTILineConfig(AIRBAG_EXTI_PORT, AIRBAG_EXTI_PIN); //ÉèÖÃPD15ΪEXTI/* Configure EXTI */// EXTI_DeInit();EXTI_InitStructure.EXTI_Line = AIRBAG_EXTI_LINE; //ÉèÖÃÒý½ÅEXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //ÉèÖÃΪÍⲿÖжÏÄʽEXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; // ÉèÖÃÉÏÉýºÍϽµÑض¼¼ì²âEXTI_InitStructure.EXTI_LineCmd = ENABLE; //ʹÄÜÍⲿÖжÏÐÂ״̬EXTI_Init(&EXTI_InitStructure); //¸ù¾ÝEXTI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèEXTI¼Ä´æÆ÷NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÍⲿÖжÏͨµÀ NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F; //ÏÈÕ¼ÓÅÏȼ¶4λ,¹²16¼¶NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F; //ÏÈÕ¼ÓÅÏȼ¶0λ,´ÓÓÅÏȼ¶4λNVIC_Init(&NVIC_InitStructure); //¸ù¾ÝNVIC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèNVIC¼Ä´æÆ÷}
中斷例程:
void EXTI15_10_IRQHandler(void){rt_interrupt_enter();/* °²È«ÆøÄÒÓ²Ïß¼ì²â´¦Àí*/vcm_airbag_exti_check();/* GPSÌìÏß¿ªÂ·¼ì²â´¦Àí*/gps_exti_check();rt_interrupt_leave();}
void vcm_airbag_exti_check(void){//¼ì²éµ±Ç°ÖжϵÄ״̬ÊÇ·ñºÏ·¨if(EXTI_GetITStatus(AIRBAG_EXTI_LINE) != RESET){ //¶ÁȹܽŵÄ״̬,ÕâÀï·µ»Ø0±íʾ°²È«ÆøÄÒ±¬¿ªif(0 ==GPIO_ReadInputDataBit(AIRBAG_GPIO_PORT,AIRBAG_GPIO_PIN)){ recv_frame.bus_type = BTYPE_HS_CAN;recv_frame.id = 0x545;recv_frame.data_buff[0] = 0x00;recv_frame.data_buff[1] = 0x01;recv_frame.data_buff[2] = 0x00;recv_frame.data_buff[3] = 0x00;recv_frame.data_buff[4] = 0x00;recv_frame.data_buff[5] = 0x00;recv_frame.data_buff[6] = 0x00;recv_frame.data_buff[7] = 0x00;VCM_DEBUG("[vcm] warning: airbag gpio interrupt rising.rn");}else{recv_frame.bus_type = BTYPE_HS_CAN;recv_frame.id = 0x545;recv_frame.data_buff[0] = 0x00;recv_frame.data_buff[1] = 0x00;recv_frame.data_buff[2] = 0x00;recv_frame.data_buff[3] = 0x00;recv_frame.data_buff[4] = 0x00;recv_frame.data_buff[5] = 0x00;recv_frame.data_buff[6] = 0x00;recv_frame.data_buff[7] = 0x00;VCM_DEBUG("[vcm] warning: airbag gpio interrupt falling.rn");}rt_mq_send(&recv_frame_mq,&recv_frame,sizeof(can_bus_frame));//ÇåÖжϱê־λEXTI_ClearITPendingBit(AIRBAG_EXTI_LINE); } }
評(píng)論