1.1.stm32按键控制

学习到了一个新的更好的按键代码

代码取自b站up:lil_jx

【STM32开源模块】更好的按键(单击+双击+长按)_哔哩哔哩_bilibili

Key.c:

#include "stm32f10x.h"                  // Device header
#include "Delay.h"

#define LONG_PRESS_TIME 1000            // 长按时间阈值,单位ms
#define CONTINUOUS_PRESS_INTERVAL 100   // 长按后连续返回按键键码的时间间隔,单位ms
#define DOUBLE_CLICK_TIME 300           // 双击时间阈值,单位ms

/*
	函数:	按键初始化,使用引脚PB0,PB1,PB10,配置为上拉输入
	参数:	无
	返回:	无
	注意:	在主循环前调用
*/
void Key_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);  // 开启GPIOB时钟
    
    GPIO_InitTypeDef GPIO_InitStructure;                    // 定义GPIOB初始化结构体
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;           // GPIOB上拉输入模式
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_10 | GPIO_Pin_11; // 使用GPIOB引脚0、1、10
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    
    GPIO_Init(GPIOB, &GPIO_InitStructure);                  // 初始化GPIOB
}

/*
	函数:	按键扫描
	参数:	无
	返回:	按键对应的键码值,可在此修改
	注意:	主函数无需调用
*/
uint8_t Key_Scan(void)
{
    if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0)
    {
        return 1;
    }
    else if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_10) == 0)
    {
        return 2;
    }
    else if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0)
    {
        return 3;
    }
    
    return 0;
}

/*
	函数:	扫描按键并获取键码值
	参数:	无
	返回:	按键对应键码值。双击按键返回值 = 键码值 + 10(例如:双击1返回11,双击2返回12,双击3返回13)
	注意:	放在主函数主循环中
*/
uint8_t Key_GetNum(void)
{
    static uint32_t PressTime = 0;       // 静态变量保持其值
    static uint32_t LastReleaseTime = 0; // 上次按键松开时间
    static uint8_t LastKeyNum = 0;       // 上次按键的键码
    uint8_t KeyNum = 0;                  // 默认键码为0
    static uint32_t CurrentTime = 0;     // 当前时间,模拟一个系统计时器
    
    KeyNum = Key_Scan();
    if (KeyNum != 0)
    {
        while(Key_Scan() == KeyNum)
        {
            Delay_ms(10);
            PressTime += 10;
            CurrentTime += 10;
            
            if (PressTime >= LONG_PRESS_TIME)
            {
                // 长按1秒后,连续返回按键键码
                while (Key_Scan() == KeyNum)
                {
                    Delay_ms(CONTINUOUS_PRESS_INTERVAL);
                    return KeyNum;
                }
            }
        }
        
        if (PressTime >= 20 && PressTime < LONG_PRESS_TIME)
        {
            // 短按,去抖动时间为20ms
            if ((LastKeyNum == KeyNum) && ((CurrentTime - LastReleaseTime) < DOUBLE_CLICK_TIME))
            {
                // 检测到双击
                LastKeyNum = 0;          // 重置LastKeyNum
                LastReleaseTime = 0;     // 重置LastReleaseTime
                PressTime = 0;           // 重置PressTime
                return KeyNum + 10;      // 返回双击键码(例如:双击1返回11,双击2返回12,双击3返回13)
            }
            else
            {
                LastKeyNum = KeyNum;     // 记录本次按键的键码
                LastReleaseTime = CurrentTime; // 记录本次按键松开的时间
                PressTime = 0;           // 重置PressTime
                return KeyNum;           // 返回单击键码
            }
        }
    }
    else
    {
        PressTime = 0;                   // 按键松开时重置PressTime
        CurrentTime += 10;               // 模拟系统时间的增加
    }
    
    return 0;
}

代码对我很新颖,很多地方值得我学习

按键捕获部分代码:

uint8_t Key_Scan(void)
{
    if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0)
    {
        return 1;
    }
    else if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_10) == 0)
    {
        return 2;
    }
    else if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0)
    {
        return 3;
    }
    
    return 0;
}c

