效果图:
完整的源码下载地址 http://www.51hei.com/f/jsjzz.rar
CODE:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
uchar LA[8];
uchar code wetable[]={
0x7f,0xbf,0xdf,0xef,
0xf7,0xfb,0xfd,0xfe};
uchar code dutable[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f};//段选0~9
long int num1,num2,num;
uchar temp;//按键需要的中间变量
uchar flag;//标记是否为数字键
uchar ch;//保存当前按下的符号标记按键的次数,按下“+-*/”
uchar date,count;//date保存按键得到的数字,count标记是第几个数字
uchar p;//p为指针,为了消除数字之前多余的0
uchar mark;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
{
for(y=0;y<=112;y++)
{
}
}
}
void init()
{
count=0;
flag=0;
mark=0;
num1=0;
num2=0;
num=0;
}
uchar keyscan()//判断按下那个键
{
P1=0xfe;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);//消除抖动
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)//证明有键按下,while语句是为了让键盘松开
{
temp=P1;
switch(temp)
{
case 0xee:flag=1;date=7;
break;
case 0xde:flag=1;date=8;
break;
case 0xbe:flag=1;date=9;
break;
case 0x7e:flag=2;ch='/';
break;
}
if(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)//等待按键松开
{
temp=P1;
temp=temp&0xf0;
}
}
}
}
P1=0xfd;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);//消除抖动
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)//证明有键按下,while语句是为了让键盘松开
{
temp=P1;
switch(temp)
{
case 0xed:flag=1;date=4;
break;
case 0xdd:flag=1;date=5;
break;
case 0xbd:flag=1;date=6;
break;
case 0x7d:flag=2;ch='*';
break;
}
if(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)//等待按键松开
{
temp=P1;
temp=temp&0xf0;
}
}
}
}
P1=0xfb;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);//消除抖动
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)//证明有键按下,while语句是为了让键盘松开
{
temp=P1;
switch(temp)
{
case 0xeb:flag=1;date=1;
break;
case 0xdb:flag=1;date=2;
break;
case 0xbb:flag=1;date=3;
break;
case 0x7b:flag=2;ch='-';
break;
}
if(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)//等待按键松开
{
temp=P1;
temp=temp&0xf0;
}
}
}
}
P1=0xf7;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);//消除抖动
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)//证明有键按下,while语句是为了让键盘松开
{
temp=P1;
switch(temp)
{
case 0xe7:flag=4;ch='C';
break;
case 0xd7:flag=1;date=0;
break;
case 0xb7:flag=3;//ch='=';
break;
case 0x77:flag=2;ch='+';
break;
}
if(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)//等待按键松开
{
temp=P1;
temp=temp&0xf0;
}
}
}
}
return date;
}
void display(long int num)
{
char i;
LA[7]=num%100000000/10000000;
LA[6]=num%10000000/1000000;
LA[5]=num%1000000/100000;
LA[4]=num%100000/10000;
LA[3]=num%10000/1000;
LA[2]=num%1000/100;
LA[1]=num%100/10;
LA[0]=num%10;
if(num==0)
{
wela=1;
P0=wetable[0];
wela=0;
dula=1;
P0=dutable[0];
dula=0;
P0=0xff;
delay(2);
}
else
{
for(i=7;i>=0;i--)
{
if(LA[i]!=0)
{
p=i;
break;
}
}
for(i=0;i<=p;i++)
{
wela=1;
P0=wetable[i];
wela=0;
dula=1;
P0=dutable[LA[i]];
dula=0;
P0=0xff;//消影
delay(2);
}
}
}
void calc(uchar date)
{
if(flag==1)
{
if(count==0)//得到的第一个完整数字
{
num1=num1*10+date;
flag=0;
num=num1;
}
if(count==1)//得到的第二个完整数字
{
num2=num2*10+date;
flag=0;
num=num2;
}
}
if(count>1&&mark==1)
{
num1=num;
flag=0;
count=1;//讲num2置为第二个数
}
display(num);
if(flag==2)//如果得到符号位
{
count++;
flag=0;
}
if(flag==3)
{
switch(ch)
{
case '+':num=num1+num2;
break;
case '-':num=num1-num2;
break;
case '*':num=num1*num2;
break;
case '/':num=num1/num2;
break;
}
num1=0;
num2=0;
flag=0;
mark=1;//here is a bug
}
display(num);
if(flag==4)
{
num1=0;
num2=0;
num=0;
count=0;
flag=0;
mark=0;
}
}
void main()
{
init();
while(1)
{
calc(keyscan());
}
}