Xmega Application Note


usart_example_polled.c File Reference


Detailed Description

XMEGA polled USART driver example source.

This file contains an example application that demonstrates the polled USART driver. The code example sends all values between 0 and 255 and checks that the values received are equal to the values sent. It can be tested, using a loop-back wire between I/O pins PC2 and PC3.

Application note:
AVR1307: Using the XMEGA USART
Documentation
For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author:
Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Revision
1694
Date
2008-07-29 14:21:58 +0200 (ti, 29 jul 2008)

Copyright (c) 2008, Atmel Corporation All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file usart_example_polled.c.

#include "usart_driver.h"
#include "avr_compiler.h"

Include dependency graph for usart_example_polled.c:

Go to the source code of this file.

Defines

#define USART   USARTC0

Functions

int main (void)
 Example application.

Variables

bool success


Define Documentation

#define USART   USARTC0

Define that selects the Usart used in example.

Definition at line 57 of file usart_example_polled.c.


Function Documentation

int main ( void   ) 

Example application.

Example applicaton. This example configures USARTC0 for with the parameters:

  • 8 bit character size
  • No parity
  • 1 stop bit
  • 9600 Baud

This function then sends the values 0-255 and tests if the received data is equal to the sent data. The code can be tested by connecting PC3 to PC2. If the variable 'success' is true at the end of the function, the values have been successfully sent and received.

Definition at line 77 of file usart_example_polled.c.

References success, USART, USART_Baudrate_Set, USART_Format_Set, USART_GetChar, USART_IsRXComplete, USART_IsTXDataRegisterEmpty, USART_PutChar, USART_Rx_Disable, USART_Rx_Enable, USART_Tx_Disable, and USART_Tx_Enable.

00078 {
00079 
00080         /* Variable used to send and receive data. */
00081         uint8_t sendData;
00082         uint8_t receivedData;
00083 
00084         /* This PORT setting is only valid to USARTC0 if other USARTs is used a
00085          * different PORT and/or pins is used. */
00086         /* PIN3 (TXD0) as output. */
00087         PORTC.DIRSET = PIN3_bm;
00088 
00089         /* PC2 (RXD0) as input. */
00090         PORTC.DIRCLR = PIN2_bm;
00091 
00092         /* USARTC0, 8 Data bits, No Parity, 1 Stop bit. */
00093         USART_Format_Set(&USART, USART_CHSIZE_8BIT_gc, USART_PMODE_DISABLED_gc, false);
00094 
00095         /* Set Baudrate to 9600 bps:
00096          * Use the default I/O clock fequency that is 2 MHz.
00097          * Do not use the baudrate scale factor
00098          *
00099          * Baudrate select = (1/(16*(((I/O clock frequency)/Baudrate)-1)
00100          *                 = 12
00101          */
00102         USART_Baudrate_Set(&USART, 12 , 0);
00103 
00104         /* Enable both RX and TX. */
00105         USART_Rx_Enable(&USART);
00106         USART_Tx_Enable(&USART);
00107 
00108 
00109         /* Assume that everything is OK. */
00110         success = true;
00111         /* Send data from 255 down to 0*/
00112         sendData = 255;
00113         while(sendData) {
00114             /* Send one char. */
00115                 do{
00116                 /* Wait until it is possible to put data into TX data register.
00117                  * NOTE: If TXDataRegister never becomes empty this will be a DEADLOCK. */
00118                 }while(!USART_IsTXDataRegisterEmpty(&USART));
00119                 USART_PutChar(&USART, sendData);
00120 
00121                 uint16_t timeout = 1000;
00122                 /* Receive one char. */
00123                 do{
00124                 /* Wait until data received or a timeout.*/
00125                 timeout--;
00126                 }while(!USART_IsRXComplete(&USART) && timeout!=0);
00127                 receivedData = USART_GetChar(&USART);
00128 
00129                 /* Check the received data. */
00130                 if (receivedData != sendData){
00131                         success = false;
00132                 }
00133                 sendData--;
00134         }
00135 
00136         /* Disable both RX and TX. */
00137         USART_Rx_Disable(&USART);
00138         USART_Tx_Disable(&USART);
00139 
00140         /* If success the program ends up inside the if statment.*/
00141         if(success){
00142                 while(true);
00143         }else{
00144                 while(true);
00145         }
00146 }


Variable Documentation

bool success

Success variable, used to test driver.

Definition at line 61 of file usart_example_polled.c.

@DOC_TITLE@
Generated on Wed Nov 5 10:23:28 2008 for AVRxxxx Application note title by doxygen 1.5.5