Up until now, the internal RC osc has been plenty accurate. In fact, the xMega's 1% calibrated internal RC osc is probably plenty accurate, but I wanted to use a crystal.
I THINK I have it running now on the 16MHz crystal. That is, I set #define F_CPU 32000000UL and _delay_ms(500) seems to be about 1/2 a second. Actually, this seems a little silly
So, do I have this right?
OSC.XOSCCTRL=OSC_FRQRANGE_12TO16_gc | OSC_XOSCSEL_XTAL_16KCLK_gc; OSC.CTRL|=OSC_XOSCEN_bm; uint8_t n=(CLK.PSCTRL & (~(CLK_PSADIV_gm | CLK_PSBCDIV1_bm | CLK_PSBCDIV0_bm))) | CLK_PSADIV_1_gc | CLK_PSBCDIV_1_1_gc; CCP=CCP_IOREG_gc; CLK.PSCTRL=n; // Wait for the external oscillator to stabilize while ((OSC.STATUS & OSC_XOSCRDY_bm)==0); // Select system clock source: External Osc. or Clock n=(CLK.CTRL & (~CLK_SCLKSEL_gm)) | CLK_SCLKSEL_XOSC_gc; CCP=CCP_IOREG_gc; CLK.CTRL=n; internal 32kHz, PLL OSC.CTRL&= ~(OSC_RC2MEN_bm | OSC_RC32MEN_bm | OSC_RC32KEN_bm | OSC_PLLEN_bm);
Code lifted right here from the Freaks.
I think I can run the thing slowly, at like 2MHz most of the time to conserve power, then switch to 32MHz when I want to update the TFT display. Well, maybe not, as that would defeat the purpose of using a crystal, that is, keeping multiple devices in sync over a period of time.