Skip to content

Instantly share code, notes, and snippets.

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?