Datasheet
PICkit™ 3 Debug Express
DS41370C-page 52 © 2009 Microchip Technology Inc.
associated interrupt enable bits are in the PIEx registers, and the IPRx register bits set
the interrupt priority as low or high. For detailed information the bits in these registers,
see Section 9.0 “Interrupts” of the PIC18F45K20 Data Sheet (DS41303).
FIGURE 3-43: LESSON 8 INTERRUPT INITIALIZATIONS
An interrupt is desired when the Demo Board button is pressed. Therefore, the
program utilizes the INT0 functionality of the RB0 pin to use it as an external interrupt
input pin. The interrupt is edge triggered, and we want it to interrupt on the falling edge
so the initial switch press is detected. The edge direction is set with
INTCON2bits.INTEDG0. INT0 is always a high priority interrupt. The flag INT0IF in
INTCON is cleared before enabling the interrupt with INT0IE. Switch debouncing is
ignored for the sake of simplicity here, but would be recommended in a product
application.
The interrupt configuration for Timer0 has been added to the Timer0_Init()
function. First, we make sure the flag TMR0IF is cleared, set the priority to low (0) with
TMR0IP, and then enable the interrupt with TMR0IE.
Enabling the individual interrupts has no effect until interrupts are enabled at the global
level. First, the IPEN bit in RCON is used to enable or disable priority interrupts. In
Lesson 8 it is set to enable priority interrupts. Low priority interrupts are enabled with
GIEL, and microcontroller interrupting is enabled with GIEH. Note that high and low
priority interrupts aren’t individually enabled with the two bits, as GIEH shuts off both
when it is off:
In this way, all interrupts may be disabled with a single bit, GIEH in INTCON.
// Set up switch interrupt on INT0
INTCON2bits.INTEDG0 = 0; // interrupt on falling edge of INT0 (switch pressed)
INTCONbits.INT0IF = 0; // ensure flag is cleared
INTCONbits.INT0IE = 1; // enable INT0 interrupt
// NOTE: INT0 is ALWAYS a high priority interrupt
// Set up global interrupts
RCONbits.IPEN = 1; // Enable priority levels on interrupts
INTCONbits.GIEL = 1; // Low priority interrupts allowed
INTCONbits.GIEH = 1; // Interrupting enabled.
void Timer0_Init(void)
{
// Set up Interrupts for timer
INTCONbits.TMR0IF = 0; // clear roll-over interrupt flag
INTCON2bits.TMR0IP = 0; // Timer0 is low priority interrupt
INTCONbits.TMR0IE = 1; // enable the Timer0 interrupt.
INTCONbits.GIEH INTCONbits.GIEL Interrupt Functions
00No Interrupts; all interrupts disabled.
01No Interrupts; all interrupts disabled.
10High priority interrupts only enabled.
11Both priority level interrupts enabled