#include
#define uchar unsigned char
#define uint unsigned int
sbit rs=P2^6;
sbit rw=P2^5;
sbit en=P2^7;
sbit k1=P3^0;
sbit k2=P3^1;
sbit k3=P3^2;
sbit k4=P3^3;
sbit k5=P3^4;
sbit k6=P3^5;
sbit k7=P3^6;
sbit k8=P3^7;
uchar t,k;
uint a,b,c;
uchar code table[]={+,-,*,/};
void delayms(int z)
{
int x,y;
for(x=z;x>0;x–)
for(y=110;y>0;y–);
}
void write_com(uchar com)
{
rs=0;
rw=0;
en=0;
P0=com;
delayms(10);
en=1;
delayms(10);
en=0;
delayms(2);
}
void write_dat(uchar dat)
{
rs=1;
rw=0;
en=0;
P0=dat;
delayms(10);
en=1;
delayms(10);
en=0;
delayms(2);
}
void write_zfc(uchar *ch)
{
while(*ch!=0)
write_dat(*ch++);
}
void init_1602()
{
write_com(0x38);
delayms(5);
write_com(0x0c);
delayms(5);
write_com(0x04);
delayms(5);
write_com(0x01);
delayms(5);
write_com(0x80+1);
delayms(5);
write_zfc(“input =”);
delayms(5);
write_com(0x80+0x40+2);
delayms(5);
write_zfc(“output “);
delayms(5);
}
void keyscan()
{
uchar bai,shi,ge;
if(k1==0)
{
delayms(5);
while(!k1);
if(t==0)
{
write_com(0x80+8);
delayms(5);
write_com(0x0f);
delayms(5);
}
if(t==1)
{
write_com(0x80+9);
delayms(5);
write_com(0x0f);
delayms(5);
}
if(t==2)
{
write_com(0x80+11);
delayms(5);
write_com(0x0f);
delayms(5);
}
t++;
if(t>2)
t=0;
}
if(k2==0)
{
delayms(5);
while(!k2);
write_com(0x0c);
t=0;
switch(k)
{
case 1:c=a+b;break;
case 2:c=a-b;break;
case 3:c=a*b;break;
default :c=a/b;break;
}
bai=c/100;
shi=(c-bai*100)/10;
ge=c;
write_com(0x80+0x40+9);
delayms(5);
write_dat(0x30+bai);
delayms(5);
write_com(0x80+0x40+10);
delayms(5);
write_dat(0x30+shi);
delayms(5);
write_com(0x80+0x40+11);
delayms(5);
write_dat(0x30+ge);
delayms(5);
}
if(k3==0)
{
delayms(5);
while(!k3);
switch(t)
{
case 1:a++;
shi=a/10;
ge=a;
write_com(0x80+7);
delayms(5);
write_dat(0x30+shi);
delayms(5);
write_com(0x80+8);
delayms(5);
write_dat(0x30+ge);
delayms(5);
write_com(0x80+8);
break;
case 2:
write_com(0x80+9);
delayms(5);
write_dat(table[k]);
write_com(0x80+9);
k++;
if(k>3)
k=0;
break;
default :b++;
shi=b/10;
ge=b;
write_com(0x80+10);
delayms(5);
write_dat(0x30+shi);
delayms(5);
write_com(0x80+11);
delayms(5);
write_dat(0x30+ge);
delayms(5);
write_com(0x80+11);
break;
}
}
if(k4==0)
{
delayms(5);
while(!k4);
switch(t)
{
case 1:a–;
shi=a/10;
ge=a;
write_com(0x80+7);
delayms(5);
write_dat(0x30+shi);
delayms(5);
write_com(0x80+8);
delayms(5);
write_dat(0x30+ge);
delayms(5);
write_com(0x80+8);
break;
case 2:
write_com(0x80+9);
delayms(5);
write_dat(table[k]);
write_com(0x80+9);
k++;
if(k>3)
k=0;
break;
default :b–;
shi=b/10;
ge=b;
write_com(0x80+10);
delayms(5);
write_dat(0x30+shi);
delayms(5);
write_com(0x80+11);
delayms(5);
write_dat(0x30+ge);
delayms(5);
write_com(0x80+11);
break;
}
}
}
void main()
{
init_1602();
while(1)
{
keyscan();
}
}