# loop back program

**URL:** <https://community.sparkfun.com/t/loop-back-program/15310>\
**Category:** MicroMod\
**Tags:** avr-atmel\
**Created:** [April 12, 2010, 7:56am UTC](https://community.sparkfun.com/t/loop-back-program/15310 "2010-04-12T07:56:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kalupora](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@kalupora](https://community.sparkfun.com/u/kalupora)\
**Post date:** [April 12, 2010, 7:56am UTC](https://community.sparkfun.com/t/loop-back-program/15310/1 "2010-04-12T07:56:16Z")

</div>

i wanted to write a program in avr using atmega324p to send data from pc using hyperterminal to a receiving xbee which will take in the receving data, and from the microcontroller, send back the receiving data back through the xbee. Below is the code i got

```auto
include <avr/io.h>

/* Prototypes */
void USART_Init( unsigned int baud );
unsigned char USART_Receive( void );
void USART_Transmit( unsigned char data );

/* Main - a simple test program*/
void main( void )
{
USART_Init( 103 ); /* Set the baudrate to 9600 bps */

for(; ; ) /* Forever */
{
USART_Transmit( USART_Receive() ) ; /* Echo the received character */
}
}

/* Initialize UART */
void USART_Init( unsigned int baud )
{
/* Set the baud rate */
UBRR1H = (unsigned char) (baud>> 8 ) ;
UBRR1L = (unsigned char) baud;

/* Enable UART receiver and transmitter */
UCSR1B = ( ( 1 << RXEN1 ) | ( 1 << TXEN1 ) );

/* Set frame format: 8 data 1stop */
UCSR1C = (0<<USBS1)|(3<<UCSZ10);
}

/* Read and write functions */
unsigned char USART_Receive( void )
{
/* Wait for incomming data */
while ( !(UCSR1A & (1<<RXC1)) )
;
/* Return the data */
return UDR1;
USART_Transmit(USART_Receive());
}

void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !(UCSR1A & (1<<UDRE1)) )
;
/* Start transmition */
UDR1 = data;
}

```

if there are any mistakes, please let me know. I want to know what is wrong.

---

<div class="post-metadata">

**Author:** ![kalupora](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@kalupora](https://community.sparkfun.com/u/kalupora)\
**Post date:** [April 12, 2010, 4:58pm UTC](https://community.sparkfun.com/t/loop-back-program/15310/2 "2010-04-12T16:58:47Z")

</div>

sori… the code works… i was doin sumthin else…
