電子玩具的人機接口有時需要三態控制。一個單軸手柄可以定義向右運動、向左運動和不動的狀態。類似地,計數器有控制按鈕,允許計數器增加、減少和保持不變。工程師通常用兩個獨立的按鈕形成這些接口,需要兩個微處理器管腳。本設計方案提供了一個只使用Atmel公司的ATmega8微處理器的一個管腳,感知中間為關閉狀態的SPDT(單刀雙擲)開關三態的方法(參考文獻1和圖1)。表1為該電路的簡單程序(程序見英文源地址)。

微處理器的管腳狀態依靠DDR位的值、端口位和其外部連接。微處理器管腳用下拉電阻R1與地相連,阻抗典型為幾百千歐,在管腳上呈現出高阻狀態。設置DDR寄存器為0。當使用者鉗位開關到位置1時,管腳通過電阻R5連接到VDD,管腳位為1,不論端口位是什么值。當使用者鉗位開關到位置3時,管腳接地,管腳位為0,與端口位的值無關。在中心關閉狀態,管腳位服從端口位。表1總結了端口不同值和外部輸入的管腳狀態。

英文原文:
Three-state switch interface uses one microcontrolle
r 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, 2/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
“ATmega8/ATmega8L 8-bit AVR with 8K Bytes In-System Programmable Flash,” Atmel Corp, 2007.