Showing posts with label Microstick II. Show all posts
Showing posts with label Microstick II. Show all posts

Thursday, 28 May 2015

PIC32 Config Bits for 40 mHz with internal Fast RC oscillator





BlinkLED.c
ConfigBitsBlinkLED.c
/*********************************************************************
 *
 *              CONFIG BITS W/ BLINKING LED example
 * The PIC32MC250F128x is a nice little chip that boosts up to 50MHz. 
 * For applications where timing can be more lax, the use of the internal 
 * Fast RC oscillator is a good option to minimize component count, specially 
 * if you want simpler designs.
 * So, here are the config bits to have it running at 40 MHz with internal 
 * FRC oscillator .
 * The internal Fast RC Oscillator frequency is 8MHz and configured to be used 
 * with PLL; FRC(8MHz)/ FPLLIDIV (2 ) *FPLLMUL (20) /FPLLODIV (2) = 40MHz 
 * Frequency for the peripheral bus clock is left at same as the System Clock 
 * ( TPB=System Clock/PBDIV), as the divisor used is 1 (PBDIV=1)= 40MHz.
 * Also , FWDTEN = OFF // Watchdog Timer Enable OFF, and #pragma config JTAGEN = OFF 
 * // JTAG Enable OFF should be considered for use of the ports assigned to the
 *  JTAG port ( PORTB pins in this case-see datasheet for more info)
 * 
 * 
 *
 *********************************************************************
 * FileName:        ConfigBitsWLedBlink.c
 *
 * Processor:       PIC32MX250F128B
 * Dev Board:       Microstick II
 * Complier:        MPLAB C32 v2.01 or higher
 *                  MPLAB IDE v8.73 or higher
 *
 * Software License Agreement
 *
 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
 *
 * Author       Date            Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *  RB        28/5/2015         Led should blink on and off.
 * 
 ********************************************************************/
#include < xc.h > // Blog code Bug : Shorten spaces to match <xc.h>
#include < plib.h > // Shorten spaces to match <plib.h> 
#include < p32xxxx.h >  // Shorten spaces to match <p32xxxx.h>
//#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_2, FWDTEN = OFF
//#pragma config POSCMOD = OFF, FNOSC = FRCPLL, FPBDIV = DIV_1
#pragma config   JTAGEN    = OFF    // JTAG Enable OFF
#pragma config   FNOSC     = FRCPLL // Fast RC w PLL 8mHz internal rc Osc
#pragma config   FPLLIDIV  = DIV_2  // PLL in 8mHz/2 = 4mHz
#pragma config   FPLLMUL   = MUL_20 // PLL mul 4mHz * 20 = 80mHz 24??
#pragma config   FPLLODIV  = DIV_2  // PLL Out 8mHz/2= 40 mHz system frequency osc
#pragma config   FPBDIV    = DIV_1  // Peripheral Bus Divisor
#pragma config   FCKSM     = CSECME // Clock Switch Enable, FSCM Enabled
#pragma config   POSCMOD   = OFF    // Primary osc disabled
#pragma config   IESO      = OFF    // Internal/external switch over
#pragma config   OSCIOFNC  = OFF    // CLKO Output Signal Active on the OSCO Pin
#pragma config   FWDTEN    = OFF    // Watchdog Timer Enable:
//
#define GetSystemClock()       (40000000ul)
#define GetPeripheralClock()    (GetSystemClock()/(1<<OSCCONbits.PBDIV))// 

main() {
    // Configure performance settings without changing PBDIV
    SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); 
    //Port Config
    mPORTAClearBits(BIT_0); //PORTA Bit 0 Clear bits to ensure light is off.
    mPORTASetPinsDigitalOut(BIT_0); //Set port RA0 as output
    // Variables
    int i;
    int j;

    while (1) { //main loop

        j = 100000;

        while (j--) { .
            mPORTAToggleBits(BIT_0); //Toggle light status. (Can be viewed in LATA SFR)

            i = j; //Time to wait in between toggle.
            while (i--) {
            } //Kill time.

            //j = j - 5000;     //Increase constant to increas blinking speed faster.
        } // while j--
    } // Main loop
} // Main


-->