#include<reg52.h>
#define uchar unsigned char
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,
};
sbit dula=P2^6;
sbit wela=P2^7;
uchar temp,flag,i;
uchar number[4];
void delay(uchar z);
void display(uchar a,uchar b,uchar c,uchar d);
void keyscan();
void main()
{
while(i!=4)
{
keyscan();
if(flag!=0)
{
number[i]=flag;
}
}
display(number[0],number[1],number[2],number[3]);
}
void display(uchar a,uchar b,uchar c,uchar d)
{
while(1)
{
P0=table[a];
dula=1;
dula=0;
P0=0xfe;
wela=1;
wela=0;
delay(50);
P0=table[b];
dula=1;
dula=0;
P0=0xfd;
wela=1;
wela=0;
delay(50);
P0=table[c];
dula=1;
dula=0;
P0=0xf7;
wela=1;
wela=0;
delay(50);
P0=table[d];
dula=1;
dula=0;
P0=0xef;
wela=1;
wela=0;
delay(50);
}
}
void keyscan()
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(80);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:
flag=0;
i++;
break;
case 0xde:
flag=1;
i++;
break;
case 0xbe:
flag=2;
i++;
break;
case 0x7e:
flag=3;
i++;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(80);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:
flag=4;
i++;
break;
case 0xdd:
flag=5;
i++;
break;
case 0xbd:
flag=6;
i++;
break;
case 0x7d:
flag=7;
i++;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(80);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:
flag=8;
i++;
break;
case 0xdb:
flag=9;
i++;
break;
default:
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
}
void delay(uchar z)
{
uchar i,j;
for(i=z;i>0;i--)
{
for(j=10;j>0;j--)
;
}
}
先贴下自己代码把。 矩阵按键四次, 在数码的第1 2 4 5 位显示按的数值 矩阵键 依次表示0~9 后面有没用上的矩阵键就不检测了。
/*
第二种主函数的写法
uchar keyscan() //有返回值
void main()
{
} |