STM32 learning record --2.GPIO input - key detection

STM32 learning record --2.GPIO input - key detection

2.1 Hardware Design

Flexible mechanical key contacts open, closed, due to the spring action of the contact, the key switch is turned on or not immediately stabilized once off signal is generated as shown in FIG corrugated required software debounce filtering process using the keys inconvenient input detection. Of course, there is a dedicated circuit to shake, to shake also has a dedicated chip, but usually we use software delay jitter method can solve the problem, no need to add extra hardware.

2.2 Software Design

With LED engineering, in order to make the project more organized, we put the key relevant code independently stored separately, to facilitate future transplant. On "Project template" New "bsp_key.c" and "bsp_key.h" files, which can also be named according to your preferences, these files are not part of the standard STM32 library, is written by ourselves according to application needs .

2.2.1 Programming Notes

  1. Clock Enable GPIO port;
  2. GPIO pin initialize certain input mode (the default level by the pin key circuit impact, floating / pull-up / pull-down were no difference);
  3. Write a simple test program, detecting the key state, a key to achieve control of LED lights.

2.2.2 Code Analysis

1. Key pins macro definition

Similarly, in the preparation of the key drivers, but also consider the situation to change the hardware environment. We detect a key pin associated macro definition to "bsp_key.h" file

#ifndef _BSP_KEY_H
#define _BSP_KEY_H
#include "sys.h"
#include "stm32f4xx.h"

/*下面的方式是通过直接操作库函数方式读取IO*/
#define KEY0   GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4) //PE4
#define KEY1   GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3) //PE3 
//#define KEY2   GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2) //PE2
#define WK_UP  GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0) //PA0

/*下面方式是通过位带操作方式读取IO*/
/*
#define KEY0   PEin(4)   //PE4
#define KEY1   PEin(3)  //PE3 
#define KEY2   PEin(2)  //P32
#define WK_UP  PAin(0)  //PA0
*/

#define KEY0_PRES  1
#define KEY1_PRES  2
#define WKUP_PRES  3
//#define KEY2_PRES 4

void KEY_Init(void); //IO初始化
u8 KEY_Scan(u8);    //按键扫描函数

#endif

2. The key initialization function GPIO

#include "bsp_key.h"
#include "delay.h"

void  KEY_Init() 
{
  GPIO_InitTypeDef  GPIO_InitStructure;
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOE, ENABLE);//使能GPIOA,GPIOE时钟
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4; //KEY2 KEY3对应引脚
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;         //普通输入模式
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;   //100M
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;         //上拉
  GPIO_Init(GPIOE, &GPIO_InitStructure);               //初始化GPIOE2,3,4
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;            //WK_UP对应引脚PA0
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN ;      //下拉
  GPIO_Init(GPIOA, &GPIO_InitStructure);               //初始化GPIOA0
} 

//按键处理函数
//返回按键值
//mode:0,不支持连续按;1,支持连续按;
//0,没有任何按键按下
//1,KEY0按下
//2,KEY1按下
//3,KEY2按下 
//4,WKUP按下 WK_UP
//注意此函数有响应优先级,KEY0>KEY1>KEY2>WK_UP!!
u8 KEY_Scan(u8 mode)
{  
 static u8 key_up=1;//按键按松开标志
 if(mode)key_up=1;  //支持连按    
 if(key_up&&(KEY0==0||KEY1==0||WK_UP==1))
 {
  delay_ms(10);//去抖动 
  key_up=0;
       if(KEY0==0)return 1;
  else if(KEY1==0)return 2;
  else if(WK_UP==1)return 3;
//  else if(KEY2==0)return 4;
  
 }else if(KEY0==1&&KEY1==1&&WK_UP==0)key_up=1;      
  return 0;// 无按键按下
}

3. The main function

#include "stm32f4xx.h"
#include "bsp_led.h"
#include "bsp_key.h"
#include "sys.h"
#include "delay.h"

main()
{ 
  u8 key;                //保存键值
 delay_init(168);        //初始化延时函数
 LED_GPIO_Config();      //初始化LED端口 
 KEY_Init();             //初始化与按键连接的硬件接口
 
 while(1)
 {
  key=KEY_Scan(0);  //得到键值
     if(key)
  {         
   switch(key)
   {               
    case 1: LED0=!LED0;break;           //控制LED0翻转
         
    case 2: LED1=!LED1;break;           //控制LED1翻转  
          
    case 3: LED0=!LED0;LED1=!LED1;break;//同时控制LED0,LED1翻转     
   }
  }
  else delay_ms(10); 
 }
}

After initialization code LED lights and buttons, while the call continues to function in KEY_Scan function, and its return value is determined, if the return value indicates that the button is pressed, the status LED is reversed.

references

[1]: "Zero dead Fun STM32- based wildfire F407 [batianhu] Boards"
[2]: Mistakes in An. 51 new concept single-chip C language tutorial: such as doors, improve, develop and expand the Raiders [M]. Beijing: Electronic Industry Press, 2018.1: 66

Released two original articles · won praise 2 · views 53

Guess you like

Origin blog.csdn.net/qq_43328313/article/details/104211186