Path: Home =>
Keys and switches on an ADC =>
Keyboard => resistor matrix
||AVR-Single-Chip-Processors AT90S, ATtiny, ATmega
of ATMEL in practical examples.
Software for a 1 to 16 key wide keypad on an AVR
This page shows the software for connecting keypads of between 1 and 16 keys to an AVR.
The software is written in Delphi-Pascal. The zipped executable for Win32 is
- the software,
- how it works,
- what output it produces,
- which experiences have been made,
The following sections of the window are visible on the left side:
On the right side of the window
- selection of the keypad column and row size, between 1 by 1 and 4 by 4,
- selection of the resistor row to be used (E12 and E24 are practicable, E48 and E96
more theoretical) and their tolerance (5% and 1% are practical, 2% is more theoretical),
- selection of the ADC resolution (8 bits for smaller sizes up to 12 keys, 10 bits for
- the current scheme with the keys and the resistors,
- the approximation steps can be selected, and
- the current results are displayed.
The Win32 software is also executable under Wine in Linux, and looks very much alike. The only
difference is the font of the result window.
To the top of the page
2.1 Selecting the keypad size
With the column and row dropdown selectors the size of the keypad can be selected. Please note
that selection of the size restarts all resistor values to their default value set. This can be
used to restart the approximation from default values if an approximation run ends with an
unfavourable result (count of overlaps larger than zero).
2.2 Selecting scheme variations
Two variations can be made to the scheme:
- The "Rmode" selector allows either stacked or single resistors. This affects R5
to R8 in the 4-by-4 matrix (see the different schemes displayed).
- The "Ymode" selector allows either inclusive or exclusive placement of the
output pin. Inclusive means that the lower resistors (R2 to R4 in the 4-by-4 matrix) add up
to the lower resistor value, while exclusive means that R1 alone is the lower resistor value
and that R2 to R4 (in the 4-by-4 version) add up to the upper resistor value (see the
2.3 Resistor type and ADC resolution
The resistor row and ADC resolution is selected automatically from the keypad size but can
be changed manually to understand the resulting effects. Reducing the E row down can result in
changes of resistors because resistor values are rounded to their nearest equivalent in the
selected E row. Changing the ADC resolution affects the number of overlapping voltage areas,
the result table and the generated assembler table (see below).
2.4 Approximation steps
The button "Step" performs a single approximation step. Approximation means that
The button "Run low" performs 100 approximation steps at once only on the lower resistors
(R2 to R4 in the 4-by-4 case), the button "Run all" includes all resistors above R1. The
number of steps performed at once can be changed in the editor field, 100 should be sufficient
in most cases. All resistors that changed their value during the last run are displayed in red.
The total number of runs is displayed in the respective label in the lower left field, together
with the distances and the overlap count. The overlap count is increased in all cases where the
Max value of a key is equal or larger than the Min value of the next key. It should be zero in
a succesful approximation run.
- a certain resistor above R1 is randomly selected,
- the current distances between the reference and nominal values for all keys (see the Ref
and Nom readings for all keys in the Result-Voltages field) is calculated and compared to
the distances resulting from decreasing the resistor value to the next lower row value
and to the distances resulting from increasing its value to the next higher row value,
- the resistor with the smallest distance is then selected (either the lower, the current
or the higher value),
- if the resistor value has been changed in that step, it is displayed in red in the scheme.
As the resistors are randomly chosen, each run can result in different resistor value combinations.
If the overlap count is not zero and no resistor value changed, just try a new approximation by
selecting the same column or row size.
2.5 Changing resistor values manually
By clicking on a resistor in the scheme a window opens that allows to change its value. k, K and M
are allowed. Clicking on the button "Set" changes the value accordingly.
To the top of the page
If the last step or run yielded the same distance between reference and nominal values and zero
overlaps, the three buttons "Save as ...", "Save" and "Asm table"
are enabled. By pressing the button
Under Windows, the files are written by default to the same folder in which the executable resides.
Under Linux Wine, the files are all written by default to the documents folder.
- "Save as ..." the scheme is saved either as bitmap or as a gif graphic file,
- "Save" the resistor list is saved as a plain text file,
- "Asm table" an include file for AVR assembler is generated.
The file names starts with "RMat_" and then includes
In that case the name of the graphic file is "RMat_StIn_E12_1%_10bit.gif".
- the variations StIn, SiIn, StEx or SiEx of the matrix,
- the E row used,
- the resistor tolerence,
- the ADC resolution.
To the top of the page
3.2 Resistor list
The resistor file holds the content of the Results-Resistors field as plain text.
3.3 Assembler table
The assembler table "filename.inc" looks like this:
It can be directly included in an assembler source code file.
; C4R4 keypad, Inclusive, Stacked
; Decimal table for assembler
Keytable: ; lower limit, upper limit+1, 'Key'
.DW 20, 22 ; '1'
.DW 54, 57 ; '2'
.DW 66, 70 ; '3'
.DW 84, 88 ; 'A'
.DW 88, 92 ; '4'
.DW 214, 222 ; '5'
.DW 253, 262 ; '6'
.DW 304, 314 ; 'B'
.DW 316, 326 ; '7'
.DW 569, 580 ; '8'
.DW 623, 634 ; '9'
.DW 683, 693 ; 'C'
.DW 732, 741 ; '*'
.DW 896, 901 ; '0'
.DW 918, 923 ; '#'
.DW 940, 944 ; 'D'
.DW 0 ; table end
.DB "123A456B789C*0#D" ; Key values ASCII
Note that with 8 bit ADC selected the include file is different as the table is in bytes instead
The inclusive mode works very fine and approximation mostly ends with zero overlaps. If not, just
restart the approximation.
The exclusive mode works fine if the number of keys is 12 or less. In case of 9 keys or above 1%
tolerance or 10-bit ADC has to be selected (or both) to achieve succesful resuts. With 16 keys most
runs end with overlaps in this mode. In that case either stick with the default values or manually
correct resistor values.
Stacked or single resistor mode results in different values, but shows no different approximation
Feedback please to info (at) avr-asm-tutorial.net (replace (at) by @ and remove blanks).
To the top of the page
©2014 by http://www.avr-asm-tutorial.net