STM32 learning notes (2): Use of external interrupts

Publisher:心境恬淡Latest update time:2015-09-07 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The status of interrupts in developing embedded systems is absolutely unquestionable. In the era of C51 microcontrollers, there were only 5 interrupts, including 2 external interrupts, 2 timer/counter interrupts and one serial port interrupt. However, in STM32, the number of interrupts has greatly increased, and the interrupt settings are more complicated. Today, we will discuss the interrupt system in STM32.

1 Basic Concepts

The ARM Coetex-M3 core supports a total of 256 interrupts, including 16 internal interrupts, 240 external interrupts and a programmable 256-level interrupt priority setting. The STM32 currently supports a total of 84 interrupts (16 internal + 68 external), as well as a 16-level programmable interrupt priority setting, 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 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 position, the front is defined as the preemptive priority, and the back is the response priority. According to this grouping, the 4-bit can be divided into 5 groups in total.

Group 0: All 4 bits are used to specify the response priority;

Group 1: The highest bit is used to specify the preemptive priority, and the following 3 bits are used to specify the response priority;

Group 2: The top 2 bits are used to specify the preemptive priority, and the next 2 bits are used to specify the response priority;

Group 3: The top 3 bits are used to specify the preemptive priority, and the last bit is used to specify the response priority;

Group 4: All 4 bits are used to specify preemptive priority.

The so-called preemptive priority and response priority, the relationship between them is: an interrupt with a high preemptive priority can be responded to during the interrupt processing with a low preemptive priority, that is, interrupt nesting.

When the preemptive priority of two interrupt sources is 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. 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 things to note:

1) If the specified preemptive priority or response priority exceeds the range defined by the selected priority group, unexpected results may occur;

2) There is no nesting relationship between interrupt sources with the same preemptive priority level;

3) If an interrupt source is assigned a preemptive priority level and there is no other interrupt source at the same preemptive priority level, any valid response priority level can be assigned to 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. 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 mechanism: NVIC. For a detailed explanation of NVIC, you can refer to "ARM Cortex-M3 Authoritative Guide", written by Joseph Yiu, translated by Song Yan, published by Beijing University of Aeronautics and Astronautics Press, Chapter 8 NVIC and Interrupt Control. The enable, suspension, priority, activity, etc. of interrupts are all managed by NVIC. Because my focus in learning STM32 is on how to develop programs, I will not explain some of the internal things in detail here. If you are interested, you can refer to the book mentioned above.

3. Program Development

In fact, the basic concepts and knowledge above are just a general understanding of the STM32 interrupt system. Using programs will help you better understand how to use interrupts. The basic steps for using external interrupts are as follows:

1. Set the corresponding clock;

2. Set the corresponding interrupt;

3. IO port initialization;

4. Set the corresponding IO port as an interrupt line (before setting the external interrupt) and initialize it;

5. Interrupt function in the response function of the selected interrupt channel.

 

Since the Struggle development board I used did not lead out the corresponding chip pins, I could only use buttons to trigger the corresponding interrupts. According to the schematic diagram, 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. Set the corresponding clock

First, you 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 functions are explained in detail in the GPIO study notes. The detailed code is as follows:

void RCC_cfg()

