51实验11:矩阵键盘16个按键分别代表0-9,a-f,用最后一位数码管显示

51实验11:矩阵键盘16个按键分别代表0-9,a-f,用最后一位数码管显示

#include<reg52.h>
#include<intrins.h>

typedef unsigned int u16;  
typedef unsigned char u8;

#define GPIO_DIG P0
#define GPIO_KEY P1

//数码管
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
//数码表
u8 code smgduan[16]={0x3f,0x06,0x5b,0x4f,0x66,
			         0x6d,0x7d,0x07,0x7f,0x6f,
			         0x77,0x7c,0x39,0x5e,0x79,
			         0x71};
//储存按键标号
u8 KeyValue;

//延时函数	
void delay(u16 i)
{
  while(i--);
}


//获取按键标号
void KeyDown()
{
	  char a=0;
      GPIO_KEY=0x0f;
	  if(GPIO_KEY!=0x0f)
		{
		   //确定列
		   switch(GPIO_KEY)
			 {
				 case(0x07):KeyValue=0;break;   
				 case(0x0b):KeyValue=1;break;
				 case(0x0d):KeyValue=2;break;          
				 case(0x0e):KeyValue=3;break;
			 }
			 GPIO_KEY=0xf0;
			 //确定行
			 switch(GPIO_KEY)
			 {
				 case(0x70):KeyValue=KeyValue;break;
				 case(0xb0):KeyValue=KeyValue+4;break;
				 case(0xd0):KeyValue=KeyValue+8;break;
				 case(0xe0):KeyValue=KeyValue+12;break;
			 }
			 while((a<50)&&(GPIO_KEY!=0xf0))
			 {
			    delay(1000);
				  a++;
			 }
		}
		
}


void main()
{
   while(1)
	 {
	      //按键标号获取
		  KeyDown();
		  //确定数码管位置
		  LSA=0;LSB=0;LSC=0;
		  //keyvalue确定数字
		  GPIO_DIG=smgduan[KeyValue];
	 }
}

猜你喜欢

转载自blog.csdn.net/wang2395/article/details/107439780