Real Time Interrupts

The real time interrupt (RTI) of the 68HC11 is a periodic hardware interrupt which can be used to measure the elapsed time between the occurrence of certain events, or to trigger actions at fixed intervals. The free running clock (note that this is different from the free running counter) provides the timer source which allows interrupts to be generated.

The length of time between interrupts can be set using two bits (RTR1 and RTR0) in the Pulse Accumulator Control Register (PACTL).


PACTL - Pulse Accumulator Control Register
DDRA7 PAEN PAMOD PEDGE - - RTR1 RTR0
7 6 5 4 3 2 1 0

The period between interrupts is determined by the values assigned to RTR1 and RTR0, as shown in the following table.

RTR1 RTR0 Real Time Interrupt Period
0 0

4.10 ms

0 1

8.19 ms

1 0

16.38 ms

1 1

32.77 ms

In addition to specifying how often a real time interrupt will occur, the RTI system may be configured for polled or interrupt-driven operation. This configuration is performed by setting the RTII bit in Timer Mask Register 2 (TMSK2).

TMSK2 - Timer Mask Register 2
TOI RTII PAOVI PAII - - PR1 PR0
7 6 5 4 3 2 1 0

Interrupt-driven mode is specified by setting RTII to one - this will cause a hardware interrupt request to be generated at the end of every real time interrupt period. The system accomplishes this by setting the RTIF bit in the TFLG2 register to one - every time RTIF is set to one, an interrupt request occurs.

TFLG2 - Timer Flags Register 2
TOF RTIF PAOVF PAIF - - - -
7 6 5 4 3 2 1 0

The actions to be performed when an interrupt occurs are written into a software function known as an Interrupt Service Routine (ISR). Within the main program, the _VECTOR command is used to associate the ISR with the real time interrupt (as opposed to a different kind of interrupt). Note that, in addition to the other actions performed, the ISR function must clear the RTIF bit; otherwise, another interrupt request will be generated immediately when control is returned to the main program. The RTIF bit is cleared by writing a logic high to it.

In polled mode (specified by setting RTII to logic low), the RTIF bit will be set to one at the end of every RTI period, but this will not generate an interrupt. Rather, the RTIF bit must be checked by the program.

The sample code below uses the real time interrupt in interrupt-driven mode to count the number of seconds that elapse:

#include <EVB.h>

/* Function prototype */
_mod2_ void Interrupt_Handler(void); /* Interrupt Service Routine */

/* Global variables */
int Timecount=0;
long Seconds=0;

void main(void)
{
  _VECTOR (Interrupt_Handler,13);  /* associates the ISR "Interrupt_Handler" */
	                                 /* with a real time interrupt */
  _H11PACTL = 0x03;  /* set the real time interrupt rate to 32.77 ms */
  _H11TMSK2 = 0x40;  /* enable real time interrupt */

  while (1)
  {
  ...
  }
}

_mod2_ void Interrupt_Handler(void)
{
  Timecount++;
  if (Timecount == 30)  /* ~30 interrupts equal one second */
  {
    Seconds++;        /* increment number of seconds */
    Timecount = 0;
  }

  _H11TFLG2 = 0x40;  /* reset the real time interrupt flag */
}