单个微处理器I/O引脚感知三位置开关的状态。
电子玩具的人机接口有时需要三态控制。一个单轴手柄可以定义向右运动、向左运动和不动的状态。类似地,计数器有控制按钮,允许计数器增加、减少和保持不变。工程师通常用两个独立的按钮形成这些接口,需要两个微处理器管脚。本设计方案提供了一个只使用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.
<!-- 2008-3-4 20:28:42-->[1]





