Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 User Manual

Product codes
SW006021-1
Page of 518
MPLAB
®
 XC8 C Compiler User’s Guide
DS52053B-page 190
 2012 Microchip Technology Inc.
An example of an interrupt function is shown here.
int tick_count;
void interrupt tc_int(void)
{
    if (TMR0IE && TMR0IF) {
        TMR0IF=0;
        ++tick_count;
        return;
    }
    // process other interrupt sources here, if required
}
Code generated by the compiler will be placed at the interrupt vector address which will 
execute this function after any context switch that is required.
Notice that the code in the interrupt function checks for the source of the interrupt, in 
this case a timer, by looking at the interrupt flag bit (TMR0IE) and the interrupt flag bit 
(TMR0IF). Checking the interrupt enable flag is required since interrupt flags associ-
ated with a peripheral may be asserted even if the peripheral is not configured to 
generate an interrupt.
The following is an example of a low priority interrupt function that could be written for 
PIC18 devices.
void interrupt low_priority tc_clr(void) {
    if (TMR1IE && TMR1IF) {
        TMR1IF=0;
        tick_count = 0;
        return;
    }
    // process any other low priority sources here
}
5.9.2
Specifying the Interrupt Vector
The interrupt function(s) cannot be changed at runtime. That is, you cannot have alter-
nate interrupt functions and select which will be active during program execution. An 
error will result if there are more interrupt functions than interrupt vectors in a program.