EE 308 -- LAB 04
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
     * January 12, 1996
     */
    #include <hc11.h>            /* Get the HC11 definitions */
    #define D_100MS 6667         /* To delay 100 ms */
    #define TRUE  1              /* A normal C define */
    
    /* Function to delay 100 milliseconds */
    void delay_100ms(void)
    {
        int i;
  
        i = 0;
        while (i < D_100MS)
        {
            i = i + 1;
        }
    }
    
    void main()                  /* The main program */
    {
        while (TRUE)             /* Do forever */
        {
            PORTB = PORTB + 1;   /* Increment Port B */
            delay_100ms();       /* Wait 100 ms */
        }
    }

Figure 1: A C program to increment Port B.

Pre-Lab

  1. To use the C compiler in the lab, you need to initialize some environment variables for DOS. This is done by giving the following command at a DOS prompt:

    e:\icc11\iccsetup

  2. Type in the above C program and give it the name inc.c. Compile the program with the following command:

    icc11 -l inc.c

    You should now have a file named inc.s in your directory. This is the assembly language file which the C compiler generated. Look at the file and try to understand what it does. Note that there will be some things which will 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.)

  3. Using the above program as a model, write C programs to implement the other functions from Lab 3.

The Lab

  1. Compile your inc.c program with the following command:

    icc11 -btext=0xb600 -bdata=0x0000 -d_stack=0x01ff inc.c

    This will produce a file inc.s19 which has the code at address 0xB600, the data at address 0x0000, and loads the stack pointer at address 0x01FF . Load this file into your HC11 and run it. (Remember that 0xB600 is in EEPROM, so you will have to load your file at 300 baud, as you did in Lab 2.) Verify that Port B increments.

  2. Change the value of the constant DELAY so that it takes 0.25 seconds to increment Port B. What value of DELAY did you use?

  3. Compile and run your other Lab 3 programs. Have an instructor verify that they work.



Bill Rison, <rison@ee.nmt.edu >
Fri Jan 12 1996
© 1996, New Mexico Tech