STM32_button interrupt

Publisher:森绿企鹅Latest update time:2018-07-21 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

*************************************************** *************************************************** *************************************

Development board: STM32

CPU: STM32F103

Development environment: keil uVsion4

*************************************************** *************************************************** ************************************

Preface: This article mainly uses key interrupts to achieve that when the key is pressed, the LED light goes off, and when the key is released, the LED light turns on. (However, I set the initial state of the LED to be on, that is, the LED light turns on when the power is turned on.)

    The schematic diagram of the GPIO pins corresponding to the buttons and LEDs on my development board is as follows:


The function of the key interrupt program I wrote is: when the key K2 is pressed, LED3 turns from bright to dark, and when the key is released, LED3 turns bright again. (Key K2 corresponds to the PC2 pin, and LED3 corresponds to the PD3 pin).


Test code:


#include "misc.h"

#include "stm32f10x_gpio.h"

#include "stm32f10x_exti.h"

#include "stm32f10x_rcc.h"

 

#define LED_ON GPIO_SetBits(GPIOD, GPIO_Pin_3);

 

//unsigned char flag=0;

//unsigned char num=0;

void LED_Config(void);

void EXTI_PC2_Config(void);

void RCC_Config(void);

//void KEY_Dither(void);

//void delay_nms(u16 time);

int main(void)

// unsigned char a = 0;

RCC_Config();

LED_Config();

EXTI_PC2_Config();

LED_ON;

while(1)

{

//KEY_Dither();

//if(num==1&&a==0){GPIO_ResetBits(GPIOD,GPIO_Pin_6);a=1;}

//else if(num==1&&a==1){GPIO_SetBits(GPIOD,GPIO_Pin_6);a=0;}

  }

}

/*void KEY_Dither(void)

{

num=0;

if(flag==1)

{   

 delay_nms(12000);

  if(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_6)==0)

{

delay_nms(12000);

  if(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_6)==0)

{

while(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_6)==0)

num=1;

  goto n_exit;

  }

   }

else ;

           n_exit:;

           flag=0;

}

}  

void delay_nms(u16 time)

{    

   u16 i=0;  

   while(time--)

   {

      i=12000; //self-defined

      while(i--) ;    

   }

}

*/

void RCC_Config(void)  

{    

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); 

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOD,ENABLE); 

     SystemInit();  

 

}  

 

void LED_Config(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

 

/*led config*/

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;

GPIO_Init(GPIOD, &GPIO_InitStructure);

 

}

void EXTI_PC2_Config(void)

{

NVIC_InitTypeDef NVIC_InitStructure;

EXTI_InitTypeDef EXTI_InitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

  /*config for NVIC*/

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

 

NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

 

/* key for exti */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

GPIO_Init(GPIOC, &GPIO_InitStructure);

 

GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource2);

  /*EXIT line(PC2) mode config */

EXTI_InitStructure.EXTI_Line = EXTI_Line2;

EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;

EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;

EXTI_InitStructure.EXTI_LineCmd = ENABLE;

EXTI_Init(&EXTI_InitStructure);

 

}

void EXTI2_IRQHandler(void)  

{  

    if (EXTI_GetITStatus(EXTI_Line2) != RESET)  

    {  

       // flag = 1; // button pressed flag 

   /*LED inversion*/

         GPIO_WriteBit(GPIOD, GPIO_Pin_3,  

                (BitAction)((1-GPIO_ReadOutputDataBit(GPIOD, GPIO_Pin_3))));

  

        EXTI_ClearITPendingBit(EXTI_Line2);  

    }  

}  

*************************************************** *************************************************** *********************************

Note: This program I wrote only realizes the simple function of turning the light from on to off when the button is pressed. However, the test found that the button jitters too much. When the button is touched lightly, the light will flash. Sometimes


When I touch the button, the LED goes out before I press it. So I use the delay_nms() delay function to debounce. The commented-out code in the code is for debounce. However, the debounce effect is not very good. There is still jitter, but it is better than before! However, the official has provided a delay function, which can be called by the official delay function. Generally, 10ms can be used for debounce.


*************************************************** *************************************************** *********************************************


The main purpose of writing this program is to complete the corresponding configuration work. The steps are as follows:



(1) Initialize the system clock and


Initialize external clock (clock configuration)

void RCC_Config(void)  

{    

      RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);  

      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOD,ENABLE); 

      SystemInit(); //System clock initialization  

}  

*************************************************** *************************************************** *************************************************** ***********************


Note: We don't need to add SystemInit(); it doesn't matter if we don't add this function, because the startup_stm32f10x_hd.s file has already done these things for us.


*************************************************** *************************************************** *************************************************** ***********************


(3) Configure LED


void LED_Config(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

 

/*led config*/

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Push-pull output

GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;

GPIO_Init(GPIOD, &GPIO_InitStructure);

 

}

(4) Configure interrupt priority


NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

 

NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

*************************************************** *************************************************** *************************************************** ***************************

Note: The interrupt priority setting can be set arbitrarily. The reason is that the project I created only uses the key interrupt, and there are no other interrupts. Of course, if there are multiple interrupts, you have to follow the interrupt vector table.


