悬赏
1
分 芯积分未解决
程序基本功能+-*/均可实现,只是在每次输入数字键7,4,1中任意一个时,1602显示屏显示的数字不是按地址加一一个挨一个显示,而是莫名其妙的中间有间隔,别的数字都可以正常输入显示,百思不得其解,求高手帮忙看看,不胜感激。
以下为源程序:
#include
typedef unsigned int uint;
typedef unsigned char uchar;
sbit dula=P2^6;
sbit wela=P2^7;
sbit lcden=P3^4;
sbit rs=P3^5;
sbit rw=P3^6;
sbit busy=P0^7;
char i,j,temp,num;
long a,b,c;
uchar 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,
4,5,6,0x2a-0x30,
1,2,3,0x2d-0x30,
0x01-0x30,0x3d-0x30,0x3d-0x30,0x2b-0x30,
};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
P0=com;
rs=0;
rw=0;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date)
{
P0=date;
rs=1;
rw=0;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_str(uchar *p)
{
while(*p)
{
write_date(*p);
p++;
}
}
void init()
{
lcden=0;
num=-1;
dula=0;
wela=0;
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
i=0;
j=0;
a=0;
b=0;
c=0;
flag=0;
fuhao=0;
write_com(0x80);
}
void keyscan()
{
P3=0xfe;
if(P3!=0xfe)
{
delay(10);
if(P3!=0xfe)
{
temp=P3&0xf0;
switch(temp)
{
case 0xe0:num=0;break;
case 0xd0:num=1;break;
case 0xb0:num=2;break;
case 0x70:num=3;break;
}
}
while(P3!=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_date(0x30+i);
}
P3=0xfd;
if(P3!=0xfd)
{
delay(10);
if(P3!=0xfd)
{
temp=P3&0xf0;
switch(temp)
{
case 0xe0:num=4;break;
case 0xd0:num=5;break;
case 0xb0:num=6;break;
case 0x70:num=7;break;
}
}
while(P3!=0xfd);
if(num==4||num==5||num==6)
{
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_date(0x30+i);
}
P3=0xfb;
if(P3!=0xfb)
{
delay(10);
if(P3!=0xfb)
{
temp=P3&0xf0;
switch(temp)
{
case 0xe0:num=8;break;
case 0xd0:num=9;break;
case 0xb0:num=10;break;
case 0x70:num=11;break;
}
}
while(P3!=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
{
flag=1;
fuhao=2;
}
i=table1[num];
write_date(0x30+i);
}
P3=0xf7;
if(P3!=0xf7)
{
delay(10);
if(P3!=0xf7)
{
temp=P3&0xf0;
switch(temp)
{
case 0xe0:num=12;break;
case 0xd0:num=13;break;
case 0xb0:num=14;break;
case 0x70:num=15;break;
}
}
while(P3!=0xf7);
switch(num)
{
case 12:{write_com(0x01);a=0;b=0;c=0;flag=0;fuhao=0;write_com(0x80);}break;
case 13:{
if(flag==0)
{
a=a*10;
write_date(0x30);
}
else if(flag==1)
{
b=b*10;
write_date(0x30);
}
}break;
case 14:{j=1;
if(fuhao==1)
{ write_com(0x80+0x4f);
write_com(0x04);
c=a+b;
while(c!=0)
{
write_date(0x30+c%10);
c=c/10;
}
write_date(0x3d);
a=0;b=0;c=0;flag=0;fuhao=0;
write_com(0x06);
}
else if(fuhao==2)
{
write_com(0x80+0x4f);
write_com(0x04);
if(a>b) c=a-b;
else c=b-a;
while(c!=0)
{
write_date(0x30+c%10);
c=c/10;
}
if(a-b<0)
write_date(0x2d);
write_date(0x3d);
a=0;b=0;c=0;flag=0;fuhao=0;
write_com(0x06);
}
else if(fuhao==3)
{
write_com(0x80+0x4f);
write_com(0x04);
c=a*b;
while(c!=0)
{
write_date(0x30+c%10);
c=c/10;
}
write_date(0x3d);
a=0;b=0;c=0;flag=0;fuhao=0;write_com(0x06);
}
else if(fuhao==4)//除法
{
write_com(0x80+0x4f);
write_com(0x04);
i=0;
c=(long)(((float)a*1.0/b)*1000);
while(c!=0)
{
write_date(0x30+c%10);
c=c/10;
i++;
if(i==3)
write_date(0x2e);
}
if(a
write_date(0x30);
write_date(0x3d);
a=0;b=0;c=0;flag=0;fuhao=0;write_com(0x06);
}
}break;
case 15:{write_date(0x30+table1[num]);flag=1;fuhao=1;}
break;
}
}
}
main()
{
init();
write_str("C51 calculator");
write_com(0xc0);
write_str("By kylelee");
delay(1000);
init();
while(1)
{
keyscan();
}
}