// ******************************************************************************************* // // 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 ) // ******************************************************************************************* // #define DELAY_AMOUNT 10000 // ******************************************************************************************* // typedef enum stateTypeEnum {WaitForPress, WaitForRelease, LEDToggle, DebouncePress, DebounceRelease} stateType; // ******************************************************************************************* // int main(void) { int i; stateType state; // TRISB controls direction for all PORTB pins, where 0 -> output, 1 -> input. // Configure RB15 as output. // // TODO: Configure TRIS bit for RB15 as output // AD1PCFG is a register for configuring input pins between analog input // and digital IO, where 0 -> analog (default), 1 -> digital. PCFG4 is // the configuration bit for input AN4, which is mapped to the same pin // as digital input RB2. We want to use RB2 to connect to an input switch. // // TODO: Configure AD1PCFG register for configuring input pins between analog input // and digital IO. // Configure RB2 as input // // TODO: Configure TRIS bit for RB2 as input // Need a pull-up resistor for RB2 // // TODO: Configure CNPU register bits to enable internal pullup resistor for RB2 // input. // Turn LED Off LATBbits.LATB15 = 1; // Initialize State state = WaitForPress; while(1) { switch (state) { case WaitForPress: // Check for button press if ( PORTBbits.RB2 == 0 ) { state = DebouncePress; i = 0; } break; case DebouncePress: // Wait for some length of time. // THERE IS A BETTER WAY TO DO THIS if ( (++i) >= DELAY_AMOUNT ) state = LEDToggle; break; case LEDToggle: // Use bit operations on entire port to control LED // Why use bit operations? Because we can!! LATB = LATB ^ 0x8000; state = WaitForRelease; break; case WaitForRelease: // Check for button release if ( PORTBbits.RB2 == 1 ) { state = DebounceRelease; i = 0; } break; case DebounceRelease: // Wait for some length of time. // THERE IS A BETTER WAY TO DO THIS if ( (++i) >= DELAY_AMOUNT ) state = WaitForPress; break; } } return 0; } // ******************************************************************************************* //