16.2.1 Guidelines for Writing ISRs

The following guidelines are suggested for writing ISRs:

  • declare ISRs with no parameters and a void return type (mandatory)
  • do not let ISRs be called by main line code (mandatory)
  • do not let ISRs call other functions (recommended)

A 16-bit device ISR is like any other C function in that it can have local variables and access global variables. However, an ISR needs to be declared with no parameters and no return value. This is necessary because the ISR, in response to a hardware interrupt or trap, is invoked asynchronously to the mainline C program (that is, it is not called in the normal way, so parameters and return values don’t apply).

ISRs should only be invoked through a hardware interrupt or trap and not from other C functions. An ISR uses the return from interrupt (RETFIE) instruction to exit from the function rather than the normal RETURN instruction. Using a RETFIE instruction out of context can corrupt processor resources, such as the Status register.

Finally, ISRs should avoid calling other functions. This is recommended because of latency issues. See Nesting Interrupts for more information.