Page 345 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 345
322 Chapter 11 Input/ Output
Figure 11.4. Simple Devices
BRCLR 0, #1, *+6 ; branch over BSR if bit one of PORTA is low
BSR alarm ; otherwise call the subroutine
As a simple example of the use of an output port, consider the light-emitting diode
(LED) display shown in Figure 11.4b. When PORTA's output is high, current flows
through the transistor and LED, being limited by the 330 Q resistor. The C program
statement PORTA = 1; will cause the LED to light up. It is optimally programmed
into assembly language as follows:
LDAB #1 ; generate a one
STAB $0 ; output it to the port
11.3 Input and Output Software
Input or output of a single word is simple, but we often need to input or output a string
of characters, an array of numbers, or a program consisting of many words. This section
reviews how vectors can be used in these situations.
The simplest and one of the most common situations occurs when one is inputting
or outputting a vector of bytes. To output a vector of bytes to PORTA, smallest indexed
byte first, execute the following C procedure:
char buffer[0x10];
void main() { char i = -0x10;
DDRA = Oxff; do PORTA = buffer[i + 0x10]; while(++i);
}
An optimized assembly language program segment for the body of this C procedure is
LDD #$FFFO ; Put $FF in Accumulator A, -$10 in Accumulator B
STAA $2 ; Put $FF in direction, to make it output
LDX #BUFFER+ $ 10 ; Set index register X to just beyond end of vector
LOOP: LDAA B, X ; Get an element of the vector
STAA $0 ; Write it to the output port
IBNE B, LOOP ; For each element of the vector