Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

ATMEGA 128 USART

2004-12-29 by Rajan

I'm trying to write a simple code which would echo back 
character typed on Hyperterminal. Below mentioned is my code snippet 
I'm trying to use. Till now I have not succedded in making it work I 
can't figure out mistake

        There is clock settings present in AVR studio in two places

1. Tools>STK500>fuses
2. Tools>STK500>Advance frequency calibration


           My guess is I'm doing something wrong with frequency 
setting can any one please review the below code and also let me 
know what exact clock setting I have to use in above mentioned two 
places ??

-Rajan Rai



#include <io.h>

/* Prototypes */
void USART0_Init( unsigned int baudrate );
unsigned char USART0_Receive( void );
void USART0_Transmit( unsigned char data );

/* Main - a simple test program*/
void main( void )
{
	USART0_Init( 11 ); /* Set the baudrate to 19,200 bps using a 
3.6864MHz crystal */
	for(;;) 	    /* Forever */
	{
		USART0_Transmit( USART0_Receive() ); /* Echo the 
received character */
	}
}

/* Initialize UART */
void USART0_Init( unsigned int baudrate )
{
	/* Set the baud rate */
	UBRR0H = (unsigned char) (baudrate>>8);
	UBRR0L = (unsigned char) baudrate;

	/* Enable UART receiver and transmitter */
	UCSR0B = ( ( 1 << RXEN0 ) | ( 1 << TXEN0 ) );

	/* Set frame format: 8 data 2stop */
	UCSR0C = (1<<USBS0)|(1<<UCSZ01)|
(1<<UCSZ00);              //For devices with Extended IO
	//UCSR0C = (1<<URSEL)|(1<<USBS0)|(1<<UCSZ01)|
(1<<UCSZ00);   //For devices without Extended IO
}


/* Read and write functions */
unsigned char USART0_Receive( void )
{
	/* Wait for incomming data */
	while ( !(UCSR0A & (1<<RXC0)) )
		;
	/* Return the data */
	return UDR0;
}

void USART0_Transmit( unsigned char data )
{
	/* Wait for empty transmit buffer */
	while ( !(UCSR0A & (1<<UDRE0)) )
		;
	/* Start transmittion */
	UDR0 = data;
}

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.