求助!矩阵键盘不知道哪里错了
按键没反应。
#include<reg51.h>
#define KEY P1
void key_down();
void delayms();
unsigned int keyValue=0;
main()
{
P1=0Xff;
P2=0xff;
while(1)
{
key_down();
switch(keyValue)
{
case 0: P2=0x02;break;
case 1:P2=0x01;break;
case 2: P2=0X03;break;
case 3:P2=0x07;break;
case 4: P2=0x0f;break;
case 5: P2=0X1F;break;
case 6:P2=0X3F;break;
}
}
}
void delayms()
{
int i;
for(i=0;i<120;i++);
}
void key_down()
{
int n=0;
KEY=0x0f;
if(KEY!=0x0f)
{
delayms();
if(KEY!=0x0f)
{
KEY=0x0f;
switch(KEY)
{
case 0x07:keyValue=0;break;
case 0x0b:keyValue=1;break;
case 0x0d:keyValue=2;break;
case 0x0e:keyValue=3;break;
}
KEY=0xf0;
switch(KEY)
{
case 0x70:keyValue = keyValue + 1;break;
case 0xb0:keyValue = keyValue + 5;break;
case 0xd0:keyValue = keyValue + 9;break;
case 0xe0:keyValue = keyValue + 13;break;
}
while((n<50)&&(KEY!=0x0f))
{
delayms();
n++;
}
}
}
}