EE 308
Homework #6
Due Mar. 4, 1999

Background on Interrupts on the HC12

To use interrupts on the HC12, you must do the following things:

  1. Load the stack pointer. (This is done for you in C by the ctrs.s startup routine.)
  2. Write an interrupt service.
    1. Do the stuff you need to do when the interrupt is received.
    2. Be sure that the interrupt service routine clears the source of the interrupt before exiting.
    3. Exit from the routine using an rti instruction rather than an rts instruction. In C, you need to tell the compiler to use an rti rather than an rts. With the Cosmic compiler you do this using @interrupt:
      @interrupt void my_isr(void)
  3. Do any setup needed for the interrupt.
  4. Clear the flag for the interrupt.
  5. Set the appropriate interrupt vector to point to your routine.
  6. Enable the specific interrupt your routine handles by setting the interrupt bit in the appropriate register.
  7. Enable interrupts in general by clearing the I bit of the Condition Code Register. This is done in assembly with the cli instruction, or in C with the enable(); function.

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 = 000
    2. if the prescaler bits PR2:0 = 101

  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 Overflow Interrupt Enable bit.
    3. The Timer Prescaler Bits.

  4. How do you clear the Timer Overflow 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 16 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 = 000
    2. if the RTI rate bits RTR2:0 = 101

  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. Add a Real Time Interrupt to your program of Problem 6. Set the RTI for an 65 ms rate. In the RTI interrupt service routine, use Port B to generate the turn signal pattern used in the previous two labs.

  12. Using your program of Problem 11, how long will it take for Port B to go through the complete pattern?