#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit d1=P2^0;
sbit d2=P2^1;
sbit d3=P2^2;
sbit d4=P2^7;
uchar duanma[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00},temp,a; // 数码管程序编码
//uchar wei[]={0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87};
void delay(uint z) //delay延时函数,每执行一次用时1毫秒
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void keyscan() //矩阵键盘显示函数
{
P3=0xfe; //给P3口赋一个初值用于下面的检测
temp=P3;
temp=temp&0xf0;//0xfe和0xf0位与,用于对P3口的检测,做出判断
while(temp!=0xf0) //当检测出有按键按下即P3口不等于oxf0时,进入循环
{
delay(10);//用延时程序消抖
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
switch(temp) //当进入循环后对P3口进行扫描,用开关语句选择显示数码
{
case 0xee: //当P3口是0xee时在一位数码管上显示1
d1=0;
d2=0;
d3=0;
P0=duanma[1];
break;
case 0xde: //当P3口是0xde时在一位数码管上显示2
d1=1;
d2=0;
d3=0;
P0=duanma[2];
break;
case 0xbe: //当P3口是0xbe时在一位数码管上显示3
d1=0;
d2=1;
d3=0;
P0=duanma[3];
break;
case 0x7e: //当P3口是0x7e时在一位数码管上显示4
d1=1;
d2=1;
d3=0;
P0=duanma[4];
break;
}
while(temp!=0xf0) //松手检测
{
temp=P3; //把P3的值读回来
temp=temp&0xf0; //再和0xf0与,再跳到while语句去判断!假就跳出while语句。
}
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(1);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:
d1=0;
d2=0;
d3=1;
P0=duanma[5];
break;
case 0xdd:
d1=1;
d2=0;
d3=1;
P0=duanma[6];
break;
case 0xbd:
d1=0;
d2=1;
d3=1;
P0=duanma[7];
break;
case 0x7d:
d1=1;
d2=1;
d3=1;
P0=duanma[8];
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:
while(1)
{
d1=0;
d2=0;
d3=0;
P0=duanma[1];
delay(1);
d1=1;
d2=0;
d3=0;
P0=duanma[0];
delay(1);
if(P3!=0xeb||P3!=0xfb)
break;
}
case 0xdb:
while(P3==0xdb||P3==0xfb)
{ d1=0;
d2=1;
d3=0;
P0=duanma[1];
d1=1;
d2=1;
d3=0;
P0=duanma[1];
}
case 0xbb:
while(P3==0xbb||P3==0xfb)
{
d1=0;
d2=0;
d3=1;
P0=duanma[1];
delay(1);
d1=1;
d2=0;
d3=1;
P0=duanma[2];
delay(1);
}
case 0x7b:
while(P3==0x7b||P3==0xfb)
{
d1=0;
d2=1;
d3=1;
P0=duanma[1];
delay(1);
d1=1;
d2=1;
d3=1;
P0=duanma[3];
delay(1);
}
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
break;
}
P3=0xf7;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7:
while(P3==0xe7||P3==0xf7)
{
d1=0;
d2=0;
d3=0;
P0=duanma[1];
delay(1);
d1=1;
d2=0;
d3=0;
P0=duanma[4];
delay(1);
}
break;
case 0xd7:
while(P3==0xd7||P3==0xf7)
{
d1=0;
d2=1;
d3=0;
P0=duanma[1];
delay(1);
d1=1;
d2=1;
d3=0;
P0=duanma[5];
delay(1);
}
break;
case 0xb7:
while(P3==0xb7||P3==0xf7)
{
d1=0;
d2=0;
d3=1;
P0=duanma[1];
delay(1);
d1=1;
d2=0;
d3=1;
P0=duanma[6];
delay(1);
}
break;
case 0x77:
while(P3==0x77||P3==0xf7)
{
d1=0;
d2=1;
d3=1;
P0=duanma[1];
delay(1);
d1=1;
d2=1;
d3=1;
P0=duanma[7];
delay(1);
}
break; }
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
}
void main()
{
d4=1;
while(1)
{
keyscan();
}
}