Detailed explanation of STM32 external interrupts

Publisher:姑苏清风泉源客Latest update time:2016-09-12 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1. Basic Concepts

 The ARM Coetex-M3 core supports a total of 256 interrupts, including 16 internal interrupts, 240 external interrupts and programmable 256-level interrupt priority settings. STM32 currently supports a total of 84 interrupts (16 internal + 68 external), and 16-level programmable interrupt priority settings, using only the upper 4 bits of the 8-bit interrupt priority setting.
     STM32 can support 68 interrupt channels, which have been fixedly assigned to the corresponding external devices. Each interrupt channel has its own interrupt priority control byte PRI_n (8 bits, but only 4 bits are used in STM32, and the upper 4 bits are valid). The 8-bit interrupt priority control word of each 4 channels constitutes a 32-bit priority register. The priority control words of 68 channels constitute at least 17 32-bit priority registers. The
4-bit interrupt priority can be divided into 2 groups. From the high bit, the front is defined as the preemptive priority, and the back is the response priority. According to this grouping, 4 bits can be divided into 5 groups:
Group 0: All 4 bits are used to specify the response priority;
Group 1: The highest 1 bit is used to specify the preemptive priority, and the following 3 bits are used to specify the response priority;
Group 2: The highest 2 bits are used to specify the preemptive priority, and the following 2 bits are used to specify the response priority;
Group 3: The highest 3 bits are used to specify the preemptive priority, and the following 1 bit is used to specify the response priority;
Group 4: All 4 bits are used to specify the preemptive priority.

      The relationship between the so-called preemptive priority and response priority is that an interrupt with a high preemptive priority can be responded to during the processing of an interrupt with a low preemptive priority, that is, interrupt nesting.
     When the preemptive priorities of two interrupt sources are the same, there will be no nesting relationship between the two interrupts. When an interrupt arrives, if another interrupt is being processed, the later interrupt will have to wait until the previous interrupt is processed before it can be processed. If the two interrupts arrive at the same time, the interrupt controller decides which one to process first based on their response priority; if their preemptive priority and response priority are equal, it decides which one to process first based on their ranking order in the interrupt table. Each interrupt source must define 2 priorities.
There are a few points to note:
1) If the specified preemptive priority or response priority exceeds the range specified by the selected priority group, unexpected results may be obtained;
2) There is no nesting relationship between interrupt sources with the same preemptive priority;
3) If an interrupt source is specified as a preemptive priority level and no other interrupt source is at the same preemptive priority level, any valid response priority level can be specified for this interrupt source.
2. GPIO external interrupt
     In STM32, each GPIO can trigger an external interrupt, but the GPIO interrupt is a unit of group bit, and only one external interrupt in the same group can be used at the same time. For example, PA0, PB0, PC0, PD0, PE0, PF0, PG0 are 1 group. If we use PA0 as the external interrupt source, then the others cannot be used anymore. In this case, we can only use external interrupt sources with different terminal numbers such as PB1 and PC2. Each group uses an interrupt flag EXTIx. The five external interrupts EXTI0 - EXTI4 have their own separate interrupt response functions, EXTI5-9 share an interrupt response function, and EXTI10-15 share an interrupt response function. For interrupt control, STM32 has a dedicated management agency: NVIC.

3. Program Implementation

    In fact, the basic concepts and knowledge above are just a general understanding of the STM32 interrupt system. Using programs to talk will deepen your understanding of how to use interrupts. The basic steps to use external interrupts are as follows:
1. Set the corresponding clock;
2. Set the corresponding interrupt;
3. Initialize the IO port;
4. Set the corresponding IO port as the interrupt line (before setting the external interrupt) and initialize it;
5. Interrupt function in the response function of the selected interrupt channel.

              Assume there are three buttons, and use the buttons to trigger the corresponding interrupts. K1/K2/K3 are connected to PC5/PC2/PC3, so I will use three external interrupts EXTI5/EXTI2/EXTI3. PB5/PD6/PD3 are connected to three LED lights respectively. The effect of the interrupt is that when the button is pressed, the corresponding LED light will be lit.

1. To set the corresponding clock,
     you first need to turn on GPIOB, GPIOC and GPIOE (because the other end of the button is connected to the PE port). Then, because it is used to trigger an interrupt, you also need to turn on the GPIO multiplexing clock. The corresponding function is explained in detail in the GPIO learning notes. The detailed code is as follows:
