|
- #include<reg52.h>
- typedef unsigned int u16;
- typedef unsigned char u8;
- //定義鍵盤連接的端口,設(shè)置鍵盤編碼數(shù)組
- #define keyboard_px P3
- u8 code key_tab[]={0xEE,0xDE,0xBE,0x7E,0xED,0xDD,0xBD,0x7D,0xEB,0xDB,0xBB,0x7B,0xE7,0xD7,0xB7,0x77};
- //延時1ms函數(shù)(晶振11.0592MHz)
- void delay(u16 num)
- {
- u16 x,y;
- for(x=num; x>0; x--){for(y=113; y>0; y--);}
- }
- //掃描函數(shù),獲取按鍵對應(yīng)的索引值
- u8 key_scan(void)
- {
- u8 temp1, temp2, i;
- keyboard_px = 0xf0;
- delay(5);
- if(keyboard_px != 0xf0)
- {
- delay(5);
- if(keyboard_px != 0xf0)
- {
- //獲取按鍵編碼
- temp1 = keyboard_px;
- keyboard_px = 0x0f;
- delay(5);
- temp2 = keyboard_px;
- temp1 |= temp2;
-
- //提取按鍵編碼對應(yīng)的數(shù)碼管編碼(或者返回i值,用于對其它數(shù)組的數(shù)值提取)
- for (i=0; i<16; i++)
- {
- if (key_tab[i] == temp1)
- {
- return i;
- break;
- }
- }
- }
- }else{
- return 0xff;
- }
- }
復(fù)制代碼 |
|