'---------------------------------------------------------------------------------------- ' Name: 4x4_KEYBOARD_MATRIX.TIG ' Type: Tiger-BASIC(tm) Source Code ' Purpose: Reading up to 4 x 4 keys from a free Tiger 8-bit port ' ' (C) - Copyright Wilke Technology, P.O.Box 1727, D-52018 Aachen, Germany '---------------------------------------------------------------------------------------- ' ' Thank you for using BASIC Tigers in your products. If you have questions, ideas ' or special needs, please contact your next distributor or the Tiger support team ' and visit our web site: ' ' Wilke Technology GmbH ' The Tiger Support Team ' P.O.Box 1727, D-52018 Aachen, Germany ' Krefelder Str. 147, D-52070 Aachen, Germany ' ' email: support@wilke-technology.com (english) ' email: support@wilke.de (german) ' Phone: +49 (241) 918 900 Mo to Fr, 7:00 to 16:00 (GMT) ' Fax: +49 (241) 918 9068 ' ' New information, new drivers and free downloads see: ' ' www.wilke-technology.com (english) ' www.wilke.de (german) ' ' Sincerely, ' ' Your Tiger Support Team ' '---------------------------------------------------------------------------------------- ' ' ' This program demonstrates how to read a 4 x 4 keyboard matrix from a ' Tiger I/O port (here: port 8) without any additional components. ' ' Please connect the keyboard matrix to the Tiger module like this: ' ' Minimal Keyboard ' ' P80_____|_____|_____|_____|_ ' \| \| \| \| ' | | | | ' P81_____|_____|_____|_____|_ ' \| \| \| \| ' 4 x in | | | | ' P82_____|_____|_____|_____|_ ' \| \| \| \| ' | | | | ' P83_____|_____|_____|_____|_ ' \| \| \| \| ' | | | | ' | | | | ' P84 P85 P86 P87 ' ' 4 x out (1 active) ' ' ' The program uses a task to continuously scan port 8 for pressed keys ' and if a key is pressed, a corresponding character is written into ' the keyboard buffer (a string variable). From your main program, you ' ask if there are characters in the buffer, read characters as needed ' and then delete the read characters from the buffer. To avoid ' unwanted characters when waiting for e.g. data entry, you can erase ' the buffer before the entry routine. ' Only single keys are read, if you press two keys simultaneously, the ' second is ignored until the first is released. There is no keyboard ' auto-repeat, you have to press a key multiple times to get multiple ' characters. '--------------------------------------------------------------------- USER_VAR_STRICT ' variables must be declared STRING KEYBUF$(64) ' keyboard buffer (global) TASK MAIN ' begin task MAIN BYTE X ' STRING USER$(100) ' INSTALL_DEVICE #1, "LCD1.TDD" ' install LCD (4 x 20) DIR_PORT 8, 255 ' all pins as input OUT 8, 0FFH, 0FFH ' set input pins of port 8 to "pull-up" KEYBUF$ = "" ' clear keyboard buffer RUN_TASK SCAN_KEY ' Start task to read keyboard USER$ = "" ' reset user string PRINT #1, "<1>"; ' clear LCD screen FOR X = 0 TO 0 STEP 0 ' endless loop WHILE LEN(KEYBUF$) <> 0 ' s.th. in keyboard buffer? USER$ = LEFT$(KEYBUF$, 1) ' read "oldest" character PRINT #1, USER$; ' output it on LCD KEYBUF$ = RIGHT$ (KEYBUF$, LEN(KEYBUF$)-1) ' delete it from buffer ENDWHILE ' NEXT ' end of endless loop END ' end of task MAIN '--------------------------------------------------------------------- ' This task continuously checks the port 8 for pressed keys '--------------------------------------------------------------------- TASK SCAN_KEY ' start task SCAN_KEY BYTE EVER, X, KEY_NO, OLD_KEY_NO ' BYTE BITVAL, INVAL, OUTVAL ' STRING KEYS$(16) ' KEYS$ = "ABCDEFGHIJKLMNOP" ' the 16 keys of the keyboard, ' starting with key no. 0 OLD_KEY_NO = 255 ' set compare to "no key" WAIT_NEXT 30 ' initialize 30 ms wait FOR EVER = 0 TO 0 STEP 0 ' endless loop WAIT_NEXT ' release task time FOR X = 4 TO 7 ' bits for keyboard columns BITVAL = EXP (2, X) ' Create value for bit x (16, 32, 64 or 128) OUTVAL = 255 - BITVAL ' Create mask for DIR_PORT (239, 223, 191, 127) DIR_PORT 8, OUTVAL ' Set new pin directions OUT 8, 255, 255 - BITVAL ' Set output pin "low" = 0, all others "high" = 1 IN 8, INVAL ' read port 8 INVAL = INVAL BITAND 15 ' use only lower 4 bits SWITCHI INVAL ' CASE 15: KEY_NO = 255 ' no key in column pressed CASE 14: KEY_NO = ((X-4)*4) ' calculate key no. X = X - 1 ' same column again CASE 13: KEY_NO = ((X-4)*4) + 1 ' calculate key no. X = X - 1 ' same column again CASE 11: KEY_NO = ((X-4)*4) + 2 ' calculate key no. X = X - 1 ' same column again CASE 7: KEY_NO = ((X-4)*4) + 3 ' calculate key no. X = X - 1 ' same column again DEFAULT: X = X - 1 ' same column again ENDSWITCH ' IF KEY_NO <> OLD_KEY_NO THEN ' if new key(value) detected OLD_KEY_NO = KEY_NO ' save it for comparing IF KEY_NO <> 255 THEN ' if not "no key" KEYBUF$ = KEYBUF$ + MID$(KEYS$, KEY_NO, 1) ' add key to keyboard buffer ENDIF ' ENDIF ' NEXT ' next column NEXT ' end of endless loop END ' end of task SCAN_KEY