void RCC_cfg()
{
       // Turn on the PE PD PC PB port clock, and turn on the multiplexing clock
       RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
}
    The RCC function required to set the corresponding clock is in stm32f10x_rcc.c, so add this file to the project.
 
2. Set up the corresponding interrupts
Setting up the corresponding interrupts is actually setting up NVIC. In the STM32 firmware library, there is a structure NVIC_InitTypeDef, which has the corresponding flag settings, and then initializes it using the NVIC_Init() function. The detailed code is as follows:
void NVIC_cfg()
{
        NVIC_InitTypeDef NVIC_InitStructure; //The first structure
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Select interrupt group 2
        
        
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQChannel; //Select interrupt channel 2
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //Set the preemptive interrupt priority to 0
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Set the responsive interrupt priority to 0
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Enable interrupt
        NVIC_Init(&NVIC_InitStructure);
 
        
        NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQChannel; //Select interrupt channel 3
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //Set the preemptive interrupt priority to 1
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //Set the responsive interrupt priority to 1 NVIC_InitStructure.NVIC_IRQChannelCmd
        = ENABLE; //Enable interrupt
        NVIC_Init(&NVIC_InitStructure);
 
        
        NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel; //Select interrupt channel 5
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //Set the preemptive interrupt priority to 2
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //Set the responsive interrupt priority to 2
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Enable interrupt
        NVIC_Init(&NVIC_InitStructure);
}
 
Since there are 3 interrupts, according to the previous article, 3 bits are required to specify the preemption priority, so select Group 2. And since EXTI5-9 share an interrupt response function, the interrupt channel selected by EXTI5 is EXTI9_5_IRQChannel. Detailed information can be found in the header file. The NVIC-related library functions used are in stm32f10x_nivc.c. This file needs to be copied and added to the project. For the specific location, see the notes about GPIO. This code compiles without any problems, but it will report an error when linking. You need to add STM32F10xR.LIB to the project. The specific location is...KeilARMRV31LIBSTSTM32F10xR.LIB.
 
3. IO port initialization
void IO_cfg()
{
       GPIO_InitTypeDef GPIO_InitStructure;
 
      
       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //Select pin 2
       GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz; //Output frequency up to 50MHz
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Output with pull-up resistor
       GPIO_Init(GPIOE,&GPIO_InitStructure);
       GPIO_ResetBits(GPIOE,GPIO_Pin_2); //Set PE.2 pin to low level output
 
      
       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_5; //Select pins 2 3 5
       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //Select input mode as floating input
       GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz; //Output frequency up to 50MHz
       GPIO_Init(GPIOC,&GPIO_InitStructure); //Set PC.2/PC.3/PC.5
      
      
       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_6; //Select pins 3 6
       GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz; //Output frequency up to 50MHz
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Output with pull-up resistor
       GPIO_Init(GPIOD,&GPIO_InitStructure);
      
      
       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //Select pin 5
       GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz; //Output frequency up to 50MHz
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Output with pull-up resistor
       GPIO_Init(GPIOB,&GPIO_InitStructure);        
}
The pin connected to the external interrupt needs to be set to input state, and the pin connected to the LED needs to be set to output state. Initializing PE.2 is to make the other end of the button output a low level. The functions in GPIO are in stm32f10x_gpio.c.
 
4. Set the corresponding IO port as the interrupt line
Since GPIO is not a dedicated interrupt pin, when using GPIO to trigger an external interrupt, it is necessary to set the corresponding GPIO pin and the interrupt line. The specific code is as follows:
void EXTI_cfg()
{
       EXTI_InitTypeDef EXTI_InitStructure;
       // Clear the interrupt flag
       EXTI_ClearITPendingBit(EXTI_Line2);
       EXTI_ClearITPendingBit(EXTI_Line3);
       EXTI_ClearITPendingBit(EXTI_Line5);
 
       // Select interrupt pins PC.2 PC.3 PC.5
       GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource2);
       GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource3);
       GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource5);
 
       EXTI_InitStructure.EXTI_Line = EXTI_Line2 | EXTI_Line3 | EXTI_Line5; //Select interrupt line 2 3 5
       EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //Set to interrupt request, not event request
       EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; //Set the interrupt trigger mode to rising and falling edge trigger
       EXTI_InitStructure.EXTI_LineCmd = ENABLE; //External interrupt enable
      EXTI_Init(&EXTI_InitStructure);
}
The functions that need to be called in EXTI_cfg are all in stm32f10x_exti.c.
 
