Skip to content
Snippets Groups Projects

XMEGA USART Hello World w/ ISR

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Jake Read

    Example of setting up registers on XMEGA for using USART module in Asynchronous mode, including the setup of an ISR to handle incoming characters.

    Edited
    xmega-usart-isr.c 1.05 KiB
    /*demo*/
    
    #include "asf.h"
    
    uint8_t data; // character holding
    
    // setup
    int main(void){
    	// bitrate selection
    	USARTD1.BAUDCTRLA = 80; // not sure about these, read datasheet!
    	USARTD1.BAUDCTRLB = 10;
    	
    	// setup interrupts
    	USARTD1.CTRLA |= USART_RXCINTLVL_LO_gc | USART_TXCINTLVL_OFF_gc | USART_DREINTLVL_OFF_gc;
    	
    	// enables the module
    	USARTD1.CTRLB = USART_TXEN_bm | USART_RXEN_bm;
    	
    	// sets the mode: async, no parity, 8 bit data
    	USARTD1.CTRLC = USART_CMODE_ASYNCHRONOUS_gc | USART_PMODE_DISABLED_gc | USART_CHSIZE_8BIT_gc;
    	
    	// setup tx pin as output (find the pins! which ones are you using! see datasheet - have to use particular pins)
    	PORTD.OUTSET = PIN2_bm;
    	PORTD.DIRSET = PIN2_bm;
    	
    	// setup rx pin as input
    	PORTD.OUTCLR = PIN3_bm;
    	PORTD.DIRCLR = PIN3_bm;
    	
    	// now we can
    
    	while(1){
    		while(!(USARTD1.STATUS & USART_DREIF_bm)); // hang-10, wait for data ready empty flag
    		USARTD1.DATA = data; // return the data we received during the ISR
    	}
    }
    
    ISR(USARTD1_RXC_vect){ // this function is called when the RXIN interrupt goes off
    	data = USARTD1->DATA;
    }
    
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment