当前位置:网站首页>Key library function based on Hal Library
Key library function based on Hal Library
2022-06-30 10:56:00 【Coder_ Ja】
key.c
/** ******************************************************************************** * @file key.c * @brief Key operation source file * @author Jia Yasen * @date 2022-6-20 * @version V0.0.1 * @copyright @Etherealize ******************************************************************************** */
/*-----------------------------Includes---------------------------------------*/
#include "key.h"
/*-----------------------------Function---------------------------------------*/
/** * @brief Key scan ( Continue to trigger ) * @param[in] None * @return uint8_t Corresponding key value * @retval 0 The default value is , No operation * 1 KEY0 Corresponding value , Express KEY0 Press down , Whether it is... Can be queried in the loop 1 To do the corresponding operation * 2 KEY1 Corresponding value * @details Continue to trigger , Then confirm that the pressed part will be executed many times during the pressed time , Because press * There are a few tenths of a second , But the program cycles every few milliseconds . So it will be executed many times during the pressing time */
uint8_t KEY_Scan1(void)
{
if(KEY0 == KEY_ON || KEY1 == KEY_ON)
{
HAL_Delay(10);// Time delay 10-20ms, Software deblurring
if(KEY0 == KEY_ON)
{
return KEY0_PRES;// The operation here will be performed multiple times
}
else if(KEY1 == KEY_ON)
{
return KEY1_PRES;// The operation here will be performed multiple times
}
// Generally, press to trigger the event , Release and return to normal state , Don't trigger
else
return 0;
}
return 0;// No default return 0
}
/** * @brief Key scan ( Single trigger ) * @param[in] None * @return uint8_t Corresponding key value * @retval 0 The default value is , No operation * 1 KEY0 Corresponding value , Express KEY0 Press down , Whether it is... Can be queried in the loop 1 To do the corresponding operation * 2 KEY1 Corresponding value * @details Single trigger : use key_up As a constraint , Pressing will only trigger one operation , The next press will not work until it is released */
uint8_t KEY_Scan2(void)
{
// Static variables are stored in static storage , It will only be initialized once at the beginning of the program , Staticizing a local variable makes the variable independent of the function , Instead of reinitializing each time a function is called
static uint8_t key_up = 1; // Key release sign
// use key_up As a constraint , After the key press is detected for the first time and the operation is performed key_up Turn into 0, It will not be triggered in the next few scans after pressing and holding the key
if(key_up && (KEY0 == KEY_ON || KEY1 == KEY_ON))
{
HAL_Delay(10);
key_up = 0;
if(KEY0 == KEY_ON)
{
return KEY0_PRES;//
}
else if(KEY1 == KEY_ON)
{
return KEY1_PRES;// The operation will only be triggered once
}
else
return 0;
}
else if(KEY0 == KEY_OFF && KEY1 == KEY_OFF) key_up = 1;// When the key is released key_up Reset , The next press will trigger
return 0;
}
/** * @brief Key scan * @param[in] mode Mode conversion . 0: Single trigger 1: Continue to trigger * @return uint8_t Corresponding key value * @retval 0 The default value is , No operation * 1 KEY0 Corresponding value , Express KEY0 Press down , Whether it is... Can be queried in the loop 1 To do the corresponding operation * 2 KEY1 Corresponding value * @details If continuously triggered , Then confirm that the pressed part will be executed many times during the pressed time , Because press * There are a few tenths of a second , But the program cycles every few milliseconds , So it will be executed many times during the pressing time . * If a single trigger : use key_up As a constraint , Pressing will only trigger one operation , Not until it is released * Make the next press work . */
uint8_t KEY_Scan(uint8_t mode)
{
static uint8_t key_up = 1; // Key release sign
if(mode) key_up = 1; //mode by 1 when , Each scan resets key_up, The operation can be triggered continuously , Continue to trigger
if(key_up == 1 && (KEY0 == KEY_ON || KEY1 == KEY_ON))
{
HAL_Delay(10); // Software deblurring
key_up = 0;
if(KEY0 == KEY_ON)
{
return KEY0_PRES;
}
else if(KEY1 == KEY_ON)
{
return KEY1_PRES;
}
else
return 0;
}
else if(KEY0 == KEY_OFF && KEY1 == KEY_OFF) key_up = 1;// Release to refresh key_up
return 0;
}
/************************ (C) COPYRIGHT Etherealize*****END OF FILE***********/
key.h
/** ******************************************************************************** * @file key.h * @brief Key operation header file * @author Jia Yasen * @date 2022-6-20 * @version V0.0.1 * @copyright @Etherealize ******************************************************************************** * @attention 1. The functions in this document are based on HAL library , Key KEY The initialization of is based on GPIO The initialization ,GPIO Corresponding pin * Initialize to input mode * 2. Because the corresponding pins of different boards are different , Before use, it is necessary to modify according to the schematic diagram KEY Corresponding pin ******************************************************************************** */
#ifndef __KEY_H__
#define __KEY_H__
/*-----------------------------Includes---------------------------------------*/
// Modify according to the chip model used
#include "stm32f1xx_hal.h"
/*-----------------------------Define-----------------------------------------*/
// take KEYx Defined as the read value of the corresponding pin
#define KEY0 HAL_GPIO_ReadPin(GPIOx,GPIO_PIN_x)
#define KEY1 HAL_GPIO_ReadPin(GPIOx,GPIO_PIN_x)
// To different KEY Set different corresponding numbers , Indicates correspondence KEY Press down ; It also means different priorities , The smaller the number, the higher the priority ; no need 0, because 0 Is the default return value of the function
#define KEY0_PRES 1
#define KEY1_PRES 2
//KEY_ON Indicates that the key is pressed ,KEY_OFF Indicates that the key is released ; Modify the corresponding relationship according to the schematic diagram before use
#define KEY_ON GPIO_PIN_RESET
#define KEY_OFF GPIO_PIN_SET
/*-----------------------------Function---------------------------------------*/
uint8_t KEY_Scan1(void);
uint8_t KEY_Scan2(void);
uint8_t KEY_Scan(uint8_t mode);
#endif
/************************ (C) COPYRIGHT Etherealize*****END OF FILE***********/
边栏推荐
- Didi open source agile test case management platform!
- MySQL从入门到精通50讲(三十二)-ScyllaDB生产环境集群搭建
- 腾讯云数据库工程师能力认证重磅推出,各界共话人才培养难题
- Qt之实现动效导航栏
- 再测云原生数据库性能:PolarDB依旧最强,TDSQL-C、GaussDB变化不大
- 我们公司使用 7 年的这套通用解决方案,打通了几十个系统,稳的一批!
- Pytorch notes: validation, model eval V.S torch. no_ grad
- OLAP数据库引擎如何选型?
- LVGL 8.2 Checkboxes as radio buttons
- The two e-commerce bigwigs' lacy news screens represent the return of e-commerce to normal, which will be beneficial to the real economy
猜你喜欢
Mysql database foundation: constraint and identification columns
焕发青春的戴尔和苹果夹击,两大老牌PC企业极速衰败
文件共享服务器
在IPhone12的推理延迟仅为1.6 ms!Snap等详析Transformer结构延迟,并用NAS搜出移动设备的高效网络结构...
When does the database need to use the index [Hangzhou multi surveyors] [Hangzhou multi surveyors _ Wang Sir]
语音识别-基础(一):简介【语音转文本】
ArrayList and sequence table
Sarsa笔记
在 sCrypt 中实现高效的椭圆曲线点加法和乘法
Mysql database foundation: views and variables
随机推荐
pytorch 笔记 torch.nn.BatchNorm1d
MySQL从入门到精通50讲(三十二)-ScyllaDB生产环境集群搭建
[proteus simulation] Arduino uno led simulated traffic light
Collectors. Tomap application
Deep dive kotlin synergy (18): hot and cold data flow
国产自研系统的用户突破4亿,打破美国企业的垄断,谷歌后悔不迭
pytorch 筆記 torch.nn.BatchNorm1d
The number of users of the home-made self-developed system exceeded 400million, breaking the monopoly of American enterprises, and Google repented
js常见问题
matplotlib 笔记: contourf & contour
【STL源码剖析】容器(待补充)
小程序中读取腾讯文档的表格数据
基于HAL库的按键(KEY)库函数
Mysql database foundation: views and variables
LVGL 8.2 Simple Image button
7 大轻量易用的工具,给开发者减压提效,助力企业敏捷上云 | Techo Day 精彩回顾...
Unity Shader - 踩坑 - BRP 管线中的 depth texture 的精度问题(暂无解决方案,推荐换 URP)
Ionic4 drag the ion reorder group component to change the item order
第一届中国数字藏品大会即将召开
TypeScript–es5中的类,继承,静态方法