STM32 study notes - external interrupt EXTI

Publisher:Tianyun2021Latest update time:2015-08-17 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Study Notes

for    STM32F103C8

redesigned  by  zhang  bin

2012-10-30

versions:V-0.1

All  Rights  Reserved

 

main.c is as follows, with detailed comments. You can basically use it after understanding the following examples and instructions:

 

 

 

 

 

//A high preemption level will interrupt other interrupts and give priority to execution, while a high response level will give priority to execution after other interrupts are completed.

//EXTI: external interrupt/event controller

 

//EXTI controller can generate up to 19 software events/interrupt requests

//Configure 19 lines as interrupt sources, 19 lines as event sources, and 19 lines as software interrupt/event lines. For details, see "STM32F103xxx User Manual"

//P135

 

//This file mainly performs various initialization configurations, including GPIO configuration, EXTI configuration, system clock configuration, etc.

//The interrupt service routine is in the stm32f10x_it.c file, which provides templates for all exception handlers and peripheral interrupt service routines. For details, see this file

 

#include  "stm32f10x_lib.h"

 

 

 

EXTI_InitTypeDef  EXTI_InitStructure;   //Define the external interrupt initialization structure variable,   which contains 4 members.  See the definition of EXTI_InitTypeDef for details.

ErrorStatus  HSEStartUpStatus; //Define the error status variable, which is an enumeration type

 

 

 

void  RCC_Configuration(void);

void  GPIO_Configuration(void);

void  NVIC_Configuration(void);

 

 

 

int  main(void)

{

#ifdef  DEBUG

  debug();

#endif

  

 

  RCC_Configuration();   //System clock configuration   

 

  NVIC_Configuration(); //NVIC configuration

    

  GPIO_Configuration();   //Configure GPIO

  

   //The external interrupt of STM32 is not fixed, but can be mapped. For example, EXTI6 can be mapped to PB6 or PA6. For details, see the mapping relationship between external interrupts/events and GPIO.

   //Register the IO port to the interrupt line (map the IO port to the interrupt line N)

  //

  GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,  GPIO_PinSource6);   //Select GPIO pin as external interrupt line

  //The first parameter selects the GPIO port used as the external interrupt line source.   The second parameter is the external interrupt line to be set, which can be GPIO_PinSourcex (x can be 0~15)

[page]

  //Configure the button interrupt line trigger mode   to set the interrupt configuration structure members

  EXTI_InitStructure.EXTI_Line  EXTI_Line6; //Select external interrupt lines   EXTI_Line0~EXTI_Line18, a total of 19 lines

  EXTI_InitStructure.EXTI_Mode  EXTI_Mode_Interrupt;    //Set to interrupt request, can be EXTI_Mode_Interrupt interrupt request or

  //EXTI_Mode_Event event request

  EXTI_InitStructure.EXTI_Trigger  EXTI_Trigger_Falling;  //Trigger mode falling edge trigger  can also be EXTI_Trigger_Rising rising edge trigger or

  //EXTI_Trigger_Rising_Falling rising edge falling edge trigger

  EXTI_InitStructure.EXTI_LineCmd  ENABLE;  //Interrupt line enable    ENABLE, DISABLE

  EXTI_Init(&EXTI_InitStructure);  //Initialize EXTI interrupt according to parameter structure

 

  

  EXTI_GenerateSWInterrupt(EXTI_Line6);  //Generate a software interrupt   EXTI_Line6 interrupt enable 

  //The interrupt configuration is now complete and you can write the interrupt handling function.

        

  while  (1)

  {

  }

}

 

 

 

void  RCC_Configuration(void)

{

//Reset RCC external device registers to default values

  RCC_DeInit();  

 

  // Turn on the external high-speed crystal oscillator

  RCC_HSEConfig(RCC_HSE_ON); 

 

 //Wait for external high-speed clock to be ready

  HSEStartUpStatus  RCC_WaitForHSEStartUp(); 

 

  //The external high-speed clock is ready

  if(HSESTartUpStatus  ==  SUCCESS)  

  {

    

    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

 

   

    FLASH_SetLatency(FLASH_Latency_2);

  

   //Configure AHB (HCLK) clock = SYSCLK

    RCC_HCLKConfig(RCC_SYSCLK_Div1);

  

    //Configure APB2 (PCLK2) clock = AHB clock

    RCC_PCLK2Config(RCC_HCLK_Div1); 

 

    //Configure APB1 (PCLK1) clock = AHB  1/2 clock

    RCC_PCLK1Config(RCC_HCLK_Div2);  

 

    //Configure ADC clock = PCLK2  1/4

    RCC_ADCCLKConfig(RCC_PCLK2_Div4); 

  

    //Configure PLL clock  ==  external high-speed crystal clock * 9 8MHz * 9MHz = 72MHz

    RCC_PLLConfig(RCC_PLLSource_HSE_Div1,  RCC_PLLMul_9); 

    

//Configure ADC clock =  PCLK2/4

    RCC_ADCCLKConfig(RCC_PCLK2_Div4);

 

   // Enable PLL clock

    RCC_PLLCmd(ENABLE);  

 

   //Wait for PLL clock to be ready

    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)  ==  RESET)  

    {

    }

 

    //Configure system clock  PLL clock

    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); 

 

   // Check if the PLL clock is used as the system clock

    while(RCC_GetSYSCLKSource()  !=  0x08)  

    {

    }

  }

   

  

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA  |RCC_APB2Periph_GPIOB

                         RCC_APB2Periph_AFIO,  ENABLE);

   // Enable GPIOA, GPIOB and APB2 of AFIO

}

 

 