通过GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_x)来监测按键是否被按下,并返回相应的值,若按键均是释放状态,函数就会返回0

返回键码部分:

uint8_t Key_GetNum(void)
{
    static uint32_t PressTime = 0;       // 静态变量保持其值
    static uint32_t LastReleaseTime = 0; // 上次按键松开时间
    static uint8_t LastKeyNum = 0;       // 上次按键的键码
    uint8_t KeyNum = 0;                  // 默认键码为0
    static uint32_t CurrentTime = 0;     // 当前时间,模拟一个系统计时器
    
    KeyNum = Key_Scan();
    if (KeyNum != 0)
    {
        while(Key_Scan() == KeyNum)
        {
            Delay_ms(10);
            PressTime += 10;
            CurrentTime += 10;
            
            if (PressTime >= LONG_PRESS_TIME)
            {
                // 长按1秒后,连续返回按键键码
                while (Key_Scan() == KeyNum)
                {
                    Delay_ms(CONTINUOUS_PRESS_INTERVAL);
                    return KeyNum;
                }
            }
        }
        
        if (PressTime >= 20 && PressTime < LONG_PRESS_TIME)
        {
            // 短按,去抖动时间为20ms
            if ((LastKeyNum == KeyNum) && ((CurrentTime - LastReleaseTime) < DOUBLE_CLICK_TIME))
            {
                // 检测到双击
                LastKeyNum = 0;          // 重置LastKeyNum
                LastReleaseTime = 0;     // 重置LastReleaseTime
                PressTime = 0;           // 重置PressTime
                return KeyNum + 10;      // 返回双击键码(例如:双击1返回11,双击2返回12,双击3返回13)
            }
            else
            {
                LastKeyNum = KeyNum;     // 记录本次按键的键码
                LastReleaseTime = CurrentTime; // 记录本次按键松开的时间
                PressTime = 0;           // 重置PressTime
                return KeyNum;           // 返回单击键码
            }
        }
    }
    else
    {
        PressTime = 0;                   // 按键松开时重置PressTime
        CurrentTime += 10;               // 模拟系统时间的增加
    }
    
    return 0;
}

代码首先定义了几个变量,KeyNum = Key_Scan()这里进行第一次赋值,当KeyNum不为0,即有任意按键被按下,继续执行判断内部代码

while(Key_Scan() == KeyNum)
        {
            Delay_ms(10);
            PressTime += 10;
            CurrentTime += 10;
            
            if (PressTime >= LONG_PRESS_TIME)
            {
                // 长按1秒后,连续返回按键键码
                while (Key_Scan() == KeyNum)
                {
                    Delay_ms(CONTINUOUS_PRESS_INTERVAL);
                    return KeyNum;
                }
            }
        }

先Key_Scan() == KeyNum判断按键是否被长按,一旦长按PressTime和CurrentTime就会持续增加,当长按时间大于1000ms(PressTime >= LONG_PRESS_TIME)则每经过0.1s返回一次键码

if (PressTime >= 20 && PressTime < LONG_PRESS_TIME)
        {
            // 短按,去抖动时间为20ms
            if ((LastKeyNum == KeyNum) && ((CurrentTime - LastReleaseTime) < DOUBLE_CLICK_TIME))
            {
                // 检测到双击
                LastKeyNum = 0;          // 重置LastKeyNum
                LastReleaseTime = 0;     // 重置LastReleaseTime
                PressTime = 0;           // 重置PressTime
                return KeyNum + 10;      // 返回双击键码(例如:双击1返回11,双击2返回12,双击3返回13)
            }
            else
            {
                LastKeyNum = KeyNum;     // 记录本次按键的键码
                LastReleaseTime = CurrentTime; // 记录本次按键松开的时间
                PressTime = 0;           // 重置PressTime
                return KeyNum;           // 返回单击键码
            }
        }

这段代码用于单击和双击的判断,当按键时间不超过设定的长按时间时,进行短按判断,若两次短按都按同一按钮(LastKeyNum == KeyNum)且两次按的时间间隔小于双击时间间隔((CurrentTime - LastReleaseTime) < DOUBLE_CLICK_TIME),则清除状态并返回双击键码

