// ******************************************************************************************* // // 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; // Initialize LEDs to off leds.LED4 = 1; leds.LED5 = 1; leds.LED6 = 1; leds.LED7 = 1; OutputLEDs(leds); // Set Timer1's Prescaler to 1:1 T1CONbits.TCKPS = 0; // Set Timer 1's period value regsiter to value for 2ms. Please note // T1CON's register settings below (internal Fosc/2 and 1:1 prescaler). // // Fosc = XTFREQ * PLLMODE // = 7372800 * 4 // = 29491200 // // Fosc/2 = 29491200 / 2 // = 14745600 // // Timer 1 Freq = (Fosc/2) / Prescaler // = 14745600 / 1 // = 14745600 // // PR1 = 2 ms / (1 / (T1 Freq)) // = 2e-3 / (1 / 14745600) // = 2e-3 * 14745600 // = 29491.2 PR1 = 29491; // Clear Timer value (i.e. current tiemr value) to 0 TMR1 = 0; // Start Timer 1 T1CONbits.TON = 0; while(1) { // Off Phase leds.LED4 = 1; OutputLEDs(leds); while (IFS0bits.T1IF == 0); IFS0bits.T1IF = 0; // On Phase leds.LED4 = 0; OutputLEDs(leds); while (IFS0bits.T1IF == 0); IFS0bits.T1IF = 0; } return 0; } // ******************************************************************************************* //