共阳极 键盘接P3口
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar temp,num,m;
sbit dula=P2^6;
sbit wela=P2^7;
void delay(uchar z);
void disply();
void keytwo();
void keyone();
void keythree();
void keyfour();
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e};
void main()
{
while(1)
{
for(m=0;m<4;m++)
{
if(m==0)
keyone();
if(m==1)
keytwo();
if(m==2)
keythree();
if(m==3)
keyfour();
}
}
}
void delay(uchar z)
{
uchar a,b;
for(a=z;a>0;a--)
for(b=110;b>0;b--);
}
void disply()
{
wela=1;
P0=0xff;
wela=0;
dula=1;
P0=table[num-1];
dula=0;
delay(5);
}
void keyone()
{
P3=0xfe;
temp=P3;
if(temp!=0xfe)
{
switch(temp)
{
case0xee:num=1;break;
case0xde:num=2;break;
case0xbe:num=3;break;
case0x7e:num=4;break;
}
disply();
delay(5);
}
}
void keytwo()
{
P3=0xfd;
temp=P3;
if(temp!=0xfd)
{
switch(temp)
{
case0xed:num=5;break;
case0xdd:num=6;break;
case0xbd:num=7;break;
case0x7d:num=8;break;
}
disply();
delay(20);
}
}
void keythree()
{
P3=0xfb;
temp=P3;
if(temp!=0xfb)
{
switch(temp)
{
case0xeb:num=9;break;
case0xdb:num=10;break;
case0xbb:num=11;break;
case0x7b:num=12;break;
}
disply();
delay(20);
}
}
void keyfour()
{
P3=0xf7;
temp=P3;
if(temp!=0xf7)
{
switch(temp)
{
case0xe7:num=13;break;
case0xd7:num=14;break;
case0xb7:num=15;break;
case0x77:num=16;break;
}
disply();
delay(20);
}
}