資源描述:
《51單片機04矩陣按鍵逐行掃描,行列掃描代碼》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、矩陣鍵盤掃描原理?方法一:?????????????逐行掃描:我們可以通過高四位輪流輸出低電平來對矩陣鍵盤進行逐行掃描,當(dāng)?shù)退奈唤邮盏降臄?shù)據(jù)不全為1的時候,說明有按鍵按下,然后通過接收到的數(shù)據(jù)是哪一位為0來判斷是哪一個按鍵被按下。?方法二:?????????????行列掃描:我們可以通過高四位全部輸出低電平,低四位輸出高電平。當(dāng)接收到的數(shù)據(jù),低四位不全為高電平時,說明有按鍵按下,然后通過接收的數(shù)據(jù)值,判斷是哪一列有按鍵按下,然后再反過來,高四位輸出高電平,低四位輸出低電平,然后根據(jù)接收到的高四位的值
2、判斷是那一行有按鍵按下,這樣就能夠確定是哪一個按鍵按下了。?//行列掃描#include#define?GPIO_KEY?P0#define?GPIO_LCD?P2unsigned?char?code?a[17]={~0xfc,~0x60,~0xda,~0xf2,~0x66,~0xb6,~0xbe,~0xe0,~0xfe,~0xf6,~0xee,~0x3e,~0x9c,~0x7a,~0xde,~0x8e,~0x00};//按位取反的用法void?delay10ms();void?k
3、eydown();//要與下面的定義一致void?main(){??GPIO_LCD=a[16];//初始化數(shù)碼管??while(1)??{???????keydown();??}}void?delay10ms(){??unsigned?char?a,b;??for(a=38;a>0;a--)????for(b=130;b>0;b--);}void?keydown()//檢測按下,按下時需要消抖,檢測松開,返回按鍵值//沒有按鍵時保持{??unsigned?char?n=0,key;??GPIO_K
4、EY=0x0f;??if(GPIO_KEY!=0x0f)//讀取按鍵是否按下??{????delay10ms();??????//延時10ms消抖????if(GPIO_KEY!=0x0f)//再次檢測按鍵是否按下????{??????GPIO_KEY=0x0f;//測試列??????switch(GPIO_KEY)??????{????????case?0x07:?key=0;break;????????case?0x0b:?key=1;break;????????case?0x0d:?key=2
5、;break;????????case?0x0e:?key=3;break;????????default?:?GPIO_LCD=a[16];??????}??????GPIO_KEY=0xf0;//測試行??????switch(GPIO_KEY)??????{????????case?0x70:?key=key;break;????????case?0xb0:?key=key+4;break;????????case?0xd0:?key=key+8;break;????????case?0xe0
6、:?key=key+12;break;????????default?:?GPIO_LCD=a[16];??????}??????GPIO_LCD=a[key];??????while(++n<5&&GPIO_KEY!=0xf0)//檢測按鍵是否松開??????{????????delay10ms();??????}??????????}??}}//逐行掃描#include#define?GPIO_KEY?P0#define?GPIO_LED?P2unsigned?char?cod
7、e?a[17]={~0xfc,~0x60,~0xda,~0xf2,~0x66,~0xb6,~0xbe,~0xe0,~0xfe,~0xf6,~0xee,~0x3e,~0x9c,~0x7a,~0xde,~0x8e,~0x00};//按位取反的用法void?delay10ms();void?keydown1();//要與下面的定義一致void?main(){??GPIO_LED=a[16];//初始化數(shù)碼管??while(1)??{???????keydown1();??}}void?keydown1()
8、{??unsigned?char?n=0,key;??GPIO_KEY=0x0f;??if(GPIO_KEY!=0x0f)//檢測按鍵是否按下??{????delay10ms();//延時10ms消抖????if(GPIO_KEY!=0x0f)//再次檢測????{??????GPIO_KEY=0x7f;//高四位輪流輸出低電平??????if(GPIO_KEY!=0x7f)??????{????????switch(GPIO_KEY)????????{???????