Application of STM32 external (IO) interrupt EXTI

Publisher:fuehrd努力的Latest update time:2015-11-12 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
a)         Purpose: Similar to serial port input, IO input without interrupts is also inefficient, and button events can be inserted through EXTI. This section is related to EXTI interrupts.

b)         Initialization function definition:

void  EXTI_Configuration(void);  //Define IO interrupt initialization function

c)         Initialization function call:

EXTI_Configuration(); //IO interrupt initialization function call Simple application:

d)         Initialization function:

void  EXTI_Configuration(void)

{

  EXTI_InitTypeDef  EXTI_InitStructure;         //EXTI initialization structure definition

 

EXTI_ClearITPendingBit(EXTI_LINE_KEY_BUTTON); //Clear interrupt flag

   GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,  GPIO_PinSource3); //Pin selection

   GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,  GPIO_PinSource4);

     GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,  GPIO_PinSource5);

     GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,  GPIO_PinSource6);

 

    EXTI_InitStructure.EXTI_Mode  EXTI_Mode_Interrupt; //Event selection

  EXTI_InitStructure.EXTI_Trigger  EXTI_Trigger_Falling; //Trigger mode

  EXTI_InitStructure.EXTI_Line  EXTI_Line3  EXTI_Line4;  //Line selection

  EXTI_InitStructure.EXTI_LineCmd  ENABLE; //Start interrupt

  EXTI_Init(&EXTI_InitStructure); //Initialization

}

 

e)         Turn on the I/O clock in the RCC initialization function

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA  ENABLE);

 

Define the input I/O pins in the GPIO initialization function.

//IO input, GPIOA's 4-pin input

  GPIO_InitStructure.GPIO_Pin  GPIO_Pin_4;

    GPIO_InitStructure.GPIO_Mode  GPIO_Mode_IPU;          //Pull-up input

  GPIO_Init(GPIOA,  &GPIO_InitStructure);                 //Initialization

f)         Add the following code in the NVIC initialization function to enable related interrupts:

  NVIC_InitStructure.NVIC_IRQChannel  EXTI9_5_IRQChannel;         //Channel

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPrio rity  0;//Priority level

  NVIC_InitStructure.NVIC_IRQChannelSubPriority  0;                         //Response level

  NVIC_InitStructure.NVIC_IRQChannelCmd  ENABLE;                         //Start

  NVIC_Init(&NVIC_InitStructure);                                                                 //Initialization

 

g)         Find the void USART1_IRQHandler function in the stm32f10x_it.c file  and add the execution code to it. Generally, there are at least three steps: first use the if statement to determine which interrupt occurred, then clear the interrupt flag, and finally assign a value to the string, or do other things.

  if(EXTI_GetITStatus(EXTI_Line3)  !=  RESET)                                   //Judge the source of the interrupt

   EXTI_ClearITPendingBit(EXTI_Line3);                                           //Clear the interrupt flag

    USART_SendData(USART1,  0x41);                                               //Send the character "a"

    GPIO_WriteBit(GPIOB,  GPIO_Pin_2,  (BitAction)(1-GPIO_ReadOutputDataBit(GPIOB,  GPIO_Pin_2))); //LED alternates between light and dark

h

       Interrupt precautions:

The interrupt bit must be cleared after the interrupt occurs, otherwise the interrupt will occur in an endless loop. Then the interrupt type needs to be determined before executing the code.

To use the EXTI I/O interrupt, three things need to be done after completing the RCC and GPIO hardware settings: initialize EXTI, enable NVIC interrupts, and write interrupt execution code. 
Keywords:STM32 Reference address:Application of STM32 external (IO) interrupt EXTI

Previous article:About dynamically changing the STM32 PWM duty cycle
Next article:STM32 PWM output

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号