|
#include<reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char table[16]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7f,
0x39,0x3f,0x79,0x71};
uchar code table1[]={
0xfe,0xfd,0xfb,0xf7,
0xef,0xdf,0xbf,0x7f};
uint i,j,z;
void delay(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
uchar keyscan()
{
uchar key,Row,Col;
P3=0x0f;
if(P3!=0x0f)
{
delay(10);//去抖
if(P3!=0x0f){
Row=P3&0x0f;//确保端口值正确(行的值)
P3=0xf0;
Col=P3&0xf0;//列值
}
while((P3&0xf0)!=0xf0);//判断键是否抬起
}
switch(Row+Col){
case 0xee:key=0;
P1=0Xfe;delay(100);P1=0xfd;delay(100);P1=0Xfb;delay(100);P1=0xf7;delay(100);
P1=0Xef;delay(100);P1=0xdf;delay(100);P1=0Xbf;delay(100);P1=0x7f;delay(100);
break;
case 0xde:key=1;P1=0xff;break;
case 0xbe:key=2;P1=0xff;break;
case 0x7e:key=3;P1=0xff;break;
case 0xed:key=4;P1=0xff;break;
case 0xdd:key=5;P1=0xff;break;
case 0xbd:key=6;P1=0xff;break;
case 0x7d:key=7;P1=0xff;break;
case 0xeb:key=8;P1=0xff;break;
case 0xdb:key=9;P1=0xff;break;
case 0xbb:key=10;
P1=0X7f;delay(100);P1=0xbf;delay(100);P1=0Xdf;delay(100);P1=0xef;delay(100);
P1=0Xf7;delay(100);P1=0xfb;delay(100);P1=0Xfd;delay(100);P1=0xfe;delay(100);
break;
case 0x7b:key=11;P1=0xff;break;
case 0xe7:key=12;P1=0xff;break;
case 0xd7:key=13;P1=0xff;break;
case 0xb7:key=14;P1=0xff;break;
case 0x77:key=15;P1=0xff;break;
}
return key;
}
void main()
{
unsigned key1;
while(1)
{
key1=keyscan();
P2=table[key1];
}
}
|
|