MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip
MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip
MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
7.6 USING MACROS<br />
Device Support Files<br />
Processor header files define, in addition to special function registers, useful macros<br />
<strong>for</strong> the 16-bit family of devices.<br />
• Configuration Bits Setup Macros<br />
• Inline Assembly Usage Macros<br />
• Data Memory Allocation Macros<br />
• ISR Declaration Macros<br />
7.6.1 Configuration Bits Setup Macros<br />
Macros are provided that can be used to set configuration bits. For example, to set the<br />
FOSC bit using a macro, the following line of code can be inserted be<strong>for</strong>e the beginning<br />
of your C source code:<br />
_FOSC(CSW_FSCM_ON & EC_PLL16);<br />
This would enable the external clock with the PLL set to 16x <strong>and</strong> enable clock switching<br />
<strong>and</strong> fail-safe clock monitoring.<br />
Similarly, to set the FBORPOR bit:<br />
_FBORPOR(PBOR_ON & BORV_27 & PWRT_ON_64 & MCLR_DIS);<br />
This would enable Brown-out Reset at 2.7 Volts <strong>and</strong> initialize the Power-up timer to 64<br />
milliseconds <strong>and</strong> configure the use of the MCLR pin <strong>for</strong> I/O.<br />
For a complete list of settings valid <strong>for</strong> each configuration bit, refer to the processor<br />
header file.<br />
7.6.2 Inline Assembly Usage Macros<br />
Some Macros used to define assembly code in C are listed below:<br />
#define Nop() {__asm__ volatile ("nop");}<br />
#define ClrWdt() {__asm__ volatile ("clrwdt");}<br />
#define Sleep() {__asm__ volatile ("pwrsav #0");}<br />
#define Idle() {__asm__ volatile ("pwrsav #1");}<br />
7.6.3 Data Memory Allocation Macros<br />
Macros that may be used to allocate space in data memory are discussed below. There<br />
are two types: those that require an argument <strong>and</strong> those that do not.<br />
The following macros require an argument N that specifies alignment. N must be a<br />
power of two, with a minimum value of 2.<br />
#define _XBSS(N) __attribute__((space(xmemory), aligned(N)))<br />
#define _XDATA(N) __attribute__((space(xmemory), aligned(N)))<br />
#define _YBSS(N) __attribute__((space(ymemory), aligned(N)))<br />
#define _YDATA(N) __attribute__((space(ymemory), aligned(N)))<br />
#define _EEDATA(N) __attribute__((space(eedata), aligned(N)))<br />
For example, to declare an uninitialized array in X memory that is aligned to a 32-byte<br />
address:<br />
int _XBSS(32) xbuf[16];<br />
To declare an initialized array in data EEPROM without special alignment:<br />
int _EEDATA(2) table1[] = {0, 1, 1, 2, 3, 5, 8, 13, 21};<br />
The following macros do not require an argument. They can be used to locate a<br />
variable in persistent data memory or in near data memory.<br />
#define _PERSISTENT __attribute__((persistent))<br />
#define _NEAR __attribute__((near))<br />
© 2008 <strong>Microchip</strong> Technology Inc. DS51284H-page 95