{

       // Turn on the PE PD PC PB port clock and turn on the multiplexed 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 interrupt

Setting the corresponding interrupt is actually setting NVIC. In the STM32 firmware library, there is a structure NVIC_InitTypeDef, which contains the corresponding flag bit settings, and then initialized with the NVIC_Init() function. The detailed code is as follows:

void NVIC_cfg()

{

        NVIC_InitTypeDef NVIC_InitStructure;

        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 the second group is selected. 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. [page]

 

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; //Maximum output frequency 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; //Maximum output frequency 50MHz

       GPIO_Init(GPIOC,&GPIO_InitStructure); //Set PC.2/PC.3/PC.5

      

      

       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_6; //Select pin 3 6

       GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz; //Maximum output frequency 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; //Maximum output frequency 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 an interrupt line

Since GPIO is not a dedicated interrupt pin, when using GPIO to trigger an external interrupt, it is necessary to connect 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

Unlike C51 microcontrollers, STM32 can use the interrupt keyword to define interrupt response functions. The STM32 interrupt response function interface is stored 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, so the function body is actually very simple:

void EXTI2_IRQHandler(void)

{

       //Light up the LED

       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);            

}

 

There is a function declaration before the main function. The main function body calls the initialization configuration function and then enters an infinite loop, waiting for the interrupt response.

 

Since this article involves many library functions, we can find out what the corresponding functions do by looking up the library function documentation in "ARM®-based 32-bit MCU STM32F101xx and STM32F103xx firmware library". There is also a Chinese version of the documentation available online for reference.

Keywords:STM32 Reference address:STM32 learning notes (2): Use of external interrupts

Previous article:STM32 Learning Notes (3): System Clock and SysTick Timer
Next article:STM32 learning notes (1): Use of GPIO ports

Recommended ReadingLatest update time:2024-11-16 17:46

STM32 (II) GPIO operation (1) - input and output operation
      GPIO is the abbreviation of general purpose input and output. In other words, it is a pin that can be controlled by the MCU. The MCU connects to external devices through GPIO pins to achieve external communication, control and data acquisition functions. This article takes the GPIO of STM32 as an example to intr
[Microcontroller]
STM32 IO port settings
1. When the IO port is used as a button: if the button is pressed at a low level, the IO port should be set to a pull-up input; if the button is pressed at a high level, the IO port should be set to a pull-down input. 2. KEIL4.7 version has a grammar check function. Today I found that when the folder is in uppercase E
[Microcontroller]
Comparison between stm32 and pic microcontrollers_Which one is better
  STM32 MCU   The STM32 series of microcontrollers launched by ST are known to all industry friends as being a series of microcontrollers with the highest cost-performance ratio, and they are extremely powerful. They are based on the ARM Cortex-M core designed specifically for embedded applications that require high p
[Microcontroller]
Comparison between stm32 and pic microcontrollers_Which one is better
STM32 port multiplexing & remapping
Let me tell you about the port remapping of the STM32 microcontroller, because I use myself as an example. Here I take the remapping of USART1 as an example. Because I want a TFT_LCD screen main control board, considering FSMC, I chose STM32F103VCT6 model CPU, and accidentally connected the serial port to USART1. Bec
[Microcontroller]
STM32 port multiplexing & remapping
IIC Topic 2 - STM32 driving AT24C02
1 Overview The EEPROM chip model on the MiniSTM32 development board is 24C02. The total capacity of the chip is 256 bytes, and the chip is connected to the outside through the IIC bus. Here we directly use the AT24C02 on the Atom board, mainly for learning software programming. 2. Hardware Connection The thre
[Microcontroller]
IIC Topic 2 - STM32 driving AT24C02
stm32.cube(七)——arm-gcc.helloworld
1. Introduction There are corresponding examples in the cube package for the three commonly used ARM IDE tools: MDK-ARM (keil), EWARM (IAR) and TrueSTUDIO (Atollic). As for arm-gcc, cube only provides the source code of the examples. Since you need to write your own Makefile, the configuration is more complicated than
[Microcontroller]
stm32 controls the rotation of the servo DS3115
1. Servo DS3115 Generally speaking, the servo is mainly composed of the following parts: steering wheel, reduction gear set, position feedback potentiometer 5k, DC motor, control circuit board, etc. Working principle: The control circuit board receives the control signal from the signal line (the specific signal will
[Microcontroller]
stm32 controls the rotation of the servo DS3115
FSMC mechanism FlaSh memory expansion of STM32 microcontroller
introduction STM32 is a 32-bit microcontroller series based on the ARM core Cortex-M3 launched by ST (STMicroelectronics). The Cortex-M3 core is specially designed for low-power and price-sensitive applications, with outstanding energy efficiency and processing speed. By adopting the Thumb-2 high-density in
[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号