基于51单片机1602液晶显示简易计算器设计Word格式.docx
《基于51单片机1602液晶显示简易计算器设计Word格式.docx》由会员分享,可在线阅读,更多相关《基于51单片机1602液晶显示简易计算器设计Word格式.docx(22页珍藏版)》请在冰豆网上搜索。
);
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'
n'
ln=1;
}break;
case2:
s'
i'
Sin=1;
case3:
c'
o'
Cos=1;
case4:
t'
a'
Tan=1;
case5:
^'
flag=5;
case6:
write_Dat('
7'
num=7;
break;
case7:
8'
num=8;
case8:
9'
num=9;
case9:
+'
flag=1;
case10:
T'
flag=6;
case11:
4'
num=4;
case12:
5'
num=5;
case13:
6'
num=6;
case14:
-'
flag=2;
case15:
if(equal_flag==1)
{
write_com(0x01);
write_com(0x80);
write_Dat('
A'
='
save_result=result;
}
else
if(Key_num!
=1&
&
Key_num!
=2&
=3&
=4&
=22&
=23&
=25)
{
write_Dat('
if(flag==0)
{
num1=save_result;
first_num=1;
}
else
num2=save_result;
second_num=1;
}
}
}
break;
case16:
1'
num=1;
case17:
2'
num=2;
case18:
3'
num=3;
case19:
*'
flag=3;
case21:
num=0;
case22:
if(flag==0)
minus1++;
if(minus1==1)
minus2++;
if(minus2==1)
}break;
case23:
{
dot1++;
if(dot1==1)
.'
dot2++;
if(dot2==1)
case24:
if(last_key_num!
/'
flag=4;
case25:
{
write_Dat('
equal_flag=1;
}break;
if(Key_num!
=15&
=25)//第一个数
if(flag==0)
{
num1=num1*10+num;
num=0;
if(dot1!
=0)
dot1_num++;
}
first_num=1;
}
if(flag!
num2=num2*10+num;
if(dot2!
dot2_num++;
second_num=1;
if(equal_flag==1&
first_num==1)
{
if(dot1!
num1=num1/pow(10,dot1_num);
dot1=0;
if(dot2!
num2=num2/pow(10,dot2_num);
dot2=0;
if(minus1!
num1=num1*(-1);
if(minus2!
num2=num2*(-1);
if(second_num!
switch(flag)
case1:
result=num1+num2;
case2:
result=num1-num2;
case3:
result=num1*num2;
case4:
if(num2!
{
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);
case6:
result=pow(num1,1/num2);
elseif(second_num==0&
Sin==0&
Cos==0&
Tan==0&
ln==0)
result=num1;
(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;
write_com(0x80+0x40+10);
for(i=0;
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(0x80+0x40+15);
write_Dat('
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;
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&
P3=0x03;
//第一行
P1=0xfe;
temp=(P3<
switch(temp)
{
case0x03de:
key_num=1;
break;
case0x03be:
key_num=2;
case0x037e:
key_num=3;
case0x02fe:
key_num=4;
case0x01fe:
key_num=5;
//第二行
P1=0xfd;
case0x03dd:
key_num=6;
case0x03bd:
key_num=7;
case0x037d:
key_num=8;
case0x02fd:
key_num=9;
case0x01fd:
key_num=10;
//第三行
P1=0xfb;
case0x03db:
key_num=11;
case0x03bb:
key_num=12;
case0x037b:
key_num=13;
case0x02fb:
key_num=14;
case0x01fb:
key_num=15;
//第四行
P1=0xf7;
case0x03d7:
key_num=16;
case0x03b7:
key_num=17;
case0x0377:
key_num=18;
case0x02f7:
key_num=19;
case0x01f7:
key_num=20;
//第五行
P1=0xef;
case0x03cf:
key_num=21;
case0x03af:
key_num=22;
case0x036f:
key_num=23;
case0x02ef:
key_num=24;
case0x01ef:
key_num=25;
returnkey_num;
voiddisplay(doubleData)
ucharxs=0,fs=0,i;
if(Data<
fs=1;
Data=Data*(-1);
if(Data>
(long)Data)
xs=1;
10)//0~9
if(xs==1)
Data=Data*100;
write_com(0x80+0x40+11);
if(fs==1)
else
'
write_Dat((long)Data/100+'
write_Dat((long)Data/10%10+'
write_Dat((long)Data%10+'
elseif(xs==0)
write_com(0x80+0x40+14);
write_Dat((long)Data+'
elseif(Data<
100)//10~99
write_com(0x80+0x40+10);
write_Dat((long)Data/1000+'
write_Dat((long)Data/100%10+'
write_com(0x80+0x40+13);
write_Dat((long)Data/10+'
1000)//100~999
write_com(0x80+0x40+9);
write_Dat((long)Data/10000+'
write_Dat((long)Data/1000%10+'
write_com(0x80+0x40+12);
els