6 RTC with XOSCHF
To use XOSCHF as clock source for the Real-Time Counter (RTC), first enable the XOSCHF similarly to the first two use cases. The crystal or input clock frequency can be up to ¼ of the main clock frequency.
/* Enable crystal oscillator with frequency range 8 MHz and 1K cycles start-up time */ ccp_write_io((uint8_t *) &CLKCTRL.XOSCHFCTRLA, CLKCTRL_RUNSTDBY_bm | CLKCTRL_CSUTHF_1K_gc | CLKCTRL_FRQRANGE_8M_gc | CLKCTRL_SELHF_CRYSTAL_gc | CLKCTRL_ENABLE_bm); /* Confirm crystal oscillator start-up */ while(!(CLKCTRL.MCLKSTATUS & CLKCTRL_EXTS_bm)) { ; } /* Clear RUNSTDBY for power save during sleep */ ccp_write_io((uint8_t *) &CLKCTRL.XOSCHFCTRLA, CLKCTRL.XOSCHFCTRLA & ~CLKCTRL_RUNSTDBY_bm);
To select the XOSCHF as clock source for RTC, the EXTCLK setting must be written to the Clock Select (RTC.CLKSEL) register.
/* Configure RTC to use XOSCHF as source */ RTC.CLKSEL = RTC_CLKSEL_EXTCLK_gc;
Now the RTC can be configured and enabled with XOSCHF as clock source.
The code for this example is available in the
RTC-with-XOSCHF folder in these github repositories: