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.
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]