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 Pins | Interrupt flag | Interrupt handling function |
PA0~PG0 | EXTI0 | EXTI0_IRQHandler |
PA1~PG1 | EXTI1 | EXTI1_IRQHandler |
PA2~PG2 | EXTI2 | EXTI2_IRQHandler |
PA3~PG3 | EXTI3 | EXTI3_IRQHandler |
PA4~PG4 | EXTI4 | EXTI4_IRQHandler |
PA5~PG5 | EXTI5 | EXTI9_5_IRQHandler |
PA6~PG6 | EXTI6 | |
PA7~PG7 | EXTI7 | |
PA8~PG8 | EXTI8 | |
PA9~PG9 | EXTI9 | |
PA10~PG10 | EXTI10 | EXTI15_10_IRQHandler |
PA11~PG11 | EXTI11 | |
PA12~PG12 | EXTI12 | |
PA13~PG13 | EXTI13 | |
PA14~PG14 | EXTI14 | |
PA15~PG15 | EXTI15 |
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.
Previous article:STM32 learning: the three main parts of STM32F4XX
Next article:STM32 Learning: FMC-Extending External SDRAM
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Sandia Labs develops battery failure early warning technology to detect battery failures faster
- LNA Design Based on ADS
- When I wrote the program, I saw TMOD=1, but I didn't see any operation on each bit. What's the meaning of these 8 bits? And some...
- Download Keysight Technologies e-books and get gifts!
- Simulating Microstrip Elliptical Low-Pass Filter Using ADS Software
- STM32L151 low power consumption problem
- RC4 key cracking assistance (chip symmetric encryption key)
- 【Beetle ESP32-C3】Unboxing
- Dandelion 4G Router Experience
- [Sipeed LicheeRV 86 Panel Review] 13. Using TCP to encapsulate HTTP packets to request weather information
- 28335 Main program error