#include "keyScan.h"
#include <reg51.h> #define KEY_BOARD P1 #define DELAY_VALUE 500 /***************** 延时子程序 *****************/ void delay() { unsigned short data i; for(i = DELAY_VALUE;i != 0;i--); } /********************** 取得当前按键码 **********************/ unsigned char getKey() { unsigned char data keyState,scan,count,keyCode; keyCode = 0xff; /*是否有键按下*/ KEY_BOARD = 0xf0; keyState = KEY_BOARD | 0x0f; keyState = ~keyState; if(!keyState) return keyCode; delay(); KEY_BOARD = 0xf0; keyState = KEY_BOARD | 0x0f; keyState = ~keyState; if(!keyState) return keyCode; /*键盘扫描*/ scan = 0xfe; for(count = 0;count < 4;count++){ KEY_BOARD = scan; scan <<= 1; scan ++; keyState = KEY_BOARD & 0xf0; if(keyState){ switch(keyState){ case 0xe0: keyCode = count; break; case 0xd0: keyCode = count + 4; break; case 0xb0: keyCode = count + 8; break; case 0x70: keyCode = count + 12; break; } } if(keyCode != 0xff) break; } //等到按键松开 do{ KEY_BOARD = 0xf0; keyState = KEY_BOARD | 0x0f; keyState = ~keyState; }while(keyState); return keyCode; } |