定时器检测按键(简单 有手就行)

        将按键检测写在while中的时候,由于采用的是轮询检测的方式,当非按键的代码部分耗时比较长的时候,就很容易检测不到按键,所以在这个时候我们可以采用外部中断检测按键或者定时器中断检测按键,接下来我会讲述定时器检测按键的方法。

        这里我用的是PB12 PB13 PB14 PB15 这四个口来连接按键,通过检测引脚的输入电平就可以知道当前哪个按键被按下。

        这里我配置的定时器周期为10ms

        这里count++这部分的代码大家请忽略,这是其他部分的,和按键不相关

        这里的代码逻辑我们运用到了状态机的思想,大家可以去了解一下

        简而言之就是每次定时器中断来临的时候都会判断一下当前按键的状态,比如是K1_bit=0的时候就代码没用按键被按下或者由于电平跳变导致的误触。当K1_bit=0的时候按键被按下这时候K1_bit=1,下一次定时器中断来临的时候它就会进行计时,如果计时时间大于某个值的时候就说明确实这个按键被按下了,但是此时如果不加K1_bit=3的判断的话,按键会被疯狂的触发,所以此时就需要K1_bit=3这个状态,这个状态是用来消抖的,如果某次定时中断来临的时候变为高电平,那么就说明按键被松开。

        如果这个时候没有被抬起就会继续计时 这时候变成K1_bit=4 再抬起来的时候就变成了K1_bit=5

这样一个按键就可以有两个按键值了 (一个按键可以当两个按键使用)

那如果把没有抬起这段话被注释的掉的话会变成一直按 疯狂触发。

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	static uint8_t count=0;
	if(htim->Instance == htim3.Instance) 
	{	
//		count++;
//		if(count==40)
//		{
//			count=0;
//			flag_show=1;
//		}
		
		//-----------------------K1
		if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12) == GPIO_PIN_RESET && K1_bit==0)//首次检测按下
		{
			K1_cnt=0;
			K1_bit=1;
		}
		if(K1_bit == 1)
		{
			K1_cnt++;
			
			if(K1_cnt >= 2)
			{
				if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12) == GPIO_PIN_RESET)//确定按下
				{
					K1_bit = 2;
				}
				else
					K1_bit = 0;
					
				
				K1_cnt = 0;
			}
		}
		if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12) == GPIO_PIN_SET && K1_bit==2)//短按后抬起
		{
				K1_bit=3;
		}
		else if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12) == GPIO_PIN_RESET && K1_bit == 2)//没有抬起
		{
			K1_cnt++;
			if(K1_cnt >= 40)//400ms
			{
				if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12) == GPIO_PIN_RESET)//确定按下
				{
					K1_bit = 4;
				}
				else
					K1_bit = 0;
					
				
				K1_cnt = 0;
			}
		}
		if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12) == GPIO_PIN_SET && K1_bit==4)//长按后抬起
		{
				K1_bit=5;
		}
	
		//-----------------------K2
		if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_13) == GPIO_PIN_RESET && K2_bit==0)//首次检测按下
		{
			K2_cnt=0;
			K2_bit=1;
		}
		if(K2_bit == 1)
		{
			K2_cnt++;
			
			if(K2_cnt >= 2)
			{
				if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_13) == GPIO_PIN_RESET)//确定按下
				{
					K2_bit = 2;
				}
				else 
					K2_bit = 0;
				
				K2_cnt = 0;
			}
		}
		if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_13) == GPIO_PIN_SET && K2_bit==2)//短按后抬起
		{
				K2_bit=3;
		}
		else if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_13) == GPIO_PIN_RESET && K2_bit == 2)//没有抬起
		{
			K2_cnt++;
			if(K2_cnt >= 40)//600ms
			{
				if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_13) == GPIO_PIN_RESET)//确定按下
				{
					K2_bit = 4;
				}
				else
					K2_bit = 0;
					
				
				K2_cnt = 0;
			}
		}
		if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_13) == GPIO_PIN_SET && K2_bit==4)//长按后抬起
		{
				K2_bit=5;
		}
		
		//-----------------------K3
		if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_14) == GPIO_PIN_RESET && K3_bit==0)//首次检测按下
		{
			K3_cnt=0;
			K3_bit=1;
		}
		if(K3_bit == 1)
		{
			K3_cnt++;
			
			if(K3_cnt >= 2)//计时
			{
				if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_14) == GPIO_PIN_RESET)
				{
					K3_bit = 2;
				}
				else 
					K3_bit = 0;
				
				K3_cnt = 0;
			}
		}
		if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_14) == GPIO_PIN_SET && K3_bit==2)//短按后抬起
		{
				K3_bit=3;
		}
		else if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_14) == GPIO_PIN_RESET && K3_bit == 2)//没有抬起
		{
			K3_cnt++;
			if(K3_cnt >= 40)//600ms
			{
				if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_14) == GPIO_PIN_RESET)//确定按下
				{
					K3_bit = 4;
				}
				else
					K3_bit = 0;
					
				
				K3_cnt = 0;
			}
		}
		if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_14) == GPIO_PIN_SET && K3_bit==4)//长按后抬起
		{
				K3_bit=5;
		}
		
		
		//-----------------------K4
		if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_15) == GPIO_PIN_RESET && K4_bit==0)//首次检测按下
		{
			K4_cnt=0;
			K4_bit=1;
		}
		if(K4_bit == 1)
		{
			K4_cnt++;
			
			if(K4_cnt >= 2)//计时
			{
				if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_15) == GPIO_PIN_RESET)
				{
					K4_bit = 2;
				}
				else 
					K4_bit = 0;
				
				K4_cnt = 0;
			}
		}
		if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_15) == GPIO_PIN_SET && K4_bit==2)//短按后抬起
		{
				K4_bit=3;
		}
		else if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_15) == GPIO_PIN_RESET && K4_bit == 2)//没有抬起
		{
			K4_cnt++;
			if(K4_cnt >= 40)//600ms
			{
				if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_15) == GPIO_PIN_RESET)//确定按下
				{
					K4_bit = 4;
				}
				else
					K4_bit = 0;
					
				
				K4_cnt = 0;
			}
		}
		if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_15) == GPIO_PIN_SET && K4_bit==4)//长按后抬起
		{
				K4_bit=5;
		}
	}
}

这里是判断键值的函数

直接在while里调用就行了

uint8_t Key_GetNum(void)
{
	if(K1_bit==3)
	{
		K1_bit=0;
		return 1;
	}
	else if(K1_bit==5)
	{
		K1_bit=0;
		return 5;
	}
	
	else if(K2_bit==3)
	{
		K2_bit=0;
		return 2;
	}
	else if(K2_bit==5)
	{
		K2_bit=0;
		return 6;
	}
	
	else if(K3_bit==3)
	{
		K3_bit=0;
		return 3;
	}
	else if(K3_bit==5)
	{
		K3_bit=0;
		return 7;
	}
	
	else if(K4_bit==3)
	{
		K4_bit=0;
		return 4;
	}
	else if(K4_bit==5)
	{
		K4_bit=0;
		return 8;
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值