Hardware Reference
In-Depth Information
5.10 Using the C Compiler
There are many C compilers that support the HCS12 microcontroller. The freeware GNU
C compiler has a port for HCS12 and Eric Engler wrote an IDE (referred to as EGNU) that pro-
vides a simple development environment for the GNU C compiler. The combination of the
EGNU IDE and the GNU C compiler for the HCS12 is free to the user. The EGNU IDE contains
a text editor, a project manager, and a terminal program and works with D-Bug12 monitor.
Freescale provides a special edition of CodeWarrior IDE that supports the entering, compilation,
and source-level debugging of C programs for the HCS12 up to the 32-kB size limit. CodeWar-
rior supports the serial monitor and several BDM debuggers. The ImageCraft C compiler is
also popular in academic institutions. The tutorials for using CodeWarrior and ImageCraft C
compilers will be given in this chapter whereas the tutorial for using the GNU C compiler and
EGNU IDE is given in Appendix E.
5.10.1 Issue in Accessing Peripheral Registers
An important part of microcontroller programming is to access peripheral registers.
An address is assigned to each peripheral register. A C program may assign a value to a pe-
ripheral register or read the contents of a peripheral register. As we have done in Section 5.8, we
assign a value to the name of a register in order to write to a register. Referring to the C language
syntax, the peripheral name actually refers to the contents of a memory location. On the other
hand, a peripheral register may be 8-bit (unsigned character type) or 16-bit (unsigned integer
type). Therefore, a peripheral register should be declared using one of the following methods:
j #define reg_name *(volatile unsigned char *) reg_addr
j #define reg_name *(volatile unsigned int *) reg_addr
The phrase “volatile unsigned char *” typecasts reg_addr to a pointer to unsigned charac-
ter whereas “volatile unsigned int *” typecasts reg_addr to a pointer to unsigned integer. After
applying the dereferencing operator *, we can assign a value to the register name to change its
value. The keyword volatile is used because the register contents may be changed by external
events instead of program assignment.
The HCS12 allows all peripheral registers as a block to be remapped to other memory loca-
tions. To support this, the reg_addr is written as the sum of a base address and an offset . By
default, the register block base address is set to 0x0000. The previous declaration can be rewrit-
ten as
#define reg_base 0x0000
#define reg_name1 *(volatile unsigned char *) (reg_base 1 offset1)
#define reg_name2 *(volatile unsigned int *) (reg_base 1 offset2)
This method is used in ImageCraft C compiler MCU header fi le. For example, the Port A data
register PORTA is defi ned as
#define REG_BASE 0x0000
#define PORTA (*(volatile unsigned char *)(REG_BASE 1 0x00000000))
Of course, there are other methods for defi ning the peripheral register name. The header fi le
hcs12.h provided in the complementary CD uses macro substitution as follows:
#define IOREGS_BASE
0x0000
#define _IO8(off)
*(unsigned char volatile *)(IOREGS_BASE 1 off)
#define _IO16(off)
*(unsigned short volatile *)(IOREGS_BASE 1 off)
#define PORTA
_IO8(0x00)
// port A data register
 
Search WWH ::




Custom Search