And the interrupt priority setting rules are set. For specific rules, you can refer to the information on the Internet. There are many, you can take a look, I will not explain it here. Here I set the first group of interrupts, the preemption priority is


0, the response priority is also 0.


*************************************************** *************************************************** *************************************************** ****************************


(5) Configure external interrupt line


/*EXIT line(PC2) mode config */

EXTI_InitStructure.EXTI_Line = EXTI_Line2;

EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //Interrupt mode

EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //Falling edge trigger

EXTI_InitStructure.EXTI_LineCmd = ENABLE;

EXTI_Init(&EXTI_InitStructure);

*************************************************** *************************************************** *************************************************** *******************************

Note: There are two interrupt modes: Event and Interrupt. Since this is an interrupt, we set it to


Interrupt, because it is Key2, the interrupt line here is EXTI_Line2. If the key is 5-9, it should be written as EXTI9_5_IRQn.

*************************************************** *************************************************** *************************************************** *********************************



(6) Interrupt handling function


void EXTI2_IRQHandler(void)

{

if (EXTI_GetITStatus(EXTI_Line2) != RESET) //Judge whether the button is pressed

{

/*LED inversion*/

GPIO_WriteBit(GPIOD, GPIO_Pin_6,  

                (BitAction)((1-GPIO_ReadOutputDataBit(GPIOD, GPIO_Pin_6))));  

EXTI_ClearITPendingBit(EXTI_Line2); //Clear interrupt flag

}

*************************************************** *************************************************** *************************************************** **********************************

Note: It is usually best to put the interrupt handling function here in the file stm32f10x_it.c. This is an official file used to store all external interrupt handling functions. When the main program is executing the program, if an interrupt occurs, the cpu will immediately jump here to execute the interrupt handling function. Because there is only a key interrupt here, the benefits of doing so are not reflected. If it is a large project and multiple interrupts are used, putting the interrupt functions in this file can greatly optimize the code.

*************************************************** *************************************************** *************************************************** *********************************


Keywords:STM32 Reference address:STM32_button interrupt

Previous article:STM32 actual combat button lights up LED (interrupt)
Next article:STM32 (II) GPIO Operation (2) - Control the switch of LED light by button

Recommended ReadingLatest update time:2024-11-17 00:04

STM32 I2C Hardware
1. The module works in slave mode by default. The interface automatically switches from slave mode to master mode after generating a start condition; when arbitration is lost or a stop signal is generated, it switches from master mode to slave mode. 2. Data and address are transmitted as 8 bits/byte, with the high bit
[Microcontroller]
stm32 IAR printf function redefinition
In the IAR compilation environment, stm32 wants to use the printf function to print serial port data.  In uVision, the redefinition methods of the two compilers are different, so you need to pay attention. In the IAR compilation environment,  steps 1-5 have been verified, and step 6, which is referenced by others, ha
[Microcontroller]
Use of STM32 external memory
1. STM32 is divided into on-chip memory and off-chip memory. (1) On-chip memory is used for task stacks, variables, etc. (2) Off-chip memory Generally, programmers will write their own functions to facilitate memory management. For example, a 16M memory is mounted through the bus, address 0x68000000. The user can mana
[Microcontroller]
STM32 MCU-ID Operation
Each STM32 chip has a unique Device ID, 96 Bit. This ID number can provide developers with many advantages, such as: 1. The ID can be used as the serial number of the user's final product to help the user manage the product. 2. Before certain functional codes that require security are run, the security of certain func
[Microcontroller]
STM32 MCU-ID Operation
[STM32 Cotex-M3 processor series programming] Button light is on
//Press S1~S4 respectively, D1~D4 will light up respectively #include "stm32f10x.h" void Delay(unsigned int x);   int main(void) {     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE,ENABLE); //IO port enable setting    GPIO_InitTypeDef GPIO_InitStructure;    //Def
[Microcontroller]
Experience with different modes of STM32 GPIO ports
       First of all, the most basic and simplest function of GPIO is that we can program it to be an input or output, and the input/output is in the form of high and low levels (usually 0V is a low level and 3.3V is a high level).        To make GPIO as input or output, you first need to configure the registers rela
[Microcontroller]
STM32 basic timer TIM6 and TIM7
1. Classification of timers on STM32 We have learned about the STM32 system timer SysTick. Its main function is to provide system ticks for the OS. Of course, we can also use it to achieve precise delay. In the STM32 microcontroller, in addition to the system timer, which is a peripheral in the CM3 core, there are s
[Microcontroller]
STM32 basic timer TIM6 and TIM7
Tips for STM32 MCUs: How to use IAR to develop STM32
BKP is not finished yet, why switch to RTC? Because RTC and BKP are somewhat related, it is impossible to separate them. The following is the introduction of RTC in the data sheet: Introduction to RTC The real-time clock is an independent timer. The RTC module has a set of counters that count continuously, and ca
[Power Management]
Tips for STM32 MCUs: How to use IAR to develop STM32
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号