EE 308 -- LAB 05
C Programming Language
Introduction
The C programming language is used extensively in programming microprocessors. In this lab you will write some simple C programs which do the things you did in assembly language in the last lab.
For example, the following C program increments Port B:
/* A C Language Program to Increment Port B on a 68HC11 * Bill Rison * February 13, 1997 */ #include <hc11.h> /* Get the HC11 definitions */ #define D_100MS 6667 /* To delay 100 ms */ #define TRUE 1 /* A normal C define */ void delay_100ms(void); void main() /* The main program */ { PORTB = 0; while (TRUE) /* Do forever */ { PORTB = PORTB + 1; /* Increment Port B */ delay_100ms(); /* Wait 100 ms */ } } /* Function to delay 100 milliseconds */ void delay_100ms(void) { int i; i = 0; while (i < D_100MS) { i = i + 1; } }
Figure 1: A C program to increment Port B.
The Lab
icc11 -l inc.c -m -btext:0x0100 -bdata:0x0000 -d_stack:0x01ff
You should now have the files inc.s, inc.s19 and inc.map in your directory.
_start
and a function
_exit
. Find the addresses of these functions.
_start
and the _exit
functions. What do these do? (Hint: the
_start
function should initialize the stack pointer, call the
function _main
, and finally call the function
_exit
.)
icc11 -l inc.c -m -btext:0xb600 -bdata:0x0000 -d_stack:0x01ffThis will compile your program so your code will go into EEPROM at address B600. If you use tables of initialized data in your program, these should be loaded into EEPROM as well. You can do this by putting a const in front of the type specifier -- e.g.,
const unsigned char table[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};Load your program into EEPROM. Note you will have to change the baud rate to 300 as you did in Lab 4. Verify that the program runs from EEPROM, and that it remains in memory after a power cycle.