#include<reg51.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit adrd=P3^7;
sbit adwr=P3^6;
sbit dula=P2^6;
sbit wela=P2^7;
uchar j,k;
double adval;
void delayms(uchar i)
{
uint x,y;
for(x=i;x>0;x--)
for(y=110;y>0;y--);
}
uchar code table[]=
{
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};
void display(uchar A1,uchar A2,uchar A3,uchar adval)
{
dula=1;
P0=table[A1];
dula=0;
wela=1;
P0=0x7e; //01111110
wela=0;
delayms(5);
dula=1;
P0=table[A2];
dula=0;
wela=1;
P0=0x7d;
wela=0;
delayms(5);
dula=1;
P0=table[A3];
dula=0;
wela=1;
P0=0x7b;//01111011
wela=0;
delayms(5);
}
void main()
{
uchar a,A1,A2,A3;
wela=1;
P0=0x7f; //01111111
wela=0;
while(1)
{
adwr=1;
_nop_();
adwr=0;
_nop_();
adwr=1;
for(a=20;a>0;a--)
{
display(A1,A2,A3,adval);
}
P1=0xff;
adrd=1;
_nop_();
adrd=0;
_nop_();
adval=P1;
adrd=1;
A1=(uint)adval/100;
A2=(uint)adval/10%10;
A3=(uint)adval%10;
}
}
注:这是整体框架,避免一致性引起考试事故,按键还需各位自己加,每个人写法不同,,各位加油。