Developing Embedded Software in C Using ICC11/ICC12/Metrowerks

Jonathan W. Valvano

Chapter 1: Program Structure
A sample program introduces C
C is a free field language
Precedence of the operator determines the order of operation
Comments are used to document the software
Preprocessor directives are special operations that occur first
Global declarations provide modular building blocks
Declarations are the basic operations
Function declarations allow for one routine to call another
Compound statements are the more complex operations
Global variables are permanent and can be shared
Local variables are temporary and are private
Source files make it easier to maintain large projects
Chapter 2: Tokens
ASCII characters
Literals include numbers characters and strings
Keywords are predefined
Names are user
Punctuation marks
Chapter 3: Literals include numbers characters and strings
How are numbers represented on the computer
8-bit unsigned numbers
8-bit signed numbers
16-bit unsigned numbers
16-bit signed numbers
Big and little Endian
Boolean (true/false)
Decimal numbers
Hexadecimal numbers
Octal numbers
Escape sequences
Chapter 4: Variables
A static variable exists permanently
A static global can be accessed only from within the same file
A static local can be accessed only in the function
We specify volatile variables when using interrupts and I/O ports
Automatic variables are allocated on the stack
We can understand automatics by looking at the assembly code
A constant local can not be changed
External variables are defined elsewhere
The scope of a variable defines where it can be accessed
Variables declarations
8-bit variables are defined with char
Discussion of when to use static versus automatic variables
Initialization of variables and constants
We can understand initialization by looking at the assembly code
Chapter 5: Expressions
Precedence and associativity
Unary operators
Binary operators
Assignment operators
Expression type and explicit casting
Selection operator
Arithmetic overflow and underflow
Chapter 6: Flow of Control
Simple statements
Compound statements
if and if-else statements
switch statements
while statements
for statements
do statements
return statements
goto statements
Null statements
Chapter 7: Pointers
Definitions of address and pointer
Declarations of pointers define the type and allocate space in memory
How do we use pointers
Memory architecture of the 6811 and 6812
Pointer math
Pointer comparisons
FIFO queue implemented with pointers
I/O port access
Chapter 8: Arrays and Strings
Array Subscripts
Array Declarations
Array References
Pointers and Array Names
Negative Subscripts
Address Arithmetic
String Functions defined in string.h
Fifo Queue Example
Chapter 9: Structures
Structure Declarations
Accessing elements of a structure
Initialization of structure data
Using pointers to access structures
Passing structures as parameters to functions
Example of MC68HC812A4 extended addressing
Example of a Linear Linked List
Example of a Huffman Code
Chapter 10: Functions
Function Declarations
Function Definitions
Function Calls
Parameter Passing
Making our C programs "look like" C++
Stack frame created by ICC11 and ICC12
Animation of ICC12 function call
Finite State Machine using Function Pointers
Linked list interpreter
Chapter 11: Preprocessor Directives
Using #define to create macros
Using #ifdef to implement conditional compilation
Using #include to load other software modules
Using #pragma to write interrupt software
Chapter 12: Assembly Language
How to insert single assembly instructions
How to compile with a mixture of assembly and C files
Assembler Directives
How to use assembly to optimize a C function
Appendix 1: Kevin Ross BDM - Adapt812 Interface
Kevin Ross's Background Debug Module
ICC12 options menu for developing software for the Adapt812
Adapt 812 Board Jumpers
What you need to get started
Development Procedure
Web sites for more information
Appendix 2. Metrowerks  TechArts 9S12C32 Setup
What you need to get started
Installing Metrowerks
Instructions for handling the Technological Arts 9S12C32 board
Development Procedure
Interrupt vectors for the 9S12C32
Web sites for more information.


Chapter 0 The Preface

Zero is an appropriate place for a book on C to start. Zero has many special meanings to the C programmer. On the 6812, zero is the address of Port A. On the 6811 zero is the first address in RAM. The compiler will initialize all global variables to zero on start-up. We use a zero to signify the end of a string. A pointer with a zero value is considered a null-pointer (doesn't point to anything). We use a zero value to signify the Boolean false, and true is any nonzero value. The array subscripts in C start with zero.

This document serves as an introduction to C programming on the Freescale 6811 and 6812 microcomputers. Its purpose is to provide a short introduction to C programming using the Metrowerks and ImageCraft ICC11 or ICC12 compilers (contact ImageCraft, 706 Colorado Ave. Suite 10-88, Palo Alto, CA 94303 or see (contact Metrowerks see ). While the ICC11/ICC12/Metrowerks manuals explain how to use their compilers, this document explains how to program in C for the Freescale 6811 and 6812. This document was written with a special emphasis on embedded systems. My philosophy about C compilers for the 6811/6812 is that there are three groups of engineers
a) I define a beginner as one who is developing very small programs (less than 1K bytes) with no budget (like a student). In this situation a free compiler is a requirement. Thus, the demo version of Metrowerks, or the freeware ICC11 version 1 could be used. Metrowerks does supply a limited 12k version for educational use.
b) Next in the progression is the intermediate programmer. This engineer has a budget, but it is very small. The object code size is larger than 1K bytes. Because these projects tend to involve a small number of engineers, the learning curve needs to be short. Because of the limited scope of the project, more time is spent on original coding/debugging and less time on software maintenance. In this situation, the low cost ICC11/ICC12 can be used.
c) For the professional developer, the software projects are large and have a high percentage of time allocated to maintenance, rather than original coding/debugging. For this environment, either ICC11/ICC12 or the full version of Metrowerks is appropriate.

This document differs from classical C programming books in its emphasis on embedded systems. While reviewing the existing literature on C programming I was stuck by the high percentage of programming examples in these books that rely on the functions scanf and printf to perform input/output. While I/O is extremely important for embedded systems, rarely is serial I/O with scanf and printf an important aspect of an embedded system. This HTML document is clearly not comprehensive rather it serves as a short refresher for those C programmers whose skills are a little rusty. This document also assists the experienced programmer trained in another language like Pascal or C++, that now wishes to program in C for an embedded 6811 or 6812 system. If the reader in interested in a more classical approach to C programming I suggest:

A Book on C: Programming in C, by Kelley and Pohl, Addison-Wesley

Send comments and suggestions about this document to:

The style and structure of this HTML document was derived from A Small C Compiler: Language, Usage, Theory, and Design, by James E. Hendrix.


Please do not distribute this document to others. Rather it is part of the software that accompanies the book,

Embedded Microcomputer Systems: Real Time Interfacing by Jonathan W. Valvano, Brooks/Cole Publishing Co., copyright (c) 2006.

Legal Statement