Tiny_STM32-按键检测库
该库适用于stm32的按键检测,可以进行按键检测,长按检测,双击检测等功能。注意需要引用sys.h头文件以实现位带操作。
头文件:
#ifndef __KEY_H
#define __KEY_H
#include "sys.h"
#define PC15 PCin(15)
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);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
int KEY_Scan(int KEY)
{
static u8 flag_key=1;
if(flag_key&&KEY==0)
{
flag_key=0;
return 1;
}
else
{
flag_key=1;
return 0;
}
}
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++;
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;
}
u8 Long_Press(int KEY)
{
static u16 Long_Press_count,Long_Press;
if(Long_Press==0&&KEY==0) Long_Press_count++;
else Long_Press_count=0;
if(Long_Press_count>200)
{
Long_Press=1;
Long_Press_count=0;
return 1;
}
if(Long_Press==1)
{
Long_Press=0;
}
return 0;
}
|