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.

 


Leave a comment

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

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

  • Alain Chebrou

    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

  • Andy Crofts

    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