Skip to main content
added 8 characters in body
Source Link
Oli Glaser
  • 55.7k
  • 3
  • 78
  • 149

Apart from the crystal sounding like it's actually a resonator (part number?), you also need to unlock the OSCCON register to switch clock sources. The compiler provides built in functions to do this, as described in the datasheet.

Here is an example from the datasheet of the code to switch over to Primary Oscillator with PLL (you need to check/set the divider/multiplier bits according to your frequency - this is for 20Mhz crystal and 80Mhz operating freq):

int main() { // Configure PLL prescaler, PLL postscaler, PLL divisor PLLFBD=30; // M = 32 CLKDIVbits.PLLPOST=0; // N1 = 2 CLKDIVbits.PLLPRE=2; // N2 = 4 // Initiate Clock Switch to Primary Oscillator with PLL (NOSC = 0b011) __builtin_write_OSCCONH(0x03); __builtin_write_OSCCONL(0x01); // Wait for Clock switch to occur while (OSCCONbits.COSC != 0b011); // Wait for PLL to lock while(OSCCONbits.LOCK!=1) {}; } 

Apart from the crystal sounding like it's actually a resonator (part number?), you also need to unlock the OSCCON register to switch clock sources. The compiler provides built in functions to do this, as described in the datasheet.

Here is an example from the datasheet of the code to switch over to Primary Oscillator with PLL (you need to check/set the divider/multiplier bits according to your frequency - this is for 20Mhz and 80Mhz operating freq):

int main() { // Configure PLL prescaler, PLL postscaler, PLL divisor PLLFBD=30; // M = 32 CLKDIVbits.PLLPOST=0; // N1 = 2 CLKDIVbits.PLLPRE=2; // N2 = 4 // Initiate Clock Switch to Primary Oscillator with PLL (NOSC = 0b011) __builtin_write_OSCCONH(0x03); __builtin_write_OSCCONL(0x01); // Wait for Clock switch to occur while (OSCCONbits.COSC != 0b011); // Wait for PLL to lock while(OSCCONbits.LOCK!=1) {}; } 

Apart from the crystal sounding like it's actually a resonator (part number?), you also need to unlock the OSCCON register to switch clock sources. The compiler provides built in functions to do this, as described in the datasheet.

Here is an example from the datasheet of the code to switch over to Primary Oscillator with PLL (you need to check/set the divider/multiplier bits according to your frequency - this is for 20Mhz crystal and 80Mhz operating freq):

int main() { // Configure PLL prescaler, PLL postscaler, PLL divisor PLLFBD=30; // M = 32 CLKDIVbits.PLLPOST=0; // N1 = 2 CLKDIVbits.PLLPRE=2; // N2 = 4 // Initiate Clock Switch to Primary Oscillator with PLL (NOSC = 0b011) __builtin_write_OSCCONH(0x03); __builtin_write_OSCCONL(0x01); // Wait for Clock switch to occur while (OSCCONbits.COSC != 0b011); // Wait for PLL to lock while(OSCCONbits.LOCK!=1) {}; } 
added 35 characters in body
Source Link
Oli Glaser
  • 55.7k
  • 3
  • 78
  • 149

Apart from the crystal sounding like it's actually a resonator (part number?), you also need to unlock the OSCCON register to switch clock sources. The compiler provides built in functions to do this, as described in the datasheet.

Here is an example from the datasheet of the code to switch over to Primary Oscillator with PLL (you need to check/set the divider/multiplier bits according to your frequency - this is for 10Mhz20Mhz and 80Mhz operating freq - for a 20MHz primary use PLL predivide of 4):

int main() { // Configure PLL prescaler, PLL postscaler, PLL divisor PLLFBD=30; // M = 32 CLKDIVbits.PLLPOST=0; // N1 = 2 CLKDIVbits.PLLPRE=0;PLLPRE=2; // N2 = 24 // Initiate Clock Switch to Primary Oscillator with PLL (NOSC = 0b011) __builtin_write_OSCCONH(0x03); __builtin_write_OSCCONL(0x01); // Wait for Clock switch to occur while (OSCCONbits.COSC != 0b011); // Wait for PLL to lock while(OSCCONbits.LOCK!=1) {}; } 

Apart from the crystal sounding like it's actually resonator, you also need to unlock the OSCCON register to switch clock sources. The compiler provides built in functions to do this, as described in the datasheet.

Here is an example from the datasheet of the code to switch over to Primary Oscillator with PLL (you need to check/set the divider/multiplier bits according to your frequency - this is for 10Mhz and 80Mhz operating freq - for a 20MHz primary use PLL predivide of 4):

int main() { // Configure PLL prescaler, PLL postscaler, PLL divisor PLLFBD=30; // M = 32 CLKDIVbits.PLLPOST=0; // N1 = 2 CLKDIVbits.PLLPRE=0; // N2 = 2 // Initiate Clock Switch to Primary Oscillator with PLL (NOSC = 0b011) __builtin_write_OSCCONH(0x03); __builtin_write_OSCCONL(0x01); // Wait for Clock switch to occur while (OSCCONbits.COSC != 0b011); // Wait for PLL to lock while(OSCCONbits.LOCK!=1) {}; } 

Apart from the crystal sounding like it's actually a resonator (part number?), you also need to unlock the OSCCON register to switch clock sources. The compiler provides built in functions to do this, as described in the datasheet.

Here is an example from the datasheet of the code to switch over to Primary Oscillator with PLL (you need to check/set the divider/multiplier bits according to your frequency - this is for 20Mhz and 80Mhz operating freq):

int main() { // Configure PLL prescaler, PLL postscaler, PLL divisor PLLFBD=30; // M = 32 CLKDIVbits.PLLPOST=0; // N1 = 2 CLKDIVbits.PLLPRE=2; // N2 = 4 // Initiate Clock Switch to Primary Oscillator with PLL (NOSC = 0b011) __builtin_write_OSCCONH(0x03); __builtin_write_OSCCONL(0x01); // Wait for Clock switch to occur while (OSCCONbits.COSC != 0b011); // Wait for PLL to lock while(OSCCONbits.LOCK!=1) {}; } 
Source Link
Oli Glaser
  • 55.7k
  • 3
  • 78
  • 149

Apart from the crystal sounding like it's actually resonator, you also need to unlock the OSCCON register to switch clock sources. The compiler provides built in functions to do this, as described in the datasheet.

Here is an example from the datasheet of the code to switch over to Primary Oscillator with PLL (you need to check/set the divider/multiplier bits according to your frequency - this is for 10Mhz and 80Mhz operating freq - for a 20MHz primary use PLL predivide of 4):

int main() { // Configure PLL prescaler, PLL postscaler, PLL divisor PLLFBD=30; // M = 32 CLKDIVbits.PLLPOST=0; // N1 = 2 CLKDIVbits.PLLPRE=0; // N2 = 2 // Initiate Clock Switch to Primary Oscillator with PLL (NOSC = 0b011) __builtin_write_OSCCONH(0x03); __builtin_write_OSCCONL(0x01); // Wait for Clock switch to occur while (OSCCONbits.COSC != 0b011); // Wait for PLL to lock while(OSCCONbits.LOCK!=1) {}; }