Xmega Application Note | |||||
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
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_interrupt.c.
#include "avr_compiler.h"
#include "usart_driver.h"
#include "IR_driver.h"
Go to the source code of this file.
Defines | |
#define | NUM_BYTES 3 |
#define | USART USARTC0 |
Functions | |
ISR (USARTC0_DRE_vect) | |
Data register empty interrupt service routine. | |
void | main (void) |
Example application. | |
__interrupt void | USARTC0_RxcIsr (void) |
Receive complete interrupt service routine. | |
Variables | |
uint8_t | receiveArray [NUM_BYTES] |
uint8_t | sendArray [NUM_BYTES] = {0x55, 0xaa, 0xf0} |
bool | success |
USART_data_t | USART_data |
#define NUM_BYTES 3 |
Number of bytes to send in test example.
Definition at line 57 of file IR_example_interrupt.c.
Referenced by main().
#define USART USARTC0 |
Define that selects the Usart used in example.
Definition at line 59 of file IR_example_interrupt.c.
Referenced by main().
ISR | ( | USARTC0_DRE_vect | ) |
Data register empty interrupt service routine.
Data register empty interrupt service routine. Calls the common data register empty complete handler with pointer to the correct USART as argument.
Definition at line 192 of file IR_example_interrupt.c.
References USART_data.
00193 { 00194 USART_DataRegEmpty(&USART_data); 00195 }
void main | ( | void | ) |
Example application.
Example application. This example configures USARTC0 for with the parameters:
This function then sends three bytes 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 three bytes have been successfully sent and received.
Definition at line 84 of file IR_example_interrupt.c.
References NUM_BYTES, receiveArray, sendArray, success, USART, and USART_data.
00085 { 00086 /* counter variable. */ 00087 uint8_t i; 00088 00089 /* This PORT setting is only valid to USARTC0 if other USARTs is used a 00090 * different PORT and/or pins are used. */ 00091 /* PC3 (TXD0) as output. */ 00092 PORTC.DIRSET = PIN3_bm; 00093 /* PC2 (RXD0) as input. */ 00094 PORTC.DIRCLR = PIN2_bm; 00095 00096 /* Use USARTC0 and initialize buffers. */ 00097 USART_InterruptDriver_Initialize(&USART_data, &USART, USART_DREINTLVL_LO_gc); 00098 00099 /* USARTC0, 8 Data bits, No Parity, 1 Stop bit. */ 00100 USART_Format_Set(USART_data.usart, USART_CHSIZE_8BIT_gc, 00101 USART_PMODE_DISABLED_gc, false); 00102 00103 /* Enable RXC interrupt. */ 00104 USART_RxdInterruptLevel_Set(USART_data.usart, USART_RXCINTLVL_LO_gc); 00105 00106 /* Set Baud rate to 9600 bps: 00107 * Use the default I/O clock frequency that is 2 MHz. 00108 * Do not use the baud rate scale factor 00109 * 00110 * Baud rate select = (1/(16*(((I/O clock frequency)/Baud rate)-1) 00111 * = 12 00112 */ 00113 USART_Baudrate_Set(&USART, 12 , 0); 00114 00115 /* Set USARTC0 in IrDA mode.*/ 00116 USART_SetMode(&USARTC0, USART_CMODE_IRDA_gc); 00117 00118 /* If using another Pulse Length than the standard 3/16 use the functions 00119 * shown below. */ 00120 /* 00121 IRCOM_TXSetPulseLength(254); 00122 IRCOM_RXSetPulseLength(254); 00123 */ 00124 00125 /* Enable both RX and TX. */ 00126 USART_Rx_Enable(USART_data.usart); 00127 USART_Tx_Enable(USART_data.usart); 00128 00129 /* Enable PMIC interrupt level low. */ 00130 PMIC.CTRL |= PMIC_LOLVLEX_bm; 00131 00132 /* Enable global interrupts. */ 00133 __enable_interrupt(); 00134 00135 /* Send sendArray. */ 00136 i = 0; 00137 while (i < NUM_BYTES) { 00138 bool byteToBuffer; 00139 byteToBuffer = USART_TXBuffer_PutByte(&USART_data, sendArray[i]); 00140 if(byteToBuffer){ 00141 i++; 00142 } 00143 } 00144 00145 /* Fetch received data as it is received. */ 00146 i = 0; 00147 while (i < NUM_BYTES) { 00148 if (USART_RXBufferData_Available(&USART_data)) { 00149 receiveArray[i] = USART_RXBuffer_GetByte(&USART_data); 00150 i++; 00151 } 00152 } 00153 00154 /* Test to see if sent data equals received data. */ 00155 /* Assume success first.*/ 00156 success = true; 00157 for(i = 0; i < NUM_BYTES; i++) { 00158 /* Check that each element is received correctly. */ 00159 if (receiveArray[i] != sendArray[i]) { 00160 success = false; 00161 } 00162 } 00163 00164 /* If success the program ends up inside the if statement.*/ 00165 if(success){ 00166 while(true); 00167 }else{ 00168 while(true); 00169 } 00170 }
__interrupt void USARTC0_RxcIsr | ( | void | ) |
Receive complete interrupt service routine.
Receive complete interrupt service routine. Calls the common receive complete handler with pointer to the correct USART as argument.
Definition at line 180 of file IR_example_interrupt.c.
References USART_data.
00181 { 00182 USART_RXComplete(&USART_data); 00183 }
uint8_t receiveArray[NUM_BYTES] |
Array to put received data in.
Definition at line 67 of file IR_example_interrupt.c.
Referenced by main().
uint8_t sendArray[NUM_BYTES] = {0x55, 0xaa, 0xf0} |
bool success |
Success variable, used to test driver.
Definition at line 69 of file IR_example_interrupt.c.
Referenced by main().
USART_data_t USART_data |
USART data struct used in example.
Definition at line 63 of file IR_example_interrupt.c.
Referenced by ISR(), main(), and USARTC0_RxcIsr().
Generated on Tue Apr 22 15:16:31 2008 for AVR1303 Use and configuration of IR communication module by ![]() |