Page 252 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 252
8.4 Constants and Variables 229
A number of constants can be created using the enum statement. Unless reinitialized
with an "=" sign, the first member has value 0, and each next member is one greater than
the previous member. Hexadecimal values are prefixed with zero ex (Ox):
enum { BETA, GAMMA, DELTA = 0x5};
defines BETA to have value 0, GAMMA to have value 1, and DELTA to be 5.
Any scalar variable can be declared and initialized by a "=" and a value; for instance,
if we want global integers i, j and k to be initially 1, 2 and 3, we write a global
declaration:
int i=l, j=2, k=3;
C procedures access global variables using direct addressing, and such global variables
may be initialized in a procedure _startup that is executed just before main is started.
Initialized local variables of a procedure should generate machine code to initialize them
just after they are allocated each time the procedure is called. The procedure
void fun{){
int i, j, k; /* allocate local variables */
i = l; j = 2; k = 3;/ * initialize local variables */
>
is equivalent to the procedure
void fun{){
int i = l, j = 2, k = 3; /* allocate and init. local vars. */
}
A 16-bit element, three-element vector 31, 17, and 10, is generated by a declaration
int v[3] and stored in memory as (hexadecimal):
00 IF
0011
OOOA
and we can refer to the first element as v[ 0 ], which happens to be 31. However, the
same sequence of values could be put in a vector of three 8-bit elements, generated by a
declaration char u [ 3 ] and stored in memory as:
IF
11
OA
The declaration of a global vector variable can be initialized by use of an "=" and a list
of values, in curly brackets. For instance, the three-element global integer vector v can
be allocated and initialized by
int v[3] = {31, 17, 10};
The vector u can be similarly allocated and initialized by the declaration
char u[3] = {31, 17, 10};