#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
sbit lcden=P2^7;
sbit rs=P2^6;// 数据/命令选择端
sbit rw=P2^5; // 读或写
sbit busy=P0^7;// 忙或闲 标志
char i,j,temp,num,num1;//字符标志位
long a,b,c; //a,第一个数 b,第二个数 c,差
//float a_c,b_c;
uchar flag,fuhao;//flag表示是否有符号键按下,fuhao表征按下的是哪个符号
uchar code table[]={ 7,8,9,0, 4,5,6,0, 1,2,3,0, 0,0,0,0};
uchar code table1[]={ 7,8,9,0x2f-0x30, //按键标值 0x2f-0x30是将字符转化为数字,
4,5,6,0x2a-0x30, //与后面的0x30+i抵消,得i值
1,2,3,0x2d-0x30,
0x01-0x30,0,0x3d-0x30,0x2b-0x30};//0x01是清屏命令
void delay(uchar z) // 延迟函数
{
uchar y;
for(z;z>0;z--) //延时z*y
for(y=0;y<110;y++);
}
void check() // 判断忙或空闲 就是是否有按键按下
{
do
{
P0=0xFF;
rs=0; //指令
rw=1; //读rs rw 值为01时,忙标志和地址计数器读出
lcden=0; //E=0,禁止读写
delay(10); //等待,液晶显示器处理数据
lcden=1; //E=1,允许读写
}
while(busy==1); //判断是否为空闲,1为忙,0为空闲
}
void write_com(uchar com) // 写指令函数
{
P0=com; //com指令付给P0口
delay(10);
rs=0;//0 0 写指令 无输出
rw=0;
lcden=0;//关闭lcd使能
check();//检验按键
lcden=1;//开启lcd使能
}
void write_data(uchar date)
{
P0=date;
delay(10);
rs=1;
rw=0;
lcden=0;
check();
lcden=1;
}
void init()
{
num=-1;
lcden=1;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x80);
write_com(0x01);
// num_1=0;
i=0;
j=0;
a=0;
b=0;
c=0;
flag=0;
fuhao=0;
}
void keyscan()
{
P1=0xfe;
if(P1!=0xfe)
{
delay(20);
if(P1!=0xfe)
{
temp=P1&0xf0;
switch(temp)
{
case 0xe0:num=0;break;
case 0xd0:num=1;break;
case 0xb0:num=2;break;
case 0x70:num=3;break;
}
}
while(P1!=0xfe);
if(num==0||num==1||num==2)
{
if(j!=0)
{
write_com(0x01);
j=0;
}
if(flag==0)
{
a=a*10+table[num];
}
else
{
b=b*10+table[num];
}
}
else
{
flag=1;
fuhao=4;
}
i=table1[num];
write_data(0x30+i);
}
P1=0xfd;
if(P1!=0xfd)
{
delay(20);
if(P1!=0xfd)
{
temp=P1&0xf0;
switch(temp)
{
case 0xe0:num=4;break;
case 0xd0:num=5;break;
case 0xb0:num=6;break;
case 0x70:num=7;break;
}
}
while(P1!=0xfd);
if(num==4||num==5||num==6&&num==7)
{
if(j!=0)
{
write_com(0x01);
j=0;
}
if(flag==0)
{
a=a*10+table[num];
}
else
{
b=b*10+table[num];
}
}
else
{
flag=1;
fuhao=3;
}
i=table1[num];
write_data(0x30+i);
}
P1=0xfb;
if(P1!=0xfb)
{
delay(20);
if(P1!=0xfb)
{
temp=P1&0xf0;
switch(temp)
{
case 0xe0:num=8;break;
case 0xd0:num=9;break;
case 0xb0:num=10;break;
case 0x70:num=11;break;
}
}
while(P1!=0xfb);
if(num==8||num==9||num==10)
{
if(j!=0)
{
write_com(0x01);
j=0;
}
if(flag==0)
{
a=a*10+table[num];
}
else
{
b=b*10+table[num];
}
}
else if(num==11)
{
flag=1;
fuhao=2;
}
i=table1[num];
write_data(0x30+i);
}
P1=0xf7;
if(P1!=0xf7)
{
delay(20);
if(P1!=0xf7)
{
temp=P1&0xf0;
switch(temp)
{
case 0xe0:num=12;break;
case 0xd0:num=13;break;
case 0xb0:num=14;break;
case 0x70:num=15;break;
}
}
while(P1!=0xf7);
switch(num)
{
case 12:
{
write_com(0x01);
a=0;
b=0;
flag=0;fuhao=0;
}
break;
case 13:
{
if(flag==0)
{
a=a*10;
write_data(0x30);
P1=0;
}
else if(flag==1)
{
b=b*10;
write_data(0x30);
}
}
break;
case 14:
{
j=1;
if(fuhao==1)
{
c=a+b;
write_com(0xcf);
write_com(0x04);
while(c!=0)
{
write_data(0x30+c%10);
c=c/10;
}
write_data(0x3d); //=号
a=0;
b=0;
flag=0;
fuhao=0;
}
if(fuhao==2)
{
if(a-b>0) c=a-b;
else c=b-a;
write_com(0xcf);
write_com(0x04);
while(c!=0)
{
write_data(0x30+c%10);
c=c/10;
}
if(a-b<0)
write_data(0x2d);
write_data(0x3d);
a=0;
b=0;
flag=0;
fuhao=0;
}
if(fuhao==3)
{
c=a*b;
write_com(0xcf);
write_com(0x04);
while(c!=0)
{
write_data(0x30+c%10);
c=c/10;
}
write_data(0x3d);
a=0;
b=0;
flag=0;
fuhao=0;
}
else if(fuhao==4)
{
i=0;
c=(long)(((float)a/b)*1000);
write_com(0xcf);
write_com(0x04);
while(c!=0)
{
write_data(0x30+c%10);
c=c/10;
i++;
if(i==3)
write_data(0x2e);//液晶.
}
if(a/b<=0)
write_data(0x30);
write_data(0x3d);
a=0;
b=0;
flag=0;
fuhao=0;
}
}
break;
case 15:
{
write_data(0x30+table1[num]);
flag=1;
fuhao=1;
break;
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
}
} |