|
想問(wèn)問(wèn)論壇里的學(xué)長(zhǎng)們,我這個(gè)鍵盤檢測(cè)是不是有一點(diǎn)問(wèn)題,為什么通過(guò)矩陣鍵盤打出來(lái)的是亂碼
我這個(gè)是郭天祥的板子,鍵盤檢測(cè)和液晶初始化,也主要是按他視頻里教學(xué)打的
#include<reg52.h>
sbit lcden=P3^4;
sbit lcdrs=P3^5;
sbit dula=P2^6;
sbit wela=P2^7;
#define uint unsigned int
#define uchar unsigned char
uchar code table2[]="MIMA?";
uchar num,temp,num1,num2;
uchar keyscan();
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)//命令子函數(shù)
{
lcdrs=0;//時(shí)序
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()//將液晶初始化函數(shù)
{
dula=0;
wela=0;
lcden=0;
write_com(0x38);
write_com(0x0f);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
uchar keyscan(uint num1)//鍵盤檢測(cè)
{
while(1)
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:num1=1;
break;
case 0xde:num1=2;
break;
case 0xbe:num1=3;
break;
case 0x7e:num1=4;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:num1=5;
break;
case 0xdd:num1=6;
break;
case 0xbd:num1=7;
break;
case 0x7d:num1=8;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
delay(5);
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:num1=9;
break;
case 0xdb:num1=10;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
}
return num1;
}
void main()
{
init();
for(num=0;num<5;num++)
{
write_data(table2[num]);//輸入 MIMA?
delay(300);
}
write_com(0x80+0x40);//換行
num2=keyscan(num1);//鍵盤檢測(cè)返回的值賦給num2
init();
switch(num2)
{
case 1:write_data('0');break;
case 2:write_data('1');break;
case 3:write_data('2');break;
case 4:write_data('3');break;
case 5:write_data('4');break;
case 6:write_data('5');break;
case 7:write_data('6');break;
case 8:write_data('7');break;
case 9:write_data('8');break;
case 10:write_data('9');break;
}
while(1);
}
|
|