Datasheet

PICkitâ„¢ 3 Debug Express Lessons
© 2009 Microchip Technology Inc. DS41370C-page 51
FIGURE 3-41: DEFINE INTERRUPT VECTORS
The Interrupt Service Routine functions themselves are then declared with the
#pragma interrupt directive for the high priority vector, and #pragma
interruptlow for the low priority. Note the names must match between the vector
GOTO argument, the #pragma attribute, and the function declaration name. The
interrupt functions may call other functions defined elsewhere in the source, though the
lesson source code does not do this.
FIGURE 3-42: INTERRUPT SERVICE FUNCTIONS
As all interrupts of the same priority vector to the same function, it is necessary in the
function to examine which of the enabled interrupt flags caused the interrupt. Once the
flag is found so that peripheral or event may be serviced, the software must clear the
interrupt flag bit to reset the interrupt. In the lesson source code, the high priority
interrupt routine looks for the INT0 pin interrupt INT0IF flag bit. Examples are shown in
the source code of how it might check for other enabled interrupts, such as Timer1
TMR1IF and the ADC ADIF although neither of these interrupts are enabled in the
lesson code. Similarly, the low priority vector checks for the Timer0 flag TMR0IF.
Setting Up Interrupts
Now that the source code has defined the interrupt vectors, and has functions to deal
with the interrupts, it must properly setup and configure the interrupting logic and
enable the individual interrupts it wants to use.
Timer0 and external pin interrupts are set up using the INTCONx Special Function
Registers. Other interrupts are setup through a number set of peripheral interrupt
SFRs: PIRx, PIEx and IPRx. The PIRx registers contain the interrupt flags. The
/** I N T E R R U P T S ***********************************************/
//----------------------------------------------------------------------------
// High priority interrupt vector
#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh (void)
{
_asm
goto InterruptServiceHigh //jump to interrupt routine
_endasm
}
//----------------------------------------------------------------------------
// Low priority interrupt vector
#pragma code InterruptVectorLow =
0x18 void InterruptVectorLow (void)
{
_asm
goto InterruptServiceLow //jump to interrupt routine _endasm
}
// Iterrupt Service Routines
#pragma interrupt InterruptServiceHigh// "interrupt" pragma for high priority
void InterruptServiceHigh(void)
{
//function statements
} // return from high-priority interrupt
#pragma interruptlow InterruptServiceLow // "interruptlow" pragma for low priority
void InterruptServiceLow(void)
{
//function statements
} // return from low-priority interrupt