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

製品コード
SW006021-1
ページ / 518
MPLAB
®
 XC8 C Compiler User’s Guide
DS52053B-page 192
 2012 Microchip Technology Inc.
5.9.4
Enabling Interrupts
Two macros are available, once you have included <xc.h>, which control the masking 
of all available interrupts. These macros are ei(), which enable or unmask all 
interrupts, and di(), which disable or mask all interrupts.
On all devices, they affect the GIE bit in the INTCON register. These macros should be 
used once the appropriate interrupt enable bits for the interrupts that are required in a 
program have been enabled.
For example:
ADIE = 1;  // A/D interrupts will be used
PEIE = 1;  // all peripheral interrupts are enabled
ei();      // enable all interrupts
// ...
di();      // disable all interrupts
5.9.5
Function Duplication
It is assumed by the compiler that an interrupt may occur at any time. As all functions 
are not reentrant (because of the dependance on the compiled stack for local objects, 
see Section 5.5.2.2.1 “Compiled Stack Operation”), if a function appears to be 
called by an interrupt function and by main-line code this could lead to code failure.
MPLAB XC8 has a feature which will duplicate the output associated with any function 
called from more than one call tree in the program’s call graph. There will be one call 
tree associated with main-line code, and one tree for the interrupt function, if 
defined.
Main-line code will call the original function’s output, and the interrupt will call the dupli-
cated function’s output. The duplication takes place only in the called function’s output; 
there is no duplication of the C source code itself. The duplicated code and data uses 
different symbols and are allocated different memory, so are fully independent.
This is similar to the process you would need to undertake if this feature was not imple-
mented in the compiler: the C function could be duplicated by hand, given different 
names and one called from main-line code; the other from the interrupt function. How-
ever, you would have to maintain both functions, and the code would need to be 
reverted if it was ported to a compiler which did support reentrancy.
The compiler-generated duplicate will have unique identifiers for the assembly symbols 
used within it. The identifiers consists of the same name used in the original output pre-
fixed with i1. Duplicated PIC18 functions use the prefixes i1 and i2 for the low- and 
high-priority interrupts, respectively.
The output of the function called from main-line code will not use any prefixes and the 
assembly names will be those normally used.
To illustrate, in a program the function main calls a function called input. This function 
is also called by an interrupt function.
Note:
Never re-enable interrupts inside the interrupt function itself. Interrupts are 
automatically re-enabled by hardware on execution of the RETFIE instruc-
tion. Re-enabling interrupts inside an interrupt function may result in code 
failure.