Freescale Semiconductor
Order this document
by AN1775/D Rev. 1
AN1775
Freescale Semiconductor, Inc...
Expanding Digital Input with an A/D Converter
By Brad Bierschenk
Freescale
Applications Engineering
Austin, Texas
Introduction
This application note documents a method of extending digital input
using the analog-to-digital converter (ADC) of a microcontroller unit
(MCU).
Many MCU applications require digital input and arbitration. For
example, determining which key of a keypad was pressed. This is
commonly done by arranging switches in a matrix configuration,
connecting to a series of digital input pins, and reading a digital input
data register to determine which key was pressed. While this method is
easily implemented, it does require the use of an MCU’s parallel port
pins.
Some applications require all available bidirectional or input-only pins for
other purposes. In such a case, an alternate method of arbitrating
keypresses is desired. By using the ADC of an MCU connected to a
resistor ladder, user input can be more efficiently processed.
© Freescale Semiconductor, Inc., 2004. All rights reserved.
© Motorola, Inc., 1998
AN1775 Rev. 1
For More Information On This Product,
Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note
Background
Dedicated Input
A microcontroller typically receives user input through digital input pins.
The simplest implementation is a single switch directly connected to a
digital input pin. This is easy to realize, but is not the most efficient use
of resources, with one pin dedicated to one input. One port data bit
represents the state of one switch.
Another method uses a keypad, a common element in embedded
systems. These are ordinarily arranged in a matrix, as shown in
Figure 1.
In this case, the byte value of an entire port data register can
be polled to determine which key was pressed. This is more efficient, as
a 4 x 4 keypad can interface 16 keys with eight input pins.
Freescale Semiconductor, Inc...
Matrix Input
Px0
Px1
Px2
Px3
Px4
Px5
Px6
Px7
Figure 1. Resistor Matrix Keypad Using Parallel Port Pins
AN1775 Rev. 1
For More Information On This Product,
Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note
Implementation
ADC Alternative
In many cases, input pins are at a premium. One can't always freely
assign input pins to the function of user input. A more efficient use of
microcontroller resources can be devised. One common feature of many
Freescale MCUs is the analog-to-digital converter, or ADC.
The ADC of a Freesca;e MCU usually features four to eight channels of
analog input, which is compared with a reference voltage and converted
to an 8-bit digital value. When a resistor ladder is connected to an analog
input through switches in each segment, the conversion result can be
used to arbitrate an input. This allows many keys to be interfaced with
one input pin, with only a little more software overhead.
Figure 2
shows
such an implementation.
V
DD
Freescale Semiconductor, Inc...
MCU
ADC
INPUT
PIN
V
In
Figure 2. Resistor Ladder Keys Using an Analog Input Pin
Implementation
ADC Operation
An MCU ADC typically has 8-bit
precision.
This means there are 2
8
, or
256, distinguishable A/D inputs, including 0. The analog inputs are
converted to a binary number, which represents the magnitude of the
input voltage in relation to a reference voltage.
AN1775 Rev. 1
For More Information On This Product,
Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note
The
range
of an ADC is the difference between its high and low
reference voltages. This means an analog input between V
REFH
and
V
REFL
will convert to an 8-bit number, with V
REFL
converting to $00 and
V
REFH
converting to $FF.
The
resolution,
defined as the range divided by the precision, defines the
analog step that a change in one least significant bit (LSB) represents.
In the case of a 5-volt, 8-bit ADC, the resolution is 5/255 (volts), or
19.6 mV. This means that a change in one LSB in the ADC data register
reflects a change of about 20 mV at the analog input.
Consider a resistor ladder connected to an ADC input, as shown in
Figure 3.
Because this arrangement is a voltage divider, each segment
in the ladder can alter the voltage at the input when grounded. If
switches are provided at each segment, one can selectively ground that
segment, altering the composition of the divider, and thus altering the
voltage presented to the ADC pin.
In this way, software can determine which switch in the ladder was
selected by reading the resulting A/D data value. The resistor R0 acts as
a pullup to maintain V
DD
on the analog input line while no keys are
active. Thus, a conversion value of $FF indicates that no key has been
pressed.
V
DD
Freescale Semiconductor, Inc...
Resistor Ladder
Voltage Divider
R0
ADC
INPUT
R2
V
In
K1
K2
R3
K3
R4
K4
R5
K5
R6
K6
R7
R8
K7
K8
Figure 3. Digital Input Alternative
AN1775 Rev. 1
For More Information On This Product,
Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note
Implementation
Considerations
Using this method, one can theoretically connect 255 input switches to
one ADC pin. However, there are many potential sources of
inaccuracies, which make it impractical to connect so many key inputs.
One should account for some error padding.
One inaccuracy is provided by the resistors themselves. Resistors are
categorized according to their variance from a labeled value. The
application should be tolerant to the precision of the resistors being used
(typically, 1 percent or 5 percent). Also, a calculated resistor value might
not be a commonly available value, so the user should plan for a range
of resistor values.
Typically, an 8-bit ADC is accurate within two least significant bits. This
should be accounted for as well. The best way to allow for these
tolerances is to assign to each key switch a range of resulting A/D data
register values.
If an analog input falls within a particular range, one can determine that
the key was pressed. By adjusting the range of ADC results which
represent a given keypress, the user can change the error margin for the
application.
To ensure the best ADC accuracy, the full range of the converter should
be used. In cases where the high reference is not variable, it is typically
fixed at the operating voltage.
A spreadsheet is a good way to determine resistor values and A/D result
ranges. An example of computing values for
Figure 3
are shown in
Table 1.
Resistor Precision
Freescale Semiconductor, Inc...
ADC Accuracy
AN1775 Rev. 1
For More Information On This Product,
Go to: www.freescale.com