Setting the configuration bitsMacro for setting configuration bits must be placed somewhere at the top of the source file, right after the #include lines. For portability sake, I recommend this method rather than setting them in IDE. Regardless of which clock source you will be using afterwards, the system is configured to use FRC at startup.
_FOSCSEL(FNOSC_FRC);Next, use this macro for FRC case
_FOSC(FCKSM_CSECMD & OSCIOFNC_ON & POSCMD_NONE);and for the external crystal case
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF & POSCMD_XT);The latter is used for both cases in Microchip document. Consult the PIC24EPdatasheet and family reference manual for the meaning of each definition.
I/O Port DefinitionsAs shown in figure 1 above, only one input port (RB2) is used for the push-button switch, and one output port (RB15) for LED. Define them accordingly
#define SW1 !_RB2 // switch is active-low #define LEDB _LATB15 // blue LEDThen put these lines in main() before the infinite loop, or in an init( ) function called by main.
ANSELA=0x0000; // use all ports RA and RB as digital ANSELB=0x000; _TRISB2 = 1; // RB2 as input _CNPUB2 = 1; // enable internal weak pull up _TRISB15 = 0; // RB15 as output