Path: Home =&ht; AVR_EN => Applications => Key matrix => Stacked keys   Diese Seite in Deutsch:
 Applications of AVR Single chip controllers AT90S, ATtiny, ATmega and ATxmega Forward/backward and right/left with resistors on an AD input

# Forward/backward and right/left keys via an AD converter channel

## 1 Special key combinations

To control a car model you need two key pairs: one for forward and backward and one for left and right. If none of those has been pressed, anything stays as it is.

Two special combinations have to be additionally considered: if the forward and the backward key are both pressed, the model shall stop immediately. The second case: if the left and right key are both pressed,the direction shall move to the middle (direction neutrality).

To recognize the state of the four keys, you can either attch them to four input pins, with the internal pull-ups activated, or you can use the shown resistor matrix to generate an analog voltage and measure this via one AD channel input pin. If none of the keys is pressed (use either ON-OFF-ON switches or, even better, four separate keys).

If any two of the keys are closed at the same time, the task of calculating voltages is not trivial, because two resistors are parallel. Therefore I have defined these voltage fractions by testing those combinations in a calculation sheet. Each of the eleven combinations has its own voltage.

## The software for iterating resistor values

The software is written in Free-Pascal. The source-code is here, the compiled version for Win64 is here. This allows to
• select the resistor row (E12/E24/E48/E96), input r,
• play with resistor tolerances 1, 2 and 5%, input t,
• set resistors values for R1 to R5 manually by inputting 1 to 5 and then the resistor value in Ohms (you can use k Or K like in 1k5 or M like in 1M,
• iterate the resistor values in single steps by inputting s,
• iterate in 100 steps at once by inputting i, and
• inputting w writes the current values to a text file in the current path.
The nominal and min/max values in the output and in the file are all made for an 8-bit ADC result. If voltages of certain combinations overlap those of other combinations, overlaps larger than 0 are shown (should be 0 for a succesful iteration). For checking the voltages with a meter, the nominal voltage of the combinations and the target voltage is displayed in mV at 5V operating voltage.

When iterating, only R1 to R4 are picked randomly and increased or decreased. R5 can only be changed manually.

The input key wwrites the current results to a text file. An example for that is shown here:
``` Resistor matrix for encoding Forw/Neutral/Backw and Left/Neutral/Right keys
---------------------------------------------------------------------------
Resistor matrix    Res-Value      Mode  min - nom. -  max Delta  mV@5V  mV-Sh
(+) (+)  (+) (+)    R1=10k     Off-Off    0 -    0 -    5     0      0     0
|   |    |   |     R2=5k1     Bck-Off   63 -   64 -   64     0   1241  1250
-   -    -   -     R3=2k2     For-Off   99 -  101 -  102    22   1964  1550
|R| |R|  |R| |R|    R4=1k      F+B-Off  125 -  127 -  128    -4   2471  2550
|1| |2|  |3| |4|    R5=3k3     Off-Lft  152 -  154 -  155     0   3000  3000
-   -    -   -                Bck-Lft  164 -  166 -  167     0   3233  3250
| O |    | O |    Row=E24     For-Lft  174 -  175 -  176    -2   3411  3450
o   o    o   o    Tol=1%      Off-Rgt  196 -  196 -  197    -1   3837  3850
\ /      \ /                 Bck-Rgt  200 -  201 -  202     0   3920  3925
o        o                  For-Rgt  203 -  204 -  205    -1   3989  4000
|        |                  Off-R+L  211 -  212 -  213     0   4138  4150
+--------+-O ADC                                   Sum=   30
----    |
|--| R5 |-- +                                    Overlaps=    0
----                                  Iteration steps=  100
Successful iteration steps=   25
(C)2020 by DG4FAC                 Last iteration step on=   R2 (successful up)

; Assembler table
ResTable: ; First value: start of key, Second value: end of key plus 1
.db 0, 6 ; Off-Off, V=    0
.db 63, 65 ; Bck-Off, V= 1241
.db 99, 103 ; For-Off, V= 1964
.db 125, 129 ; F+B-Off, V= 2471
.db 152, 156 ; Off-Lft, V= 3000
.db 164, 168 ; Bck-Lft, V= 3233
.db 174, 177 ; For-Lft, V= 3411
.db 196, 198 ; Off-Rgt, V= 3837
.db 200, 203 ; Bck-Rgt, V= 3920
.db 203, 206 ; For-Rgt, V= 3989
.db 211, 214 ; Off-R+L, V= 4138
```
With the assembler table at the end one can easily write the assembler source code for recognition.

Some additional hints:
• With a tolerance of 5% no overlap-free resistor combination can be achieved.
• Those who want to change the voltages of the 11 combinations have to do that in the Pascal source code in the Array aeSh.
• Those who want to additionally detect if all four keys have been pressed, can add this state at the end of the assembler table. Calculate the range of this combination and the tolerances manually.

The following modification might be useful for those who need a fifth key, e. g. as alarm key or as emergency off: just add it to the matrix and connect it without a resistor to the operating voltage. And add a few codelines that detect if the ADC is at or near its upper end. Simple, isn't it?

Many success.

©2020 by http://www.avr-asm-tutorial.net