Page 147 - Programming Microcontrollers in C
P. 147
132 Chapter 3 What Are Microcontrollers?
port has programmable I/O, it will have an associated data direction
register—DDRA, DDRB, and so forth. The ports are usually named PORTA,
PORTB, and so forth. DDRAis associated with PORTA. Each bit in DDRA
has a corresponding bit in PORTA. If a bit in DDRAis set, the corresponding
bit in PORTA is an output. The same is true for PORTB, PORTC, PORTD,
PORTE, and so forth if these ports exist on the part.
A port pin can be made into an output. When this occurs, this pin
becomes a latched output. In other words, when this bit is set it will
remain set until it is reset by the program, and vice versa. Just because a
port pin is designated to be an output does not mean that its state cannot
be read by the computer. When a port is read in, the state of all of the
outputs as well as the state of the inputs will be shown in the result.
Some I/O pins are multiplexed and serve multiple functions. For
example, microcontrollers with analog-to-digital converters, ADC,
usually allow the ADC pins to serve as digital input pins as well. In
that case you need merely read the input port, and those pins that are
above the high threshold will indicate one, and those below the low
threshold will indicate zero. Reading the port does not affect the
ADC operation at all.
Analog-to-Digital Converters
The ADC subsystem on most microcontrollers consists of a single
successive approximation analog-to-digital converter preceded by an
analog multiplexer that can switch the converter to any of several input
pins. The program controls this switching. The electromagnetic
environment of the surface of a microcontroller die is about as bad as
can be found anywhere. Therefore, attempts to do fine resolution
measurements of analog voltages in these parts is fraught with problems.
Most ADCs use a resistive ladder to act as a digital-to-analog converter.
The inputs to this ladder are sequenced in a prescribed manner to build
a voltage that matches the voltage being measured. The input to the D-
to-A is then the digital equivalent to the voltage being measured.
Precision resistors are very difficult to manufacture on silicon,
and even precision matching between resistors is extremely difficult.
While making precision capacitors is very difficult on a silicon die,
it is possible to make several capacitors with highly accurate ratios
between the capacitor values. Therefore, the approach is to use a set
of matched capacitors and a charge balance technique to accomplish