#include "stm32f0xx.h" // Device header
#include "Delay.h"
#include "BEE.h"
#include "Key.h"
#include "main.h"
uint8_t KZFH=0,QL=0;
uint16_t KZT,LJ;
void Key_Init()
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_StructInit(&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN;
//GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_0;
GPIO_InitStruct.GPIO_PuPd= GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed= GPIO_Speed_Level_3;//50
GPIO_Init(GPIOB,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN;
//GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_7|GPIO_Pin_6;
GPIO_InitStruct.GPIO_PuPd= GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed= GPIO_Speed_Level_3;//50
GPIO_Init(GPIOA,&GPIO_InitStruct);
}
//返回按键变量
uint8_t Key_Z(void)
{
uint8_t KeyNum=0;
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0)
{
KeyNum=1;
}
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)==0)
{
KeyNum=2;
}
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6)==0)
{
KeyNum=3;
}
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7)==0)
{
KeyNum=4;
}
return KeyNum;
}
uint8_t Key_GetNum(void)
{
uint8_t KZ;
KZ=Key_Z();//接收按键按下的值
if(KZ!=0)
{
QL=KZ;//检测到按键按下
if(KZ==3||KZ==4)//连击按键
{
if(KZT<=1001)
{
KZT++;
}
LJ++;
}
if(KZT>1000)//按下一定时间检测到长按
{
if(LJ>75)//这里设置连击速度
{
KZFH=KZ;
LJ=0;
return KZFH;
}
}
}
else if(QL!=0)//检测到按键松开并且按下过
{
Delay_ms(10);//消抖
KZFH=QL;
KZT=0;
QL=0;
return KZFH;
}
return 0;
}
按键松手检测不堵塞主程序+按键连击
最新推荐文章于 2024-04-19 16:52:06 发布