main.c:

#include "stm32f10x.h"                  // Device header
#include "OLED.h"
#include "Key.h"
#include "Delay.h"

int main(void)
{
	/*初始化*/
	OLED_Init();
	Key_Init();
	
	/*定义变量*/
	uint8_t KeyNum;					//键码
	uint8_t Count = 0;				//单击和长按计数
	uint8_t DoubleClickCount = 0;	//双击计数
	
	/*主循环*/
	while(1)
	{
		KeyNum = Key_GetNum();	//获取键码
		
		//单击和长按
		if(KeyNum == 1)
		{
			Count++;
		}
		else if(KeyNum == 3)
		{
			Count--;
		}
		else if(KeyNum == 2)
		{
			Count = 0;
		}
		
		//双击
		else if(KeyNum == 11)
		{
			DoubleClickCount++;
		}
		else if(KeyNum == 13)
		{
			DoubleClickCount--;
		}
		else if(KeyNum == 12)
		{
			DoubleClickCount = 0;
		}
		
		//OLED显示
		OLED_ShowChinese(0,0,"单击,长按");
		OLED_ShowNum(88,0,Count,3,OLED_8X16);
		OLED_ShowChinese(0,20,"双击");
		OLED_ShowNum(88,20,DoubleClickCount,3,OLED_8X16);
		OLED_Update();
	}
}

main里没什么好说的,就一个计数器和oled显示

接线图:

演示效果:

### 使用 STM32 和 HAL 库实现按键设置温度阈值功能 为了实现通过按键设置温度阈值的功能,可以按照以下方法编写代码并配置硬件。 #### 硬件连接 确保按键和温度传感器正确连接到STM32开发板上。通常情况下,按键会连接至某个GPIO引脚,并且温度传感器也会连接到另一个GPIO引脚或ADC通道[^3]。 #### 初始化外设 初始化必要的外设,包括GPIO、ADC以及中断服务程序来处理按键事件: ```c #include "stm32f4xx_hal.h" #include "key.h" // 定义全局变量用于存储当前设定的温度阈值 uint16_t temperature_threshold = 25; // 默认初始值为25摄氏度 void SystemClock_Config(void); static void MX_GPIO_Init(void); int main(void){ HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); while (1) { // 主循环中读取温度传感器数据并与阈值比较... if(HAL_GetTick() % 1000 == 0){ // 每秒执行一次 printf("Current Temperature Threshold: %d°C\n", temperature_threshold); } } } /** * @brief GPIO Initialization Function */ static void MX_GPIO_Init(void){ __HAL_RCC_GPIOA_CLK_ENABLE(); // 启用GPIOA时钟 GPIO_InitTypeDef GPIO_InitStruct = {0}; /* 配置按键输入引脚 */ GPIO_InitStruct.Pin = GPIO_PIN_0; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* 设置外部中断优先级 */ HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0); HAL_NVIC_EnableIRQ(EXTI0_IRQn); } ``` #### 处理按键按下事件 当检测到按键被按下的时候,在相应的中断服务函数(ISR)里增加或减少温度阈值: ```c extern uint16_t temperature_threshold; void EXTI0_IRQHandler(void){ HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0); } void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){ static int8_t adjust_direction = 1; // 方向标志位,默认向上调整 if(GPIO_Pin == GPIO_PIN_0){ // 延迟去抖动 HAL_Delay(20); if(!HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0)){ // 如果再次触发,则改变方向 adjust_direction *= -1; // 调整温度阈值 temperature_threshold += adjust_direction; // 边界检查 if(temperature_threshold < 0 || temperature_threshold > 50){ temperature_threshold -= adjust_direction; } printf("Temperature threshold set to:%d °C\n", temperature_threshold); } } } ``` 上述代码展示了如何利用STM32及其HAL库创建一个简单的应用程序,该应用允许用户通过单个按钮交互式地修改预定义的温度阈值。每次点击都会使阈值上下变化直到达到边界条件为止[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值