public
Created

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.