/*********************************/ /* program: keypad.c */ /* by Jack Changfan */ /* Jan 8 '97 */ /*********************************/ #progma SMALL db CODE #include sfr keypad = 0x90; /* Port 1: Data in/out */ void LCD_DISPLAY(unsigned char ch) { /* you can write this part by yourself */ } unsigned char scan() { int i; unsigned char scandrive = 0xfe; /* scanner drive */ /* higher 4-bit write 1's to setup input mode */ /* lower 4-bit output exactly one 0 to the column to be scanned */ unsigned char key, row, col; unsigned char keyTab[16] = {'1','2','3','A','4','5','6','B', '7','8','9','C','*','0','#','.'}; key=0xf0; /* preset key */ while ((scandrive != 0xef) && ((key & 0xf0)==0xf0)) /* when scandrive=0xef, all columns has been scanned */ /* when (key & 0xf0)!=0xf0, some 0 has been read */ { Keypad = scandrive; key = Keypad; scandrive = (scandrive*2)+1; /* shift left */ } if (key == 0xf7) return 0xff; /* scan all columns, no key pressed */ else { for (i=0; i<4; i++) { if ( (key & 0x01) == 0 ) col=i; /* locate the column with 0 */ key = (key >> 1); } for (i=0; i<4; i++) { if ( (key & 0x01) == 0 ) row=i; /* locate the row with 0 */ key = (key >> 1); } return keyTab[row*4+col]; } } void main() { unsigned char keypress; /* variable for keypress */ while (1) { keypress = scan(); if (keypress != 0xff) LCD_DISPLAY(); /* some key is pressed */ /* note that scan return 0xff when no key is pressed */ Keypad = 0xf0; /* scan all columns */ while (Keypad != 0xf0); /* wait until all keys are released */ } }