Xmega Application Note


usart_example_interrupt.c File Reference


Detailed Description

XMEGA USART interrupt driven driver example source.

This file contains an example application that demonstrates the interrupt driven USART driver. The code example sends three bytes, waits for three bytes to be received and tests if the received data equals the sent data.

Application note:
AVR1307: Using the XMEGA USART
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
1694
Date
2008-07-29 14:21:58 +0200 (ti, 29 jul 2008)

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

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

Include dependency graph for usart_example_interrupt.c:

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.
 ISR (USARTC0_RXC_vect)
 Receive complete interrupt service routine.
int main (void)
 Example application.

Variables

uint8_t receiveArray [NUM_BYTES]
uint8_t sendArray [NUM_BYTES] = {0x55, 0xaa, 0xf0}
bool success
USART_data_t USART_data


Define Documentation

#define NUM_BYTES   3

Number of bytes to send in test example.

Definition at line 55 of file usart_example_interrupt.c.

Referenced by main().

#define USART   USARTC0

Define that selects the Usart used in example.

Definition at line 57 of file usart_example_interrupt.c.

Referenced by main().


Function Documentation

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 179 of file usart_example_interrupt.c.

References USART_DataRegEmpty().

00180 {
00181         USART_DataRegEmpty(&USART_data);
00182 }

Here is the call graph for this function:

ISR ( USARTC0_RXC_vect   ) 

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 167 of file usart_example_interrupt.c.

References USART_RXComplete().

00168 {
00169         USART_RXComplete(&USART_data);
00170 }

Here is the call graph for this function:

int 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 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 82 of file usart_example_interrupt.c.

References NUM_BYTES, receiveArray, sendArray, success, Usart_and_buffer::usart, USART, USART_Baudrate_Set, USART_Format_Set, USART_InterruptDriver_Initialize(), USART_Rx_Enable, USART_RXBuffer_GetByte(), USART_RXBufferData_Available(), USART_RxdInterruptLevel_Set, USART_Tx_Enable, and USART_TXBuffer_PutByte().

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 }

Here is the call graph for this function:


Variable Documentation

uint8_t receiveArray[NUM_BYTES]

Array to put received data in.

Definition at line 64 of file usart_example_interrupt.c.

Referenced by main().

uint8_t sendArray[NUM_BYTES] = {0x55, 0xaa, 0xf0}

Test data to send.

Definition at line 62 of file usart_example_interrupt.c.

Referenced by main().

bool success

Success variable, used to test driver.

Definition at line 66 of file usart_example_interrupt.c.

Referenced by main().

USART data struct used in example.

Definition at line 60 of file usart_example_interrupt.c.

@DOC_TITLE@
Generated on Wed Nov 5 10:23:28 2008 for AVRxxxx Application note title by doxygen 1.5.5