Page 344 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 344

11.2  Parallel Ports                                                321


        Note that DDRA is declared an unsigned char variable. Then, any time after that, to
        output a char variable i to PORTA, put
                                       PORTA = i;
        Note that PORTA is declared an unsigned char variable. To make PORTA an input
        port, we can write
                                        DDRA = 0;
        Then, any time after, to input PORTA into an unsigned char variable i we write
                                       i = PORTA;
        Generally, the direction port is written into before the port is used the first time and need
        not be written into again. However, one can change the direction port from time to time,
            PORTA and PORTS together, and their direction ports DDRA and DDRB together, can
        be treated as a 16-bit port because they occupy consecutive locations. Therefore, they can
        be read from or written into using LDD and STD instructions. To make PORTAB an
        output port, we can write in assembly language:
                               LDD #$FFFF ; generate all ones
                               STD $2         ; put them in direction bits for output
        Then, any time after that, to output accumulator D to PORTAB we can write
                               STD $0         ; output accumulator D
        To make PORTAB an input port, we can write
                               CLR $2         ; put zeros in high direction bits for input
                               CLR $ 3        ; put zeros in low direction bits for input
         Then, any time after that, to input PORTAB into accumulator D we can write
                               LDD $ 0        ; read PORTA into accumulator D
        Also, some of the 16 bits can be made input, and some can be output. In manner similar
        to how 8-bit ports are accessed in C, 16-bit ports can be declared in a header file that is
        #included in each program as follows:
                                 int PORTAB@0, DDRAB@2;
            To make PORTA and PORTS an output port, we can write: DDRAB = Oxf ff f;.
        Note that DDRAB is declared an int variable. Then, any time after that, to output an
        int variable i, high byte to PORTA and low byte to PORTS, we can write PORTAB =
        i;. Note that PORTAB is declared an int variable. To make PORTA and PORTS an
        input port, we write: DDRAB = 0;. Then, any time after that, to input PORTA (as high
        byte) and PORTB (as low byte) into an int variable i we can write i = PORTAB;.
        The ports A and B, or the combined port AB, can be made an input or output port and
        can be easily accessed in assembly language or in C.
            As a simple example of the use of an input port, consider a home security system.
        See Figure 11.4a. Three switches, each attached to a different window, are normally
        closed. When any window opens, its switch opens, and the pull-up resistor makes
        PORTA's input high; otherwise the input is low. This signal is sensed in PORTA bit 0.
        The C program statement if (PORTA & 1) alarm(); will execute procedure alarm if
        any switch is opened. It is optimally programmed into assembly language as follows:
   339   340   341   342   343   344   345   346   347   348   349