Simply record the use of STM32EXTI external interrupt

1, Understand the concept of external interrupt (take STM32F4XX series chip as an example)

Each IO of STM32F4 can be used as an external interrupt input.

The interrupt controller of STM32F4 supports 22 external interrupt / event requests:

EXTI line 0~15: input interrupt corresponding to external IO port.

EXTI line 16: connected to PVD output.

EXTI line 17: connected to RTC alarm events.

EXTI cable 18: connect to USB OTG FS wake-up event.

EXTI line 19: connect to Ethernet wake-up event.

EXTI cable 20: connect to USB OTG HS (configured in FS) for wake-up events.

EXTI line 21: connected to RTC intrusion and timestamp events.

EXTI line 22: connected to RTC wake-up event.

Each external interrupt line can be independently configured with triggering mode (rising edge, falling edge or double edge triggering), triggering / shielding and special status bits. It can be seen from the above that STM32F4 has only 16 interrupt lines for IO, but STM32F4xx series has hundreds of IO ports. How can the interrupt lines correspond to the IO ports?

2, External interrupt / event line image:

Find the interrupt line mapping table in the STM32 reference manual to know the interrupt line corresponding to the IO port.

3, External interrupt / event controller block diagram:

4, Press the key to trigger the input signal to determine whether the input is high level or low level (PA0 as an example)

 

5, The driver code configuration steps are as follows:

We use STM32 standard library functions to write the driver code, and the registers are not posted.

1. turn on the IO port clock and configure the corresponding IO pin as the input mode.

2. enable system configuration controller (SYSCFG)

3. map the IO port to the corresponding interrupt line

4. configure interrupt initialization function parameters

5. configure interrupt priority grouping

6. configure interrupt controller initialization function

7. enable interrupt channel (it is unnecessary to call after configuring interrupt controller initialization)

8. call interrupt service function

9. clear the interrupt flag

#include "stm32f4xx.h"     
int main(void)
{
    //1. turn on the IO port clock and configure the corresponding IO pin as the input mode.
	GPIO_InitTypeDef Gpio_Key_Init;                                //Define GPIO structure variables
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);          //Turn on GPIOA clock
	Gpio_Key_Init.GPIO_Pin = GPIO_Pin_0;  			               //Select 0 pin
	Gpio_Key_Init.GPIO_Mode = GPIO_Mode_IN; 		               //Input mode
	Gpio_Key_Init.GPIO_PuPd = GPIO_PuPd_DOWN;			           //drop-down 
	GPIO_Init(GPIOA,&Gpio_Key_Init);                               //Select PA group and configure parameter initialization
    //2. enable system configuration controller (SYSCFG)
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);          //Turn on the system configuration controller (SYSCFG) clock
    //3. map the IO port to the corresponding interrupt line	
    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,EXTI_PinSource0);   //Map PA0 pin to EXTI0 line
    //4. configure interrupt initialization function parameters
    EXTI_InitTypeDef EXTI_Key_Init;                                //Define EXTI external interrupt structure variable
    EXTI_Key_Init.EXTI_Line = EXTI_Line0;                          //Select line 0
	EXTI_Key_Init.EXTI_LineCmd = ENABLE;                           //Open interrupt request from line 0
	EXTI_Key_Init.EXTI_Mode = EXTI_Mode_Interrupt;                 //Interrupt mode
	EXTI_Key_Init.EXTI_Trigger = EXTI_Trigger_Rising;              //Rising edge trigger
	EXTI_Init(&EXTI_Key_Init);                                     //Initialize external interrupt configuration
    //5. configure interrupt priority grouping
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);                //Select primary priority group 2
    //6. configure interrupt controller initialization function
    NVIC_InitTypeDef NVIC_Key_Init;                                //Define EXTI external interrupt structure variable
    NVIC_Key_Init.NVIC_IRQChannel=EXTI0_IRQn;                      //Select interrupt source
    NVIC_Key_Init.NVIC_IRQChannelCmd=ENABLE;                       //Enable interrupt source
    NVIC_Key_Init.NVIC_IRQChannelPreemptionPriority=2;             //Set preemption priority
    NVIC_Key_Init.NVIC_IRQChannelSubPriority=2;                    //Set response priority
    NVIC_Init(&NVIC_Key_Init);                                     //Initialize interrupt controller parameter configuration
    //7. enable interrupt channel (it is unnecessary to call after configuring interrupt controller initialization)
    // NVIC_EnableIRQ(EXTI0_IRQn)// Enable external interrupt line 0 to work
	while(1)
	{
		
	}
	
}
    //8. call interrupt service function
void EXTI0_IRQHandler(void)  
{
    //9. clear the flag bit that interrupts the EXTI0 line
	EXTI_ClearFlag(EXTI_Line0);
    //You can light the lamp, call the print function, etc., set the flag bit, and the verification can pass
}

Summary:

External interrupts are often used for keys, which can avoid that the key input cannot be scanned during the main cycle.

Tags: stm32 Embedded system Single-Chip Microcomputer

Posted by yoma31 on Mon, 30 May 2022 01:03:43 +0530