#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar l,h,i,k=0,Q=0,a=0,b=0,c,k,num;
void delay(uint x);
void lcd_init();; //初始化
void write_com(uchar com); //写指令
void write_data(uchar data); //写数据
//void read_data(uchar data); //读数据
void keyscan(); //矩阵键盘扫描
void lcd_dispay(); //LCD1602显示
uchar code table[num]={
1,2,3,4,5,6,7,8,9,0,
0x2b,0x2d,0x2a,0x2f,0x11,0x3d};
uchar code table1[ ]={
'e','r','r','o'};
uchar code table2[ ]={
'e','r','r','o','i','s','b' };
sbit E =P2^7; //1602使能
sbit RW=P2^6; //1602读写
sbit RS=P2^5; //1602数据/命令
void lcd_init() //初始化
{
delay(15);
write_com(0x38);
delay(5);
write_com(0x38);
delay(5);
write_com(0x38);
write_com(0x38);
write_com(0x0f); //开显示,显示闪烁光标
write_com(0x06); //读写后光标移动
write_com(0x01); //清屏
}
void write_com(uchar com) //写指令
{
RS=0;
RW=0;
P0=com;
E=1;
delay(5);
E=0;
}
void write_data(uchar data) //写数据
{
RS=1;
RW=0;
P0=data;
E=1;
delay(5);
E=0;
}
/*void read_data(uchar data) //读数据
{
RS=1;
RW=1;
E=1;
P0=data;
dalay(5);
E=0;
} */
void keyscan()
{
if(k=0) {a=a+table[num];} //没输入符号时输入为a
if(k=1|K=2|K=3|K=4) {b=b+table[num];} //输入符号后输入赋为b
if(Q=1) {Q=0;write_data(0x01);} // 清屏键
P1=0x0f; //给P1赋值00001111
l=P1&0x0f;
if(l!=0x0f)
{
delay(100);
if(l!=0x0f)
l=P1&0x0f; //若有键按下,得出低四位的值
}
P1=0xf0; //给P1赋值11110000
h=P1&0xf0;
if(h!=0xf0)
{
delay(100);
if(h!=0xf0)
h=P1&0xf0; //若有键按下,得出高4位的值
}
switch(h+l)
{
case 0xee:write_data(table[0]);break;
case 0xde:write_data(table[1]);break;
case 0xbe:write_data(table[2]);break;
case 0x7e:write_data(table[3]);break;
case 0xed:write_data(table[4]);break;
case 0xdd:write_data(table[5]);break;
case 0xbd:write_data(table[6]);break;
case 0x7d:write_data(table[7]);break;
case 0xeb:write_data(table[8]);break;
case 0xdb:write_data(table[9]);break;
case 0xbb:write_data(table[10]);k=1;break; //输入+的符号,并使前面输入数为a
case 0x7b:write_data(table[11]);k=2;break; //输入-的符号,并使前面输入数为a
case 0xe7:write_data(table[12]);k=3;break; //输入*的符号,并使前面输入数为a
case 0xd7:write_data(table[13]);k=4;break; //输入/的符号,并使前面输入数为a
case 0xb7:write_data(table[14]);Q=1;break; //清屏键
case 0x77:write_data(table[15]);K=5;break; //输入=的符号
}
}
void lcd_dispay() //当输入=键时在下一行显示输出结果
{
keyscan();
while(K=5)
switch(k)
{
case 1: c=a+b;write_data(c);break; 、// +运算时输出结果
case 2: if(a>b){c=a-b;write_data(c);};break;
else { for(i=6;i>0;i--) write_data(table1[i])};break; // -运算时输出结果
case 3: c=a*b;write_data(c);break; //*运算时输出结果
case 4: if(b!=0){c=a/b;write_data(c);}; break;
else { for(i=9;i>0;i--)write_data(table2[i])};break; // /运算时输出结果
}
}
void main()
{
while(1)
{
lcd_init();
delay(5);
lcd_display();
}
}
void delay(uint x) //延时xms
{
uint p,j;
for(p=x;i>0;i--)
for(p=110;j>0;j--);
}
明天就要交了,望各位大侠高手们解救。。。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
谢谢哈,已经调试好了
一周热门 更多>