Xmega Application Note


IR_example_polled.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00051 #include "avr_compiler.h"
00052 #include "IR_driver.h"
00053 #include "usart_driver.h"
00054 
00055 
00057 #define USART USARTC0
00058 
00059 
00061 bool success;
00062 
00063 
00077 void 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 are used. */
00086         /* PIN3 (TXD0) as output. */
00087         PORTC.DIRSET   = PIN3_bm;
00088         /* PC2 (RXD0) as input. */
00089         PORTC.DIRCLR   = PIN2_bm;
00090 
00091         /* USARTC0, 8 Data bits, No Parity, 1 Stop bit. */
00092         USART_Format_Set(&USART, USART_CHSIZE_8BIT_gc, USART_PMODE_DISABLED_gc, false);
00093 
00094         /* Set Baud rate to 9600 bps:
00095          * Use the default I/O clock frequency that is 2 MHz.
00096          * Do not use the baud rate scale factor
00097          *
00098          * Baud rate select = (1/(16*(((I/O clock frequency)/Baud rate)-1)
00099          *                 = 12
00100          */
00101         USART_Baudrate_Set(&USART, 12 , 0);
00102 
00103         /* Set USARTC0 in IrDA mode.*/
00104         USART_SetMode(&USARTC0, USART_CMODE_IRDA_gc);
00105 
00106         /* If using another Pulse Length than the standard 3/16 use the functions
00107          * shown below. */
00108         /*
00109         IRCOM_TXSetPulseLength(254);
00110         IRCOM_RXSetPulseLength(254);
00111         */
00112 
00113         /* Enable both RX and TX. */
00114         USART_Rx_Enable(&USART);
00115         USART_Tx_Enable(&USART);
00116 
00117 
00118         /* Assume that everything is OK. */
00119         success = true;
00120         /* Send data from 255 down to 0*/
00121         sendData = 255;
00122         while(sendData) {
00123             /* Send one char. */
00124                 do{
00125                 /* Wait until it is possible to put data into TX data register.
00126                  * NOTE: If TXDataRegister never becomes empty this will be a DEADLOCK. */
00127                 }while(!USART_IsTXDataRegisterEmpty(&USART));
00128                 USART_PutChar(&USART, sendData);
00129 
00130                 uint16_t timeout = 1000;
00131                 /* Receive one char. */
00132                 do{
00133                 /* Wait until data received or a timeout.*/
00134                 timeout--;
00135                 }while(!USART_IsRXComplete(&USART) && timeout!=0);
00136                 receivedData = USART_GetChar(&USART);
00137 
00138                 /* Check the received data. */
00139                 if (receivedData != sendData){
00140                         success = false;
00141                 }
00142                 sendData--;
00143         }
00144 
00145         /* Disable both RX and TX. */
00146         USART_Rx_Disable(&USART);
00147         USART_Tx_Disable(&USART);
00148 
00149         /* If success the program ends up inside the if statement.*/
00150         if(success){
00151                 while(true);
00152         }else{
00153                 while(true);
00154         }
00155 }
@DOC_TITLE@
Generated on Tue Apr 22 15:16:31 2008 for AVR1303 Use and configuration of IR communication module by doxygen 1.5.5