Three-state switch interface uses one microcontroller pin
A single microcontroller I/O pin senses the state of a three-position switch.
Kartik Joshi, Netaji Subhas Institute of Technology, New Delhi, India; Edited by Charles H Small and Fran Granville -- EDN, February 21, 2008
Human interfaces for electronic gadgets sometimes require three states for control. A single-axis joystick has states to define motions to the right, to the left, and with no motion. Similarly, a timer has control buttons that allow the timer to increment, decrement, and remain untouched. Engineers usually create these interfaces by using two independent pushbuttons, requiring two microcontroller pins. This Design Idea presents a way to sense three states of an SPDT (single-pole/double-throw) switch with a center-off state, using only a single pin of Atmel’s ATmega8 microcontroller (Reference 1 and Figure 1). Listing 1 is a simple program for the circuit.
The status of the pin of the microcontroller depends upon values of the DDR bit, the port bit, and its external connection. The microcontroller’s pin connects to ground using pulldown resistor R1 with resistance, typically, of a few hundred kilohms to impress the high-impedance state on the pin. You set the DDR register to zero. When the user toggles the switch to Position 1, the pin connects to VDD through resistor R5, and the pin bit is one, regardless of the value of the port bit. When the user toggles the switch to Position 3, the pin is grounded, and the pin bit is zero, regardless of the value of the port bit. In the center-off state, the pin bit follows the port bit. Table 1 summarizes the states of the pin for different values of the port and the external input.
| Reference |
|
-
Sam, yes I think that's what the sample code is doing.
The pulldown resistor R1 is very weak (330k) so the AVR's internal pullup can overcome it, so when the switch is in the centre position, the readable state in the PINB register follows the state written to POUTB, which controls the AVR's internal pullup if the DDRB bit is zero.
If the switch is in either end position, it overrides the state wanted by the pullup and pulldown resistors.
R5 (1.5k) between VCC and the switch would only be needed if the switch is actually two independent buttons that could be pressed at the same time, shorting VCC to GND. A single centre-off switch must be non-shorting by definition so R5 isn't needed.
Kris Heidenstrom - 2008-5-3 13:39:00 PST -
I found this article hard to understand.
Basically you are saying:-
Continuously toggle the internal pullup.
If the PIN is the same as the pullup state both on and off then its open.
If the PIN does not and its low then its position 1
If the PIN does not and its high then its position 2
Am I correct?
Sam Crumpet - 2008-28-2 13:14:00 PST


















