新聞中心

[C51代碼]4*4鍵盤掃描

作者: 時(shí)間:2016-11-10 來源:網(wǎng)絡(luò) 收藏
程序如下:
#include
#define uint unsigned int
#define uchar unsigned char
sbit key3 =P2^0; //按鍵的行檢測(cè)信號(hào)
sbit key2 =P2^1;
sbit key1 =P2^2;
sbit key0 =P2^3;
sbit dis0 =P2^4;
sbit dis1 =P2^5;
sbit dis2 =P2^6;
sbit dis3 =P2^7;
sbit dis4 =P3^3;
uchar keyx,key;
uchar code disp[17]={0x00,0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void delay(uint i)
{for(;i>0;i--)
;
}
uchar keyscan() //數(shù)碼管顯示掃描與鍵盤檢測(cè)
{
static dis=0;
dis=(dis+1)%5;
dis0=1;dis1=1;dis2=1;dis3=1;dis4=1;
switch(dis) //位選碼輸出
{
case 0: dis0=0; break;
case 1: dis1=0; break;
case 2: dis2=0; break;
case 3: dis3=0; break;
case 4: dis4=0; break;
}
if(!key0) keyx=dis+1; //鍵盤的行檢測(cè)與譯碼
if(!key1) keyx=dis+5;
if(!key2) keyx=dis+9;
if(!key3) keyx=dis+13;
return keyx;
}
void main(void)
{ while(1)
{
key=keyscan();
P0=disp[key];
delay(3000);
}
}


關(guān)鍵詞: C51代碼4*4鍵盤掃

評(píng)論


技術(shù)專區(qū)

關(guān)閉