EE 308 -- LAB 05
C Programming Language

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

  1. Type in the above C program (or download it from here) and give it the name inc.c. Open a 4DOS window. At the DOS prompt, give the command iccsetup to set some environment variables needed by the C compiler. Compile the program with the following command:
         icc11 -l inc.c -m -btext:0x0100 -bdata:0x0000 -d_stack:0x01ff

    You should now have the files inc.s, inc.s19 and in your directory.

    1. inc.s is the assembly language file generated by the C compiler. Look at the file and try to understand what it does. Note that there may be some things which do not make sense to you. At the very least, find the assembly language code which adds one to what was in Port B, and stores it back into Port B. (Note that the C compiler produces assembly code in decimal rather than hexadecimal. Port B has an address of 0x1004, or 0d4100. You'll want to see where inc.s accesses address 4100.)

    2. Look at the file This shows the addresses of the start of the functions in the program, as well as the addresses of any variables. Note that the function and variable names are preceded by an underscore. Note also that there is a function _start and a function _exit. Find the addresses of these functions.

    3. Look at the file inc.s19. This contains the op codes that will be loaded into the HC11. Reverse assemble the _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.)

  2. Load the file inc.s19 into your HC11 and run it. Verify that Port B increments.

  3. Using inc.c as a model, write a C program to implement the functions from Lab 4.

  4. Compile and run your program. Have an instructor verify that it works.

  5. Compile the above program with the command
         icc11 -l inc.c -m -btext:0xb600 -bdata:0x0000 -d_stack:0x01ff
    This 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 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.

Bill Rison, < >
Thr Feb 13 1997

© 1997, New Mexico Tech