Pic18 Interrupt 2
Pic18 Interrupt 2
CSE 3442/5442
1
Interrupts
An asynchronous (could happen any time) signal indicating the need for immediate attention to the processor. A way to tell the processor that a peripheral needs attention, without the processor constantly polling that peripheral. The processor can elect to ignore (mask) various interrupts or can elect to suspend its current execution and jump to a ROM location to execute an interrupt handler routine.
2
As there is limited space at these addresses it is a good idea to place a GOTO instruction at the interrupt vector jumping to a remote location for the ISR implementation.
5
What Happens When an Interrupt Hits? The current instructions execution is finished and the next instructions address is pushed to the stack. Interrupts are disabled (GIE or GIEH or GIEL cleared). The PC is loaded with the interrupt vector (jump to the ISR) The instructions in the ISR are executed until a RETFIE instruction RETFIE will cause the microcontroller to pop the PC from the stack and resume normal operations (unless more interrupts pending). Interrupts are enabled (GIE or GIEH or GIEL set).
Masking Interrupts
By default though, all interrupts are masked (disabled). It is up to the user to enable them if they are needed. Enabling/disabling interrupts is done through designated registers in the SFR:
INTCON, INTCON2, INTCON3 RCON PIR1, PIR2, PIR3 PIE1, PIE2, PIE3 IPR1, IPR2, IPR3
All interrupts can be masked by clearing the GIE (general Interrupt enable) bit in INTCON (default).
To make things confusing, some peripherals can be masked in a group by a PEIE mask.
A very simplified view with omitting some flags and some masks
Enabling Interrupts GIE in INTCON has to be set (BSF INTCON, GIE) We need to find the interrupt mask for the peripheral we want to use and set it high. As some interrupts are designated as peripheral interrupts (IMHO a bad nomenclature) for some interrupts the PEIE bit needs to be set (BSF INTCON, PEIE)
10
11
12
Some Remarks
As interrupts are asynchronous (especially if they are external), there is a latency for handling them (CPU has to finish current task). This latency can be from two instruction cycles to four instruction cycles (especially for external interrupts). In many cases the interrupt flag has to be cleared when handling the interrupt to avoid the flag to recursively re-interrupt the device. Some flags are automatically cleared when reading a port. Interrupt flags are set even if the interrupts are not enabled (they are the way peripherals signal to polling or interrupts). And vice versa, many of the flags can be set from code, thus causing invocation of an interrupt. If more interrupts are pending at RETFIE then a new interrupt will be started. It is customary to check at the ISR first which flag caused the interrupt (obvious). Dont use MOVFF insider an ISR to modify an interrupt register (why would you?)
14
15
16
18
INTCON
19
INTCON2
20
INTCON3
21
PIR1
22
PIR2
23
PIR3
24
PIE1
25
PIE2
26
PIE3
27
IPR1
28
IPR2
29
IPR3
30
RCON
31
RETFIE 0x01
32
Defining ISRs in C
Defining functions that are for high priority and low priority ISRs: At the beginning of the program, have a prototype of all functions (including ISRs) Use #pragma interrupt function_name and #pragma interruptlow function_name to tell compiler that a function is an interrupt function (so it can use proper RETFIE returns and fast context switching)
34
Placing ISRs in C
Make sure that our ISRs are in the right place. At the beginning of the code insert goto instructions to the interrupt vectors
#pragma code My_Hi_Priority_Int = 0x0008 void My_Hi_Priority_Int(void) { _asm GOTO chk_isr _endasm }
35
36
Summary
Interrupts are a great way to handle peripheral attention or external happenings. Some of the most used interrupts are timers (later), external hardware, serial communications, and ADC ready. All interrupts in the PIC18 can be masked in a group or individually. We can have two levels of priorities, with an almost fully configurable what interrupt belong to what level relationship. Programming ISRs from C requires knowledge of how the compiler is told about ISRs. (The sample provided here is for the C18 compiler.)
37