EE 308 - LAB 7

Final version for 2001

HC12 Timer Interrupts

First we will explore the timer and the TCNT register. Then we will write a simple program which uses the timer overflow interrupt and the input capture interrupt.


You should write the program for Part 7 (which includes the program for Parts 4 and 5). You should also answer the questions for Part 4 (a) and (b).

The Lab

Connect your HC12 to your computer. At the D-Bug12 prompt, display the contents of the TCNT register. Do this several times. How do the values compare?

Use D-Bug12 to modify the TSCR register to enable the counter. Repeat Part 1.

Use D-Bug12 to modify the TSCR register to disable the counter. Repeat Part 1.

Start with the following program, which is just a do-nothing infinite loop:
             #include <hc12.h>

             #define TRUE 1

                 DDRA = 0xff;       /* Make all bits of Port A output */
                 PORTA = 0x00;
                 while (TRUE) 

Add a Timer Overflow Interrupt service routine to increment Port A. Do this with the prescaler set to 0, so the overflow rate is 8 ms. Answer the following questions:

Calculate how long it should take Port A to overflow. Measure this time. How long does it take Port A to overflow?

Change the prescaler so the TOF interrupt will occur every 262 ms. Calculate how long it should take Port A to overflow. Measure this time. How long does it take Port A to overflow?

What happens when you fail to clear the TOF flag as part of your TOF interrupt service routine? Check your conjecture by commenting this line out of your program and running the new program. When done, be sure to restore your program to clear the TOF flag.

Add the following to the program from Part 4. (Keep the TOF rate at 262 ms.) Set up Bit 3 of Port T for output compare. Generate a 250 Hz square wave on this pin. Verify the frequency of the square wave with the logic analyzer.

Connect one of your debounced switches to Input Capture 2 (Port T, Bit 2), as shown below:

Figure 1: Circuit to measure speed of button pushing.
\epsfig{file=lab06_fig1.eps, width=6in}\end{center}

The right part of Figure 1 is what the signal to IC2 will look like if you push the pushbutton twice.

Add to your program code to measure the time TR between the two falling edges of the signal in Figure 1. Use input capture interrupts to determine the times the pushbutton is pushed.

It is inconvenient to have to stop the HC12 and look at a memory location to determine what your result was. D-Bug12 has built in routines to write information to the terminal. This is documented in the application note Using the Callable Routines in D-Bug12 included with your HC12 board. In your program, TR should be a sixteen-bit unsigned number. D-Bug12 provides a C-like printf() function you can use to print information to the screen. The information below shows some examples of how to do this.

To use the D-Bug12 routines you will have to include the header file DBug12.h. This can be downloaded from the EE 308 homepage.

Here is a program to print hello, world to the terminal:

#include "DBug12.h"

    DBug12FNP->printf("hello, world\n\r");

Here is a program to print an unsigned number to the screen in both hexadecimal and decimal:

#include "DBug12.h"

    unsigned int x;

    x = 0xf000;
    DBug12FNP->printf("Hex:  %x, Decimal:  %u\n\r",x,x);

Add to your program a printf() function to print out the result - the number of timer ticks between pushes of the button. (Do not try to calculate the actual time as a floating point number. When you use floating point numbers the programs will be too large to load into the HC12 without using expanded memory.) You should write your program as an infinite loop so that after pressing the button twice your program will print out the result, the go wait for the next two presses.

Test your program on your EVBU. See how fast you can push the switch twice.

Note: to compile a program with interrupts, be sure to have your interrupt vector array in a separate file called vector.c. Change you cc.bat program to look like this:

  cx6812 -vl -ax +debug crts.s %1.c vector.c
  clnk -o %1.h12 -m %1.lkf
  chex -o %1.s19 %1.h12
  clabs %1.h12

The interrupt vectors for DBug-12 start at address 0x0b10. To tell the linker to put the interrupt vector file at the right place, change your lkf file to look like this:

  # link command file for test program
  +seg .text -b 0x0800 -n .text # program start address
  +seg .const -a .text          # constants follow code
  +seg .data -b 0x0900          # data start address
  crts.o                        # startup routine
  lab06.o                       # application program
  +seg .const -b 0x0b10         # interrupt vectors start here
  vector.o                      # file of interrupt vectors
  +def __stack=0x0A00           # stack pointer initial value

Bill Rison