#include
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6; //申明U1锁存器的锁存端
sbit wela=P2^7; //申明U2锁存器的锁存端
unsigned char a1,a2,num;
unsigned char code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f};
void delayms(uint xms)
{
unsigned int i,j;
for(i=xms;i>0;i--) //i=xms即延时约xms毫秒
for(j=110;j>0;j--);
}
void display(uchar tum,uchar dat)
{
uchar i;
dula=0;
P0=table[dat]; //显示函数只送段选数据
dula=1;
dula=0;
wela=0;
i=0xff;
i=i&(~((0x01)<<(tum)));
P0=i;
wela=1;
wela=0;
}
void matrixkeyscan()
{
uchar temp,a3;
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:
a3=1;
break;
case 0xde:
a3=2;
break;
case 0xbe:
a3=3;
break;
case 0x7e:
a3=4;
break;
}
if(num>0)
{
a2=a3;
display(1,a2);
}
if(num==0)
{
a1=a3;
display(0,a1);
num++;
}
while(temp!=0xf0)
{
temp=P3;
temp=a3&0xf0;
}
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:
a3=5;
break;
case 0xdd:
a3=6;
break;
case 0xbd:
a3=7;
break;
case 0x7d:
a3=8;
break;
}
if(num>0)
{
a2=a3;
display(1,a2);
}
if(num==0)
{
a1=a3;
display(0,a1);
num++;
}
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:
a3=9;
if(num>0)
{
a2=a3;
display(1,a2);
}
if(num==0)
{
a1=a3;
display(0,a1);
num++;
}
break;
case 0xdb:
a3=0;
if(num>0)
{
a2=a3;
display(1,a2);
}
if(num==0)
{
a1=a3;
display(0,a1);
num++;
}
break;
case 0xbb:
a3=11; display(2,a1+a2);
break;
case 0x7b:
a3=11;display(2,a1-a2);
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xf7;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7:
a3=11;display(2,a1*a2);
break;
case 0xd7:
a3=11;display(2,a1/a2);
break;
case 0xb7:
a3=11;
break;
case 0x77:
a3=11;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
}
void main()
{
while(1)
{
matrixkeyscan();//不停调用键盘扫描程序
}
}