next up previous
Next: Writing the ISR Up: Output Compare Interrupts Previous: How do I install

Initializes the interrupt handler

Initialization of a hardware interrupt is done in the function init(). The basic things that must be done are

  1. initialization of any global variables and counters in the ISR
  2. arming the interrupt
  3. acknowledging any previously caught interrupts
Since this learning module is interested in installing an ISR for an output compare event, we'll focus on the initialization of this particular interrupt. In particular, we'll initialize the output compare 4 or OC4 interrupt.

The following source codes shows the listing for the init() function that only initializes the output compare event. This function is called at the start of any program you write. In earlier learning modules you used init() to initialize the SCI subsystem, but in the following example, we only show the instruction required to initialize the OC4 interrupt.

 void init(void){
   asm(" sei");
   CONFIG=0x04;  //disable watchdog timer
   _Time=0;
   TMSK2 = 0x03;
   TMSK1 |= OC4I;
   TFLG1 |= OC4F;
   TOC4 = TCNT + 256
   asm(" cli");
}

The first and last lines of this function are assembly language instructions that disable/enable interrupt handling. The first instruction (asm(" sei")) sets the I bit in the condition code register, thereby disabling any interrupts. This prevents us from catching an interrupt while we're trying to initialize the system. The last instruction (asm(" cli")) clears the I bit, thereby re-enabling interrupt handling.

The second instruction in the program disables the watchdog timer. The watchdog timer is a special hardware timer that automatically resets the micro-controller at specified time intervals. The watchdog timer is used to restart a system that may be "hung". In our case, we don't want this to happen, so the second instruction in init() is used to disable the watchdog timer. In all of the programs used in these learning modules, we run with the watchdog timer disabled.

The main body of the function (lines 3-7) initializes the OC4 interrupt. The first instruction is this block (_Time=0) zeros a global variable, _Time. This global variable is a counter that keeps track of the number of times the OC4 handler has been executed. Due to its global nature, _Time is accessible by all functions in your program. So all of your functions can treat _Time as a variable that holds the current real-time.

The second instruction (TMSK2 = 0x03) sets the rate at which the TCNT counter is incremented. In particular, setting TMSK2 to verb!0x03! causes TCNT to be incremented once every 500 nanoseconds.

The third instruction TMSK1 |= OC4I arms the output compare 4 interrupt. OC4I is the logical name of the bit in TMSK1 that arms the OC4 interrupt.

The instruction TFLG1 |= OC4F acknowledges any previously received OC4 interrupts, thereby allowing your program to catch the next OC4 event. In this case, OC4F is the logical name of the bit in TFLG1 that signals when the OC4 event occurs.

Finally, we set the output compare register TOC4 to the next time we want an OC4 event to occur. This deadline is obtained by taking the current value of TCNT and adding 256 to it. So the next OC4 event should occur $256 nanoseconds from the current time, or rather the next OC4 event should occur in 128 $\mu$seconds.


next up previous
Next: Writing the ISR Up: Output Compare Interrupts Previous: How do I install
Bill Goodwine 2002-09-29