Page 179 - Programming Microcontrollers in C
P. 179
164 Chapter 4 Small 8-Bit Systems
piled code. The M68HC05 family is a family of 8-bit machines. There
has been no discussion of the programmers’ register model of these
devices. Programmer models of the larger microcontrollers will be
discussed because knowledge of the programmers’ model might help
in crafting good C code. For these small machines, the watchword is
8-bit. The internal structure of the system is all 8-bit. The width of
the single index register is 8 bits, and the width of the accumulator is
also 8 bits. The program counter is more than 8 bits in most cases,
but it is wide enough to address only the range of the internal com
puter memory. In fact, the width of the stack pointer in the
M68HC05Bx family is only 6 bits. There is no luxury of spare bits in
any register.
Therefore, when writing code for the M68HC05 family, keep fore
most in your mind that you are dealing with an 8-bit device. If at all
possible, avoid 16-bit operations because they will always result in larger
memory and/or code usage. The following code demonstrates an ex
ample of the careless use of 16-bit implied code in an 8-bit machine.
Consider the erase() routine from above. This function could
have been written as follows:
void erase(int *x)
{
EEPROM_CTL.E1LAT=1; /* set the E1LAT bit */
EEPROM_CTL.E1ERA=1; /* set the E1ERA erase bit */
*x=0; /* select the address */
EEPROM_CTL.E1PGM=1; /* turn on the charge pump*/
delay(PROG_TIME); /* wait the appropriate time*/
EEPROM_CTL.E1LAT=0; /* reset the E1LAT bit turns
off both E1PGM and E1ERA
bits */
} /* return when done */
The only change in this version is to pass the integer *x to the
function by reference. Remember, since all addresses in the M68HC05
family of parts are greater than 8 bits, the compiler must handle the
transfer of the pointer x as a 16-bit number. The statement *x=0;
compiles into an inline function at the address range 0x81d to 0x82c in
the compiled version of the code shown below. This function creates a
subroutine that does an indexed store with a 16-bit offset. First, the
value to be programmed is placed in the accumulator. Then the op