Microchip Technology SW006022-2N Data Sheet
MPLAB
®
XC16 C Compiler User’s Guide
DS52071B-page 166
2012 Microchip Technology Inc.
11.2
INTERRUPT OPERATION
The compiler incorporates features allowing interrupts to be fully handled from C code.
Interrupt functions are often called ISRs.
Interrupt functions are often called ISRs.
The 16-bit devices allow interrupts to be generated from many interrupt sources. Most
sources have their own dedicated interrupt vector collated in an interrupt vector table
(IVT). Each vector consists of an address at which is found the entry point of the inter-
rupt service routine. Some of the interrupt table vector locations are for traps, which are
nonmaskable interrupts which deal with erroneous operation of the device, such as an
address error.
sources have their own dedicated interrupt vector collated in an interrupt vector table
(IVT). Each vector consists of an address at which is found the entry point of the inter-
rupt service routine. Some of the interrupt table vector locations are for traps, which are
nonmaskable interrupts which deal with erroneous operation of the device, such as an
address error.
On some devices, an alternate interrupt vector table (AIVT) is provided, which allow
independent interrupt vectors to be specified. This table can be enabled when required,
forcing ISR addresses to be read from the AIVT rather than the IVT.
independent interrupt vectors to be specified. This table can be enabled when required,
forcing ISR addresses to be read from the AIVT rather than the IVT.
Interrupts have a priority associated with them. This can be independently adjusted for
each interrupt source. When more than interrupt with the same priority are pending at
the same time, the intrinsic priority, or natural order priority, of each source comes into
play. The natural order priority is typically the same as the order of the interrupt vectors
in the IVT.
each interrupt source. When more than interrupt with the same priority are pending at
the same time, the intrinsic priority, or natural order priority, of each source comes into
play. The natural order priority is typically the same as the order of the interrupt vectors
in the IVT.
The compiler provides full support for interrupt processing in C or inline assembly code.
Interrupt code is the name given to any code that executes as a result of an interrupt
occurring. Interrupt code completes at the point where the corresponding return from
interrupt instruction is executed.
occurring. 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.
main part of the program that executes after Reset.
11.3
WRITING AN INTERRUPT SERVICE ROUTINE
Following the guidelines in this section, you can write all of your application code,
including your interrupt service routines, using only C language constructs.
including your interrupt service routines, using only C language constructs.
11.3.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).
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.
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 Section 11.6 “Nesting Interrupts” for more information.
latency issues. See Section 11.6 “Nesting Interrupts” for more information.