Skip to content

Instantly share code, notes, and snippets.

@suovula
Created October 2, 2012 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suovula/3822369 to your computer and use it in GitHub Desktop.
Save suovula/3822369 to your computer and use it in GitHub Desktop.
bool setClock(ClockScheme clock) {
switch (clock) {
case Internal2Mhz : {
OSC.CTRL |= b00001; // Bit 0 – RC2MEN: 2MHz Internal Oscillator Enable
while((OSC.STATUS & b00001) == 0) { } // Wait oscillator to stabilize
uint8_t psctrl = b00000 << 2 | b00 << 0; // No System Clock prescalers, protected by CCP!
CCP = 0xD8; // Unlock Configuration Change Protection
CLK.PSCTRL = psctrl;
if (CLK.PSCTRL != psctrl) return false; // Failed?
uint8_t ctrl = b000; // Select 2MHz internal oscillator to System Clock
CCP = 0xD8; // Unlock Configuration Change Protection
CLK.CTRL = ctrl;
if (CLK.CTRL != ctrl) return false; // Failed?
} break;
case Internal32Mhz : {
OSC.CTRL |= b00010; // Bit 1 – RC32MEN: 32MHz Internal Oscillator Enable
while((OSC.STATUS & b00010) == 0) { } // Wait oscillator to stabilize
uint8_t psctrl = b00000 << 2 | b00 << 0; // No System Clock prescalers, protected by CCP!
CCP = 0xD8; // Unlock Configuration Change Protection
CLK.PSCTRL = psctrl;
if (CLK.PSCTRL != psctrl) return false; // Failed?
uint8_t ctrl = b001; // Select 32MHz internal oscillator to System Clock
CCP = 0xD8; // Unlock Configuration Change Protection
CLK.CTRL = ctrl;
if (CLK.CTRL != ctrl) return false; // Failed?
} break;
case Internal32768hz : {
OSC.CTRL |= b00100; // Bit 2 – RC32KEN: 32.768kHz Internal Oscillator Enable
while((OSC.STATUS & b00100) == 0) { } // Wait oscillator to stabilize
uint8_t psctrl = b00000 << 2 | b00 << 0; // No System Clock prescalers, protected by CCP!
CCP = 0xD8; // Unlock Configuration Change Protection
CLK.PSCTRL = psctrl;
if (CLK.PSCTRL != psctrl) return false; // Failed?
uint8_t ctrl = b010; // Select 32.768kHz internal oscillator to System Clock
CCP = 0xD8; // Unlock Configuration Change Protection
CLK.CTRL = ctrl;
if (CLK.CTRL != ctrl) return false; // Failed?
} break;
case External32768hz : {
OSC.XOSCCTRL = b00 << 6 | b1 << 5 | b0010; // Enable 32768Hz TOSC in Low Power Mode with 16K CLK start-up time
OSC.CTRL |= b01000; // Enable External Oscillator
while((OSC.STATUS & b01000) == 0) { } // Wait External Oscillator to stabilize
uint8_t psctrl = b00000 << 2 | b00 << 0; // No System Clock prescalers, protected by CCP!
CCP = 0xD8; // Unlock Configuration Change Protection
CLK.PSCTRL = psctrl;
if (CLK.PSCTRL != psctrl) return false; // Failed?
uint8_t ctrl = b011; // Select external oscillator to System Clock
CCP = 0xD8; // Unlock Configuration Change Protection
CLK.CTRL = ctrl;
if (CLK.CTRL != ctrl) return false; // Failed?
} break;
case External16x2Mhz : {
OSC.XOSCCTRL = b11 << 6 | b1011; // Enable 16MHz XTAL with 16K CLK start-up time
OSC.CTRL |= b01000; // Enable External Oscillator
while((OSC.STATUS & b01000) == 0) { } // Wait External Oscillator to stabilize
OSC.PLLCTRL = b11 << 6 | b00010; // Select External clock source with 2x PLL
OSC.CTRL |= b10000; // Enable PLL
while((OSC.STATUS & b10000) == 0) { } // Wait PLL to stabilize
uint8_t psctrl = b00000 << 2 | b00 << 0; // No System Clock prescalers, protected by CCP!
CCP = 0xD8; // Unlock Configuration Change Protection
CLK.PSCTRL = psctrl;
if (CLK.PSCTRL != psctrl) return false; // Failed?
uint8_t ctrl = b100; // Select PLL to System Clock
CCP = 0xD8; // Unlock Configuration Change Protection
CLK.CTRL = ctrl;
if (CLK.CTRL != ctrl) return false; // Failed?
} break;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment