// ******************************************************************************************* // // Include file for PIC24FJ64GA002 microcontroller. This include file defines // MACROS for special function registers (SFR) and control bits within those // registers. #include "p24fj64ga002.h" // ******************************************************************************************* // // Configuration bits for CONFIG1 settings. // // Make sure "Configuration Bits set in code." option is checked in MPLAB. // This option can be set by selecting "Configuration Bits..." under the Configure // menu in MPLAB. // // These settings are appropriate for debugging the PIC microcontroller. If you need to // program the PIC for standalone operation, change the COE_ON option to COE_OFF. _CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & BKBUG_ON & COE_ON & ICS_PGx1 & FWDTEN_OFF & WINDIS_OFF & FWPSA_PR128 & WDTPS_PS32768 ) // ******************************************************************************************* // // Configuration bits for CONFIG2 settings. // Make sure "Configuration Bits set in code." option is checked in MPLAB. // This option can be set by selecting "Configuration Bits..." under the Configure // menu in MPLAB. _CONFIG2( IESO_OFF & SOSCSEL_SOSC & WUTSEL_LEG & FNOSC_PRIPLL & FCKSM_CSDCMD & OSCIOFNC_OFF & IOL1WAY_OFF & I2C1SEL_PRI & POSCMOD_XT ) // ******************************************************************************************* // typedef struct tagLEDs { union { struct { unsigned :4; unsigned LED4:1; unsigned LED5:1; unsigned LED6:1; unsigned LED7:1; }; struct { unsigned :4; unsigned lower:2; unsigned upper:2; }; struct { unsigned :4; unsigned all:4; }; }; } LEDS; // ******************************************************************************************* // // Assigns LEDs configuration from input variable to upper 4 bits of LATB. void OutputLEDs(LEDS leds) { _LATB15 = leds.LED4; _LATB14 = leds.LED5; _LATB13 = leds.LED6; _LATB12 = leds.LED7; } // ******************************************************************************************* // int main(void) { LEDS leds; // TRISB controls direction for all PORTB pins, where 0 -> output, 1 -> input. // Configure RB15-RB12 as outputs. _TRISB15 = 0; _TRISB14 = 0; _TRISB13 = 0; _TRISB12 = 0; // Configure RB5 as input _TRISB5 = 1; // Turn upper LEDs on and lower LEDs off leds.LED4 = 0; leds.LED5 = 0; leds.LED6 = 1; leds.LED7 = 1; OutputLEDs(leds); while(1) { // Button press detected if( PORTBbits.RB5 == 0 ) { // Toggle upper and lower LEDS leds.upper = ~leds.upper; leds.lower = ~leds.lower; OutputLEDs(leds); // wait for button release while( PORTBbits.RB5 == 0 ); } } return 0; } // ******************************************************************************************* //