// ******************************************************************************************* // // Include file for PIC24FJ64GA002 microcontroller. This include file defines // MACROS for special function registers (SFR) and control bits within those // registers. #include "p24fj64ga002.h" #include // ******************************************************************************************* // // 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 ) // ******************************************************************************************* // // Defines to simply UART's baud rate generator (BRG) regiser // given the osicllator freqeuncy and PLLMODE. #define XTFREQ 7372800 // On-board Crystal frequency #define PLLMODE 4 // On-chip PLL setting (Fosc) #define FCY (XTFREQ*PLLMODE)/2 // Instruction Cycle Frequency (Fosc/2) // ******************************************************************************************* // // TODO: Deteremine Pulse Width Modulation period and prescaler for desried PWM freqeuncy. #define PWM_PRESCALER // TODO #define PWM_PERIOD // TODO // ******************************************************************************************* // // #defines used for assigning the duty cycle regsiters (OCxRS) with descriptive names. #define R_LED OC2RS #define G_LED OC3RS #define B_LED OC4RS // ******************************************************************************************* // // Varaible used for controlling when to update the r, g, b varaiables to create the desired // light pattern. volatile unsigned char ledUpdate = 0; // ******************************************************************************************* // int main(void) { // Varaibles for storing the current r, g, and b values used to control the // the RGB LED long r = 0; long g = 0; long b = 0; // ******************************************************************************* // // TODO: Configure peripheral pin mapping to connect Ouptut Compare Modules // 2, 3, and 4 to RP12, RP13, and RP14 respectively. RPOR6bits.RP12R = ; RPOR6bits.RP13R = ; RPOR7bits.RP14R = ; // ******************************************************************************* // // Configure Timer 2 as clock source for Output Compare Modules 2, 3 and 4. The // prescaler and perioid register value will define the PWM frequency. T2CONbits.TCKPS = PWM_PRESCALER; PR2 = PWM_PERIOD; TMR2 = 0; // Configures output compare clock source as Timer 2. Setting the OCTSEL will // select between Timer 2 and Timer 3 as the clock source (0 -> Timer 2, 1 -> Timer 3). OC2CONbits.OCTSEL = 0; OC3CONbits.OCTSEL = 0; OC4CONbits.OCTSEL = 0; // Configure Output Compare Modules 2, 3, and 3 for Pulse Width Modulation (PWM). OC2CONbits.OCM = 6; OC3CONbits.OCM = 6; OC4CONbits.OCM = 6; // Calculate initialize duty cycles for red, green, and blue LEDs R_LED = r * PWM_PERIOD / 255; G_LED = g * PWM_PERIOD / 255; B_LED = b * PWM_PERIOD / 255; // Initialize OCxR duty cycle registers. OC2R = R_LED; OC3R = G_LED; OC4R = B_LED; // Start Timer 2 to generate the PWM output T2CONbits.TON = 1; // ******************************************************************************* // ledUpdate = 0; // ******************************************************************************* // // Set Timer 1' prescalar to 1:256 T1CONbits.TCKPS = 3; // Set Timer 1 period value regsiter to value for 1ms. Please note // T1CON's register settings below (internal Fosc/2 and 1:256 prescalar). // // Fosc = XTFREQ * PLLMODE // = 7372800 * 4 // = 29491200 // // Fosc/2 = 29491200 / 2 // = 14745600 // // Timer Freq = (Fosc/2) / Prescaler // = 14745600 / 256 // = 57600 // // PRy = 1ms / (1 / (T1 Freq)) - 1 // = 1e-3 / (1 / 57600) - 1 // = 1e-3 * 57600 - 1 // = 57.6 - 1 // = 57 (approximately) PR1 = 57; TMR1 = 0; // Clear Timer 1 IFS0bits.T1IF = 0; // Clear Timer 1 interrupt flag IEC0bits.T1IE = 1; // Enable Timer 1 interrupt T1CONbits.TON = 0; // Start Timer 1 // ******************************************************************************* // while(1) { if( ledUpdate ) { ledUpdate = 0; // TODO: Update r, g, and b variables to desried values. // Update duty cycles for red, green, and blue LEDs R_LED = r * PWM_PERIOD / 255; G_LED = g * PWM_PERIOD / 255; B_LED = b * PWM_PERIOD / 255; } } return 0; } // ******************************************************************************************* // // Timer 1 interrupt asserts an ledUpdate variable that is accessed in the main execution loop // for updating the r, g, b values for the LEDs. void _ISR _T1Interrupt(void) { // Clear Timer 1 interrupt flag to allow another Timer 1 interrupt to occur. IFS0bits.T1IF = 0; ledUpdate = 1; } // ******************************************************************************************* //