'---------------------------------------------------------------------------------------- ' Name: KEY_DIRECT_DEMO_001.TIG ' Type: Tiger-BASIC(tm) Source Code ' Purpose: Introduction and Demo for function: "KEY_DIRECT" ' ' Up to 4 x 4 keys / switches in a matrix on a Tiger port or bus ' ' (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 ' '---------------------------------------------------------------------------------------- ' ' How to connect a 4 x 4 k e y - m a t r i x without extra I/O-pins needed ' ' ' 1.) The situation... ' You are using BASIC-Tiger, TINY-Tiger or Econo-Tiger in your instrument and ' there are a few input keys needed. ' ' This example describes how to connect: ' ' - up to 16 keys (switches) ' - with no or few components needed ' ' in your design. ' ' ' 2.) This example... ' ...was written, compiled and tested on a "Plug & Play Lab", using a BASIC-Tiger ' AXI-4/4 computer module and the Tiger-BASIC Compiler V5.01e. ' ' ' 3.) The idea... ' To arrange a key matrix as a 4 x 4 matrix in a computer system, a certain ' number of I/O lines are used from the system. Also, a few components normally ' are required. ' ' Here we want to deal with 2 different scenarios: ' ' a) For other peripherals (as an LCD for example), we are using the Tiger BUS ' on port 6. ' ' Simply by connecting a 4 x 4 key-matrix with 4 diodes and 4 resistors to ' the bus can give us a well working keypad of up to 16 keys. ' So, effectively no extra I/O pin is needed and the bus function is still ' fully working. ' ' b) The other scenario is, if we do have an unused port as port 8, and we want ' to add up to 16 keys or switches without the need for extra components. ' Here we can ommit diodes and resistors by simply arranging the keys in a ' 4 x 4 array. ' ' ' 4.) Adaptable to specific needs... ' 2 modes of operation, setable scanning speed and selectable ports are available ' to adapt to specific needs. - This example code illustrates the functionality. ' '---------------------------------------------------------------------------------------- ' ' Num = KEY_DIRECT (Port, Column, Mode, Speed) ' Read a key column ' Num = KEY_DIRECT (Port, Column, Mode) ' Read a key column ' ' ' Port: 6 = port 6 or 8 = port 8 ' ' Column: Poll now this column: 0...3 ' ' Mode: 0 ==> get a key no. back: 0,1,2,3 for first key in this column ' -1 if NO key pressed ' 1 ==> get back the scanned nibble as is: 0 ==> key/switch o p e n ' 1 ==> key/switch c l o s e d ' ' Speed: Slow down scanning a bit (use for high resistor values as needed): 1...20 ' 1 = fast, 20 = slowest. ' ' Num: Mode 0: Function value: 0...3: --> first found key that is pressed in ' this column ' -1: --> no key pressed at all ' Mode 1: Function value: 4 bits = 4 switches as are: 0 = open switch ' 1 = closed switch ' ' ' A short BASIC routine is used to read the key-matrix and convert into characters, ' organized in an keyboard input buffer. ' ' Special function keys and functions as shift, ctrl, repeat... etc. may be included ' to the keyboard routine. ' ' A keyboard inputting routine can just watch for single keys pressed or additinally ' for any key combinations. ' ' ' ' C o n n e c t i o n s c h e m e: ' ' ' O p t i o n a l: ' 4 x R 4 x R ' apprx 20...40 K Key-No: 0...F Pull-Up resistors ' ' !------! ! ! ! ! !-------! ' Row-0 L60 ---! R !---(0)-----(4)-----(8)-----(C)----! !----+ ' !------! ! ! ! ! !-------! ! ' ! ! ! ! ! ' !------! ! ! ! ! !-------! ! ' Row-1 L61 ---! R !---(1)-----(5)-----(9)-----(D)----! !----+ ' !------! ! ! ! ! !-------! ! ' ! ! ! ! ! ' !------! ! ! ! ! !-------! ! ' Row-2 L62 ---! R !---(2)-----(6)-----(A)-----(E)----! !----+ ' !------! ! ! ! ! !-------! ! ' ! ! ! ! ! ' !------! ! ! ! ! !-------! ! ' Row-3 L63 ---! R !---(3)-----(7)-----(B)-----(F)----! !----+---(+) Vcc ' !------! ! ! ! ! !-------! ' ! ! ! ! ' ! ! ! ! ' ! ! ! ! ' ! ! ! ! ' ! / Diode ! ! ! ! ' Column-0 L64 ---!<----------+ ! ! ! ' ! \ ! ! ! ' ! ! ! ' ! / Diode ! ! ! ' Column-1 L65 ---!<------------------+ ! ! ' ! \ ! ! ' ! ! ' ! / Diode ! ! ' Column-2 L66 ---!<--------------------------+ ! ' ! \ ! ' ! ' ! / Diode ! ' Column-3 L67 ---!<----------------------------------+ ' ! \ ' ' '---------------------------------------------------------------------------------------- USER_VAR_STRICT ' check for proper variable declaration ! 'USER_STRING_SIZE 60 ' default max. size of strings '---------------------------------------------------------------------------------------- #DEFINE LCD 1 ' device no. of driver LONG EVER, KEY, COL ' STRING BIN$ (10) ' '---------------------------------------------------------------------------------------- TASK Main ' begin task MAIN INSTALL_DEVICE #LCD, "LCD1.TDD" ' install LCD driver: BASIC Tiger ' INSTALL_DEVICE #LCD, "LCD1.TDD", 0,0,0,0,0,0,80h,8 ' install LCD driver: TINY-Tiger FOR EVER = 0 TO 0 STEP 0 ' <----- endless loop ----------- PRINT #LCD, "<1BH>A<0><0><0F0H>"; ' cursor to HOME position FOR COL = 0 TO 3 ' (Port, Column, Mode, Speed) ' check all 4 columns KEY = KEY_DIRECT ( 8, COL, 1, 12) ' read a key column CALL NUM_TO_4BIN (KEY, BIN$) ' make it a binary-type string PRINT #LCD, BIN$ ' show the results NEXT ' next column WAIT_DURATION 200 ' wait 200 ms NEXT ' ------ endless loop ----------> END ' end of task MAIN '---------------------------------------------------------------------------------------- ' Make a 4 digit binary-type string '---------------------------------------------------------------------------------------- SUB NUM_TO_4BIN (LONG K; VAR STRING B$) ' begin subroutine NUM_TO_4BIN LONG N ' B$ = "" ' N = K ' LOOP 4 ' build string B$: 4 binary digits IF N = 2*(N/2) THEN ' B$ = B$ + "." ' <--- key / switch is OPEN ELSE ' B$ = B$ + "#" ' <--- a key is pressed ENDIF ' B$ = B$ + " " ' N = N/2 ' ENDLOOP ' END ' endof subroutine NUM_TO_4BIN