Xmega Application Note


IR_example_polled.c File Reference


Detailed Description

XMEGA IR driver polled example source.

This file contains an example application that demonstrates the IR driver, and parts of the USART driver. It shows how to set up the IR communication module. USART C0 is used in this example. It can be tested, using a loop-back wire between I/O pins PC2 and PC3

Application note:
AVR1303: Use and configuration of IR communication module
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
481
Date
2007-03-06 10:12:53 +0100 (ty, 06 mar 2007)

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 IR_example_polled.c.

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

Include dependency graph for IR_example_polled.c:

Go to the source code of this file.

Defines

#define USART   USARTC0

Functions

void 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 IR_example_polled.c.


Function Documentation

void main ( void   ) 

Example application.

Example application. 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 IR_example_polled.c.

References success, and USART.

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 }


Variable Documentation

bool success

Success variable, used to test driver.

Definition at line 61 of file IR_example_polled.c.

@DOC_TITLE@
Generated on Tue Apr 22 15:16:32 2008 for AVR1303 Use and configuration of IR communication module by doxygen 1.5.5