STM32 Learning: External Interrupts

Publisher:huanguuLatest update time:2018-10-21 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

STM32 GPIO external interrupt summary

1. STM32 interrupt grouping:


  Each GPIO of STM32 can be configured as an external interrupt trigger source, which is also the strength of STM32. STM32 divides many interrupt trigger sources into different groups according to the pin numbers. For example, PA0, PB0, PC0, PD0, PE0, PF0, PG0 are the first group, and so on. We can conclude that there are 16 groups in total. STM32 stipulates that only one interrupt trigger source can work at the same time in each group, so the maximum number of working external interrupts is 16. The interrupt controller of STM32F103 supports 19 external interrupt/event requests. Each interrupt has a status bit, and each interrupt/event has independent trigger and mask settings. The 19 external interrupts of STM32F103 are:

Lines 0~15: correspond to the input interrupt of the external IO port.

GPIO PinsInterrupt flagInterrupt handling function
PA0~PG0EXTI0EXTI0_IRQHandler
PA1~PG1EXTI1EXTI1_IRQHandler
PA2~PG2EXTI2EXTI2_IRQHandler
PA3~PG3EXTI3EXTI3_IRQHandler
PA4~PG4EXTI4EXTI4_IRQHandler
PA5~PG5EXTI5EXTI9_5_IRQHandler
PA6~PG6EXTI6
PA7~PG7EXTI7
PA8~PG8EXTI8
PA9~PG9EXTI9
PA10~PG10EXTI10EXTI15_10_IRQHandler
PA11~PG11EXTI11
PA12~PG12EXTI12
PA13~PG13EXTI13
PA14~PG14EXTI14
PA15~PG15EXTI15

 

 

 

 

 

 

 

 

 

 

 

 

 

 



Line 16: Connect to PVD output.


Line 17: Connected to the RTC alarm event.


Line 18: Connected to USB wakeup event.


 


2: Configuration process of external interrupt:


  1. Configure the trigger source GPIO port:


  Because the GPIO port is used as a trigger source, the GPIO port is configured to input mode. The trigger modes are as follows:


  a.GPIO_Mode_AIN, analog input (ADC analog input, or power saving in low power consumption)


  b.GPIO_Mode_IN_FLOATING, floating input


  c.GPIO_Mode_IPD, with pull-down input


  d.GPIO_Mode_IPU, with pull-up input 



  GPIO_InitTypeDef GPIO_InitStructure; //Define the structure


  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE); //Enable clock


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //Select IO port PE2


  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //Set to pull-up input


  GPIO_Init(GPIOE, &GPIO_InitStructure); //Use structure information to initialize IO port



  2. Enable AFIO multiplexed clock function:


  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); 

  


  3. Map the GPIO port to the interrupt line: 


  GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource2);

 


  4. Initialize the interrupt on the interrupt line: 


  EXTI_InitTypeDef EXTI_InitStructure; //Define the initialization structure


  EXTI_InitStructure.EXTI_Line=EXTI_Line2; //The interrupt line number range is EXTI_Line0~EXTI_Line15


  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //Interrupt mode, optional values ​​are interrupt EXTI_Mode_Interrupt and event EXTI_Mode_Event.


  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //Trigger mode, can be falling edge trigger EXTI_Trigger_Falling, rising edge trigger EXTI_Trigger_Rising, or any level (rising and falling edges) trigger EXTI_Trigger_Rising_Falling


  EXTI_InitStructure.EXTI_LineCmd = ENABLE;


  EXTI_Init(&EXTI_InitStructure); //Initialize according to the structure information


 


  5. Interrupt priority configuration: 



  NVIC_InitTypeDef NVIC_InitStructure; //define the structure


  NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; //Enable the channel where the external interrupt is located


  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //Preemption priority 2, 


  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //Subpriority 2


  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Enable external interrupt channel 


  NVIC_Init(&NVIC_InitStructure); //Initialize priority according to structure information 


 


  6. Writing of external interrupt service function:


  The external interrupt functions are:


  EXPORT   EXTI0_IRQHandler          


  EXPORT   EXTI1_IRQHandler       


  EXPOR T   EXTI2_IRQHandler         


  EXPORT   EXTI3_IRQHandler        


  EXPORT   EXTI4_IRQHandler          


  EXPORT   EXTI9_5_IRQHandler        


  EXPORT   EXTI15_10_IRQHandler


  Interrupt lines 0-4 each correspond to an interrupt function, interrupt lines 5-9 share the interrupt function EXTI9_5_IRQHandler, and interrupt lines 10-15 share the interrupt function EXTI15_10_IRQHandler.   



  void EXTI2_IRQHandler(void)

  {

    if(EXTI_GetITStatus(EXTI_Line2)!=RESET)//Judge whether an interruption on a line occurs 


    {

      Interrupt logic...

      EXTI_ClearITPendingBit(EXTI_Line2); //Clear the interrupt flag on LINE

    }     

  }


 


3. Configuration instructions for using GPIO port to connect buttons for external interrupts:


  When using a button for external interrupt, it is generally necessary to perform key delay debounce and hand release detection related processing. The interrupt function can refer to the following code:


  void EXTI2_IRQHandler(void)

  {

    delay_ms(10); //delay to eliminate jitter

    if(KEY2==0) //The key is really pressed

      {

        LED0=!LED0;

      }

    while(KEY2!=0);//Wait for release

    EXTI_ClearITPendingBit(EXTI_Line2); //Clear interrupt flag 

  }


  Of course, if your button allows long press function, then other logical operations will be performed, which will not be studied here.


Keywords:STM32 Reference address:STM32 Learning: External Interrupts

Previous article:STM32 learning: the three main parts of STM32F4XX
Next article:STM32 Learning: FMC-Extending External SDRAM

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号