4.8 Interrupts

The MPLAB XC8 compiler incorporates features allowing interrupts to be fully handled from C code. Interrupt functions are often called Interrupt Service Routines, or ISRs.

The following are the general steps you need to follow to use interrupts. More detail about these steps is provided in the sections that follow.

  • Write as many interrupt functions as required. Consider one or more additional functions to handle accidental triggering of unused interrupt sources.
  • At the appropriate point in your code, enable the interrupt sources required.
  • At the appropriate point in your code, enable the global interrupt flag.

Interrupt functions must not be called directly from C code (due to the different return instruction used), but can call other functions, such as user-defined and library functions.

Interrupt code is the name given to any code that executes as a result of an interrupt occurring, including functions called from the ISR and library code. Interrupt code completes at the point where the corresponding return from interrupt instruction is executed. This contrasts with main-line code, which, for a freestanding application, is usually the main part of the program that executes after Reset.