当前位置:网站首页>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***********/
边栏推荐
- 7 大轻量易用的工具,给开发者减压提效,助力企业敏捷上云 | Techo Day 精彩回顾...
- JS FAQs
- 深潜Kotlin协程(十六):Channel
- Gd32 RT thread flash driver function
- [rust weekly database] num bigint - large integer
- 数学知识复习:第二型曲线积分
- sCrypt 中的 ECDSA 签名验证
- scratch绘制正方形 电子学会图形化编程scratch等级考试二级真题和答案解析2022年6月
- Deep dive kotlin synergy (16): Channel
- ionic4 ion-reorder-group组件拖拽改变item顺序
猜你喜欢

Migrate full RT thread to gd32f4xx (detailed)

Pytorch notes torch nn. BatchNorm1d

DQN笔记

The number of users of the home-made self-developed system exceeded 400million, breaking the monopoly of American enterprises, and Google repented

第一届中国数字藏品大会即将召开

WireGuard简单配置

pytorch 笔记 torch.nn.BatchNorm1d

再测云原生数据库性能:PolarDB依旧最强,TDSQL-C、GaussDB变化不大

Overview of currency

历史上的今天:微软收购 PowerPoint 开发商;SGI 和 MIPS 合并
随机推荐
The number of users of the home-made self-developed system exceeded 400million, breaking the monopoly of American enterprises, and Google repented
吴恩达2022机器学习专项课测评来了!
Smith chart view of semi steel coaxial RF line and RF line matching calibration of network analyzer e5071c
When does the database need to use the index [Hangzhou multi surveyors] [Hangzhou multi surveyors _ Wang Sir]
LVGL 8.2 Simple Drop down list
LVGL 8.2图片缩放及旋转
Pandora IOT development board learning (HAL Library) - Experiment 1 running lantern (RGB) experiment (learning notes)
Mysql database foundation: views and variables
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
LeetCode Algorithm 86. 分隔链表
电化学氧气传感器寿命、工作原理及应用介绍
What is erdma as illustrated by Coptic cartoon?
第一届中国数字藏品大会即将召开
From introduction to mastery of MySQL 50 lectures (32) -scylladb production environment cluster building
[STL source code analysis] iterator
Dell et Apple, deux entreprises de PC établies, se sont effondrées rapidement
[rust daily] the first rust monthly magazine on January 22, 2021 invites everyone to participate
7 大轻量易用的工具,给开发者减压提效,助力企业敏捷上云 | Techo Day 精彩回顾...
Anhui "requirements for design depth of Hefei fabricated building construction drawing review" was printed and distributed; Hebei Hengshui city adjusts the pre-sale license standard for prefabricated
Pytorch Notebook. Nn. Batchnorm1d