Holiday Lighting Control
Holiday Lighting Control

How to configure an AVR XMega for 32MHz

Here’s how to quickly and easily get your Atmel AVR XMEGA running at a very stable 32MHz without a crystal.  This will enable both the 32Khz and 32MHz internal oscillators, using the 32KHz oscillator for DFLL calibration and switch the XMEGA to the 32MHz clock.  I’ve used the USART at 115,200 on a few projects with this configuration and its been very stable.

// Configure clock to 32MHz
OSC.CTRL |= OSC_RC32MEN_bm | OSC_RC32KEN_bm;  /* Enable the internal 32MHz & 32KHz oscillators */
while(!(OSC.STATUS & OSC_RC32KRDY_bm));       /* Wait for 32Khz oscillator to stabilize */
while(!(OSC.STATUS & OSC_RC32MRDY_bm));       /* Wait for 32MHz oscillator to stabilize */
DFLLRC32M.CTRL = DFLL_ENABLE_bm ;             /* Enable DFLL - defaults to calibrate against internal 32Khz clock */
CCP = CCP_IOREG_gc;                           /* Disable register security for clock update */
CLK.CTRL = CLK_SCLKSEL_RC32M_gc;              /* Switch to 32MHz clock */
OSC.CTRL &= ~OSC_RC2MEN_bm;                   /* Disable 2Mhz oscillator */

 

Leave a comment

Your email address will not be published. Required fields are marked *

5 thoughts on “How to configure an AVR XMega for 32MHz”