基于51单片机1602液晶显示简易计算器设计.docx
- 文档编号:29622712
- 上传时间:2023-07-25
- 格式:DOCX
- 页数:28
- 大小:91.94KB
基于51单片机1602液晶显示简易计算器设计.docx
《基于51单片机1602液晶显示简易计算器设计.docx》由会员分享,可在线阅读,更多相关《基于51单片机1602液晶显示简易计算器设计.docx(28页珍藏版)》请在冰豆网上搜索。
基于51单片机1602液晶显示简易计算器设计
#include
#include
#defineuintunsignedint
#defineucharunsignedchar
#definePI3.141592
sbitRS=P2^0;
sbitRW=P2^1;
sbitEN=P2^2;
sbitled=P2^4;
sbitspeek=P3^7;
uchartable0[]={"Welcometouse"};
uchartable1[]={"madebyMs.Li"};
uchartable2[]={"error"};
ucharcount;
voidmain(void)
{
ucharerror=0,i,first=0,dot1,dot2,dot1_num,dot2_num,minus1,minus2;//错误标
志、第一次清屏标记、小数点标记以及小数点个数负号标记、负号个数
ucharSin,Cos,Tan,ln;
ucharKey_num,last_key_num;
//键号
ucharflag=0,equal_flag;
doublenum1=0,num2=0,num=0,result=0,save_result;
//运算符、等于符
//第一个数、第二个
数、计算结果
ucharfirst_num=0,Ans=0,second_num=0;
InitLcd();
EA=1;
ET0=1;
TMOD=0X01;
TH0=(65536-500)/256;
TL0=(65536-500)%256;
write_com(0x80+0x40+15);
write_Dat('0');
write_com(0x80);
while
(1)
{
while(key_scan()==0xff);
TR0=1;
if(first==0)
{
first=1;
write_com(0x01);
}
Key_num=key_scan();
switch(key_scan())
{
case1:
if(last_key_num!
=Key_num)
{
write_Dat('l');
write_Dat('n');
ln=1;
}break;
case2:
if(last_key_num!
=Key_num)
{
write_Dat('s');
write_Dat('i');
write_Dat('n');
Sin=1;
}break;
case3:
if(last_key_num!
=Key_num)
{
write_Dat('c');
write_Dat('o');
write_Dat('s');
Cos=1;
}break;
case4:
if(last_key_num!
=Key_num)
{
write_Dat('t');
write_Dat('a');
write_Dat('n');
Tan=1;
}break;
case5:
if(last_key_num!
=Key_num)
{
write_Dat('^');
flag=5;
}break;
case6:
write_Dat('7');num=7;break;
case7:
write_Dat('8');num=8;break;
case8:
write_Dat('9');num=9;break;
case9:
if(last_key_num!
=Key_num)
{
write_Dat('+');
flag=1;
}break;
case10:
if(last_key_num!
=Key_num)
{
write_Dat('T');
flag=6;
}break;
case11:
write_Dat('4');num=4;break;
case12:
write_Dat('5');num=5;break;
case13:
write_Dat('6');num=6;break;
case14:
if(last_key_num!
=Key_num)
{
write_Dat('-');
flag=2;
}break;
case15:
if(last_key_num!
=Key_num)
{
if(equal_flag==1)
{
write_com(0x01);
write_com(0x80);
write_Dat('A');
write_Dat('n');
write_Dat('s');
write_Dat('=');
save_result=result;
}
else
{
if(Key_num!
=1&&Key_num!
=2&&Key_num!
=3&&Key_num!
=4&&Key_num!
=22&&Key_num!
=23&&Key_num!
=25)
{
write_Dat('A');write_Dat('n');write_Dat('s');
if(flag==0)
{
num1=save_result;
first_num=1;
}
else
{
num2=save_result;
second_num=1;
}
}
}
}
break;
case16:
write_Dat('1');num=1;break;
case17:
write_Dat('2');num=2;break;
case18:
write_Dat('3');num=3;break;
case19:
if(last_key_num!
=Key_num)
{
write_Dat('*');
flag=3;
}break;
case21:
write_Dat('0');num=0;break;
case22:
if(last_key_num!
=Key_num)
{
if(flag==0)
{
minus1++;
if(minus1==1)
{
write_Dat('-');
}
}
else
{
minus2++;
if(minus2==1)
{
write_Dat('-');
}
}
}break;
case23:
if(last_key_num!
=Key_num)
{
if(flag==0)
{
dot1++;
if(dot1==1)
{
write_Dat('.');
}
}
else
{
dot2++;
if(dot2==1)
{
write_Dat('.');
}
}
}break;
case24:
if(last_key_num!
=Key_num)
{
write_Dat('/');
flag=4;
}break;
case25:
if(last_key_num!
=Key_num)
{
write_Dat('=');
equal_flag=1;
}break;
}
if(Key_num!
=1&&Key_num!
=2&&Key_num!
=3&&Key_num!
=4&&Key_num!
=15&&Key_num!
=22
&&Key_num!
=23&&Key_num!
=25)//第一个数
{
if(flag==0)
{
num1=num1*10+num;
num=0;
if(dot1!
=0)
{
dot1_num++;
}
first_num=1;
}
if(flag!
=0)
{
num2=num2*10+num;
num=0;
if(dot2!
=0)
{
dot2_num++;
}
second_num=1;
}
}
if(equal_flag==1&&first_num==1)
{
if(dot1!
=0)
{
num1=num1/pow(10,dot1_num);
dot1=0;
}
if(dot2!
=0)
{
num2=num2/pow(10,dot2_num);
dot2=0;
}
if(minus1!
=0)
{
num1=num1*(-1);
}
if(minus2!
=0)
{
num2=num2*(-1);
}
if(second_num!
=0)
{
switch(flag)
{
case1:
result=num1+num2;break;
case2:
result=num1-num2;break;
case3:
result=num1*num2;break;
case4:
if(num2!
=0)
{
result=num1/num2;
}
else
{
write_com(0x80+0x40+10);
for(i=0;i<5;i++)
{
write_Dat(table2[i]);
}
error=1;
}break;
case5:
result=pow(num1,num2);break;
case6:
result=pow(num1,1/num2);break;
}
}
elseif(second_num==0&&Sin==0&&Cos==0&&Tan==0&&ln==0)
result=num1;
elseif(second_num==0&&(Sin==1||Cos==1||Tan==1||ln==1))
{
if(Sin==1)
result=sin(num1/180*PI)+0.005;
if(Cos==1)
result=cos(num1/180*PI)+0.005;
if(Tan==1)
result=tan(num1/180*PI)+0.005;
if(ln==1)
{
if(num1>0)
result=log(num1)+0.005;
else
{
write_com(0x80+0x40+10);
for(i=0;i<5;i++)
{
write_Dat(table2[i]);
}
error=1;
}
}
}
if(error!
=1)
display(result);
}
if(Key_num==20)
{
//清零
num1=num2=result=0;
flag=equal_flag=0;
error=0;first=0;
dot1_num=dot2_num=0;
minus1=0;minus2=0;
first_num=0,second_num=0;
Sin=Cos=Tan=ln=0;
write_com(0x01);
write_com(0x80+0x40+15);
write_Dat('0');
}
last_key_num=Key_num;
while(key_scan()!
=0xff);
}
}
voidtime0()interrupt1
{
TH0=(65536-600)/256;
TL0=(65536-600)%256;
count++;
speek=!
speek;
if(count>50)
{
TR0=0;
count=0;
}
}
voiddelay(uintx)
{
uchary;
while(x--)
for(y=0;y<120;y++);
}
voidwrite_com(unsignedcharc)
{
RS=0;
RW=0;
EN=1;
delay(5);
P0=c;
EN=0;
}
voidwrite_Dat(unsignedcharc)
{
RS=1;
RW=0;
EN=1;
delay(5);
P0=c;
EN=0;
}
voidInitLcd()
{
write_com(0x38);//displaymode
write_com(0x06);//write_com(0x0c);//write_com(0x01);//}
显示光标挪动地点
显示开及光标设置
显示清屏
ucharkey_scan()
{
ucharkey_num=0xff;
uinttemp;
P3=0x03;
P1=0xe0;
temp=(P3<<8)|P1;
if((temp&0x03e0)!
=0x03e0)
{
delay(10);
if((temp&0x03e0)!
=0x03e0)
{
P3=0x03;
P1=0xfe;
temp=(P3<<8)|P1;
switch(temp)
{
//第一行
case0x03de:
key_num=1;break;
case0x03be:
key_num=2;break;
case0x037e:
key_num=3;break;
case0x02fe:
key_num=4;break;
case0x01fe:
key_num=5;break;
}
P3=0x03;
P1=0xfd;
temp=(P3<<8)|P1;
switch(temp)
{
//第二行
case0x03dd:
key_num=6;break;
case0x03bd:
key_num=7;break;
case0x037d:
key_num=8;break;
case0x02fd:
key_num=9;break;
case0x01fd:
key_num=10;break;
}
P3=0x03;
P1=0xfb;
temp=(P3<<8)|P1;
switch(temp)
{
//第三行
case0x03db:
key_num=11;break;
case0x03bb:
key_num=12;break;
case0x037b:
key_num=13;break;
case0x02fb:
key_num=14;break;
case0x01fb:
key_num=15;break;
}
P3=0x03;
P1=0xf7;
//第四行
temp=(P3<<8)|P1;
switch(temp)
{
case0x03d7:
key_num=16;break;
case0x03b7:
key_num=17;break;
case0x0377:
key_num=18;break;
case0x02f7:
key_num=19;break;
case0x01f7:
key_num=20;break;
}
P3=0x03;//第五行
P1=0xef;
temp=(P3<<8)|P1;
switch(temp)
{
case0x03cf:
key_num=21;break;
case0x03af:
key_num=22;break;
case0x036f:
key_num=23;break;
case0x02ef:
key_num=24;break;
case0x01ef:
key_num=25;break;
}
}
}
returnkey_num;
}
voiddisplay(doubleData)
{
ucharxs=0,fs=0,i;
if(Data<0)
{
fs=1;
Data=Data*(-1);
}
if(Data>(long)Data)
{
xs=1;
}
if(Data<10)//0~9
{
if(xs==1)
{
Data=Data*100;
write_com(0x80+0x40+11);
if(fs==1)
write_Dat('-');
else
write_Dat('');
write_Dat((long)Data/100+'0');
write_Dat('.');
write_Dat((long)Data/10%10+'0');
write_Dat((long)Data%10+'0');
}
elseif(xs==0)
{
write_com(0x80+0x40+14);
if(fs==1)
write_Dat('-');
else
write_Dat('');
write_Dat((long)Data+'0');
}
}
elseif(Data<100)//10~99
{
if(xs==1)
{
Data=Data*100;
write_com(0x80+0x40+10);
if(fs==1)
write_Dat('-');
else
write_Dat('');
write_Dat((long)Data/1000+'0');
write_Dat((long)Data/100%10+'0');
write_Dat('.');
write_Dat((long)Data/10%10+'0');
write_Dat((long)Data%10+'0');
}
elseif(xs==0)
{
write_com(0x80+0x40+13);
if(fs==1)
write_Dat('-');
else
write_Dat('');
write_Dat((long)Data/10+'0');
write_Dat((long)Data%10+'0');
}
}
elseif(Data<1000)//100~999
{
if(xs==1)
{
Data=Data*100;
write_com(0x80+0x40+9);
if(fs==1)
write_Dat('-');
else
write_Dat('');
write_Dat((long)Data/10000+'0');
write_Dat((long)Data/1000%10+'0');
write_Dat((long)Data/100%10+'0');
write_Dat('.');
write_Dat((long)Data/10%10+'0');
write_Dat((long)Data%10+'0');
}
elseif(xs==0)
{
write_com(0x80+0x40+12);
if(fs==1)
write_Dat('-');
else
write_Dat('');
write_Dat((long)Data/100+'0');
write_Dat((long)Data/10%10+'0');
write_Dat((long)Data%10+'0');
}
}
elseif(Data<10000)//1000~9999
{
if(xs==1)
{
Data=Data*100;
write_com(0x80+0x40+8);
if(fs==1)
write_Dat('-');
else
write_Dat('');
write_Dat((long)Data/100000+'0');
write_Dat((long)Data/10000%10+'0');
write_Dat((long)Data/1000%10+'0');
write_Dat((long)Data/100%10+'0');
write_Dat('.');
write_Dat((long)Data/10%10+'0');
write_Dat((long)Data%10+'0');
}
elseif(xs==0)
{
write_com(0x80+0x40+11);
if(fs==1)
write_Dat('-');
else
write_Dat('');
write_Dat((long)Data/1000+'0');
write_Dat((long)Data/100%10+'0');
write_Dat((long)Data/10%10+'0');
write_Dat((long)Data%10+'0');
}
}
elseif(Data<100000)//10000~99999
{
if(xs==1)
{
Data=Data*100;
write_com(0x80+0x40+7);
if(fs==1)
write_Dat('-');
else
write_Dat('');
write_Dat((long)Data/1000000+'0');
write_Dat((long)Data/100000%10+'0');
write_Dat((long)Data/10000%10+'0');
write_Dat((long)Data/1000%10+'0');
write_Dat((long)Data/100%10+'0');
write_Dat('.');
write_Dat((long)Data/10%10+'0');
write_Dat((long)Data%10+'0');
}
elseif(xs==0)
{
write_com(0x80+0x40+10);
if(fs==1)
write_Dat('-');
else
write_Dat('');
write_Dat((long)Data/10000+'0');
write_Dat((long)Data/1000%10+'0');
write_Dat((long)Data/100%10+'0');
write_Dat((long)Data/10%10+'0');
write_Dat((long)Data%10+'0');
}
}
elseif(Data<1000000)//100000~999999
{
if(xs==1)
{
Data=Data*100;
write_com(0x80+0x40+7);
if(fs==1)
write_Dat('-');
else
write_Dat('');
write_Dat((long)Data/10000000+'0');
write_Dat((long)Data/1000000%10+'0');
write_Dat((long)Data/100000%10+'0');
write_Dat((long)Data/10000%10+'0');
write_Dat((long)Data/1000%10+'0');
write_Dat((long)Data/100%10+'0');
write_Dat('.');
write_Dat((long)Data/100000%10+'0');
write_Dat((long)Data/100000%10+'0');
write_Dat((long)Data/10000%10+'0');
write_Dat((long)Data/1000%10+'0');
w
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 基于 51 单片机 1602 液晶显示 简易 计算器 设计