51单片机简易计算器可算小数.docx
《51单片机简易计算器可算小数.docx》由会员分享,可在线阅读,更多相关《51单片机简易计算器可算小数.docx(12页珍藏版)》请在冰豆网上搜索。
51单片机简易计算器可算小数
51单片机简易计算器
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
sbitbeep=P2^3;
sbitdula=P2^6;
sbitwela=P2^7;
sbitrw=P1^1;
sbitrs=P1^0;
sbitlcden=P2^5;
voidyunsuan();
voidkeyinput(chars);
voidwrite_data(charddata);
voidwrite_com(charcommand);
voiddisplay(longa);
voidinit();
voiddealerror();
voiddataoverflow();
voidwelcome();
charcodetable1[]={0xee,0xde,0xbe,0x7e,
0xed,0xdd,0xbd,0x7d,
0xeb,0xdb,0xbb,0x7b,
0xe7,0xd7,0xb7,0x77};
charcodetable2[]="789/456*123-.0=+";
charj,k,temp,temp1,temp2,key,mchar,m,flag2=0,flag3=0;
longx,y,num;
intoperators,input,iny;
charerror[5]="error";
charoverflow[8]="overflow";
charwelcome_[16]="welcometouse!
";
voiddelay(uintx)//延时
{
uinta,b;
for(a=x;a>0;a--)
for(b=10;b>0;b--);
}
voiddelay_ms(uintx)//延时
{
uinta,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
voidwrite_com(ucharcom)//写命令
{
P0=com;
rs=0;
lcden=0;
delay(10);
lcden=1;
delay(10);
lcden=0;
}
voidwrite_data(uchardate)//写数据
{
P0=date;
rs=1;
lcden=0;
delay(10);
lcden=1;
delay(10);
lcden=0;
}
longming(intx)
{
longm;
switch(x)
{
case9:
m=1000000000;returnm;break;
case8:
m=100000000;returnm;break;
case7:
m=10000000;returnm;break;
case6:
m=1000000;returnm;break;
case5:
m=100000;returnm;break;
case4:
m=10000;returnm;break;
case3:
m=1000;returnm;break;
case2:
m=100;returnm;break;
case1:
m=10;returnm;break;
case0:
m=1;returnm;break;
}
}
voiddisplay(longa)
{
longd;
inti,flag1=0,temp,c=-1;
init();
if(a<0)
{
a=a*c;
write_data('-');
}
for(i=9;i>=0;i--)
{
d=ming(i);
temp=a/d;
a=a%d;
if((temp==0)&&(flag1==0))
;
else
{
write_data(0x30+temp);
flag1=1;
}
if(i==2)
{
write_data('.');
flag1=1;
}
}
}
voidinit()//LCD初始化
{
rw=0;
dula=0;
wela=0;
write_com(0x38);//显示模式设置:
16×2显示,5×7点阵,8位数据接口
delay(20);
write_com(0x0e);//显示模式设置
delay(20);
write_com(0x06);//显示模式设置:
光标右移,字符不移
delay(20);
write_com(0x01);//清屏幕指令,将以前的显示内容清除
delay(20);
}
voidyunsuan()//运算
{
if(iny)
{
switch(operators)
{
case1:
x=x+y;
num=x;
if(num<10000000000&&num>-10000000000)
{
display(num);
}
else
dataoverflow();
break;
case2:
x=x-y;
num=x;
if(num<10000000000&&num>-10000000000)
{
display(num);
}
else
dataoverflow();
break;
case3:
x=x*y;
num=x/100;
if(num<10000000000&&num>-10000000000)
{
display(num);
}
else
dataoverflow();
break;
case4:
if(y==0)
dealerror();
else
{
if(y<100)
{
x=x*100;
y=y*100;
}
y=y/100;
x=x/y;
num=x;
if(num<10000000000&&num>-10000000000)
{
display(num);
}
else
dataoverflow();
}
break;
}
y=0;
}
}
voiddealerror()//除数为0
{
inti=0;
write_com(0x01);
for(i=0;i<5;i++)
write_data(error[i]);
}
voiddataoverflow()//数值溢出
{
inti=0;
write_com(0x01);
for(i=0;i<8;i++)
write_data(overflow[i]);
}
voidwelcome()//欢迎界面
{
inti=0;
write_com(0x01);
for(i=0;i<16;i++)
write_data(welcome_[i]);
}
voidkeyinput(chars)//键盘输入
{
if(s<='9'&&s>='0')//判断按下的键是否为数值
{
if(flag3==0)
num=num*10+100*(s-'0');
else
num=num*10+10*(s-'0');
if(flag2==1)
{
num=num/10;
flag3=1;
}
if(operators>0)
{
y=num;
iny=1;}
else
x=num;
if(num<10000000000&&num>-10000000000)//当前数值是否超出限定范围
display(num);
else
dataoverflow();
}
else
switch(s)
{
case'.':
/*iny=0;
operators=0;*/
flag2=1;
break;
case'=':
write_data(table2[14]);
delay(10);
yunsuan();
iny=0;
operators=0;
num=0;
flag2=0;
flag3=0;
break;
case'+':
if(operators)
yunsuan();
operators=1;
write_data(table2[15]);
num=0;
flag2=0;
flag3=0;
break;
case'-':
if(operators)
yunsuan();
operators=2;
write_data(table2[11]);
num=0;
flag2=0;
flag3=0;
break;
case'*':
if(operators)
yunsuan();
operators=3;
write_data(table2[7]);
num=0;
flag2=0;
flag3=0;
break;
case'/':
if(operators)
yunsuan();
operators=4;
write_data(table2[3]);
num=0;
flag2=0;
flag3=0;
break;
}
}
voidmain()//主函数
{
chari;
charflag0=1;
init();
welcome();
for(i=0;i<10;i++)
delay_ms(100);
init();
while
(1)
{
P3=0xf0;
temp1=P3;
P3=0x0f;
temp2=P3;
temp=temp1|temp2;
while(P3!
=0x0f)
{
beep=0;
}
beep=1;
for(i=0;i<16;i++)
{
if(temp==table1[i])
{
m=table2[i];
flag0=0;
}
}
if(flag0==0)
{
keyinput(m);
flag0=1;
}
}
}