STM32 External Interrupt Configuration

Publisher:创新驿站Latest update time:2016-10-18 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1 Placement interrupted

1. Allocate interrupt vector table:

/* Set the Vector Table base location at 0x20000000 */

NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);

2. Set interrupt priority:

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //Set interrupt priority

3. Initialize external interrupt:

/*Enable EXTI4 interrupt*/

       NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQChannel; //Interrupt channel

       NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PreemptionPriorityValue; //Enforce priority

       NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Sub-priority

       NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Channel interrupt enable

       NVIC_Init(&NVIC_InitStructure); //Initialize interrupt

Note: If the external pin we configure is PA4, or PB4, or PC4, PD4, etc., then the external interrupt used must also be EXTI4. Similarly, if the external interrupt pin is PA1, PB1, PC1, PD1, then the interrupt must use EXTI1, and so on.

 

2Configure the GPIO pin as a trigger event for an external interrupt

1. Select IO pins

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

Note that if the pin is pin 4 of the port, the configured interrupt must be EXTI4

2. Configure the pin as input

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

3. Initialize the pins

GPIO_Init(GPIOD,&GPIO_InitStructure);

 

 

3. Configure the EXTI line to connect the interrupt line and IO pin line.

1. Connect the EXTI line to the IO port

Connect EXTI line 4 to pin 4 of port GPIOD

       GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource4);

       Note: If the configured pin is number 4, then the parameter must be GPIO_PinSource4

              If the configured pin is number 3, then the parameter must be GPIO_PinSource3

2. Configure interrupt edge

       /*Configure EXTI line 0 to generate an interrupt when a falling edge occurs*/

       EXTI_InitStructure.EXTI_Line = EXTI_Line4;

       Note: If pin 4 is configured, then EXTI_Line4 is required

       EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;

       EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //Falling edge trigger

       EXTI_InitStructure.EXTI_LineCmd = ENABLE; //Interrupt line enable

       EXTI_Init(&EXTI_InitStructure); //Initialize interrupt

       EXTI_GenerateSWInterrupt(EXTI_Line4); //EXTI_Line4 interrupt is enabled

 

Now the interrupt configuration is complete and you can write the interrupt handling function.

Example:

Configuration Function

/*************************************************************************

* Function name NVIC_Configration

* Description Configure each interrupt register

* Input None

* Output None

* Return value None

****************************************************************************/

void NVIC_Configration(void)

{

        NVIC_InitTypeDef NVIC_InitStructure;

//#ifdef VECT_TAB_RAM

      /* Set the Vector Table base location at 0x20000000 */

      NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);

//#else /* VECT_TAB_FLASH */

      /* Set the Vector Table base location at 0x08000000 */

      //NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);  

//#endif  

       NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //Set interrupt priority

       /*Enable EXTI4 interrupt*/

       NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQChannel;

       NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PreemptionPriorityValue;

       NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

       NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

       NVIC_Init(&NVIC_InitStructure);      

       /*Enable EXTI9 interrupt*/

       NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel;

       NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

       NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

       NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

       NVIC_Init(&NVIC_InitStructure);      

       /*Configure SysTick processing priority: priority and sub-priority*/

      

}

 

/************************************************************************

* Function name: GPIO_Configuration(void)

* Description: Configure TIM2 pins

* Input: None

* Output: None

* Return: None

************************************************************************/

void GPIO_Configuration(void){

/*    GPIO_InitTypeDef GPIO_InitStructure;

       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;

       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

       GPIO_Init(GPIOA,&GPIO_InitStructure); */

 

       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;

       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

       GPIO_Init(GPIOC,&GPIO_InitStructure);

 

       /*Configure the first pin of GPIOD as floating input*/

       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

       GPIO_Init(GPIOD,&GPIO_InitStructure);

 

       /*Configure the 9th pin of GPIOB as floating input*/

       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

       GPIO_Init(GPIOB,&GPIO_InitStructure);

}

 

 

/**************************************************************

* Function SysTick_Configuration

* Description Set SysTick

* Input None

* Output None

* Return value None

***************************************************************/

void SysTick_Configuration(void)

{

      /*Configure HCLK clock as SysTick clock source*/

       SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //System clock divided by 8 72MHz

 

       NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 8,2);

       /*SysTick Interrupt each 1000Hz with HCLK equal to 72MHz*/

       SysTick_SetReload(9000); //Interrupt cycle 1ms

       /*Enable the SysTick Interrupt */

       SysTick_ITConfig(ENABLE); //Enable interrupt          

       SysTick_CounterCmd(SysTick_Counter_Enable);

       SysTick_CounterCmd(SysTick_Counter_Clear);                

}

 

 

/******************************************************************************

* Function name EXTI_Configuration

* Description Configure EXTI line

* Input None

* Output None

* Return value None

******************************************************************************/

void EXTI_Configuration(void){

 

       /* Connect EXTI line 0 to PA0 */

       GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource4);

       /*Configure EXTI line 0 to generate an interrupt when a falling edge occurs*/

       EXTI_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);

       EXTI_GenerateSWInterrupt(EXTI_Line4);

      

       /*Connect EXTI line 9 to PB9*/

       GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource9);

       /*Generate an interrupt when a falling edge appears on EXTI line 9*/

       EXTI_InitStructure.EXTI_Line = EXTI_Line9;

       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_Line9);       

}

 

Interrupt function:

void EXTI4_IRQHandler(void)

{

       if (EXTI_GetITStatus(EXTI_Line4)!= RESET){

              EXTI_ClearITPendingBit(EXTI_Line4);

              if(Ledflag == 0){

                     Ledflag = 1;

                     GPIOC->ODR |= 0X00000080;

              }

              else{

                     Ledflag = 0;          

                     GPIOC->ODR &= 0XFFFFFF7F;

              }

       }

}


Keywords:STM32 Reference address:STM32 External Interrupt Configuration

Previous article:STM32 uses timer to accurately delay (non-SysTick)
Next article:STM32 uses systick to accurately delay nms

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号