Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 ユーザーズマニュアル

製品コード
SW006021-1
ページ / 518
C Language Features
 2012 Microchip Technology Inc.
DS52053B-page 189
5.9
INTERRUPTS
The MPLAB XC8 compiler incorporates features allowing interrupts to be fully handled 
from C code. Interrupt functions are often called Interrupt Service Routines, or ISRs. 
There is only one interrupt vector on mid-range and Enhanced mid-range devices. 
Regardless of the source of the interrupt, the device will vector to one specific location 
in program memory and execution continues from that address. This address is a attri-
bute of the device and cannot be changed.
Each mid-range device interrupt source typically has a control flag in an SFR which can 
disable that interrupt source. In addition there is a global interrupt enable flag that can 
disable all interrupts sources and ensure that an interrupt can never occur. There is no 
priority of interrupt sources. Check your device data sheet for full information how your 
device handles interrupts.
PIC18 devices have two separate interrupt vectors and a priority scheme to dictate 
which interrupt code is executed. The two interrupts are designated as low and high 
priority. Peripherals are associated one of the interrupt priorities (vectors) through set-
tings in the peripheral’s SFRs.
Individual interrupt sources can be disabled via a control flag in an SFR associated with 
that interrupt source. In addition to the global interrupt enable flag, there are other flags 
that can disable each interrupt priority.
Interrupt code is the name given to any code that executes as a result of an interrupt 
occurring, including functions called from the ISR and library code. Interrupt code com-
pletes at the point where the corresponding return from interrupt instruction is exe-
cuted. This contrasts with main-line code, which, for a freestanding application, is 
usually the main part of the program that executes after Reset.
5.9.1
Writing an Interrupt Service Routine
The function qualifier interrupt may be applied to a C function definition so that it 
will be executed once the interrupt occurs. The compiler will process the interrupt 
function differently to any other functions, generating code to save and restore any reg-
isters used and return using a special instruction.
If the xc8 option --STRICT is used, the interrupt keyword becomes 
__interrupt
.
An interrupt function must be declared as type void interrupt and may not have 
parameters. This is the only function prototype that makes sense for an interrupt func-
tion since they are never directly called in the source code.
On PIC18 devices, interrupt functions default to being high priority. To create a low-pri-
ority interrupt function, use the qualifier low_priority in addition to interrupt in 
the function definition.
Interrupt functions must not be called directly from C code (due to the different return 
instruction that is used), but they themselves may call other functions, both 
user-defined and library functions.
There may be many sources of interrupt that share the same interrupt vector, but there 
is only ever one interrupt function associated with each vector. The interrupt function 
must then contain code to determine the source of the interrupt before proceeding. An 
error will result if there are more interrupt functions than interrupt vectors in a program.
Note:
Baseline devices do not utilize interrupts and so the following sections are 
only applicable for mid-range, Enhanced mid-range and PIC18 devices.