EE 308
Homework #6
Due March 1, 2000

Below is a program which uses the Timer Overflow Interrupt. It interrupts the HC12 every 33 ms. On each interrupt, it writes the one's complement of Port A to Port B.

```        /*
* Program to use Timer Overflow Interrupt
*/

#include "hc12.h"
#define TRUE 1

main()
{

DDRA = 0x00;    /* Make Port A input */
DDRB = 0xff;    /* Make Port B output */
TSCR = 0x80;    /* Turn on timer */
TMSK2 = 0x82;   /* Enable timer overflow interrupt, set prescaler */
TFLG2 = 0x80;   /* Clear timer interrupt flag */
enable();       /* Enable interrupts (clear I bit) */

while (TRUE) { }       /* Do nothing for now */
}

/*
* Tell the HC12 what to do when it receives a TOF interrupt
*/
@interrupt void tof_isr(void)
{
PORTB = ~PORTA;        /* Write 1's complement of A to B */

TFLG2 = 0x80;          /* Clear source of interrupt */
}
```

### Homework Problems

1. What is the address of the 16-bit free-running counter in the HC12?

2. Consider an HC12 using a 16 MHz crystal (8 MHz E clock). How often will the TOF interrupt be called
1. if the prescaler bits PR2:0 = 001
2. if the prescaler bits PR2:0 = 100

3. Give the name of the register, the register address, and the apropriate bit(s) for the following functions:
1. The Timer Overflow Flag.
2. The Timer Prescaler Bits.
3. The Timer Overflow Interrupt Enable bit.

4. How do you clear the Timer Overflow Interrupt Flag? Write a line of C code which will clear the Timer Overlow Interrupt Flag.

5. Using the above program as a model, write an interrupt service routine which increments Port A whenever the timer overflows. Set the timer overflow interval to be 32 ms.

6. How long will it take Port A to overflow, using your program of Problem 5?

7. Consider an HC12 using a 16 MHz crystal (8 MHz E clock). How often will the RTI interrupt be called
1. if the RTI rate bits RTR2:0 = 001
2. if the RTI rate bits RTR2:0 = 100

8. Give the name of the register, the register address, and the apropriate bit(s) for the following functions:
1. The Real Time Interrupt Flag.
2. The Real Time Interrupt Enable bit.
3. The bits which control the Real Time Interrupt rate.

9. To enable the RTI interrupt, which bit of which register must you set?

10. How do you clear the Real Time Interrupt Flag?

11. Consider an HC12 using a 16 MHz crystal (8 MHz E clock). You want to measure the time difference between two rising edges on Bit 2 of Port T. The maximum time difference will be 250 ms.
1. What should the timer prescaler be set to?
2. Write some C code which will set up Bit 2 of Port T to capture the time of a rising edge.
3. Write an interrupt service routine which will read the time of the edge captured by IC2 and store it in a variable called "first".

12. Write the program for Lab 6. Note that to determine the time difference you need to enter the same interrupt service routine twice. To make this work you should have a global flag with some initial value (say 0). When you enter the ISR, if the flag has a value of 0, you know it is the first time you entered, and you can set first to the time, and change the global flag to have a value of 1. When you enter the ISR, if the flag has a value of 1, you know it is the second time through, and you can set second to the time, calulate the time diffence(second - first), and set the global flag to 2. The main program monitors the value of the flag. When it sees the flag with a value of 2, it prints the time to the screen (discussed here) and resets the flag to 0, so you can repeat the process.