數(shù)碼管顯示矩陣鍵盤掃描值
/*矩陣鍵盤掃描出鍵值,在數(shù)碼管上顯示出來(lái)
P0口接數(shù)碼管的段選碼,P2口通過(guò)反相器接數(shù)
管的位選碼*/
#includereg52.h> //所用的器件是AT89S52
#define uchar unsigned char
void delayms(uchar j); //聲明延時(shí)函數(shù)
uchar getkey(void); //聲明掃描函數(shù)
void keydisp(uchar k); //聲明顯示函數(shù)
void disp(uchar l); //聲明顯示子程序
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,
0x86,0x8e};
//共陽(yáng)數(shù)碼管0-F顯示代碼
void main(void) //主程序
{
uchar key;
while(1)
{
key=getkey();
delayms(20);
keydisp(key);
}
}
void delayms(uchar j) //延時(shí)子程序
{
uchar n,m;
for(n=0;nj;n++)
{
for(m=0;m120;m++) //延時(shí)時(shí)間為1ms
{;}
}
}
uchar getkey(void) //掃描取鍵值程序
{
uchar rcode,ccode;
P1=0xf0;
if((P10xf0)!=0xf0)
{
delayms(20); //為消除抖動(dòng),延時(shí)20ms
if((P10xf0)!=0xf0)
{
rcode=0xfe; //開始發(fā)行掃描信號(hào)
while((rcode0x10)!=0)
{
P1=rcode;
if((P10xf0)!=0xf0)
{
ccode=(P10xf0)|0x0f;
do{;}while((P10xf0)!=0xf0); //當(dāng)松鍵后顯示
return((~rcode)+(~ccode)); //返回掃描代碼
}
else rcode=(rcode1)|0x01; //此行無(wú)鍵按下,變化行掃描信號(hào)
}
}
}
return 0; //無(wú)鍵按下,返回0
}
void keydisp(uchar k) //顯示程序
{
switch(k)
{
case 0x11:disp(0);break;
case 0x21:disp(1);break;
case 0x41:disp(2);break;
case 0x81:disp(3);break;
case 0x12:disp(4);break;
case 0x22:disp(5);break;
case 0x42:disp(6);break;
case 0x82:disp(7);break;
case 0x14:disp(8);break;
case 0x24:disp(9);break;
case 0x44:disp(10);break;
case 0x84:disp(11);break;
case 0x18:disp(12);break;
case 0x28:disp(13);break;
case 0x48:disp(14);break;
case 0x88:disp(15);break;
default:break;
}
}
void disp(uchar l) //顯示子程序
{
P0=table[l];
delayms(20);
P2=0x7f; //共陽(yáng)極數(shù)碼管的位選碼
}
評(píng)論