Path: Home =&ht; AVR_EN => Applications => Key matrix => Stacked keys   Diese Seite in Deutsch: Flag DE Logo
Forward/backward and right/left symbol 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).

Schematic of the forw/backw and right/left combination with resistors 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).

Fractions of the operating voltage on the ADC 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

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 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:

Five keys 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