Page 342 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 342
11,1 Input And Output Devices 319
lines on the data bus, and the processor will copy the word on the data bus into some
internal memory register. The microcontroller can also write a word into an output port,
such as word 3, at a particular address in one clock cycle by putting the address on the
address bus, putting the word to be written on the data bus, and making the signal low
on the read/write line throughout the clock cycle. At the clock cycle's end, the
microcontroller will write the word on the data bus into the device. The written data wilt
become available on the device's output lines, until changed by another output
instruction.
For example, whenever the microcontroller writes data into location $0000, such as
in the instruction
STAA $00
the data written are put on the output lines of the device. This instruction can use the
page-zero addressing mode because the address is at location zero. The simplest output
port is writable but not readable: This "write-only memory" is usually a topic only for a
computer scientist's joke collection, but it is a real possibility in an output port.
For example, location $0000 may be an input port, and a hardware input device will
be built to input data from that port. Whenever the microcontroller reads data from
location $0000, as in the instruction
LDAA $00
the signals on the input lines of the input device will be read into the microcontroller
just like a word read in from memory. Note that an input operation "takes a snapshot" of
the data fed into the input device at the end of the last clock cycle of the load instruction
and is insensitive to the data values before or after that point in the last clock cycle. A
final aspect is whether the port can be read from or written in, or both.
In a sense, the basic input and output devices trick the microcontrollers. The
microcontroller thinks it is reading or writing a word in its memory at some address.
However, the microcontroller designer has selected that address as an input or an output
port and built hardware to input or output data that are read from or written into that port.
By means of the hardware, the designer tricks the microcontrollers into inputting data
when it reads a word at the address of an input port or into outputting data when it writes
data into the word at the address of an output port.
One of the most common faulty assumptions in port architecture is that I/O ports
are eight bits wide. For instance, in the 6812, the byte-wide LDAB instructions are used
in I/O programs in many texts. There are a number of 16-bit I/O ports on I/O chips that
are designed for 16-bit microcontrollers. But neither 8 nor 16 bits is a fundamental width.
In this chapter, where we emphasize fundamentals, we avoid that assumption. Of course,
if the port is 8 bits wide, the LDAB instruction can be used, and used in C by accessing a
variable of type char. There are also 16-bit ports. They can be read by LDD
instructions, or as an int variable in C or C++. A port can be 1 bit wide; if so, a 1-bit
input port is read in bit 7; reading it will set the N condition code bit, which a BMI
instruction easily tests. Many ports read or write ASCII data. ASCII data is 7 bits wide,
not 8 bits wide. If you read a 10-bit analog-to-digital converter's output, you should read
a 10-bit port.