STM32
| GPIO
输入之 按键检测
时间:2023-03-19
文章目录
1.硬件设计
按键按下,
GPIO
输入为高电平;
2.代码编写
- 使能
GPIO
端口时钟;- 初始化
GPIO
目标引脚为输入模式(浮空输入);- 编写简单测试程序,检测按键的状态,实现按键控制
LED
灯。
2-1.新建文件
flyer@ThinkPad-FLY MINGW64 /d/05_study/STM32/stm32-f103rct6-mini-prj/code/02-FWLib_LED/User (master)
$ mkdir Key
flyer@ThinkPad-FLY MINGW64 /d/05_study/STM32/stm32-f103rct6-mini-prj/code/02-FWLib_LED/User (master)
$ cd Key/
flyer@ThinkPad-FLY MINGW64 /d/05_study/STM32/stm32-f103rct6-mini-prj/code/02-FWLib_LED/User/Key (master)
$ vim bsp_key.c
flyer@ThinkPad-FLY MINGW64 /d/05_study/STM32/stm32-f103rct6-mini-prj/code/02-FWLib_LED/User/Key (master)
$ cp bsp_key.c bsp_key.h
flyer@ThinkPad-FLY MINGW64 /d/05_study/STM32/stm32-f103rct6-mini-prj/code/02-FWLib_LED/User/Key (master)
$ vim bsp_key.h
flyer@ThinkPad-FLY MINGW64 /d/05_study/STM32/stm32-f103rct6-mini-prj/code/02-FWLib_LED/User/Key (master)
$ ls
bsp_key.c bsp_key.h
2-2.编码
stm32-f103rct6-mini-prj\code\02-FWLib_LED\User\Key\bsp_key.h
/*******************************************************************
* > File Name: bsp_key.h
* > Create Time: Sun Mar 19 19:45:51 2023
******************************************************************/
#ifndef __BSP_KEY_H__
#define __BSP_KEY_H__
#include "stm32f10x.h"
#define KEY_ON 1
#define KEY_OFF 0
#define KEY1_GPIO_CLK RCC_APB2Periph_GPIOA
#define KEY1_GPIO_PORT GPIOA
#define KEY1_GPIO_PIN GPIO_Pin_0
#define KEY2_GPIO_CLK RCC_APB2Periph_GPIOC
#define KEY2_GPIO_PORT GPIOC
#define KEY2_GPIO_PIN GPIO_Pin_13
extern void Key_GPIO_Config(void);
extern uint8_t Key_Scan(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
#endif
stm32-f103rct6-mini-prj\code\02-FWLib_LED\User\Key\bsp_key.c
/*******************************************************************
* > File Name: bsp_key.c
* > Create Time: Sun Mar 19 19:45:51 2023
******************************************************************/
#include "bsp_key.h"
void Key_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(KEY1_GPIO_CLK|KEY2_GPIO_CLK,ENABLE);
GPIO_InitStructure.GPIO_Pin = KEY1_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(KEY1_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = KEY2_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(KEY2_GPIO_PORT, &GPIO_InitStructure);
}
uint8_t Key_Scan(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
if(GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) == KEY_ON){
while(GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) == KEY_ON);
return KEY_ON;
}
else
return KEY_OFF;
}
stm32-f103rct6-mini-prj\code\02-FWLib_LED\User\main.c
/*******************************************************************
* > File Name: main.c
* > Create Time: Sun Mar 19 15:37:45 2023
******************************************************************/
#include "stm32f10x.h"
#include "bsp_led.h"
#include "bsp_key.h"
#define SOFT_DELAY Delay(0xFFFFF);
void Delay(__IO uint32_t nCount)
{
for(;nCount != 0; nCount --);
}
void tester_led(void)
{
LED1(ON);
SOFT_DELAY;
LED1(OFF);
LED2(ON);
SOFT_DELAY;
LED2(OFF);
}
void tester_key(void)
{
if(Key_Scan(KEY1_GPIO_PORT, KEY1_GPIO_PIN) == KEY_ON){
LED1_Toggle();
}
if(Key_Scan(KEY2_GPIO_PORT, KEY2_GPIO_PIN) == KEY_ON){
LED2_Toggle();
}
}
int main(void)
{
LED_GPIO_Config();
Key_GPIO_Config();
while(1){
tester_key();
}
return 0;
}
stm32-f103rct6-mini-prj\code\02-FWLib_LED\User\Led\bsp_led.c
void LED1_Toggle(void)
{
static char isLedOn = 0;
if(isLedOn == 0){
LED1(ON);
isLedOn = 1;
}else{
LED1(OFF);
isLedOn = 0;
}
}
void LED2_Toggle(void)
{
static char isLedOn = 0;
if(isLedOn == 0){
LED2(ON);
isLedOn = 1;
}else{
LED2(OFF);
isLedOn = 0;
}
}
其他文章:
STM32 | F103MINI开发板开发与学习之点灯
STM32 | STM32使用固件库点亮LED