怎么实现在51单片机上按下一个按键,1602液晶屏上显示出相应的键码

#include<reg52.h>
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcden=P3^4;
sbit lcdrs=P3^5;
sbit wela=P2^7;
sbit dula=P2^6;
uchar code table[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
uchar num,temp;
uchar key;
void delay(uint x)
{
uchar i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void write_com(uchar com)
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar date)
{
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
dula=0;
wela=0;
lcden=0;
write_com(0x38);
write_com(0x0f);
write_com(0x06);
write_com(0x01);
}
void keyscan()
{

P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:
key=0;
break;

case 0xde:
key=1;
break;

case 0xbe:
key=2;
break;

case 0x7e:
key=3;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;

}
write_com(0x80+1);
write_data(table[key]);
delay(5);
write_com(0x80+1);
}
}
}
void main()
{
init();
while(1);

keyscan();
}

第1个回答  2014-05-06
你的程序没问题啊,不知道你是什么意思。追问

就是调试好程序后,按下一个键后在液晶屏上显示的是乱码,而且只有按0的时候才会出现乱码的字,按其他的没有任何反应,调试了好久都不知道为什么

追答

内置字库吗?

本回答被提问者采纳