EE 308 Spring 2006
C Language Programming, Interrupts and Timer Hardware
In this sequence of three labs, you will learn how to write simple C language programs
for the MC9S12 microcontroller, and how to use interrupts and timers.
WEEK 1
Introduction and Objectives
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 first lab. For example, the following C program increments Port B:
/* A C language program to increment Port B on a 68HC12 */
#include <iodp256.h> /* get the HCS12DP256 definitions */
#define D_1MS (24000/6) /* inner delay loop takes 6 cycles*/
/* 24000 cycles = 1 ms
*/
/* for 24 MHz clock */
#define TRUE 1 /* a normal C define */
void delay(unsigned int ms)
void main( ) /* the main program */
{
DDRB = 0xff; /* make all bits of Port B output */
PORTB = 0;
while (TRUE) /* do forever */
{
PORTB = PORTB + 1 /* increment Port B */
Delay(100); /* wait 100 ms */
}
}
/* Function to delay ms milliseconds */
void delay(unsigned int ms)
{
int i;
while (ms > 0 )
{
I = D_1MS;
while ( i > 0)
{
i = i – 1 ;
}
ms = ms – 1;
}
}
Figure 1. A C program to increment Port B.
Page 1