bwoogie
bwoogie

Reputation: 4427

AVR XMEGA USART Simulation

Using the test code below, I'm trying to send data over the USART of an xmega128a3u using the simulator in Atmel Studio. Watching the I/O view the Data register is never set even though I'm setting it. Is there something wrong with my code or the simulator or what?

#include <avr/io.h>
#include <avr/interrupt.h>

#define bscale 0
#define bsel 0x0003 //250kbps
#define packetFormat (USART_SBMODE_bm | USART_CHSIZE_8BIT_gc | USART_PMODE_DISABLED_gc)


uint8_t n;

int main(void)
{
    //ALLOW PORTB AND PORTF TO BE WRITTEN TO! TURNS OFF JTAG
    CCP = 0xD8; //Allow Protected IO changing
    MCU_MCUCR = 0x1;

    //CRYSTAL SETUP
    OSC_XOSCCTRL = OSC_FRQRANGE_12TO16_gc | OSC_XOSCSEL_XTAL_16KCLK_gc; // 16Mhz Crystal
    OSC_CTRL |= OSC_XOSCEN_bm;
    while(!(OSC_STATUS & OSC_XOSCRDY_bm)); //Wait for crystal to stabilize.
    CCP = CCP_IOREG_gc;
    CLK_CTRL = CLK_SCLKSEL_XOSC_gc;
    //END CRYSTAL SETUP


    cli();


    //Enable Interrupts
    USARTF0.CTRLA = USART_TXCINTLVL_LO_gc | USART_DREINTLVL_LO_gc;

    //Enable transmitter
    USARTF0.CTRLB = USART_TXEN_bm;

    PMIC.CTRL |= PMIC_LOLVLEX_bm;

    //Set baud
    USARTF0.BAUDCTRLB = bscale;
    USARTF0.BAUDCTRLA = bsel;

    //Set packet format
    USARTF0.CTRLC = packetFormat;

    sei();

    while (1) 
    {
        if(n < 255) {
            USARTF0.DATA  = n;

        } else {
            n = 0;  
        }
    }
}

ISR(USARTF0_TXC_vect) {
    n++;
}

ISR(USARTF0_DRE_vect) {
    n++;
}

enter image description here

Upvotes: 1

Views: 1079

Answers (1)

CodingHero
CodingHero

Reputation: 191

Are other registers being updated, just not DATA? If not, make sure you've enabled the clock to the USART. Many micros also use one DATA register for reads and writes. Thus attempting to read the DATA register after writing (I.e., the debug view is doing a read) won't return anything unless data has been received. Thus you'd likely see the same behavior even if executing on hardware.

That said, in my experience the simulator in Atmel Studio isn't very good at simulating interrupts or peripheral operation.

Edit to include informatino from comments below: Since you can't read back something you just wrote to the DATA register, checking the DREIF flag in the STATUS register right after you write the data will confirm whether or not the data is being transmitted.

Upvotes: 1

Related Questions