stm32------(3)按键检测(GPIO输入)

使用stm32单片机,通过按键扫描,完成对按键状态的检测,输出指示灯状态

keil软件版本:V5.35.00

st官网:STM32 固件 - 意法半导体STMicroelectronics

单片机型号:STM32F103VET6

配置步骤大致为:对照原理图确认引脚和硬件连接关系--->初始化对应引脚--->添加扫描函数--->在主函数中调用,实现功能

step1:硬件原理图(按键检测电路,有KEY1和KEY2两个按键,按键按下时对应引脚为高电平;按键松开时对应引脚为低电平)

step2:初始化引脚

            KEY1:PA0脚

            KEY2:PC13脚

复制代码
 1 /**
 2   ******************************************************************************
 3   * @file    template/User/GPIO_KEY.h
 4   * @author  ChuanWang
 5   * @version V5.35
 6   * @date    09-April-2023
 7   * @brief   key scan program body
 8   ******************************************************************************
 9   * @attention
10   *
11   ******************************************************************************
12   */  
13 
14 #ifndef __GPIO_KEY_H
15 #define __GPIO_KEY_H
16 
17 /* Includes ------------------------------------------------------------------*/
18 #include "stm32_userconfig.h"
19 
20 
21 
22 
23 //KEY1按键
24 #define KEY1_GPIO_PORT  GPIOA
25 #define KEY1_GPIO_CLK   RCC_APB2Periph_GPIOA
26 #define KEY1_GPIO_PIN   GPIO_Pin_0
27 
28 
29 //KEY2按键
30 #define KEY2_GPIO_PORT  GPIOC
31 #define KEY2_GPIO_CLK   RCC_APB2Periph_GPIOC
32 #define KEY2_GPIO_PIN   GPIO_Pin_13
33 
34 
35 
36 
37 
38 /*KEY使用的GPIO引脚初始化*/
39 extern void KEY_SCAN_GPIO_Config(void);
40 
41 
42 #endif /*__GPIO_KEY_H*/
GPIO_KEY.h
复制代码
复制代码
 1 /**
 2   ******************************************************************************
 3   * @file    template/User/GPIO_KEY.c
 4   * @author  ChuanWang
 5   * @version V5.35
 6   * @date    09-April-2023
 7   * @brief   key scan gpio program body
 8   ******************************************************************************
 9   * @attention
10   *
11   ******************************************************************************
12   */  
13 
14 /* Includes ------------------------------------------------------------------*/
15 #include "GPIO_KEY.h"
16 
17 
18 
19 /* Private functions ---------------------------------------------------------*/
20 /**
21   * @brief  KEY_SCAN_GPIO_Config program.
22   * @param  None
23   * @retval None
24   */
25 void KEY_SCAN_GPIO_Config(void)
26 {
27     /*定义一个GPIO_InitTypeDef类型的结构体*/
28     GPIO_InitTypeDef  GPIO_InitStructure;
29 
30     /*开启LED相关的GPIO外设时钟*/
31     RCC_APB2PeriphClockCmd(KEY1_GPIO_CLK | KEY2_GPIO_CLK, ENABLE);    //使用宏来赋值,方便移植
32     
33     /*选择需要控制点GPIO引脚*/
34     GPIO_InitStructure.GPIO_Pin = KEY1_GPIO_PIN;
35     
36     /*设置引脚模式为浮空输入模式*/
37     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
38     
39     /*设置引脚速率为50MHz*/
40     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
41     
42     /*调用库函数,初始化GPIO*/
43     GPIO_Init(KEY1_GPIO_PORT,&GPIO_InitStructure);
44     
45     
46     
47     /*选择需要控制点GPIO引脚*/
48     GPIO_InitStructure.GPIO_Pin = KEY2_GPIO_PIN;
49     
50     /*调用库函数,初始化GPIO*/
51     GPIO_Init(KEY2_GPIO_PORT,&GPIO_InitStructure);
52 }
GPIO_KEY.c
复制代码

step3:自定义扫描函数,返回对应引脚状态

复制代码
 1 /**
 2   ******************************************************************************
 3   * @file    template/User/key_scan_operation.h
 4   * @author  ChuanWang
 5   * @version V5.35
 6   * @date    02-April-2023
 7   * @brief   key scan operation program body
 8   ******************************************************************************
 9   * @attention
10   *
11   ******************************************************************************
12   */  
13 
14 #ifndef __KEY_SCAN_OPERATION_H
15 #define __KEY_SCAN_OPERATION_H
16 
17 /* Includes ------------------------------------------------------------------*/
18 #include "stm32_userconfig.h"
19 
20 
21 typedef enum {KEY_OFF = 0, KEY_ON = !RESET} KeySwitchStatus;
22 
23 
24 
25 
26 /*key按键检测*/
27 extern uint8_t Key_Scan(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
28 
29 
30 #endif /*__KEY_SCAN_OPERATION_H*/
key_scan_operation.h
复制代码
复制代码
 1 /**
 2   ******************************************************************************
 3   * @file    template/User/key_scan_operation.c
 4   * @author  ChuanWang
 5   * @version V5.35
 6   * @date    02-April-2023
 7   * @brief   key scan operation program body
 8   ******************************************************************************
 9   * @attention
10   *
11   ******************************************************************************
12   */  
13 
14 /* Includes ------------------------------------------------------------------*/
15 #include "key_scan_operation.h"
16 
17 
18 
19 
20 
21 
22 /**
23   * @brief  Key_Scan program.
24   * @param  None
25   * @retval None
26   */
27 uint8_t Key_Scan(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
28 {
29     /*检测是否有按键按下*/
30     if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == SET)
31     {
32         /*等待按键释放*/
33         while(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == SET);
34         return KEY_ON;
35     }
36     else
37     {
38         return KEY_OFF;
39     }
40 }
key_scan_operation.c
复制代码

step4:主函数中循环扫描,根据引脚状态输出执行相应功能(这里为切换指示灯开关)

复制代码
 1 /**
 2   ******************************************************************************
 3   * @file    template/User/main.c 
 4   * @author  ChuanWang
 5   * @version V5.35
 6   * @date    09-April-2023
 7   * @brief   Main program body
 8   ******************************************************************************
 9   * @attention
10   *
11   ******************************************************************************
12   */  
13 
14 /* Includes ------------------------------------------------------------------*/
15 #include "stm32_userconfig.h"
16 
17 
18 /* Private functions ---------------------------------------------------------*/
19 
20 
21 
22 
23 /**
24   * @brief  Main program.
25   * @param  None
26   * @retval None
27   */
28 int main(void)
29 {
30     LED_GPIO_Config();
31     KEY_SCAN_GPIO_Config();
32     
33     for(;;)
34     {
35         if(Key_Scan(KEY1_GPIO_PORT,KEY1_GPIO_PIN) == KEY_ON)
36         {
37             LED1_TOGGLE;
38         }
39         
40         if(Key_Scan(KEY2_GPIO_PORT,KEY2_GPIO_PIN) == KEY_ON)
41         {
42             LED2_TOGGLE;
43         }
44         
45     }
46 }
main.c
复制代码

至此,使用扫描方式的简单按键检测完成;

 

参考:1、[野火EmbedFire]《STM32库开发实战指南——基于野火指南者开发板》;

   2、野火F103-指南者bilibili教程视频13-GPIO输入—按键检测_哔哩哔哩_bilibili

 

小弟才疏学浅,如有错误或不足之处,还请大佬批评指正,深表感谢!

posted @   王川贝壳子  阅读(290)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示