STM32F4 external interrupt EXTI

Publisher:千变万化Latest update time:2016-12-19 Source: eefocusKeywords:STM32F4 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

It is also a half-baked thing, and a simple interrupt processing is performed.
Software environment: MDK470a
Hardware environment: STM32F4-Discovery. Button B1 is connected to the PA0 pin of the F4 chip. When the button is pressed, the pin level is pulled down.
Function description: When button B1 is pressed, an external interrupt is triggered. After entering the interrupt processing function, a message is sent through the serial port.
Implementation steps:
1. Turn on the PA clock and set the PA0 pin as input.

void EXTI_GPIO_Congig(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);
  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  _InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Pin = GPIO_PinSource0;
  GPIO_InitStructure.GPIO_Speed ​​=GPIO_Speed_100MHz;
  GPIO_Init(GPIOA ,&GPIO_InitStructure);
  
}

2. Turn on the system configuration controller clock.
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);
The function of the system control register (SYSCFG) is newly added in the F4 series. The functions are as follows:
The system configuration controller is mainly used to remap the memory accessible in the code area, select the Ethernet PHY interface and manage the external interrupt line 
connection to the GPIOs.
SYSCFG is mainly used to map the memory access to the CODE area, select the Ethernet interface, and manage the connection of the external interrupt line to the GPIO.
To set an external interrupt, you must also set the external interrupt configuration register of SYSCFG.
3. Configure the working mode of the external interrupt EXTI.
    Map it to PA0, that is, line 0, and use the interrupt mode falling edge trigger.
   Setting the working mode of the EXTI register is handed over to the library function.

void EXTI_Config(void)
{
  EXTI_GPIO_Congig();
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);
  
  EXTI_InitStructure.EXTI_Line = EXTI_Line0; EXTI_InitStructure.EXTI_LineCmd
  = ENABLE;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Inter rupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  EXTI_Init(&EXTI_InitStructure);
  
}

4. Write an interrupt handling function to print information to the serial port.
     Fixed function name: void EXTI0_IRQHandler(void).
     After entering the interrupt handling function, first check whether it is an interrupt on line 0. If so, clear the interrupt flag. Then you can send a message.
     After the message is sent, clear the external interrupt that arrived during the external interrupt processing. Use EXTI_ClearITPendingBit() to complete

void EXTI0_IRQHandler(void)
{
  if(SET == EXTI_GetITStatus(EXTI_Line0))
  {    
    EXTI_ClearFlag(EXTI_Line0);
    printf("i am in exti irqhandler\r\n");
    printf("and the extiflag is cleared\r\n" );
    EXTI_ClearITPendingBit(EXTI_Line0);
  }  
}


Keywords:STM32F4 Reference address:STM32F4 external interrupt EXTI

Previous article:STM32F4 ADC1
Next article:STM32F4 MDK creates a new project using the library

Latest Microcontroller Articles
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号