c51单片机实现矩阵键盘用数码管动态依次显示键值

c51单片机实现矩阵键盘用数码管动态依次显示键值

//4*4矩阵按键0-F4位LED轮流显示,按键从左到右从上到下0-F 
#include
#include
#define uchar unsigned char
#define uint unsigned int

uchar count=0,down=0;//全局变量用于表示按键个数

	
//函数声明
uchar keyscan();
void delay(uchar z);
void delay1();
uchar keyscan(void);


uchar a[]={0,0,0,0,0};//用于暂存按键

uchar code table[]=
{
	0x3f,0x06,0x5b,0x4f,
	0x66,0x6d,0x7d,0x07,
	0x7f,0x6f,0x77,0x7c,
	0x39,0x5e,0x79,0x71,
};

//延时函数
void delay(uchar z)//动态扫描延时
{
	while(z--);
}

void delay1()//按键延时消抖函数
{
	uchar i,j;
	for(i=50;i>0;i--)
	for(j=200;j>0;j--);
}

//扫描函数
uchar keyscan(void)
{
	uchar sccode,recode,key;
	P1=0xf0;
	if((P1&0xf0)!=0xf0)
	{
		delay1();
		if((P1&0xf0)!=0xf0)
		{
			sccode=0xfe;
			while((sccode&0x10)!=0)
			{
				P1=sccode;
				if((P1&0xf0)!=0xf0)
				{
					recode=P1&0xf0;
					sccode=sccode&0x0f;
					switch(sccode+recode)
					{
						case 0xee: key=0;break;
						case 0xde: key=1; break;
						case 0xbe:key=2;break;
						case 0x7e:key=3;break;
						case 0xed:key=4;break;
						case 0xdd:key=5;break;
						case 0xbd:key=6;break;
						case 0x7d:key=7;break;
						case 0xeb:key=8;break;
						case 0xdb:key=9;break;
						case 0xbb:key=10;break;
						case 0x7b:key=11;break;
						case 0xe7:key=12;break;
						case 0xd7:key=13;break;
						case 0xb7:key=14;break;
						case 0x77:key=15;break;
					}
					count++;
					down=1;
					//return key;
				}
				else
					sccode=(sccode<<1)|0x01;
			}
		}
	}
	return key;
}

//主函数
void main()
{
	uchar temp,j,b;
	temp=0xf7;
	P2=0x7f;
	P0=0x3f;
	while(1)
	{
		a[0]=keyscan();
		if(down==1){
		for(j=4;j>0;j--)//存放数据
			{
				a[j]=a[j-1];
			}
			down=0;	
		}
		for(b=0;b<100;b++)//动态扫描
		{
			for(j=4;j>0;j--)
			{
				P2=_cror_(temp,j-1);//数组角标表示移了多少位数
				P0=0x00;//消隐
				P0=table[a[j]];
				delay(100);
			}
		}
 }
}

你可能感兴趣的:(c51单片机实现矩阵键盘用数码管动态依次显示键值)