I'm coding on PIC24F and I'm still fighting with oscillator configuration. Between configuration bits and SFRs such as CLKDIV and OSCCON I am a 'bit' lost... I've been making things work by trial and error but haven't yet understood exactly what I am doing :/ (like when you finally understand pointers and have the "ohh" moment).
I think the parts that I am missing are:
- are configuration bits shortcuts for SFRs? do they overlap?
- what are the default/start values and which ones should I touch?
- how can I verify that the clock is running at the correct speed?
If more specific this needs to be, I'm trying to setup a PIC24FJ with an external 12Mhz crystal.
// CONFIG2 #pragma config POSCMOD = HS // Primary Oscillator Select (HS Oscillator mode selected) #pragma config I2C1SEL = PRI // I2C1 Pin Location Select (Use default SCL1/SDA1 pins) #pragma config IOL1WAY = OFF // IOLOCK Protection (IOLOCK may be changed via unlocking seq) #pragma config OSCIOFNC = OFF // Primary Oscillator Output Function (OSC2/CLKO/RC15 functions as CLKO (FOSC/2)) #pragma config FCKSM = CSDCMD // Clock Switching and Monitor (Clock switching and Fail-Safe Clock Monitor are disabled) #pragma config FNOSC = PRI // Oscillator Select (Primary Oscillator (XT, HS, EC)) #pragma config SOSCSEL = SOSC // Sec Oscillator Select (Default Secondary Oscillator (SOSC)) #pragma config WUTSEL = LEG // Wake-up timer Select (Legacy Wake-up Timer) #pragma config IESO = ON // Internal External Switch Over Mode (IESO mode (Two-Speed Start-up) enabled) // CONFIG1 #pragma config WDTPS = PS32768 // Watchdog Timer Postscaler (1:32,768) #pragma config FWPSA = PR128 // WDT Prescaler (Prescaler ratio of 1:128) #pragma config WINDIS = OFF // Watchdog Timer Window (Windowed Watchdog Timer enabled; FWDTEN must be 1) #pragma config FWDTEN = OFF // Watchdog Timer Enable (Watchdog Timer is disabled) #pragma config ICS = PGx1 // Comm Channel Select (Emulator EMUC1/EMUD1 pins are shared with PGC1/PGD1) #pragma config GWRP = OFF // General Code Segment Write Protect (Writes to program memory are allowed) #pragma config GCP = OFF // General Code Segment Code Protect (Code protection is disabled) #pragma config JTAGEN = ON // JTAG Port Enable (JTAG port is enabled)
Then in my main's init():
OSCCONbits.COSC = 0b010; CLKDIVbits.DOZE = 0b000;
Am I right to assume the following:?
#define FOSC 12000000UL #define FCY FOSC/2