Xmega Application Note


usart_example_polled.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00052 #include "usart_driver.h"
00053 #include "avr_compiler.h"
00054 
00055 
00057 #define USART USARTC0
00058 
00059 
00061 bool success;
00062 
00063 
00077 int main(void)
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 }
@DOC_TITLE@
Generated on Wed Nov 5 10:23:27 2008 for AVRxxxx Application note title by doxygen 1.5.5