STM32F10X EXTI control LED on and off tutorial

Publisher:DazzlingSpiritLatest update time:2019-04-10 Source: eefocusKeywords:STM32F10X  LED Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The development board I use here is the MiniSTM32 from Zhengdian Atom, and the chip model is STM32F103RCT6. This is easy, so you can do that very easily!

***REMENBER STM32 is you! :)


We will give a popular explanation here. For detailed content, you can refer to the STM32 manual later.


Here we first introduce the concept of interruption. An example of interruption is that I am eating now, and someone knocks on the door. Then I have to put down my bowl and chopsticks to open the door, and then come back to continue eating. Why do we need interruption? Because we definitely don't want to always check if there is anyone at the door when we are eating, which will take up my eating time! This is of great significance in the program!


So we think what to do if there are many interruptions? For example, when I am eating, someone knocks on the door, but the water is turned on again. These two things happen almost at the same time. What should I do instead? At this time, the concept of priority comes into play. I will do the most important thing first!


Here we use the example of switch controlling LED to illustrate some usage of the three contents of GPIO EXTI NVIC!


The LED light interface we use is PA8, and the switch we use is PC5, which is the pull-up mode.


The example here is that the switch uses the EXTI interrupt to control the light on and off (note that our code here is continuous):

//@Time:2018/2/16


//@author:junwencui

//@wether: sun

//@location: yunnan


void key_Config(void){

 

    GPIO_InitTypeDef GPIO_InitStructure;

EXTI_InitTypeDef EXTI_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;


RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //It is better to enable multiplexing here

The above is the beginning of the configuration function. First, we define the structure, and then we need to turn on the clock. It should be noted that in KEIL, turning on the clock needs to be placed after the definition of the structure, otherwise an error will be reported!


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //Pull-up input mode is used here

  GPIO_Init(GPIOC,&GPIO_InitStructure); //Initialize structure definition

The mode GPIO_Mode_IPU here is used when using pull-up input, of course you can also use

GPIO_Mode_IPD Pull-down input

The difference here is your circuit hardware!


  GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource5); //map EXTI to PC5 here

 

  EXTI_InitStructure.EXTI_Line = EXTI_Line5;

  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;

  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;

  EXTI_InitStructure.EXTI_LineCmd = ENABLE;

  EXTI_Init(&EXTI_InitStructure);

Here, because PC5 is defined, the corresponding EXTI_Line5 is simply used in interrupt mode. Because PC5 is connected to a pull-up resistor, it is of course triggered when the level drops! Use EXTI_Trigger_Falling

  NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //Here, specify the external interrupt as EXTI5~9

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //Set the preemption priority to 2

  NVIC_InitStructure.NVIC_IRQChannelSubPriority=0; //Set the response priority to 0

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // Enable NVIC

  NVIC_Init(&NVIC_InitStructure); //Initialize the structure

 

 

}

The external interrupt here uses EXTI_Line5, but the stm32 library function defines interrupt lines 5 to 9, all using EXTI9_5_IRQn. This is actually not a big deal, because we still have to make judgments inside the interrupt function later! The priority of interrupts follows the rule: first look at the preemption priority, then look at the response priority. When the preemption priorities are the same and they occur at the same time, look at the response priority. The smaller the value, the higher the level! So when there are multiple interrupts, we need to carefully arrange these priorities.

Of course we can also define our priority group, example: NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 

                                                                    //The preemption priority can be 0~3, and the response priority can be 0~3;

This is the priority group selection range, you must not exceed the range!

The above is the content of EXTI interrupt configuration! Next, let's look at the interrupt function:

static u8 led_red_flag =0; //This is the flag bit

void EXTI9_5_IRQHandler(void){

 

    delay_ms(8); //debounce

 

   if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5) && !led_red_flag){ //Read the status of Pin_5

   

      GPIO_ResetBits(GPIOA,GPIO_Pin_8); 

     led_red_flag =1;

   

  }else if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5) && led_red_flag){

   

    GPIO_SetBits(GPIOA,GPIO_Pin_8); 

     led_red_flag =0;      

  }

  

  EXTI_ClearITPendingBit(EXTI_Line5);

}


The interrupt function here is fixed. We can check it in stm32f10x_it.h. From the name, EXTI9_5_IRQHandler means interrupt line 5~9, which is consistent with the above content! The debounce here is a common operation to prevent interference! In the interrupt function, there will be a statement to clear the interrupt flag bit, which is EXTI_ClearITPendingBit(EXTI_Line5);

This is crucial and will never be missed!


void led_Congfig(void){

    

   GPIO_InitTypeDef GPIO_InitStructure; //PA8

   

   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

 

   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Use push-pull output

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;

   GPIO_InitStructure.GPIO_Speed ​​=GPIO_Speed_50MHz;

   GPIO_Init(GPIOA,&GPIO_InitStructure);

 

   GPIO_SetBits(GPIOA,GPIO_Pin_8);

}

Here is the function to configure the LED, using push-pull output and a working speed of 50Mhz!

int main(){

 

    led_Congfig();

   key_Config();

 

   while(1){

  

  }

return 0;

}


Here is our main function! Note that while(1{} here cannot be omitted, otherwise the program will exit directly without interruption!!!


Keywords:STM32F10X  LED Reference address:STM32F10X EXTI control LED on and off tutorial

Previous article:STM32 beginner's LED button interrupt
Next article:STM32 Practice 3. Timer controls LED flashing (Timer 1)

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号