第一次以写博客的方式记录自己在嵌入式学习过程中的学习经历和踩的那些坑~
Day1 那么开始叭!
STM32G4系列MCU学习笔记:按键模块
前言
我所学过的按键模块有独立按键和矩阵键盘两种,其实两者原理类似,本文主要介绍按键模块中实现长按和短按功能。
注意了:本文是基于HAL库来进行介绍的,如果你想用标准库实现也比较简单,类比移植即可。
由于作者第一次写博客,且自身水平有限,如文中有误,还请大家指正,谢谢~
先放一张开发板的实物图,它长这样~
一、硬件操作
在应用层中我利用不同的点灯程序来区分短按和长按所进行的操作。所以先来看看该模块所设计到的硬件原理图。
1. 原理图
注意了:点灯的这个模块,我们这里忽略这个锁存器芯片,不用它的锁存功能,着重于分析按键模块。
我们先把所使用到的所有GPIO口列出来:(方便待会儿看程序更加清楚)
1.点灯相关的IO口:PC8,PC9,PC10,PC11,PC12,PC13,PC14,PC15;
2.按键相关的IO口:PB0,PB1,PB2,PA0。
2. 硬件分析
从原理图里可以发现,我们如果将PB1设置为输入模式,去采集该GPIO上的电平状态,按键松开时,PB1上的电平应该为高电平,当我们按下按键后,例如:B2按键,PB1上的电平就会被拉低,其余按键类似,所以根据这样一个简单原理,即可实现按键的输入检测。
3. 初始化代码
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();//使用默认配置即可
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
(1)时钟设置
这里我使用的内部时钟源HSI = 16MHz;配置HCLK = PCLK1 = PCLK2 = 80MHz。
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
{
Error_Handler();
}
}
(2)GPIO初始化
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_8
|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
/*Configure GPIO pins : PC13 PC14 PC15 PC8
PC9 PC10 PC11 PC12 */
GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_8
|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pin : PA0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pins : PB0 PB1 PB2 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pin : PD2 */
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
}
二、按键模块的驱动层实现
1. 硬件框图
避坑点:
短按:按键释放时判断时间
长按:直接判断时间
2. 按键驱动层代码实现
下面直接上C代码:
(1)key.c
extern unsigned int keyCount;
extern uint8_t keyValue;
uint8_t keyflag = 0;
void Key_Scan(void)
{
//keyState 按键状态;
//keyPass 记录哪一个按键被按下
//keyCount 记录按键按下时间
//keyflag 开始计数标志位,方便在Systick中断中进行计时
//keyValue 返回给应用层的键值,用于判断处理相应的事件
//ShoutKeyx和LongKeyx是两个宏,为了区分短按和长按
//ShoutKeyx 短按x键值,
//LongKeyx 长按x键值
static uint8_t keyState = 0,keyPass = 0;
switch(keyState)
{
case 0:if(KEYB_1==0||KEYB_2==0||KEYB_3==0||KEYB_4==0)
{
HAL_Delay(100);//消抖
keyState = 1;
keyPass = 0xff; //初始化按键
}
break;
case 1: if(KEYB_1==0||KEYB_2==0||KEYB_3==0||KEYB_4==0)
{
keyState = 2;
keyflag = 1;//开始计数
if(KEYB_1==0){
keyPass = 1;//返回对应键值
}
else if(KEYB_2==0)keyPass = 2;
else if(KEYB_3==0)keyPass = 3;
else if(KEYB_4==0)keyPass = 4;
}
else keyState = 0;//返回检测按键
break;
case 2:
switch(keyPass)
{
case 1:if(KEYB_1!=0) //判断是否释放按键
{
if((10 < keyCount)&&(keyCount < 800))//短按
{
keyCount = 0;
keyflag = 0;
keyValue = ShoutKey1;
keyState = 0;
}
}
break;
case 2:if(KEYB_2!=0)
{
if((10 < keyCount)&&(keyCount < 800))//短按
{
keyCount = 0;
keyflag = 0;
keyValue = ShoutKey2;
keyState = 0;
}
}
break;
case 3:if(KEYB_3!=0)
{
if((10 < keyCount) && (keyCount < 800))//短按
{
keyCount = 0;
keyflag = 0;
keyValue = ShoutKey3;
keyState = 0;
}
}
break;
case 4:if(KEYB_4!=0)
{
if((10 < keyCount)&&(keyCount < 800))//短按
{
keyCount = 0;
keyflag = 0;
keyValue = ShoutKey4;
keyState = 0;
}
}
break;
}
if(keyCount > 800)//长按
{
if(KEYB_1==0)keyValue = LongKey1;
else if(KEYB_2==0)keyValue = LongKey2;
else if(KEYB_3==0)keyValue = LongKey3;
else if(KEYB_4==0)keyValue = LongKey4;
keyState = 3;
}
break;
case 3: if(KEYB_1==1&&KEYB_2==1&&KEYB_3==1&&KEYB_4==1)
{
keyCount = 0;
keyflag = 0;
keyState = 0;
keyValue = 0xff;//长按之后释放按键代表不执行操作,就没有键值传出
}
break;
}
}
(2)key.h
#ifndef __KEY_H
#define __KEY_H
#include "stm32g4xx_hal.h"
#define KEYB_1 HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0) //PB0
#define KEYB_2 HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) //PB1
#define KEYB_3 HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2)//PB2
#define KEYB_4 HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0)//PA0
#define ShoutKey1 1
#define ShoutKey2 2
#define ShoutKey3 3
#define ShoutKey4 4
#define LongKey1 5
#define LongKey2 6
#define LongKey3 7
#define LongKey4 8
void Key_Scan(void);
#endif
3. 计时操作
我使用Systick写了一个简单的定时中断函数,(触发Systick的定时中断时间是1ms)去处理计时操作,这是为了给按键驱动提供短按和长按的时间,便于在驱动层进行相应的判断,所以我不去深挖Systick模块的细节,本文会用即可,后面再单独来记录学习Systick模块。
下面直接来看看Systick的中断服务函数叭,很简单,就这么几句~
extern uint8_t keyflag;
unsigned int keyCount = 0;
void SysTick_Handler(void)
{
HAL_IncTick(); //这个api是用于HAL库的延时函数HAL_Delay(uint32_t Delay)
if(keyflag == 1) //若有按键按下就开始计时
keyCount++;
}
三、应用层简单逻辑实现
应用层要做的事情在文章开篇其实已经说了,因为逻辑很简单,就是根据长按和短按实现不同的点灯程序,所以直接上代码叭~
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_All, GPIO_PIN_SET); //关闭所有灯
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET); //关闭锁存器
while (1)
{
Key_Scan();
if(keyValue) //有按键按下
{
if(keyValue < 5) //短按
{
switch(keyValue)
{
case 1:
keyValue = 0;
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_8);
HAL_Delay(100);
break;
case 2:
keyValue = 0;
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_9);
HAL_Delay(100);
break;
case 3:
keyValue = 0;
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_10);
HAL_Delay(100);
break;
case 4:
keyValue = 0;
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_11);
HAL_Delay(100);
break;
}
}
else
{
switch(keyValue)
{
case 5:
keyValue = 0;
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_15);
HAL_Delay(100);
break;
case 6:
keyValue = 0;
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_14);
HAL_Delay(100);
break;
case 7:
keyValue = 0;
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
HAL_Delay(100);
break;
case 8:
keyValue = 0;
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_12);
HAL_Delay(100);
break;
}
}
}
}