新人請教4*4鍵盤掃描細(xì)節(jié)問題,望達(dá)人不吝賜教!:鍵盤掃描程序如下(程序及圖附件中都有),下面這個程序是對的,但問題有以下幾點(都在掃描部分):
一、用if語句可以,但用while語句不可以(語法上大致明白,但理解不清楚)。
二、我用語句if(P2&0x0f!=0x0f)判斷時,直接出現(xiàn)兩個警告(圖見附件warning.jpg),原因不明。當(dāng)改用
P2=light[j];
k=P2&0x0f;
if(k!=0x0f)
這三條替換if(P2&0x0f!=0x0f)時,程序正確,不知道為什么。
三、以上只是按鍵判定,松手判定,我試了好幾種方式(見程序中),都不行。
#include<at89x51.h>
#define uint unsigned int
#define uchar unsigned char
uchar code light[]={0x7f,0xbf,0xdf,0xef};
uchar code LED[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0};
uchar key,j;
void delay(uint m) //延時
{
uint i;
for(;m>0;m--)
for(i=124;i>0;i--)
;
}
uchar readkey(uchar scan) //讀取鍵碼值
{
P2=scan;
switch(P2)
{
case 0x7e:return(0);
case 0x7d:return(1);
case 0x7b:return(2);
case 0x77:return(3);
case 0xbe:return(4);
case 0xbd:return(5);
case 0xbb:return(6);
case 0xb7:return(7);
case 0xde:return(8);
case 0xdd:return(9);
case 0xdb:return(10);
case 0xd7:return(11);
case 0xee:return(12);
case 0xed:return(13);
case 0xeb:return(14);
case 0xe7:return(15);
default:return(16);
}
}
void keyscan() //掃描程序
{
uchar k;
for(j=0;j<4;j++)
{
P2=light[j];
k=P2&0x0f;
if(k!=0x0f)
{
delay(5);
P2=light[j];
k=P2&0x0f;
if(k!=0x0f)
{
key=readkey(light[j]);
// while(k!=0x0f); //判松手
//while(P2&0x0f!=0x0f);
/* P2=light[j];
k=P2&0x0f;
while(k!=0x0f);
*/
}
}
}
}
void main()
{
key=16;
while(1)
{
keyscan();
P1=LED[key];
}
}