STM32 study notes EXTI (external interrupt)

Publisher:MindfulBeingLatest update time:2018-09-11 Source: eefocusKeywords:STM32  EXTI Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1: EXTI (external interrupt) description

 

===========================================dividing line==========================================

 

       The external interrupt/event controller consists of 19 edge detectors that generate event/interrupt requests. Each input line can be independently configured

Input type (pulse or pending) and corresponding trigger event (rising edge or falling edge or both edges). Each input line can be independently

The Pending Register holds the interrupt request for the Status Line.

===========================================dividing line==========================================

The 19 interrupts are as follows:

Unnamed

17 - EXTI line 16 connected to PVD output

18 - EXTI line 17 connected to the RTC alarm event

19 - EXTI line 18 connected to USB wake-up event

 

Note: From the above figure, we can see that the pins connected to EXTI0 are PA0, PB0, PC0, PD0, PE0, PF0, PG0, and other external interrupts EXTI1——

EXTI15 is similar. So when using, try to configure the required external interrupts on different EXTIx.

      For example, if three external interrupts are needed, we can configure them to PA0, PB4, and PG3. At this time, each interrupt has its own interrupt handler.

     If PA0, PB0, and PC1 are configured, PA0 and PB0 will share an interrupt program segment.

     It can be designed like this.

===========================================dividing line==========================================


Configuration usage:


Initialize the corresponding GPIO pins

Configure external interrupt sources and connect interrupt sources to GPIO

Write the interrupt handler for the corresponding interrupt source

===========================================dividing line==========================================


code


Initialize the corresponding GPIO pins

GPIO_InitTypeDef GPIO_InitStructure;

 

 

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);           

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

 

 

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   

GPIO_Init(GPIOE, &GPIO_InitStructure);

 

 

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   

GPIO_Init(GPIOB, &GPIO_InitStructure);

 

Note: GPIO multiplexing function must be turned on, as shown in the red part

 


Configure external interrupt sources and connect interrupt sources to GPIO

 


GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource0);

GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource1);

GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource8);

GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9);

 

 

EXTI_InitStructure.EXTI_Line = EXTI_Line0|EXTI_Line1|EXTI_Line8|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);

 


Write the interrupt handler for the corresponding interrupt source

 


void EXTI0_IRQHandler(void)

{

  if(Sys_Status > MIN_STATUS)

  {

    Sys_Status --;

  }

  EXTI_ClearITPendingBit(EXTI_Line0);

}

 

 

void EXTI1_IRQHandler(void)

{

  PEout(2) = ~PEout(2);

  EXTI_ClearITPendingBit(EXTI_Line1);

}

 

 

#define Exti_From_Pin8 0x00000100

#define Exti_From_Pin9 0x00000200

 

void EXTI9_5_IRQHandler(void)

{

 

  u32 Temp = 0x00; 

 

  PEout(2) = ~PEout(2);

  Temp = EXTI->PR; //Read the interrupt pin

  switch(Temp)

  {

    case Exti_From_Pin8:

      EXTI_ClearITPendingBit(EXTI_Line8);

      break;

    case Exti_From_Pin9:

      if(Sys_Status < MAX_STATUS)

      {

        Sys_Status ++;

      }

      EXTI_ClearITPendingBit(EXTI_Line9);

      break;

    default:break;

  }

}

===========================================dividing line==========================================


Interrupt handler description: Since external interrupts EXTI5-EXTI9 share an interrupt (EXTI10-EXTI15 are similar),


Differentiating different interrupt sources requires corresponding judgment. As shown in EXTI9_5_IRQHandler above, the judgment is made by reading the EXTI->PR register.


The source of the interrupt.



Hardware: Using PA11 pin


It should be clear that the PxN pins share the external interrupt line EXTIN and the external interrupt vector EXTIN_IRQn and the interrupt service routine entry EXTIN_IRQHandler, but it should be noted that [9...5] share EXTI9_5_IRQn and EXTI9_5_IRQHandler, and [15...10] share EXTI15_10_IRQn and EXTI15_10_IRQHandler


 


2: Use of Stm32 external interrupt EXTI


Basic process:


1. Set the clock



RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);  

Note that you need to turn on the AFIO clock


2. Configure GPIO


 


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;  

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  

GPIO_Init(GPIOA, &GPIO_InitStructure);  

Use floating input method


3. Connect the GPIO pin to the external interrupt line




GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource11);  

Quite important


4. Configure EXTI



EXTI_InitStructure.EXTI_Line = EXTI_Line11;  

EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;  

EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;  

EXTI_InitStructure.EXTI_LineCmd = ENABLE;  

EXTI_Init(&EXTI_InitStructure);  

Including triggering methods, etc.


6. Configure NVIC




NVIC_InitTypeDef NVIC_InitStructure;      

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);  

   

NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; //PPP external interrupt line  

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  

NVIC_Init(&NVIC_InitStructure);  

7. Write interrupt service routine




void EXTI15_10_IRQHandler(void)  

{  

    if (EXTI_GetITStatus(EXTI_Line11) != RESET)  

    {  

        EXTI_ClearITPendingBit(EXTI_Line11);//Clear flag  

        ...  

    }  

}

 


Keywords:STM32  EXTI Reference address:STM32 study notes EXTI (external interrupt)

Previous article:Relationship between EXTI and NVIC in STM32
Next article:Reading and writing data on the I2C bus in STM32.

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号