#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e};
void delayms(uint a)
{
uint x,y;
for(x=a;x>0;x--)
for(y=110;y>0;y--);
}
uchar keyscan()
{
uchar temp;
static uchar keyvalue=0; //定义静态按键值,否则每次进来会清零,也可以定义全局变量
P1=0xfe; //清零P1口第0位,检测
temp=P1&0xf0;
if(temp!=0xf0)
{
delayms(5);
temp=P1&0xf0;
if(temp!=0xf0)
{
if(temp==0xe0)
keyvalue=1; //test
if(temp==0xd0)
keyvalue=2;
if(temp==0xb0)
keyvalue=3;
if(temp==0x70)
keyvalue=4;
}
while(temp!=0xf0)//等待松手,很重要
temp=P1&0xf0;//一直检测输入状态
}
P1=0xfd; //清零P1口第1位,检测
temp=P1&0xf0;
if(temp!=0xf0)
{
delayms(5);
temp=P1&0xf0;
if(temp!=0xf0)
{
if(temp==0xe0)
keyvalue=5; //test
if(temp==0xd0)
keyvalue=6;
if(temp==0xb0)
keyvalue=7;
if(temp==0x70)
keyvalue=8;
}
while(temp!=0xf0)//等待松手,很重要
temp=P1&0xf0;//一直检测输入状态
}
P1=0xfb; //清零P1口第2位,检测
temp=P1&0xf0;
if(temp!=0xf0)
{
delayms(5);
temp=P1&0xf0;
if(temp!=0xf0)
{
if(temp==0xe0)
keyvalue=9; //test
if(temp==0xd0)
keyvalue=10;
if(temp==0xb0)
keyvalue=11;
if(temp==0x70)
keyvalue=12;
}
while(temp!=0xf0)//等待松手,很重要
temp=P1&0xf0;//一直检测输入状态
}
P1=0xf7; //清零P1口第3位,检测
temp=P1&0xf0;
if(temp!=0xf0)
{
delayms(5);
temp=P1&0xf0;
if(temp!=0xf0)
{
if(temp==0xe0)
keyvalue=13; //test
if(temp==0xd0)
keyvalue=14;
if(temp==0xb0)
keyvalue=15;
if(temp==0x70)
keyvalue=16;
}
while(temp!=0xf0)//等待松手输入状态
temp=P1&0xf0;//一直检测输入状态
}
return (keyvalue); //返回键值
}
int main(void)
{
uchar num=1;
P0=0xff; //作外部输入时要初始化
while(1)
{
num=keyscan();
P0=table[num-1];
}
}
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e};
void delayms(uint a)
{
uint x,y;
for(x=a;x>0;x--)
for(y=110;y>0;y--);
}
uchar keyscan()
{
uchar temp;
static uchar keyvalue=0; //定义静态按键值,否则每次进来会清零,也可以定义全局变量
P1=0xfe; //清零P1口第0位,检测
temp=P1&0xf0;
if(temp!=0xf0)
{
delayms(5);
temp=P1&0xf0;
if(temp!=0xf0)
{
if(temp==0xe0)
keyvalue=1; //test
if(temp==0xd0)
keyvalue=2;
if(temp==0xb0)
keyvalue=3;
if(temp==0x70)
keyvalue=4;
}
while(temp!=0xf0)//等待松手,很重要
temp=P1&0xf0;//一直检测输入状态
}
P1=0xfd; //清零P1口第1位,检测
temp=P1&0xf0;
if(temp!=0xf0)
{
delayms(5);
temp=P1&0xf0;
if(temp!=0xf0)
{
if(temp==0xe0)
keyvalue=5; //test
if(temp==0xd0)
keyvalue=6;
if(temp==0xb0)
keyvalue=7;
if(temp==0x70)
keyvalue=8;
}
while(temp!=0xf0)//等待松手,很重要
temp=P1&0xf0;//一直检测输入状态
}
P1=0xfb; //清零P1口第2位,检测
temp=P1&0xf0;
if(temp!=0xf0)
{
delayms(5);
temp=P1&0xf0;
if(temp!=0xf0)
{
if(temp==0xe0)
keyvalue=9; //test
if(temp==0xd0)
keyvalue=10;
if(temp==0xb0)
keyvalue=11;
if(temp==0x70)
keyvalue=12;
}
while(temp!=0xf0)//等待松手,很重要
temp=P1&0xf0;//一直检测输入状态
}
P1=0xf7; //清零P1口第3位,检测
temp=P1&0xf0;
if(temp!=0xf0)
{
delayms(5);
temp=P1&0xf0;
if(temp!=0xf0)
{
if(temp==0xe0)
keyvalue=13; //test
if(temp==0xd0)
keyvalue=14;
if(temp==0xb0)
keyvalue=15;
if(temp==0x70)
keyvalue=16;
}
while(temp!=0xf0)//等待松手输入状态
temp=P1&0xf0;//一直检测输入状态
}
return (keyvalue); //返回键值
}
int main(void)
{
uchar num=1;
P0=0xff; //作外部输入时要初始化
while(1)
{
num=keyscan();
P0=table[num-1];
}
}