Xmega Application Note


IR_example_interrupt.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 "usart_driver.h"
00053 #include "IR_driver.h"
00054 
00055 
00057 #define NUM_BYTES  3
00058 
00059 #define USART USARTC0
00060 
00061 
00063 USART_data_t USART_data;
00065 uint8_t sendArray[NUM_BYTES] = {0x55, 0xaa, 0xf0};
00067 uint8_t receiveArray[NUM_BYTES];
00069 bool success;
00070 
00084 void main(void)
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 }
00171 
00172 
00179 #pragma vector=USARTC0_RXC_vect
00180 __interrupt void USARTC0_RxcIsr(void)
00181 {
00182         USART_RXComplete(&USART_data);
00183 }
00184 
00185 
00192 ISR(USARTC0_DRE_vect)
00193 {
00194         USART_DataRegEmpty(&USART_data);
00195 }
00196 
@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