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
- What is the address of the 16-bit free-running counter in the HC12?
 - Consider an HC12 using a 16 MHz crystal (8 MHz E clock).  How often will the
TOF interrupt be called
- if the prescaler bits PR2:0 = 001
 - if the prescaler bits PR2:0 = 100
 
 - Give the name of the register, the register address, and the apropriate
bit(s) for the following functions:
- The Timer Overflow Flag.
 - The Timer Prescaler Bits.
 - The Timer Overflow Interrupt Enable bit.
 
 - How do you clear the Timer Overflow Interrupt Flag?  Write a line of C
code which will clear the Timer Overlow Interrupt Flag.
 - 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.
 - How long will it take Port A to overflow, using your program of
Problem 5?
 - Consider an HC12 using a 16 MHz crystal (8 MHz E clock).  How often will the
RTI interrupt be called
- if the RTI rate bits RTR2:0 = 001
 - if the RTI rate bits RTR2:0 = 100
 
 - Give the name of the register, the register address, and the apropriate
bit(s) for the following functions:
- The Real Time Interrupt Flag.
 - The Real Time Interrupt Enable bit.
 - The bits which control the Real Time Interrupt rate.
 
 - To enable the RTI interrupt, which bit of which register must you set?
 - How do you clear the Real Time Interrupt Flag?
 - 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.
- What should the timer prescaler be set to?
 - Write some C code which will set up Bit 2 of Port T to capture the time of
a rising edge.
 - Write an interrupt service routine which will read the time of the edge
captured by IC2 and store it in a variable called "first".
 
 -  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.
 
 
 
Bill Rison,
<rison@ee.nmt.edu >