Page 279 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 279
256 Chapter 9 Implementation of C Procedures
A C program can have global variables, and it can have global constants. Global
constants are initialized by writing them into RAM when the program is downloaded or
in ROM in a stand-alone microcontroller. In the HiWare compiler, a #pragma
into_rom preceeds the storage of such constant variables declared as const char, and
the like.
Global variables (as opposed to global constants) must be initially cleared, unless
they are indictated as being initialized to some other value, before main() is executed,
The following program segment can be used to clear 39 bytes beginning at $800:
LDX #$800 ; initialize pointer to beginning of global storage
LDD #3 9 ; initialize counter to number of bytes in global storage
L: CLR 1, X+ ; clear a byte
DBNE D r L ; loop until all bytes cleared
If a global variable is declared and initialized, as in the statement
unsigned char guc = 4; int gsi = 4;
then the following assembly-language program segment should be executed after the
above described program segment that clears all global variables, before main is called.
LDD #4 ; generate the constant 4 for both assignments
STAB guc ; store low-order 8 bits into char variable
STD gsi ; store all 16 bits into int variable
Finally, we discuss I/O access as a variant of global addressing. The 6812's main
I/O ports are on page zero. They can be accessed as if they are global variables:
volatile unsigned char PORTA, PORTB, DIRA, DIRB
which are linked to a segment at location 0. This is equivalent to an assembler sequence:
org 0 ; put this "global data" at the beginning of I/O (0)
PORTA ds. B 1 ; port A is at location 0
PORTB ds. B 1 ; port B is at location 1
DIRA ds. B 1 ; port A direction register is at location 2
DIRB ds. B 1 ; port B direction register is at location 3
An output statement to output 5 to port A is written PORTA = 5; which is implemented
LDAB #5 ; generate constant 5
STAB PORTA ; write it to the output port
An input statement guc = PORTA; to input port A to guc is implemented
LDAB PORTA ; read input data
STAB guc ; write it into global variable guc