Go to Triangle Digital Support Home Page TDS2020F TECHNICAL MANUAL
Hardware support
Matrix keypad support
Live website search
Enter key words
 

MATRIX KEYPAD SUPPORT

DEFINITIONS

INKEY

( - n )

Scans keypad. n=0 if all keys open. n=1 to 64 if any key pressed.

NEWKEY

( - n )

Calls INKEY but only returns n=1 to 64 if a new key is pressed.

LASTKEY

( - a )

User variable containing code of last key found by NEWKEY .

HARDWARE

Many applications of the TDS2020F 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 keypad-encoding scheme built in to the TDS2020F as a combination of special hardware and software.

The diagrams indicate the connections for 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 keypad. Even if all the keys are not there you cannot use spare bits of Port B for other purposes.

The 8 pull-down resistors shown on the data bus lines are needed only for the keypad and all 8 must be present even if the keypad is not using all the data lines. A lower value is recommended when using certain LCDs on the bus, see ALPHANUMERIC LCDS, page 100. Leads to the keypad 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, see EXTENDING THE BUS, page 303. The diodes indicated are Schottky types for their low forward voltage drop. 1N4148 types will work but with reduced noise immunity.

When using the keypad mode in an application do not use chip select CS81B0* for any other purpose since this is used to internally strobe the output of Port B in the key matrix.

SOFTWARE

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 that 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 keypad 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 hex 81B0. Eight times in succession a different pattern is put to Port B and then address 81B0 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 keypad words are:

 

INKEY       17�s
NEWKEY      65�s

 

Here are some tips on using the keypad support words. The first example is a word that 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@ ; DECIMAL

 

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 LIQUID CRYSTAL DISPLAYS, page 97,  for further details.

 

: REQUEST ( - ) \ Input from key matrix to LCD and PAD
   WIPE           \ blank LCD
   PAD 40 BLANK   \ clear buffer
   PAD            \ start of buffer
   BEGIN KEY TRANSLATE DUP $0D <>
   WHILE DUP LCDEMIT OVER C! 1+
   REPEAT 2DROP ;

 

We escape from the word REQUEST only when the key that 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.

Go to Triangle Digital Support Home Page Go to top   Next page