
My application required a single-pole, six-position rotary switch for six different user selections. The direct approach would require six µP pins, one for each selection, plus a pullup resistor for each pin.
If conserving I/O pins is your objective, the circuit in Fig 1 shows a better approach using A/D conversion. This circuit requires only one input pin and one output pinbut uses more processing time than the straightforward approach. The program in Listing 1 enables the processor to display the number corresponding to the switch position selected.
The resistors Rthrough Rcon- nected to each switch position and the capacitor Cset up a different RC time delay for each position. Software keeps the pin at RA0 (output) low until it is time to read the rotary switch. RA0 then goes high. The software then starts an appropriately scaled counter and then monitors the input pin RA1 until the pin goes high. You can tell which position the user selected by reading the counter's value at this point.
You may have to measure the logic threshold voltage level of the input pin to make the time-delay calculations correct for a different processor. The following equation approximates the time delay:
where Vis the rated asserted voltage level for an output pin, Vis the threshold voltage level for which the processor considers the input high, C corresponds to the timing capacitor C1, and R is the equivalent Thevenin resistance of the resistor values for the desired switch position (R1+R2+...).
The range of the counter values for a particular position will depend on the precision of its resistor. You can implement the counter in software or you can use the processor's hardware counter. You can also substitute a keypad for the rotary switch. EDN BBS /DI_SIG #1365