基于51单片机的数码管简易计算器Word文档下载推荐.docx
《基于51单片机的数码管简易计算器Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《基于51单片机的数码管简易计算器Word文档下载推荐.docx(9页珍藏版)》请在冰豆网上搜索。
五、软件编程部份
#include<
reg52.h>
#define ucharunsignedchar#define uint unsignedint
//ucharcodenum[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x40};
//共阴极
// 0 1 2 3 4 5 6 7 8 9 熄灭 -
//ucharcodeloc[]={0xff,0xfe,0xfd,0xfb,0xf7};
//ucharcodeero[]={0x79,0x50,0x5c};
ucharcodenum[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0x40};
//共阳极
ucharcodeloc[]={0x00,0x80,0x40,0x20,0x10};
ucharcodeero[]={~0x79,~0x50,~0x5c};
uint n=0,n1=0,n2=0;
//赋初值
ucharflag=0;
//计算类型选择关键字voiddelay(intt);
voiddisplay(intn);
voiderror();
main()
{
while
(1)
uchartemp;
//第一行检测P3=0xfe;
temp=P3;
temp=temp&
0xf0;
if(temp!
=0xf0)
delay(10);
0xfe;
=0xfe)
temp=P3;
switch(temp)
//清零
//0
//=
case 0xee:
n1=0;
n2=0;
n=0;
flag=0;
break;
case0xde:
n1=10*n1+0;
n=n1;
case 0xbe:
if(flag==1)n=n2+n1;
if(flag==2)n=n2-n1;
if(flag==3)n=n2*n1;
if(flag==4)n=n2/n1;
case0x7e:
//+n2=n1;
flag=1;
}
while(temp!
//扫描第二行
P3=0xfd;
case0xed:
n1=10*n1+1;
//4
case0xdd:
n1=10*n1+2;
//5
case0xbd:
n1=10*n1+3;
//6case0x7d:
//-
n2=n1;
flag=2;
//扫描第三行P3=0xfb;
//*
case0xeb:
n1=10*n1+4;
case0xdb:
n1=10*n1+5;
case0xbb:
n1=10*n1+6;
case 0x7b:
flag=3;
//扫描第四行
P3=0xf7;
case0xe7:
n1=10*n1+7;
//7
case0xd7:
n1=10*n1+8;
//8
case0xb7:
n1=10*n1+9;
//9
case0x77:
///
flag=4;
display(n);
//延时函数voiddelay(intt)
intx,y;
for(x=0;
x<
t;
x++)
for(y=0;
y<
y++);
//数码管显示voiddisplay(intn)
//溢出处理ucharg,s,b,q;
intabs;
if((n>
9999)||(n<
-999))
error();
//正数if((n>
=0)&
&
(n<
=9999))
g=n%10;
s=n/10%10;
b=n/100%10;
q=n/1000%10;
P0=num[g];
delay(5);
P2=loc[4];
delay
(2);
P2=loc[0];
delay(3);
if(n>
=10)
P0=num[s];
P2=loc[3];
if(n>
=100)
P0=num[b];
P2=loc[2];
=1000)
P0=num[q];
P2=loc[1];
//负数if((n<
0)&
(n>
=-999))
abs=-n;
g=abs%10;
s=abs/10%10;
b=abs/100%10;
q=abs/1000%10;
P0=num[g];
if((abs/10%10>
0)||(abs/100%10>
0))
P2=loc[3];
;
delay
(2);
if((abs/100%10>
if((abs/1000%10>
else
}
P0=num[11];
//溢出显示voiderror()
P2=loc[1];
P0=ero[0];
P2=loc[2];
P0=ero[1];
P2=loc[4];
P0=ero[2];