鍵盤的應(yīng)用和分類:
鍵盤分為編碼鍵盤和非編碼鍵盤,鍵盤上閉合鍵的識別是由專門的硬件編碼器實現(xiàn),并產(chǎn)生鍵編碼號或者是鍵值的成為編碼鍵盤,如計算機(jī)的鍵盤
靠軟件編程來識別的稱為非編碼鍵盤;
在單片機(jī)組成的各種系統(tǒng)中,用的最多的是非編碼鍵盤,也有用到編碼鍵盤的
非編碼鍵盤又有獨立鍵盤和矩陣鍵盤。
要先像鍵盤里面先寫1;在讀取操作
示例代碼:
#include
#define uint unsigned int
#define uchar unsigned char
sbit ld1 = P1^0;
sbit key1 = P3^4;
sbit dula = P2^6;
sbit wela = P2^7;
uchar num;
uchar code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void display(num);
main()
{
P3 = 0xff;
while(1)
{
display(num);
if(key1==0)
{
ld1 = 0;
num++;
if(num==10)
num = 0;
while(key1!=1);//松手檢測
}
else
ld1 = 1;
}
}
void display(num)
{
wela = 1;
P0 = 0xfe;
wela = 0;
P0 = 0x0;
dula = 1;
P0 = table[num];
dula = 0;
dula = 1;//關(guān)燈操作
P0 = 0x0;
dula = 0;
}
View Code
這個程序看上去無懈可擊,但是在實際過程中,有一個相當(dāng)大打bug,那就是,你按住鍵盤的時候,數(shù)碼管不顯示數(shù)字,這不是很坑爹,所以,位選信號是開始就要打開的正確的代碼是
#include
#define uint unsigned int
#define uchar unsigned char
sbit ld1 = P1^0;
sbit key1 = P3^4;
sbit dula = P2^6;
sbit wela = P2^7;
uchar num;
uchar code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void display(num);
main()
{
P3 = 0xff;
wela = 1;
P0 = 0xfe;//打開位選線
wela = 0;
while(1)
{
if(key1==0)
{
ld1 = 0;
num++;
if(num==10)
num = 0;
while(key1!=1);//松手檢測
}
else
ld1 = 1;
dula = 1;
P0 = table[num];//在循環(huán)中不斷送入段選
dula = 0;
}
}
去抖:由于按鍵接觸的時候會出現(xiàn)抖動,所以,要進(jìn)行去抖操作
去抖有硬件消抖和軟件消抖
硬件消抖要用專門的硬件消抖電路,導(dǎo)致外部電路復(fù)雜,在單片機(jī)中用不著
軟件消抖,一般是延時5毫秒檢測
#include
#define uint unsigned int
#define uchar unsigned char
sbit ld1 = P1^0;
sbit key1 = P3^4;
sbit dula = P2^6;
sbit wela = P2^7;
uchar num;
uchar code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delay(uint x);
main()
{
P3 = 0xff;
wela = 1;
P0 = 0xfe;//打開位選線
wela = 0;
while(1)
{
if(key1==0)
{
delay(10);//延時十毫秒
if(key1 == 0)//確實按下去了
{
ld1 = 0;
num++;
if(num==10)
num = 0;
while(key1!=1);//松手檢測
delay(10); //檢測是否松手
while(!key1);
}
}
else
ld1 = 1;
dula = 1;
P0 = table[num];//在循環(huán)中不斷送入段選
dula = 0;
}
}
void delay(uint x)
{
uint y,z;
for(y=x;y>0;y--)
for(z=110;z>0;z--);
}
矩陣鍵盤:無論是矩陣鍵盤還是獨立鍵盤,單片機(jī)檢測其是否被按下去的依據(jù)都是一樣的,也就是檢測該鍵盤對應(yīng)的I/O口是否為低電平,獨立鍵盤有一段固定是低電平,單片機(jī)寫程序時檢測比較方便,電路時矩陣鍵盤的兩端都與單片機(jī)deI/O口相連,因此在檢測時,先送入一列為低電平,其余的全部為高電平,此時我們確定了列數(shù),然后立即輪流檢測一次各行是否有低電平,檢測到某一行為低電平(這是我們有確定了行數(shù)),著我們便可確定是哪一行哪一列的按鍵被按下去。
示例代碼:
#include
sbit wela =P2^6;
sbit dula = P2^7;
#define uchar unsigned char
#define uint unsigned int
void delay(uint z);
uchar num,num1,temp;
uchar keyscan();
uchar code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x0};
main()
{
wela = 1;
P0 = 0x0;
wela = 0;
while(1)
{
num1 = keyscan();
dula =1;
P0 = table[num1];
dula = 0;
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
uchar keyscan()
{
P3 = 0xfe;//1111 1110
temp = P3;
temp = temp&0xf0;//11110000
while(temp!=0xf0)//如果有鍵摁下
{
delay(5);//再次檢測P3口
temp = P3;
temp = temp&0xf0;
while(temp!=0xf0)
{
temp = P3;
switch(temp)
{
case 0x7e:num = 1;
break;
case 0xbe:num = 2;
break;
case 0xde:num = 3;
break;
case 0xee:num = 4;
break;
}
while(temp!=0xf0)//松手檢測
{
temp = P3;
temp = temp&0xf0;
}
}
}
P3 = 0xfd;//1111 1101
temp = P3;
temp = temp&0xf0;//11110000
while(temp!=0xf0)//如果有鍵摁下