资源描述
#include <reg51.h>
#include<intrins.h>
#include<math.h>
#include<stdlib.h>
sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
sbit BUZZER=P1^0;
#define unchar unsigned char
#define unint unsigned int
void delay(unsigned int t)
{
unsigned int i,j;
for(i = 0;i < t;i ++)
for(j = 0;j < 120;j ++);
}
void busy() //判断忙状态
{
RS=0;
RW=1;
E=1;
P0=0xff;
while((P0&0X80)==0x80);
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();
}
void write_com( unsigned char com)
{ _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();
busy();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();
RS=0;
RW=0;
E=1;
P0=com;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();
E=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();
}
void write_date(unchar date)
{ _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();
busy();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();
RS=1;
RW=0;
E=1;
P0=date;
E=0;
}
void init() //液晶初始化
{
write_com(0x30); //选择基本指令,选择8位数据流
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();
write_com(0x0c); //开显示(无游标,不反白) 0x0e显示游标
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();
write_com(0x01); //清除显示,并且设定地址指针为00H
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();
write_com(0x06);// 设定游标移动方向
}
void sendaddr(unchar n,unchar m) //送地址
{
switch(n)
{
case 1:write_com(0x80+m);break;
case 2:write_com(0x90+m);break;
case 3:write_com(0x88+m);break;
case 4:write_com(0x98+m);break;
}
}
void display(unchar n,unchar m,unchar *s) //显示字符串
{
sendaddr(n,m);
while(*s>0)
{
write_date(*s);
s++;
}
}
void clearscreen() //清屏
{
write_com(0x01);
}
void tishi()
{
clearscreen();
display(1,0,"超出计算范围");
display(2,0,"请按复位键");
display(3,0,"并重新输入");
display(4,0,"否则会出现错误");
}
void displayshu(unchar n,unchar m, long rzt) //显示字符串
{ unchar i,j,k,x,p;
unchar t[10]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char array[10];
array[9]=(int)rzt%10;
array[8]=((int)rzt/10)%10;
array[7]=((int)rzt/100)%10;
array[6]=((int)rzt/1000)%10;
array[5]=((int)rzt/10000)%10;
array[4]=((int)rzt/100000)%10;
array[3]=((int)rzt/1000000)%10;
array[2]=((int)rzt/10000000)%10;
array[1]=((int)rzt/100000000)%10;
array[0]=(int)rzt/1000000000;
sendaddr(n,m);
for(i=0;i<10;i++)
{
if(array[i]==0)
p=i;
else
break;
}
for(j=p+1;j<10;j++)
{
t[k]=array[j] ;
k++;
}
while(t[x]!=0xff)
{
write_date(t[x]+0x30);
x++;
}
}
void main(void)
{
unchar i,j,opt,p;
long b,c,rzt;
start:
init();
delay(20);
i=0;j=0;rzt=0;b=0;c=0;
write_com(0x80);
while(1) //输入第一个数
{
P3=0xfe;
if(P3!=0xfe)
{
delay(40);
if(P3!=0xfe)
{
switch(P3&0xf0)
{
case 0x70:BUZZER=0;delay(80);BUZZER=1;write_date(0+0x30);b=b*10+0;i++;if(i==11)tishi(); break;
case 0xb0:BUZZER=0;delay(80);BUZZER=1;write_date(1+0x30);b=b*10+1;i++;if(i==11)tishi(); break;
case 0xd0:BUZZER=0;delay(80);BUZZER=1;write_date(2+0x30);b=b*10+2;i++;if(i==11)tishi(); break;
case 0xe0:BUZZER=0;delay(80);BUZZER=1;write_date(3+0x30);b=b*10+3;i++;if(i==11)tishi(); break;
}
if(P3!=0xfe);
delay(30);
while(P3!=0xfe);
}
}
P3=0xfd;
if(P3!=0xfd)
{
delay(40);
if(P3!=0xfd)
{
switch(P3&0xf0)
{
case 0x70:BUZZER=0;delay(80);BUZZER=1;write_date(4+0x30);b=b*10+4;i++;if(i==11)tishi(); break;
case 0xb0:BUZZER=0;delay(80);BUZZER=1;write_date(5+0x30);b=b*10+5;i++;if(i==11)tishi(); break;
case 0xd0:BUZZER=0;delay(80);BUZZER=1;write_date(6+0x30);b=b*10+6;i++;if(i==11)tishi(); break;
case 0xe0:BUZZER=0;delay(80);BUZZER=1;write_date(7+0x30);b=b*10+7;i++;if(i==11)tishi(); break;
}
if(P3!=0xfd);
delay(30);
while(P3!=0xfd);
}
}
P3=0xfb;
if(P3!=0xfb)
{
delay(40);
if(P3!=0xfb)
{
switch(P3&0xf0)
{
case 0x70:BUZZER=0;delay(80);BUZZER=1;write_date(8+0x30);b=b*10+8;i++;if(i==11) tishi(); break;
case 0xb0:BUZZER=0;delay(80);BUZZER=1;write_date(9+0x30);b=b*10+9;i++;if(i==11) tishi(); break;
case 0xd0:BUZZER=0;delay(80);BUZZER=1;p=1;write_com(0x97);write_date(0x2b);opt=10; goto two; break;
case 0xe0:BUZZER=0;delay(80);BUZZER=1;p=1;write_com(0x97);write_date(0x2d);opt=11; goto two; break;
}
if(P3!=0xfb);
delay(30);
while(P3!=0xfb);
}
}
P3=0xf7;
if(P3!=0xf7)
{
delay(40);
if(P3!=0xf7)
{
switch(P3&0xf0)
{
case 0x70:BUZZER=0;delay(80);BUZZER=1;p=1;write_com(0x97);write_date(0x2a);opt=12; goto two; break;
case 0xb0:BUZZER=0;delay(80);BUZZER=1;p=1;write_com(0x97);write_date(0x2f);opt=13; goto two; break;
case 0xd0:BUZZER=0;delay(80);BUZZER=1; clearscreen();goto start;break;
case 0xe0:BUZZER=0;delay(80);BUZZER=1; clearscreen();goto start;break;
}
if(P3!=0xf7);
delay(30);
while(P3!=0xf7);
}
}
}
two:
write_com(0x88);
while(1) //输入第二个数
{
P3=0xfe;
if(P3!=0xfe)
{
delay(40);
if(P3!=0xfe)
{
switch(P3&0xf0)
{
case 0x70:BUZZER=0;delay(80);BUZZER=1;write_date(0+0x30);c=c*10+0;j++;if(j==11)tishi(); break;
case 0xb0:BUZZER=0;delay(80);BUZZER=1;write_date(1+0x30);c=c*10+1;j++;if(j==11)tishi(); break;
case 0xd0:BUZZER=0;delay(80);BUZZER=1;write_date(2+0x30);c=c*10+2;j++;if(j==11)tishi(); break;
case 0xe0:BUZZER=0;delay(80);BUZZER=1;write_date(3+0x30);c=c*10+3;j++;if(j==11)tishi(); break;
}
if(P3!=0xfe);
delay(30);
while(P3!=0xfe);
}
}
P3=0xfd;
if(P3!=0xfd)
{
delay(40);
if(P3!=0xfd)
{
switch(P3&0xf0)
{
case 0x70:BUZZER=0;delay(80);BUZZER=1;write_date(4+0x30);c=c*10+4;j++;if(j==11)tishi();break;
case 0xb0:BUZZER=0;delay(80);BUZZER=1;write_date(5+0x30);c=c*10+5;j++;if(j==11)tishi();break;
case 0xd0:BUZZER=0;delay(80);BUZZER=1;write_date(6+0x30);c=c*10+6;j++;if(j==11)tishi();break;
case 0xe0:BUZZER=0;delay(80);BUZZER=1;write_date(7+0x30);c=c*10+7;j++;if(j==11)tishi();break;
}
if(P3!=0xfd);
delay(30);
while(P3!=0xfd);
}
}
P3=0xfb;
if(P3!=0xfb)
{
delay(40);
if(P3!=0xfb)
{
switch(P3&0xf0)
{
case 0x70:BUZZER=0;delay(80);BUZZER=1;write_date(8+0x30);c=c*10+8;j++;if(j==11)tishi();
break;
case 0xb0:BUZZER=0;delay(80);BUZZER=1;write_date(9+0x30);c=c*10+9;j++;if(j==11)tishi();
break;
case 0xd0:BUZZER=0;delay(80);BUZZER=1;if(p==0){ write_com(0x97); write_date(0x2b);}
break;
case 0xe0:BUZZER=0;delay(80);BUZZER=1;if(p==0){ write_com(0x97); write_date(0x2d);}
break;
}
if(P3!=0xfb);
delay(30);
while(P3!=0xfb);
}
}
P3=0xf7;
if(P3!=0xf7)
{
delay(40);
if(P3!=0xf7)
{
switch(P3&0xf0)
{
case 0x70:BUZZER=0;delay(80);BUZZER=1;if(p==0){ write_com(0x97); write_date(0x2a);} break;
case 0xb0:BUZZER=0;delay(80);BUZZER=1;if(p==0){ write_com(0x97); write_date(0x2f);} break;
case 0xd0:BUZZER=0;delay(80);BUZZER=1;goto answer;break;
case 0xe0:BUZZER=0;delay(80);BUZZER=1; clearscreen();goto start;break;
}
if(P3!=0xf7);
delay(30);
while(P3!=0xf7);
}
}
}
answer:
write_com(0x98);
write_date(0x3d);
if(c==0&&opt==13) //错误处理
{
display(2,0,"分母不能为零");clearscreen();goto start;
}
switch(opt)
{
case 10:rzt=(b+c);displayshu(4,2,rzt);break;
case 11:rzt=(b-c);if(rzt<0){rzt=c-b;write_date(0x2d);} displayshu(4,2,rzt);break;
case 12:rzt=(b*c);displayshu(4,2,rzt);break;
case 13:rzt=(b/c);
if((int)b%(int)c==0)
displayshu(4,2,rzt);
else
rzt=rzt*1000;
displayshu(4,2,rzt);
break;
}
//错误处理与显示
if(rzt>9999999999)
{
clearscreen();
display(2,1,"超出计算范围");
delay(1000);
goto start;
}
while(1)
{
P3=0xf7;
if(P3!=0xf7)
{
delay(40);
if(P3!=0xf7)
{
if((P3&0xf0)==0xe0)
{
goto start;
}
if(P3!=0xf7);
delay(30);
while(P3!=0xf7);
}
}
}
}
展开阅读全文