#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
sbit key1=P3^4;
sbit key2=P3^5;
sbit key3=P3^6;
sbit key4=P3^7;
uchar code table[]=
{0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code list[]=
{0xfe,0xfd,0xfb,0xf7,
0xef,0xdf,0xbf,0x7f};
uchar num,temp[8],b;
uint cc;
void delay(uint x);
void display();
void keyma();
void show(uchar P);
uint output();
void jieguo();
void main()
{
cc=0;
TMOD=0x01;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
if(num==4)
{
num=0;
keyma();
}
display();
}
}
void delay(uint x)
{
uint i,j;
for(i=x;i>0;i--)
for(j=220;j>0;j--);
}
void T0_time() interrupt 1
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
num++;
}
void display()
{
uint i;
for(i=0;i<8;i++)
{
wela=1;
P0=list[i];
wela=0;
dula=1;
P0=temp[i];
dula=0;
delay(1);
}
}
void keyma()
{
uchar a;
P3=0xfe;
a=P3;
a=a&0xf0;
if(a!=0xf0)
{
delay(5);
a=P3;
a=a&0xf0;
if(a!=0xf0)
{
a=P3;
switch(a)
{
case 0xee:
b=0;break;
case 0xde:
b=1;break;
case 0xbe:
b=2;break;
case 0x7e:
b=3;break;
}
while(a!=0xf0)
{
a=P3;
a=a&0xf0;
}
show(b);}
}
P3=0xfd;
a=P3;
a=a&0xf0;
if(a!=0xf0)
{
delay(5);
a=P3;
a=a&0xf0;
if(a!=0xf0)
{
a=P3;
switch(a)
{
case 0xed:
b=4;break;
case 0xdd:
b=5;break;
case 0xbd:
b=6;break;
case 0x7d:
b=7;break;
}
while(a!=0xf0)
{
a=P3;
a=a&0xf0;
}
show(b);}
}
P3=0xfb;
a=P3;
a=a&0xf0;
if(a!=0xf0)
{
delay(5);
a=P3;
a=a&0xf0;
if(a!=0xf0)
{
a=P3;
switch(a)
{
case 0xeb:
b=8;break;
case 0xdb:
b=9;break;
case 0xbb:
b=10;break;
case 0x7b:
jieguo();break;
}
while(a!=0xf0)
{
a=P3;
a=a&0xf0;
}
show(b);}
}
P3=0xf7;
a=P3;
a=a&0xf0;
if(a!=0xf0)
{
delay(5);
a=P3;
a=a&0xf0;
if(a!=0xf0)
{
a=P3;
cc=output();
switch(a)
{
case 0xe7:
b=11;break;
case 0xd7:
b=12;break;
case 0xb7:
b=13;break;
case 0x77:
b=14;break;
}
while(a!=0xf0)
{
a=P3;
a=a&0xf0;
}
} }
}
void show(uchar p)
{
uint i;
for(i=0;i<8;i++)
temp[i]=temp[i+1];
temp[7]=table[p];
}
//****************************************************************************************************************************************//
uint output()
{
uint aa,i;
for(i=0;i<8;i++)
{
aa=aa*10+temp[i];
}
for(i=0;i<8;i++)
{
temp[i]=0;
}
return aa;
}
void jieguo()
{
uint sum,dd,i,ge[8];
dd=output();
if(b==11)
{
sum=dd+cc;
}
if(b==12)
{
sum=dd-cc;
}
if(b==13)
{
sum=dd*cc;
}
if(b==14)
{
sum=dd/cc;
}
ge[7]=sum%10;
ge[6]=sum%100/10;
ge[5]=sum%1000/100;
ge[4]=sum%10000/1000;
ge[3]=sum%100000/10000;
ge[2]=sum%1000000/100000;
ge[1]=sum%10000000/1000000;
ge[0]=sum%100000000/10000000;
for(i=0;i<8;i++)
temp[i]=table[ge[i]];
display();
}
这是自制的简易计算器,1、输入时时间摁长时系统就会卡住,必须等一会才能继续按。
2、计算结果有误,结果显示是最后一次输入的最后一位。
求大神指教。。。。。。 |