The interrupt handler's source code will be found in
kernel.c
. ISR's must be handled differently than
regular functions. In particular, the compiler needs to en
sure that the return from an ISR is handled by the
rti
assembly instruction. A regular function
generally uses the rtn
assembly instruction.
You write the ISR as if it were an ordinary function. But
you need to alert the compiler that this function is an
interrupt handler, not a regular function. You alert the
compiler using a pre-compiler directive known as a
pragma
. A listing of the OC4 handler is shown
below. The ISR has the name OC4han()
. This listing
shows the correct way of declaring an ISR using the
Imagecraft ICC11 compiler.
#pragma interrupt_handler OC4han() void OC4han(void){ TOC4 = TOC4 + 256; _Time = _Time + 1; TFLG1 |= OC4F; }
The first line of the above listing has the pragma
that tells the ICC11 compiler that function OC4han()
is an interrupt handler.
The body of the OC4han()
ISR has only three
instructions. In general, we want interrupt handlers to be
extremely short. The first instruction
(TOC4 = TOC4 + 256
)
resets the output compare register TOC4
to
the next time we want this interrupt to be issued. In this
case, that corresponds to 128 seconds.
The second instruction ( _Time = _Time + 1;
)
increments the global variable _Time
. Recall that
this variable can be accessed from anywhere in your program
and used as a count of the current time.
The final instruction ( TFLG1 |= OC4F
) resets
acknowledges that the OC4
interrupt was caught.
Recall that the bit OC4F
is cleared whenever the OC4
interrupt is caught by the ISR. This bit must be
explicitly set back to 1, if we want to catch the next OC4
event.
As you can see the processing in this interrupt handler is
extremely small. All it does is reset the OC4 interrupt to
generate the OC4
event in 128 seconds and it
increments the global
_Time
variable.