EDN logo


Design Ideas: September 15, 1994

Switch conserves microcontroller's I/O pins

Franklyn L Dahlberg Jr,
US Air Force, Hill AFB, UT



The I/O pins of small, inexpensive microcontrollers are usually at a premium. The PIC16C54 has only one and a half ports (12 I/O pins). Some processors have even fewer.

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 pin—but 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:

t=-RC ln((Vout-VIN)/VOUT)

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


| EDN Access | feedback | subscribe to EDN! |
| design features | design ideas | columnist |


Copyright © 1995 EDN Magazine. EDN is a registered trademark of Reed Properties Inc, used under license.