怎么实现在51单片机上按下一个按键,1602液晶屏上显示出相应的键码
#include<reg52.h>#include<math.h>#defineucharunsignedchar#defineuintunsignedintsbitlc...
#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();
} 展开
#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个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询