基于stc89c52单片机制作的 计算器 程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#include
typedef unsigned char uchar;
typedef unsigned int uint;
sbit RS=P3^5;
sbit RW=P3^6;
sbit EN=P3^4;
sbit duan=P2^6;
sbit wei=P2^7;
signed long d,f[10],jg;
uchar flag=0,j=0;
bit flag1=0;
uchar out[]={"out"};
uchar c;
uchar e[10];
uchar b[]={0x00,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x2b,0x2a,0x2f,0x2d,0x3d,0x2e};//0,1,2,3,4,5,6,7,8,9,+,*,\,-,.,=
void delay(uint z)
{
uint x,y;
for(x = z; x > 0; x--)
for(y = 114; y > 0 ; y--);
}
void read_busy()
{
uchar busy;
P0=0xff;
RS=0;
RW=1;
do
{
EN=1;
busy=P0;
EN=0;
}
while(busy&0x80);
}
void write_cmd(uchar cmd)
{
read_busy();
RS=0;
RW=0;
P0=cmd;
EN=1;
EN=0;
}
void write_dat(uchar dat)
{
read_busy();
RS=1;
RW=0;
P0=dat;
EN=1;
EN=0;
}
void write_zfc(uchar x,uchar y,uchar *zfc)
{
if(y) x|=0x40;
x|=0x80;
write_cmd(x);
while(*zfc!='\0')
{
write_dat(*zfc++);
delay(500);
}
}
void write_zf(uchar x,uchar y,uchar dat)
{
if(y) x|=0x40;
x|=0x80;
write_cmd(x);
write_dat(dat);
}
void init()
{
duan=0;
wei=0;
write_cmd(0x38);
write_cmd(0x01);
write_cmd(0x0c);
write_cmd(0x06);
}
jianpan0()
{
uchar cord_l,cord_h;
P3 = 0xf0;
if( (P3 & 0xf0) != 0xf0)
{
delay(5);
if( (P3 & 0xf0) != 0xf0)
{
cord_l = P3 & 0xf0;
P3 = cord_l | 0x0f;
cord_h = P3 & 0x0f;
while( (P3 & 0x0f) != 0x0f );
return (cord_l + cord_h);
}
}
}
void shuchu(uchar i)
{
if(i!=0)
{
if(c>15)
{
write_cmd(0x18);
}
if(c==38)
{
write_cmd(0x01);
write_zfc(0,0,&out[0]);
write_cmd(0x08);
c=0;
init();
d=0;jg=0;for(j=0;j<10;j++){f[j]=0;}flag=0;j=0;flag1=0;i=0;
}
else{
write_zf(c,0,b[i]);
c++;
delay(10);}
}
}
void shuchu1(signed long f)
{
uint i=0;
if(f<0)
{
f=-f;
shuchu(14);
}
if(f==0)
{
shuchu(1);
}
if(f>0)
{
while(f!=0)
{
e[i++]=(long int)f%10;
(long int)f/=10;
}
i=i-1;
while(i!=-1)
{
if(c==38){break;}
shuchu((e[i--]+1));
delay(50);
}
}
}
void jianpan()
{
uchar a=0;
switch( jianpan0() )
{
case 0xee:if(flag1){shuchu(11);shuchu(8);a=7;d=10*d+a;flag1=0;}else{shuchu(8);a=7;d=10*d+a;}break;
case 0xde:if(flag1){shuchu(11);shuchu(9);a=8;d=10*d+a;flag1=0;}else{shuchu(9);a=8;d=10*d+a;}break;
case 0xbe:if(flag1){shuchu(11);shuchu(10);a=9;d=10*d+a;flag1=0;}else{shuchu(10);a=9;d=10*d+a;}break;
case 0x7e:shuchu(11);switch(flag){case 0:f[j]=d;d=0;flag=1;break;
case 1:f[j]=d;d=0;flag=1;break;
case 2:f[j]=-d;d=0;flag=1;break;
case 3:f[j-1]=f[j-1]*d;d=0;flag=1;j--;break;
case 4:if(d==0){c=38;}
f[j-1]=f[j-1]/d;d=0;flag=1;j--;break;
}if(!flag1){j++;}flag1=0;
break;
case 0xed:if(flag1){shuchu(11);shuchu(5);a=4;d=10*d+a;flag1=0;}else{shuchu(5);a=4;d=10*d+a;}break;
case 0xdd:
if(flag1){shuchu(11);shuchu(6);a=5;d=10*d+a;flag1=0;}else{shuchu(6);a=5;d=10*d+a;}break;
case 0xbd:if(flag1){shuchu(11);shuchu(7);a=6;d=10*d+a;flag1=0;}else{shuchu(7);a=6;d=10*d+a;}break;
case 0x7d:shuchu(14);switch(flag){case 0:f[j]=d;d=0;flag=2;break;
case 1:f[j]=d;d=0;flag=2;break;
case 2:f[j]=-d;d=0;flag=2;break;
case 3:f[j-1]=f[j-1]*d;d=0;flag=2;j--;break;
case 4:if(d==0){c=38;}f[j-1]=f[j-1]/d;d=0;flag=2;j--;break;
}if(!flag1){j++;}flag1=0;
break;
case 0xeb:if(flag1){shuchu(11);shuchu(2);a=1;d=10*d+a;flag1=0;}else{shuchu(2);a=1;d=10*d+a;}break;
case 0xdb:if(flag1){shuchu(11);shuchu(3);a=2;d=10*d+a;flag1=0;}else{shuchu(3);a=2;d=10*d+a;}break;
case 0xbb:if(flag1){shuchu(11);shuchu(4);a=3;d=10*d+a;flag1=0;}else{shuchu(4);a=3;d=10*d+a;}break;
case 0x7b:shuchu(12);switch(flag){case 0:f[j]=d;d=0;flag=3;break;
case 1:f[j]=d;d=0;flag=3;break;
case 2:f[j]=-d;d=0;flag=3;break;
case 3:f[j-1]=f[j-1]*d;d=0;flag=3;j--;break;
case 4:if(d==0){c=38;}f[j-1]=f[j-1]/d;d=0;flag=3;j--;break;
}if(!flag1){j++;}flag1=0;
break;
case 0xe7:shuchu(16);break;
case 0xd7:if(flag1){shuchu(11);shuchu(1);a=0;d=10*d+a;flag1=0;}else{shuchu(1);a=0;d=10*d+a;}break;
case 0xb7:shuchu(15);
switch(flag){case 0:f[j]=d;d=0;flag=0;break;
case 1:f[j]=d;d=0;flag=0;break;
case 2:f[j]=-d;d=0;flag=0;break;
case 3:f[j-1]=f[j-1]*d;d=0;flag=0;j--;break;
case 4:if(d==0){c=38;}f[j-1]=f[j-1]/d;d=0;flag=0;j--;break;
}
for(j=0;j<10;j++){jg=jg+f[j];}shuchu1(jg);
for(j=0;j<10;j++){f[j]=0;}
f[0]=jg;
d=0;
jg=0;
flag=0;
flag1=1;j=1;
break;
case 0x77:shuchu(13);switch(flag){case 0:f[j]=d;d=0;flag=4;break;
case 1:f[j]=d;d=0;flag=4;break;
case 2:f[j]=-d;d=0;flag=4;break;
case 3:f[j-1]=f[j-1]*d;d=0;flag=4;j--;break;
case 4:if(d==0){c=38;}f[j-1]=f[j-1]/d;d=0;flag=4;j--;break;
}if(!flag1){j++;}flag1=0;
break;
}
}
void main()
{
init();
while (1)
{
jianpan();
}
}