Getting Started with STM32 Function Library External Interrupts

Publisher:Meilin8888Latest update time:2015-07-02 Source: 新手入门使用STM32函数库之外部中断 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
      Without further ado, let me first take a look at the button schematic on my Alienware development board.

        The board is lazy, or it is called making full use of the pull-up function of the IO port, and no common pull-up resistor is added here. When programming, just enable the pull-up of the IO~ Look at the interface below to know that KEY0 is connected to PA13 of STM32!

        CZZ once told me in a dream that any IO of STM32 can be used as an external interrupt input. Wow, it’s super powerful!

        The general procedure steps are as follows:

        1. System initialization, such as system clock initialization, so that it enters 72MHZ main frequency;
        2. GPIO configuration, be sure to turn on the AFIO clock when turning on the GPIO clock.
        3. EXTI configuration, here configure which pin to select as the interrupt pin.
        4. NVIC configuration, this is also an extra part compared to the microcontroller. We must enable the corresponding channel in NVIC and set the priority level.
        5. Use while(1) to perform an infinite loop, and write in the interrupt program how to handle the interrupt when it occurs.

        Follow the above method step by step to realize the function~

        First, define the relevant structure.

        GPIO_InitTypeDef GPIO_InitStructure;
         EXTI_InitTypeDef EXTI_InitStructure;
         NVIC_InitTypeDef NVIC_InitStructure;

 

         The second step is to configure IO and its functions.

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //Here is the code to configure the LED light for easy observation!
         GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
         GPIO_Init(GPIOA, &GPIO_InitStructure);

         GPIO_WriteBit(GPIOA,GPIO_Pin_8,Bit_SET); //Turn off the LED immediately after power on
 
         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //Configure PA13 pull-up input 
         GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //Pull-up input
         GPIO_Init(GPIOA, &GPIO_InitStructure);

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE); //Make sure to turn on the GPIOA and AFIO clocks!!!

         The third step is to configure external interrupts, which is equivalent to the interrupt settings of the microcontroller.

        GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource13); //Configure pin 13 of port A as interrupt
         EXTI_ClearITPendingBit(EXTI_Line13); //Clear interrupt, it seems that there is no problem without adding it, but it is safe!
 
         EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //External interrupt
         EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //Falling edge trigger
         EXTI_InitStructure.EXTI_Line = EXTI_Line13;
         EXTI_InitStructure.EXTI_LineCmd = ENABLE;        
         EXTI_Init(&EXTI_InitStructure);

        The fourth step is to configure NVIDIA, which is also a feature of STM32.

        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
         NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; //Channel 13 belongs to 15-10, they are shared!
         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //Preemptive priority 0
         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Sub-priority 0
         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
         NVIC_Init(&NVIC_InitStructure);

        while(1); //wait for interrupt to occur

        The fifth step is to write the interrupt processing function ISR and add the following code in stm32f10x_it.c.

void EXTI15_10_IRQHandler(void)
{
         if ( EXTI_GetITStatus(EXTI_Line13) == SET) //Judge whether it is the interrupt of pin 13
         {
                   EXTI_ClearITPendingBit(EXTI_Line13); //Clear the interrupt flag!
                  GPIO_WriteBit(GPIOA,GPIO_Pin_8,Bit_RESET); //Turn on the LED            
         }
}

        OK, the program is complete. Let’s test it!

Reference address:Getting Started with STM32 Function Library External Interrupts

Previous article:Causes and solutions for Flash download failed-Cortex-M3
Next article:Design of comprehensive monitoring system for urban rail energy-feed power supply system based on ARMCortex-A8

Recommended ReadingLatest update time:2024-11-16 14:28

AVR microcontroller frequency meter design frequency input source STM32 port
//---------------------STM32 3.3V to 5V output circuit------------------------ Download the complete program source code: http://www.51hei.com/f/avrplj.rar //---------------------AVR Code-----------------------   //Chip uses ATMEG16      #include iom16.h   #define uchar unsigned char #defin
[Microcontroller]
AVR microcontroller frequency meter design frequency input source STM32 port
STM32 CAN bus principle
Introduction: CAN is the abbreviation of Controller Area Network (hereinafter referred to as CAN), which is an ISO internationally standardized serial communication protocol. It was first proposed by German electrical company Bosch in 1986. Since then, CAN has been standardized through ISO11898 and ISO11519. It is no
[Microcontroller]
STM32 CAN bus principle
STM32 study notes: infrared remote control
1. Brief Introduction Infrared remote control is a wireless, non-contact control technology with strong anti-interference ability, reliable information transmission, low power consumption and low cost. The encoding methods for infrared remote control that are currently widely used are: NEC protocol for PWM (pulse wi
[Microcontroller]
STM32 study notes: infrared remote control
STM32 learning: keil measurement function running time
Set the operating frequency during Trace (that is, the current system clock, such as stm32f103, f107 is 72MHz, f407 is 168M, and f429 is 180M): 2. Enter the debugging state, set a breakpoint before a statement and execute to this point, then execute to another statement. The following Sec is the execution time
[Microcontroller]
Application of MS5540c pressure and temperature sensor based on STM32
3.5 library functions, stm32f10x series MCU, there are few examples of this chip on the Internet, and the official code an510 document cannot be found for download, and the datasheet is not very clear. Here is my basic program for your reference: #include "main.h" #include "Flash_LED.h" #include "SPI_MS5540C.h" #incl
[Microcontroller]
Analysis of WM_REDRAW.c source code of transplanting ucGUI on STM32
1. Transplantation of wm_redraw.c     In my previous note, I talked about how to port ucGUI to STM32 to develop bare-metal programs. In this note, I will analyze the porting and source code of wm_redraw.c. First, open the ported ucGUI project file, and then copy the functions in the WM_Redraw.c file in the ucGUI\Sampl
[Microcontroller]
【STM32】Interrupts and interrupt priorities
  In the process of embedded system development, it is inevitable to deal with "interrupts". Many beginners do not have a deep understanding of interrupts, and often see interrupts and go around. In fact, it is not the case. Spending some time to understand interrupts can solve many problems.   Interrupt in English is
[Microcontroller]
STM32 MCU GPIO register
Each GPIO port has two 32-bit configuration registers (GPIOx_CRL, GPIOx_CRH) to control the high and low eight bits of each port respectively. If the IO port is 0-7, write the CRL register, if the IO port is 8-15, write the CRH register, two 32-bit data registers (GPIOx_IDR, GPIOx_ODR), one is read-only as input dat
[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号