void  GPIO_Configuration(void)

{

  GPIO_InitTypeDef  GPIO_InitStructure;    //Define the structure for configuring GPIO

   

  GPIO_InitStructure.GPIO_Pin  GPIO_Pin_8; //Select the 8th pin

  GPIO_InitStructure.GPIO_Speed  ​​=  GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode  GPIO_Mode_Out_PP;        //Push-pull output

  GPIO_Init(GPIOB,  &GPIO_InitStructure); //Initialize GPIOB with the configured structure PB8 is used to drive the LED

 

  GPIO_InitStructure.GPIO_Pin  GPIO_Pin_6;    //Select pin 6

  GPIO_InitStructure.GPIO_Mode  GPIO_Mode_IN_FLOATING;    //Configure floating input

  GPIO_Init(GPIOB,  &GPIO_InitStructure);   //Initialize GPIOB    PB6 pin to connect to the interrupt source of external interrupt line 6

  //All ports have external interrupt capability. To use the external interrupt line, the port must be configured as input mode.

 

}

 

 [page]

void  NVIC_Configuration(void)   //Configure the nested vector interrupt controller

{

  NVIC_InitTypeDef  NVIC_InitStructure; //Define the structure variable to initialize NVIC 

  

#ifdef   VECT_TAB_RAM  

    //Configure the vector table base address

  NVIC_SetVectorTable(NVIC_VectTab_RAM,  0x0);    //Assign interrupt vector table

#else  

   

  NVIC_SetVectorTable(NVIC_VectTab_FLASH,  0x0);   

#endif

 

  

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);   //Set interrupt priority

  //Set the priority group: preemptive priority and secondary priority   input parameters to set the priority group bit length.   In this example, NVIC_PriorityGroup_1: preemptive priority 1, secondary priority 3

  //For details, see "STM32 Function Description" P165

  //Priority grouping can only be set once

  

  //EXTI5-9 share one interrupt source, EXTI10-15 share one interrupt source

     //For details, please refer to "STM32 Function Description" P166

  NVIC_InitStructure.NVIC_IRQChannel  EXTI9_5_IRQChannel;  //Interrupt channel   specifies the IRQ channel. Here, the external interrupt line 9-5 is specified   because EXTI6 is used.

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPrio rity   0;   //Set the preemption priority of the specified IRQ channel

  NVIC_InitStructure.NVIC_IRQChannelSubPriority  0; //Set the slave priority of the specified IRQ channel

  NVIC_InitStructure.NVIC_IRQChannelCmd  ENABLE;   //Set the specified IRQ channel interrupt enable   DISABLE disable

  NVIC_Init(&NVIC_InitStructure); //Initialize NVIC interrupt according to the parameters specified in the structure

}

 

#ifdef   DEBUG

 

void  assert_failed(u8*  file,  u32  line)

  

 

  

  while  (1)

  {

  }

}

#endif

 

 

 

 

 

The interrupt service function in the stm32f10x_it.c file is as follows:

 

void  EXTI9_5_IRQHandler(void) //Interrupt handler for external line 5-9

{

  if(EXTI_GetITStatus(EXTI_Line6)  !=  RESET)  //Check whether the specified EXTI6 line trigger request occurs.  

  //This function checks whether the specified EXTI line departure request occurs

  {

      //Actually it is to invert PB8

    GPIO_WriteBit(GPIOB,  GPIO_Pin_8,  (BitAction)((1-GPIO_ReadOutputDataBit(GPIOB,  GPIO_Pin_8))));

//This function can operate the specified port bit.   Specify the port and bit, then specify the operation. The third parameter specifies the value to be written.

//This parameter must take one of the values ​​of the enumeration type BitAction, which can be Bit_RESET: clear the data port bit and Bit_SET: set the data port bit

//GPIO_ReadOutputDataBit() function reads the output of the specified IO port pin, the parameters specify the port and bit

 

 //EXTI_ClearFlag(EXTI_Line6);     //Clear interrupt flag (required)    I saw this requirement online, but it is not included in this example

    

    EXTI_ClearITPendingBit(EXTI_Line6);  //Clear EXTI line pending bit

  }

}

Keywords:STM32 Reference address:STM32 study notes - external interrupt EXTI

Previous article:ucos-ii study notes-creation of the first multitasking program
Next article:STM32 study notes - use of system timer SysTick

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号