各位盆友,我想用lcd162顯示矩陣鍵盤掃描值,結(jié)果一上電一直顯示000000000,我是初學(xué)者,不會改 求各位大神幫忙改
#include
#define uchar unsigned char
#define uint unsigned int
unsigned char code table[]={'0','1','2','3','4','5','6','7','8',
'9','A','B','C','D','E','F'};//轉(zhuǎn)換成液晶顯示的字符 uchar num,temp; sbit dula=P2^6;
sbit wela=P2^7;
sbit lcden=P2^5;
sbit lcdrs=P1^0;
sbit lcdwr=P1^1; 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)
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0; }
void write_date(uchar date)
{
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
} void init ()
{ wela=0; dula=0; lcdwr=0; lcden=0; write_com(0x38); write_com(0x0f); write_com(0x06); write_com(0x01); write_com(0x80);} void main ()
{
init (); while (1)
{
uchar keyscan(); write_date(table[keyscan()]); delay(20);}
}
uchar keyscan() //鍵盤掃描函數(shù),返回一個值num
{ P3=0xfe; temp=P3; temp=temp&0xf0; while(temp!=0xf0) { delay(5); temp=P3; temp=temp&0xf0; while(temp!=0xf0) { temp=P3; switch(temp) { case 0xee:num=1; break; case 0xde:num=2; break; case 0xbe:num=3; break; case 0x7e:num=4; break; } while(temp!=0xf0) { temp=P3; temp=temp&0xf0; } } } P3=0xfd; temp=P3; temp=temp&0xf0; while(temp!=0xf0) { delay(5); temp=P3; temp=temp&0xf0; while(temp!=0xf0) { temp=P3;//必須重新讀取P3口的值,否則temp將一直不等于0xf0 switch(temp) { case 0xed:num=5; break; case 0xdd:num=6; break; case 0xbd:num=7; break; case 0x7d:num=8; break; } while(temp!=0xf0) { temp=P3; temp=temp&0xf0; } } } P3=0xfb; temp=P3; temp=temp&0xf0; while(temp!=0xf0) { delay(5); temp=P3; temp=temp&0xf0; while(temp!=0xf0) { temp=P3; switch(temp) { case 0xeb:num=9; break; case 0xdb:num=10; break; case 0xbb:num=11; break; case 0x7b:num=12; break; } while(temp!=0xf0) { temp=P3; temp=temp&0xf0; } } } P3=0xf7; temp=P3; temp=temp&0xf0; while(temp!=0xf0) { delay(5); temp=P3; temp=temp&0xf0; while(temp!=0xf0) { temp=P3; switch(temp) { case 0xe7:num=13; break; case 0xd7:num=14; break; case 0xb7:num=15; break; case 0x77:num=16; break; } while(temp!=0xf0) { temp=P3; temp=temp&0xf0; } } }return num; }
|