按键松手检测不堵塞主程序+按键连击

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值