#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar temp,key_num;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0};
uchar code scan[4] = {0xfe, 0xfd, 0xfb, 0xf7};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
uchar keyscan();
void mian()
{
P2=0x0f;
while(1)
{
keyscan();
delay(10);
P0=table[key_num];
}
}
uchar keyscan()
{
uint i;
for(i=0;i<4;i++)
{
P1=scan;//按鍵檢測(cè)
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(10);
P1=scan;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp&0xf0)
{
case 0xe0: key_num=i*4;
break;
case 0xd0: key_num=i*4+1;
break;
case 0xb0: key_num=i*4+2;
break;
case 0x70: key_num=i*4+3;
break;
}
while(temp!=0xf0)//松手檢測(cè)
{
temp=P1;
temp=temp&0xf0;
}
}
}
}
return key_num;
}
這是我自己寫的一個(gè)鍵盤掃描的程序,p1口接矩陣鍵盤,p0口接數(shù)碼管,p2的第四位控制數(shù)碼管的位選端,把程序?qū)戇M(jìn)去的時(shí)候,數(shù)碼管一直都顯示不出來,不知道哪里出了問題,請(qǐng)求高手解答
| 歡迎光臨 (http://www.raoushi.com/bbs/) | Powered by Discuz! X3.1 |