这是计算器部分,单独不加时钟和温度是挺正常的
P1=0xfe;
if(P1!=0xfe)
{
delay(20);// 延迟20ms
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)//如果按下的是'7','8'或'9
{
if(j!=0)
{
LCD_Write(LCD_COMMAND,0x01);
j=0;
}
if(flag==0)//没有按过符号键
{
a=a*10+table[num];
}
else//如果按过符号键
{
b=b*10+table[num];
}
}
else//如果按下的是'/'
{
flag=1;
fuhao=4;//4表示除号已按
}
i=table1[num];
LCD_Write(LCD_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)//如果按下的是'4','5'或'6'
{
if(j!=0)
{
LCD_Write(LCD_COMMAND,0x01);
j=0;
}
if(flag==0)//没有按过符号键
{
a=a*10+table[num];
}
else//如果按过符号键
{
b=b*10+table[num];
}
}
else//如果按下的是'/'
{
flag=1;
fuhao=3;//3表示乘号已按
}
i=table1[num];
LCD_Write(LCD_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)//如果按下的是'1','2'或'3'
{
if(j!=0)
{
LCD_Write(LCD_COMMAND,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;//2表示减号已按
}
i=table1[num];
LCD_Write(LCD_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:{LCD_Write(LCD_COMMAND,0x01);a=0;b=0;flag=0;fuhao=0;}//按下的是"清零"
break;
case 13:{ //按下的是"0"
if(flag==0)//没有按过符号键
{
a=a*10;
LCD_Write(LCD_DATA,0x30);
P1=0;
}
else if(flag>=1)//如果按过符号键
{
b=b*10;
LCD_Write(LCD_DATA,0x30);
}
}
break;
case 14:{j=1;
if(fuhao==1)
{
LCD_Write(LCD_COMMAND,0x80+0x4f);//按下等于键,光标前进至第二行最后一个显示处
LCD_Write(LCD_COMMAND,0x04); //设置从后住前写数据,每写完一个数据,光标后退一格
c=a+b;
while(c!=0)
{
LCD_Write(LCD_DATA,0x30+c%10);
c=c/10;
}
LCD_Write(LCD_DATA,0x3d); //再写"="
a=0;b=0;flag=0;fuhao=0;
}
else if(fuhao==2)
{
LCD_Write(LCD_COMMAND,0x80+0x4f);//光标前进至第二行最后一个显示处
LCD_Write(LCD_COMMAND,0x04); //设置从后住前写数据,每写完一个数据,光标后退一格(这个照理说顺序不对,可显示和上段一样)
if(a-b>0)
c=a-b;
else
c=b-a;
while(c!=0)
{
LCD_Write(LCD_DATA,0x30+c%10);
c=c/10;
}
if(a-b<0)
LCD_Write(LCD_DATA,0x2d);
LCD_Write(LCD_DATA,0x3d); //再写"="
a=0;b=0;flag=0;fuhao=0;
}
else if(fuhao==3)
{
LCD_Write(LCD_COMMAND,0x80+0x4f);
LCD_Write(LCD_COMMAND,0x04);
c=a*b;
/*while(c!=0)
{
LCD_Write(LCD_DATA,0x30+c%10);
c=c/10;
} */
LCD_Write(LCD_DATA,0x3d);
a=0;b=0;flag=0;fuhao=0;
}
else if(fuhao==4)
{LCD_Write(LCD_COMMAND,0x80+0x4f);
LCD_Write(LCD_COMMAND,0x04);
i=0;
if(b!=0)
{
c=(long)(((float)a/b)*1000);
while(c!=0)
{
LCD_Write(LCD_DATA,0x30+c%10);
c=c/10;
i++;
if(i==3)
LCD_Write(LCD_DATA,0x2e);
}
if(a/b<=0)
{
if(i<=2)
{
if(i==1)
LCD_Write(LCD_DATA,0x30);
LCD_Write(LCD_DATA,0x2e);
LCD_Write(LCD_DATA,0x30);
}
LCD_Write(LCD_DATA,0x30);
}
LCD_Write(LCD_DATA,0x3d);
a=0;b=0;flag=0;fuhao=0;
}
else
{
LCD_Write(LCD_DATA,'!');
LCD_Write(LCD_DATA,'R');
LCD_Write(LCD_DATA,'O');
LCD_Write(LCD_DATA,'R');
LCD_Write(LCD_DATA,'R');
LCD_Write(LCD_DATA,'E');
}
}
}
break;
case 15:{LCD_Write(LCD_DATA,0x30+table1[num]);flag=1;fuhao=1;}
break;
}
主程序这样写的
void main()
{
time=1; //时钟停止标志
LCD_Initial(); //液晶初始化
Init_DS18B20( ) ; //DS18B20初始化
Initial_DS1302(); //时钟芯片初始化
up_flag=0;
down_flag=0;
done=0; //进入默认液晶显示
while(1)
{
if(flag1==0)
{
if(done==0)
{
show_time(); //液晶显示数据
time=0;
Setkey(); //扫描各功能键
outkey();
}
if(done==1)
{
keydone(); //进入调整模式
}
}
if(flag1==1)
{
LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); //清屏
init();
while(1)
{key();}
}
}
}
主程序开始明明没调用计算器键盘,发现只要加上运算部分,写不写键盘检测屏幕都会是空的,不加这功能就显示正常
case 14:{j=1;
if(fuhao==1)
{
LCD_Write(LCD_COMMAND,0x80+0x4f);//按下等于键,光标前进至第二行最后一个显示处
LCD_Write(LCD_COMMAND,0x04); //设置从后住前写数据,每写完一个数据,光标后退一格
c=a+b;
while(c!=0)
{
LCD_Write(LCD_DATA,0x30+c%10);
c=c/10;
}
LCD_Write(LCD_DATA,0x3d); //再写"="
a=0;b=0;flag=0;fuhao=0;
}
else if(fuhao==2)
{
LCD_Write(LCD_COMMAND,0x80+0x4f);//光标前进至第二行最后一个显示处
LCD_Write(LCD_COMMAND,0x04); //设置从后住前写数据,每写完一个数据,光标后退一格(这个照理说顺序不对,可显示和上段一样)
if(a-b>0)
c=a-b;
else
c=b-a;
while(c!=0)
{
LCD_Write(LCD_DATA,0x30+c%10);
c=c/10;
}
if(a-b<0)
LCD_Write(LCD_DATA,0x2d);
LCD_Write(LCD_DATA,0x3d); //再写"="
a=0;b=0;flag=0;fuhao=0;
}
else if(fuhao==3)
{
LCD_Write(LCD_COMMAND,0x80+0x4f);
LCD_Write(LCD_COMMAND,0x04);
c=a*b;
/*while(c!=0)
{
LCD_Write(LCD_DATA,0x30+c%10);
c=c/10;
} */
LCD_Write(LCD_DATA,0x3d);
a=0;b=0;flag=0;fuhao=0;
}
else if(fuhao==4)
{LCD_Write(LCD_COMMAND,0x80+0x4f);
LCD_Write(LCD_COMMAND,0x04);
i=0;
if(b!=0)
{
c=(long)(((float)a/b)*1000);
while(c!=0)
{
LCD_Write(LCD_DATA,0x30+c%10);
c=c/10;
i++;
if(i==3)
LCD_Write(LCD_DATA,0x2e);
}
if(a/b<=0)
{
if(i<=2)
{
if(i==1)
LCD_Write(LCD_DATA,0x30);
LCD_Write(LCD_DATA,0x2e);
LCD_Write(LCD_DATA,0x30);
}
LCD_Write(LCD_DATA,0x30);
}
LCD_Write(LCD_DATA,0x3d);
a=0;b=0;flag=0;fuhao=0;
}
else
{
LCD_Write(LCD_DATA,'!');
LCD_Write(LCD_DATA,'R');
LCD_Write(LCD_DATA,'O');
LCD_Write(LCD_DATA,'R');
LCD_Write(LCD_DATA,'R');
LCD_Write(LCD_DATA,'E');
}
}
}
求懂的或者遇见这样问题的大神指点下,看了两天了,不知道咋回事
一周热门 更多>