51蓝桥——按键的基本应用(IO模式)(正在更新)

发布时间:2024年01月19日

目录: 附STC15F2K60S2封装图和按键原理图见结尾

? ? ? ? ? ?1.独立按键的检测与简单消抖;

? ? ? ? ? ?2.矩阵按键4*4普通扫描检测;

? ? ? ? ? ?3.定时器扫描矩独立按键与定时器消抖

? ? ? ? ? ?4.按键的长按与短按;

? ? ? ? ? ?5.按键双击检测;

如果用<STC15F2K60S2.H>进行编程,原理图P37替换P44,P36替换P42,不理解的话直接看结尾芯片封装图和按键原理图。如果用<REG51·H>则不需替换

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1.独立按键的检测与简单消抖(跳线帽选择独立键盘)

······················································································
#include <STC15F2K60S2.H>
#include "ALL.H"
unsigned char key_Init()
{
	unsigned char keynum = 0;
	if(P30==0){Delay1ms(20);while(P30==0);Delay1ms(20);keynum = 1;}
	if(P31==0){Delay1ms(20);while(P30==0);Delay1ms(20);keynum = 2;}
	if(P32==0){Delay1ms(20);while(P30==0);Delay1ms(20);keynum = 3;}
	if(P33==0){Delay1ms(20);while(P30==0);Delay1ms(20);keynum = 4;}
	return keynum;
}
//简单的按键消抖:当按键按下的一瞬间延时20ms,按键不松开则陷入while循环,松开的一瞬间延时20ms
······················································································

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2.矩阵按键4*4普通扫描检测坐标法(跳线帽选择矩阵键盘)

`````````````````````````````````````````````````````````````````````````````````````````
#include <STC15F2K60S2.H>
#include <ALL.H>
unsigned char matrix_key()  //逐列逐行扫描,先扫描列在扫描行
{
	unsigned char keynum = 0;
	P44 = 0;P42 = 1;P35 = 1;P34 = 1;
	if(P30==0){Delay1ms(20);while(P30==0);Delay1ms(20);keynum = 1;}
	if(P31==0){Delay1ms(20);while(P31==0);Delay1ms(20);keynum = 5;}
	if(P32==0){Delay1ms(20);while(P32==0);Delay1ms(20);keynum = 9;}
	if(P33==0){Delay1ms(20);while(P33==0);Delay1ms(20);keynum = 13;}
	
	P44 = 1;P42 = 0;P35 = 1;P34 = 1;
	if(P30==0){Delay1ms(20);while(P30==0);Delay1ms(20);keynum = 2;}
	if(P31==0){Delay1ms(20);while(P31==0);Delay1ms(20);keynum = 6;}
	if(P32==0){Delay1ms(20);while(P32==0);Delay1ms(20);keynum = 10;}
	if(P33==0){Delay1ms(20);while(P33==0);Delay1ms(20);keynum = 14;}
	
	P44 = 1;P42 = 1;P35 = 0;P34 = 1;
	if(P30==0){Delay1ms(20);while(P30==0);Delay1ms(20);keynum = 3;}
	if(P31==0){Delay1ms(20);while(P31==0);Delay1ms(20);keynum = 7;}
	if(P32==0){Delay1ms(20);while(P32==0);Delay1ms(20);keynum = 11;}
	if(P33==0){Delay1ms(20);while(P33==0);Delay1ms(20);keynum = 15;}
	
	P44 = 1;P42 = 1;P35 = 1;P34 = 0;
	if(P30==0){Delay1ms(20);while(P30==0);Delay1ms(20);keynum = 4;}
	if(P31==0){Delay1ms(20);while(P31==0);Delay1ms(20);keynum = 8;}
	if(P32==0){Delay1ms(20);while(P32==0);Delay1ms(20);keynum = 12;}
	if(P33==0){Delay1ms(20);while(P33==0);Delay1ms(20);keynum = 16;}
	return keynum;
}
`````````````````````````````````````````````````````````````````````````````````````````

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

? ?3.定时器扫描独立按键与定时器消抖(跳线帽选择独立按键)

中断每20ms调用一次扫描函数key_loop();相当于实现了消抖;按键按下长按无反应,松开的时刻才会有;

````````````````````````````````````````````````````````````````main
//定时器扫描按键与数码管
#include <STC15F2K60S2.H>
#include "ALL.H"
unsigned char keys;
void main()
{
	Timer0_Init();
	switch_138(5);P0 &= 0XAF;
	while(1)
	{
		keys = key_set();
		if(keys){nixie_set(1,keys);}
	}
}

void Timer0_Rutine() interrupt 1
{
	static unsigned char c1=0,c2=0;
	c1++;c2++;
	if(c1==2){nixie_loop();c1=0;}
	if(c2==20){key_loop();c2=0; }
	
}
·································································KEY.c
#include <STC15F2K60S2.H>
sbit K1 = P3^0; sbit K2 = P3^1; sbit K3 = P3^2; sbit K4 = P3^3;
unsigned char keynum;
unsigned char key_set()//为什么要单独写一个返回函数?key_loop函数已经放中断中了,主函数里妹有接收返回值的函数
{
	unsigned char cash=0;//中转
	cash = keynum;
	keynum = 0;
	return cash;
}
unsigned char key()//在中断中每隔20ms调用一次,相当于进行了消抖
{
	unsigned char num=0;
	if(K1==0){num = 1;}
	if(K2==0){num = 2;}
	if(K3==0){num = 3;}
	if(K4==0){num = 4;}
	return num;
}
void key_loop()//松手检测,放中断中,if上一刻按键按下和现在按键没有按下,返回对应值
{
	static unsigned char lastdata,nowdata;
	lastdata = nowdata;nowdata = key();
	if(lastdata==1&&nowdata==0){keynum = 1;}
	if(lastdata==2&&nowdata==0){keynum = 2;}
	if(lastdata==3&&nowdata==0){keynum = 3;}
	if(lastdata==4&&nowdata==0){keynum = 4;}
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

4.按键的长按与短按

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

462116a9b89c43c8bc37f54f9e8c254f.png

2facdf4f900848b99c6ace29964e8588.png

文章来源:https://blog.csdn.net/doctor_Le/article/details/135662219
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。