SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By kalupora
#98298
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
Code: Select all
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.