Datasheet

PICkit™ 3 Debug Express Lessons
© 2009 Microchip Technology Inc. DS41370C-page 49
See Section 19.0 10-Bit Analog-to-Digital Converter (A/D) Module” in the
PIC18F45K20 Data Sheet (DS41303) for more information on the ADC peripheral.
3.8 LESSON 8: INTERRUPTS
This lesson changes the Lesson 7 code to use interrupts to act on the switch press and
Timer0 events instead of polling them. The switch uses the RB0/INT0 external interrupt
capability.
3.8.1 PIC18FXXXX Interrupt Architecture
When a peripheral requires attention or an event occurs, it sets an interrupt flag. Each
flag has an interrupt enable bit that determines whether it will generate an interrupt to
the microcontroller or not. In the previous lessons, interrupt flags such as TMR0IF were
polled, but did not create an interrupt as the enable bit was not set. The enable bits
allow only selected events to cause in interrupt. All interrupts are ORed together, and
then ANDed with a global interrupt enable.
FIGURE 3-39: SIMPLIFIED INTERRUPT LOGIC
Note: If TMR0L is added to the Watch window, it will cause incorrect operation
when stepping through the following 2 lines of code:
TMR0H = ADC_Convert();
TMR0L = 0;
This is caused by the buffered nature of TMR0H. When “Stepping Over” the
TMR0H assignment statement, the MPLAB IDE will read the TMR0L
register to update the value in the Watch window. When TMR0L is read, the
upper byte of TMR0 is loaded into the TMR0H buffer, wiping out the value
written in the previous TMR0H assignment statement.
One workaround to be able to add TMR0L to the Watch window is to make
sure not to step from the TMR0H to the TMR0L statement. Set a breakpoint
on the TMR0L assignment statement, and Run from the TMR0H
assignment statement.
Key Concepts
- An interrupt is a hardware-based event that “interrupts” the program code to
execute a special function. When the interrupt function exits, program exe-
cution returns to where it left off.
- The PIC18FXXXX supports a single interrupt priority or two levels of inter-
rupt priority.
- A Low Priority interrupt can interrupt the main program. A High Priority inter-
rupt can interrupt the main program or a low priority interrupt.
- The directives #pragma interruptlow and #pragma interrupt are
used to define the interrupt functions.