Tiny_STM32-按键检测库
该库适用于stm32的按键检测,可以进行按键检测,长按检测,双击检测等功能。注意需要引用sys.h头文件以实现位带操作。
头文件:
/*
* _oo0oo_
* o8888888o
* 88" . "88
* (| -_- |)
* 0\ = /0
* ___/`---'\___
* .' \\| |// '.
* / \\||| : |||// \
* / _||||| -:- |||||- \
* | | \\\ - /// | |
* | \_| ''\---/'' |_/ |
* \ .-\__ '-' ___/-. /
* ___'. .' /--.--\ `. .'___
* ."" '< `.___\_<|>_/___.' >' "".
* | | : `- \`.;`\ _ /`;.`/ - ` : | |
* \ \ `_. \_ __\ /__ _/ .-` / /
* =====`-.____`.___ \_____/___.-`___.-'=====
* `=---='
*
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* 佛祖保佑 永不宕机 永无BUG
*/
#ifndef __KEY_H
#define __KEY_H
#include "sys.h"
/**
* @brief 该库用于stm32固件库的按键检测,需要位带操作,引用sys.h头文件
* _ __
* | |/ /___ _ _
* | ' </ -_) || |
* |_|\_\___|\_, |
* |__/
*
*/
//按键检测程序 更换按键时修改下面的定义
/**
* @brief How TO Use?
* 直接调用函数即可
* eg:
* KEY_Scan(PC15)
*
*/
//1.在此处增加宏定义
#define PC15 PCin(15)
//2.在初始化函数里初始化GPIO引脚
void KEY_Init(void); //按键初始化
int KEY_Scan(int KEY); //按键扫描
u8 Long_Press(int KEY); //长按检测
u8 click_N_Double (int KEY, u8 time); //按键扫描-双击
#endif
.c文件:
#include "key.h"
/**************************************************************************
函数功能:按键初始化
入口参数:无
返回 值:无
**************************************************************************/
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //使能PA端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
GPIO_Init(GPIOC, &GPIO_InitStructure); //根据设定参数初始化GPIOA
GPIO口初始化函数写在这里
}
/**************************************************************************
函数功能:按键扫描
入口参数:无
返回 值:按键状态 0:无动作 1:单击
**************************************************************************/
int KEY_Scan(int KEY)
{
static u8 flag_key=1;//按键按松开标志
if(flag_key&&KEY==0)
{
flag_key=0;
return 1; // 按键按下
}
else //if(1==KEY)
{
flag_key=1;
return 0;//无按键按下
}
}
/**************************************************************************
函数功能:按键扫描
入口参数:双击等待时间
返回 值:按键状态 0:无动作 1:单击 2:双击
**************************************************************************/
u8 click_N_Double (int KEY, u8 time)
{
static u8 flag_key,count_key,double_key;
static u16 count_single,Forever_count;
if(KEY==0) Forever_count++; //长按标志位未置1
else Forever_count=0;
if(0==KEY&&0==flag_key) flag_key=1;
if(0==count_key)
{
if(flag_key==1)
{
double_key++;
count_key=1;
}
if(double_key==2)
{
double_key=0;
count_single=0;
return 2;//双击执行的指令
}
}
if(1==KEY) flag_key=0,count_key=0;
if(1==double_key)
{
count_single++;
if(count_single>time&&Forever_count<time)
{
double_key=0;
count_single=0;
return 1;//单击执行的指令
}
if(Forever_count>time)
{
double_key=0;
count_single=0;
}
}
return 0;
}
/**************************************************************************
函数功能:长按检测
入口参数:无
返回 值:按键状态 0:无动作 1:长按2s
**************************************************************************/
u8 Long_Press(int KEY)
{
static u16 Long_Press_count,Long_Press;
if(Long_Press==0&&KEY==0) Long_Press_count++; //长按标志位未置1
else Long_Press_count=0;
if(Long_Press_count>200)
{
Long_Press=1;
Long_Press_count=0;
return 1;
}
if(Long_Press==1) //长按标志位置1
{
Long_Press=0;
}
return 0;
}