5. Write interrupt response function
STM32 is not like C51 microcontroller, which can use interrupt keyword to define interrupt response function. The interrupt response function interface of STM32 exists in the interrupt vector table, which is given by the startup code. The default interrupt response function is in stm32f10x_it.c. Therefore, we need to add this file to the project.
In this file, we found that many functions only have a function name, but no function body. We found the function EXTI2_IRQHandler(), which is the function for EXTI2 interrupt response. My goal is to light up the LED light, so the function body is actually very simple:
void EXTI2_IRQHandler(void)
{
       //Light up the LED light
       GPIO_SetBits(GPIOD,GPIO_Pin_6);
       //Clear the interrupt flag to prevent continuous interruption
       EXTI_ClearITPendingBit(EXTI_Line2);
}
 
void EXTI3_IRQHandler(void)
{
       GPIO_SetBits(GPIOD,GPIO_Pin_3);
       EXTI_ClearITPendingBit(EXTI_Line3);
}
 
void EXTI9_5_IRQHandler(void)
{
       GPIO_SetBits(GPIOB,GPIO_Pin_5);
 
       EXTI_ClearITPendingBit(EXTI_Line5);
}
Since EXTI5-9 share the same interrupt response function, all response functions of EXTI5 - EXTI9 are written in this one.
 
6. Write the main function
#include "stm32f10x_lib.h"
 
void RCC_cfg();
void IO_cfg();
void EXTI_cfg();
void NVIC_cfg();
 
int main()
{
          RCC_cfg();
          IO_cfg();
          NVIC_cfg();
          EXTI_cfg();
 
          while(1);            
}

Keywords:stm32 Reference address:Detailed explanation of STM32 external interrupts

Previous article:STM32 AD dual channel DMA mode
Next article:Understanding of STM32 serial port

Recommended ReadingLatest update time:2024-11-16 22:57

Detailed explanation of FSMC usage of STM32
LCD has the following control lines: CS: Chip Select, low level valid RS: Register Select WR: Write write signal, low level valid RD: Read read signal, low level valid RESET: Reset signal, low level valid DB0-DB15: Data line Suppose all these lines are controlled by ordinary IO ports. According to the LCD control chip
[Microcontroller]
Basic understanding of stm32 clock
STM32 is a powerful microcontroller with rich peripherals and powerful functions. Designers must pay attention to its power consumption at first, so clocks are generated. This is easy to understand. Just like our computers, will you open all the software when using them? Do you want to open any video software or games
[Microcontroller]
Determination of STM32 ADC sampling frequency
1. Determination of STM32 ADC sampling frequency 1. : First look at some information to determine the ADC clock: (1) The ADCCLK clock provided by the clock controller is synchronized with PCLK2 (APB2 clock). The CLK controller provides a dedicated programmable prescaler for the ADC clock.     (2) In general, the PC
[Microcontroller]
LCD design based on RA8806 controller of STM32
0 Introduction In modern electronic devices, the application of touch screens makes electronic devices more intelligent and user-friendly. In mobile phones, navigators, electronic instruments, consulting terminals and other devices, as a medium for information exchange, touch screens have the characteristics of flexi
[Microcontroller]
LCD design based on RA8806 controller of STM32
Things to note when learning STM32 under Keil MDK
I have been learning to use STM32 under MDK for almost two weeks. I have basically mastered the development process of STM32 under MDK environment and the writing of basic hardware programs. During this period, I encountered many problems, many of which took me a lot of time to solve. Now I list these problems and thei
[Microcontroller]
[STM32 Motor Vector Control] Record 11——DMA Transfer
DMA transfer: Principle: DMA transfer copies data from one address space to another. DMA transfers data, but does not need to occupy the MCU, that is, when transferring data, the MCU can do other things, such as multithreading. Data is transferred from peripherals to memory or from memory to memory. The DMA controller
[Microcontroller]
STM32 MCU serial port receives GPS data and parses NMEA GPRMC
The program mainly realizes the full analysis of GPRMC data, including time analysis, position analysis, heading analysis, combination analysis, positioning status analysis, etc. Directly use the stm32 series microcontroller to process the data received by the GPS board to obtain the corresponding information 1. gps.h
[Microcontroller]
The difference between STM32 push-pull output and open-drain output
When the port is configured as output:  Open-drain mode: When outputting 0, N-MOS is turned on, P-MOS is not activated, and output is 0.  When outputting 1, N-MOS is high impedance, P-MOS is not activated, and output is 1 (external pull-up circuit is required); this mode can use the port as a bidirectional IO.  Push-
[Microcontroller]
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号