Xmega Application Note


usart_example_interrupt.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00051 #include "usart_driver.h"
00052 #include "avr_compiler.h"
00053 
00055 #define NUM_BYTES  3
00056 
00057 #define USART USARTC0
00058 
00060 USART_data_t USART_data;
00062 uint8_t sendArray[NUM_BYTES] = {0x55, 0xaa, 0xf0};
00064 uint8_t receiveArray[NUM_BYTES];
00066 bool success;
00067 
00068 
00082 int main(void)
00083 {
00084         /* counter variable. */
00085         uint8_t i;
00086 
00087         /* This PORT setting is only valid to USARTC0 if other USARTs is used a
00088          * different PORT and/or pins are used. */
00089         /* PC3 (TXD0) as output. */
00090         PORTC.DIRSET   = PIN3_bm;
00091         /* PC2 (RXD0) as input. */
00092         PORTC.DIRCLR   = PIN2_bm;
00093 
00094         /* Use USARTC0 and initialize buffers. */
00095         USART_InterruptDriver_Initialize(&USART_data, &USART, USART_DREINTLVL_LO_gc);
00096 
00097         /* USARTC0, 8 Data bits, No Parity, 1 Stop bit. */
00098         USART_Format_Set(USART_data.usart, USART_CHSIZE_8BIT_gc,
00099                      USART_PMODE_DISABLED_gc, false);
00100 
00101         /* Enable RXC interrupt. */
00102         USART_RxdInterruptLevel_Set(USART_data.usart, USART_RXCINTLVL_LO_gc);
00103 
00104         /* Set Baudrate to 9600 bps:
00105          * Use the default I/O clock frequency that is 2 MHz.
00106          * Do not use the baudrate scale factor
00107          *
00108          * Baudrate select = (1/(16*(((I/O clock frequency)/Baudrate)-1)
00109          *                 = 12
00110          */
00111         USART_Baudrate_Set(&USART, 12 , 0);
00112 
00113         /* Enable both RX and TX. */
00114         USART_Rx_Enable(USART_data.usart);
00115         USART_Tx_Enable(USART_data.usart);
00116 
00117         /* Enable PMIC interrupt level low. */
00118         PMIC.CTRL |= PMIC_LOLVLEX_bm;
00119 
00120         /* Enable global interrupts. */
00121         sei();
00122 
00123         /* Send sendArray. */
00124         i = 0;
00125         while (i < NUM_BYTES) {
00126                 bool byteToBuffer;
00127                 byteToBuffer = USART_TXBuffer_PutByte(&USART_data, sendArray[i]);
00128                 if(byteToBuffer){
00129                         i++;
00130                 }
00131         }
00132 
00133         /* Fetch received data as it is received. */
00134         i = 0;
00135         while (i < NUM_BYTES) {
00136                 if (USART_RXBufferData_Available(&USART_data)) {
00137                         receiveArray[i] = USART_RXBuffer_GetByte(&USART_data);
00138                         i++;
00139                 }
00140         }
00141 
00142         /* Test to see if sent data equals received data. */
00143         /* Assume success first.*/
00144         success = true;
00145         for(i = 0; i < NUM_BYTES; i++) {
00146                 /* Check that each element is received correctly. */
00147                 if (receiveArray[i] != sendArray[i]) {
00148                         success = false;
00149                 }
00150         }
00151 
00152         /* If success the program ends up inside the if statement.*/
00153         if(success){
00154                 while(true);
00155         }else{
00156                 while(true);
00157         }
00158 }
00159 
00160 
00167 ISR(USARTC0_RXC_vect)
00168 {
00169         USART_RXComplete(&USART_data);
00170 }
00171 
00172 
00179 ISR(USARTC0_DRE_vect)
00180 {
00181         USART_DataRegEmpty(&USART_data);
00182 }
@DOC_TITLE@
Generated on Wed Nov 5 10:23:27 2008 for AVRxxxx Application note title by doxygen 1.5.5