矩阵键盘按下去没反映,啥回事儿
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcdrs=P2^6;
sbit lcdwr=P2^5;
sbit lcden=P2^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar table1[]="Key is";
uint num,num1;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void keyscan()
{
uchar temp;
P1=0x7f;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
switch(temp)
{
case 0x77: num=1;break;
case 0x7b: num=2;break;
case 0x7d: num=3;break;
case 0x7e: num=4;break;
}
}
}
P1=0xbf;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
switch(temp)
{
case 0xb7: num=5;break;
case 0xbb: num=6;break;
case 0xbd: num=7;break;
case 0xbe: num=8;break;
}
}
}
P1=0xdf;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
switch(temp)
{
case 0xd7: num=9;break;
case 0xdb: num=10;break;
case 0xdd: num=11;break;
case 0xde: num=12;break;
}
}
}
P1=0xef;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
switch(temp)
{
case 0xe7: num=13;break;
case 0xeb: num=14;break;
case 0xed: num=15;break;
case 0xee: num=16;break;
}
}
}
}
void write_com(uchar com)
{
lcdrs=0;
lcdwr=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar dat)
{
lcdrs=1;
lcdwr=0;
P0=dat;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
void main()
{
P1=0xf0;
if(P1!=0xf0)
{
keyscan();
}
delay(10);
init();
for(num1=0;num1<6;num1++)
{
write_data(table1[num1]);
delay(5);
}
write_com(0x80+0x07);
write_data(num);
delay(5);
}
你的while()函数呢,主函数缺少死循环程序会跑飞
你的按键扫描程序可以再简化一些,完全没必要那么长
哦。怎么简化。我现在的程序是,然后还是有问题,就是按3,7, B,F显示不出来.第四列的键显示都是G。
void keydown()
{
P1=0xf0;
if(P1!=0xf0)
keyscan();
}
void keyscan()
{
P1=0xf0;
temp=P1;
temp=temp&0xf0;
temp=~((temp>>4)|0xf0);
if(temp==1)
key=0;
else if(temp==2)
key=1;
else if(temp==4)
key=2;
else if(temp==8)
key=3;
else
key=16;
P1=0x0f;
temp=P1;
temp=temp&0x0f;
temp=~(temp|0xf0);
if(temp==1)
key=key+0;
else if(temp==2)
key=key+4;
else if(temp==4)
key=key+8;
else if(temp==8)
key=key+12;
else
key=16;
}
P1=0x0f;
temp=P1;
temp=temp&0x0f;
temp=~(temp|0xf0);
像这样你可以直接
P1=0x0f;
if(P1!=0X0F)
TEMP=P1|0XF0;
在进行判断,你用的是逐行扫描可以查下他的写法