MATRIX KEYBOARD SUPPORT
Many applications of the TDS9092 computer module involve input from a keypad. Sometimes a full alphanumeric keyboard is required. Other uses involve inputs from microswitches or relay contacts. All of these requirements can usually be met by means of the keyboard encoding scheme built in to the TDS9092 as combination of special hardware and software.
The diagrams on pages 54 and 55 indicate the connections between the TDS9092 and up to 64 keys. Although this is shown as an 8 x 8 matrix you may choose to use only part of it, e.g. 4 x 4 to input from a hex keypad. Alternatively the switches may represent microswitches etc. and not buttons. Port B is used for the keyboard. Even if all the keys are not there you cannot use any spare bits of Port B for other purposes.
The 8 pull-down resistors shown on the data bus lines are needed only for the keyboard and all 8 must be present even if the keyboard is not using all the data lines. A lower value if recommended when using certain LCDs on the bus, see page 57. Leads to the keyboard should be kept as short as practicable, preferably under 300mm (12 inches). If they are too long, wrong codes can result but the bus can be buffered with a 74HC245 device if needed. The diodes indicated are Schottky types for their low forward voltage drop. 1N4148 types will work but with reduced noise immunity.
When using the keyboard mode in an application do not use chip select CS01B0* for any other purpose since this is used to internally strobe the output of port B in the key matrix.
If a key is kept closed during multiple calls of NEWKEY only the first time will show which is pressed. Later ones give n=0. This is the word which is used most.
The scheme works as follows. A single 1 is put in turn to each of the 8 outputs from Port B. However the keyboard words put Port B into a special mode under which these outputs do not appear at once. They only occur if the processor calls address 01B0. Eight times in succession a different pattern is put to Port B and then address 01B0 is read. One row of the matrix is scanned each time and a pattern showing which switches are closed is input to the microprocessor. Software decodes these 8 patterns to give the number of the key. If more than one key is pressed the highest number is returned.
Typical times for execution of the keyboard words are:
Here are some tips on using the keyboard support words. The first example is a word which waits until a key is pressed. In function this is similar to KEY on the serial input:
: KEY ( - n wait for key, n=1 to 64
BEGIN REGULAR NEWKEY -DUP UNTIL ;
The word REGULAR is included to show that you can have the computer do many other jobs while waiting for a key. Define REGULAR to include whatever else the machine must do all the time, but keep it to a maximum of 100ms execution time so that response to a key is not delayed.
Usually you really need a value associated with each key which is not just the simple key number. For example on an alphanumeric keypad the ASCII code will be wanted. The conversion is best done using a look-up table in this way:
HEX CREATE CODES ( look-up table
34 C, 65 C, 2C C, 78 C, BA C, 43 C, 4F C, 7E C,
36 C, A2 C, 4D C, 69 C, 11 C, 0F C, EE C, 23 C,
56 C, EF C, FF C, A2 C, 33 C, 89 C, 2A C, 12 C,
9A C, C5 C, 5D C, 9E C, FC C, 23 C, 75 C, 82 C,
E5 C, 6A C, A6 C, 9D C, E8 C, 4C C, 44 C, 73 C,
7A C, AF C, BB C, 43 C, 68 C, 23 C, 11 C, 24 C,
67 C, 87 C, 98 C, 63 C, 0F C, E3 C, E0 C, F0 C,
55 C, FD C, AF C, F2 C, 45 C, 76 C, 9A C, 0D C,
: TRANSLATE ( n1 - n2 change key code n1 into n2
' CODES + 1- C@ ;
In this example the particular codes chosen for each key are arbitrary, the actual content of the table will depend on the application.
The final example combines the previous two and puts the code generated, assumed to be in ASCII, on an LCD. See the section LIQUID CRYSTAL DISPLAYS for further details.
: QUERY ( - input from key matrix to LCD and PAD
WIPE ( blank LCD
PAD 40 BLANKS ( clear buffer
PAD ( start of buffer
BEGIN KEY TRANSLATE DUP $0D <>
WHILE DUP LCDEMIT OVER C! 1+
REPEAT 2DROP ;
We escape from the word QUERY only when the key which translates to code 0D (ASCII for carriage return) is pressed. The message typed out is put to the LCD and also is left in the PAD for further processing by the program.