This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |