Applications of AVR Single chip controllers AT90S, ATtiny, ATmega and ATxmega DIP switches and resistors on an ADC input |

With the scheme to the left, the number of necessary pins can be reduced from 4 down to 1 if an AD converter input channel measures the voltage produced by the resistors.

A warning at the early beginning: do not try this with more than four switches, it does not work! Rather use two AD channels for 6 or 8 bit switches.

It is clear that the resistors have to be different: each switch has to produce roughly the double voltage increase than its previous one. Unfortunately the math of parallel resistors is a little bit strange:

And the resulting voltage of

is anything else than straight-forward and linear math.

So selecting the otimal resistors is a task for a large table calculation or, better, a dynamic iteration task.

- resistor values are organized in rows. The E12 row provides 12 different values er decade (1.0, 1.2, 1.5, ..., 8.2) while E24 has 24 values (1.0, 1.1, 1.2, 1.3, 1.5, ..., 9.1) per decade. Theoretically also E48 and E96 are defined but are not held in stock by electronic shops.
- resistor values are not exact due to production tolerances. Available tolerances are +/-5%, +/-2% and +/-1%. If you measure the resistor value manually you can achieve less tolerance but this is unsuitable for more than only a few devices.
- the ADC can measure in two modes: 8 bit or 10 bit. Higher accurancies require extra chips and are more subject to noise, so better forget this.

You can select

- the number of DIP switches (2, 3 or 4),
- the resistor row to be selected from (E12, E24 or E48 - if you find a trader offering that),
- the resistor tolerance (5%, 2%, 1% or 0.5% and 0.1% if you want to hand-select them),
- the ADC's resolution (8 or 10 bit).

By pushing the button "Iterate1" a resistor between 1 and N is randomly selected. For the next resistor in the row, for the previous resistor in the row and for its current value the averaged squared sum of differences between the voltage required and the resulting voltage is calculated. If the previous or the next resistor value produce less differences those are selected. A window appears that displays the resistors and the resulting differences, so you can follow the iteration process.

The button "Iterate100" repeats this 100 times without displaying interim results.

The resistors are displayed in the upper right corner. If you click onto a resistor line, you can change its value manually by clicking the "Change Rn" button. Resistor values are selected from the currently selected E row only.

The result window on the bottom displays

- the target value of the voltage (in mV/V),
- the nominal value of the voltage (resistors = nominal resistance),
- the lowest and the highest voltage value taking resistor tolerances into account),
- the nominal and the lowest and highest ADC readings for the selected resolution.

The button "Source code" writes an AVR assembler table like this (include source code is here):

```
;
; Table for recognizing mouse piano state
; 4 switches, ADC resolution = 1024
; Resistors E24, tolerance = 0.5%
; R0 = 4k7
; R1 = 68k
; R2 = 27k
; R3 = 10k
; R4 = 2k4
;
PianoTab:
.dw 66,68 ; N=1
.dw 150,154 ; N=2
.dw 199,203 ; N=3
.dw 325,330 ; N=4
.dw 356,362 ; N=5
.dw 398,404 ; N=6
.dw 423,429 ; N=7
.dw 675,680 ; N=8
.dw 683,688 ; N=9
.dw 694,700 ; N=10
.dw 701,707 ; N=11
.dw 722,728 ; N=12
.dw 728,734 ; N=13
.dw 737,742 ; N=14
.dw 742,747 ; N=15
.dw 0,0 ; No more stages
;
; (+)
; | __ / S4
; o--|R4|--O O--
; | -- |
; | __ / S3|
; o--|R3|--O O--o
; -- |
; | __ / S2|
; o--|R2|--O O--o
; -- |
; | __ / S1|
; --|R1|--O O--o--> ADC input
; -- |
; -
; | |R0
; |_|
; |
; ---
;
```

The button "Schematic" draws a graphic of the current results. By clicking
on it it can be saved as a PNG or BMP file.
©2018 by http://www.avr-asm-tutorial.net