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 */
5 thoughts on “How to configure an AVR XMega for 32MHz”
Thank you for the example, very useful.
I use it to DFFL 32MHz calibration with external 32KHz osc of A3BU_XPLAINED.
here is my code in addition to your:
// Enable and start TOSC 32KHz external oscillator (Belong to VBAT device!)
VBAT.CTRL |= VBAT_ACCEN_bm;
VBAT.CTRL |= VBAT_XOSCEN_bm ;
OSC.XOSCCTRL = 1<<1 ; // Select 32KHz TOSC
OSC.CTRL |= OSC_RC32MEN_bm | OSC_XOSCEN_bm; // Enable the internal 32MHz and external oscillators
while (! (OSC.STATUS & OSC_XOSCRDY_bm)); // Wait for 32Khz oscillator to stabilize
while(!(OSC.STATUS & OSC_RC32MRDY_bm)); // Wait for 32MHz oscillator to stabilize
OSC.DFLLCTRL = OSC_RC32MCREF_XOSC32K_gc ; // 32 MHz calibration reference
DFLLRC32M.CTRL = DFLL_ENABLE_bm ; // Enable DFLL
// The following lines don't work with optimizo None (-O0), so use ccp_write_io()
// CCP = CCP_IOREG_gc; // Disable register security for clock update
// CLK.CTRL = CLK_SCLKSEL_RC32M_gc ;
ccp_write_io((uint8_t *) & CLK.CTRL, CLK_SCLKSEL_RC32M_gc) ;
OSC.CTRL &= ~OSC_RC2MEN_bm; // Disable 2Mhz oscillator
Thank You both.
Exactly what I was looking for. My confusion was using an older App Note from Atmel (AVR1003) that implied that using an external clock (in my case from a DS3232M “Extremely Accurate RTC”) wasn’t an option.
It is, and the code you’ve provided proves exactly that on my test rig.
-Andy
nice good job
Hello. Thank you very much!!! Simply great!
I had a lot of progress in my project with this post!
Thanks!