Atmega 644 & UART - strange output

Thread Starter

tudstudent

Joined Jun 6, 2009
20
Hi all,

Last couple of days I have started with my Atmega 644 (still had it arround from a previous project which did not start).
However I am capable of building an blinking led (without troubles), based on delay.h I am not capable in adding a serial port interface.

On the net I have not found a lot of info for the 644, since it is a little different with its registers. I think I have set them all correctly, but as said I get strang output (after code snippet examples):

Code:
/*

A simple program to demonstrate the use of USART of AVR micro

Hardware:
   ATmega644 @ 20MHz

   Suitable level converter on RX/TX line
   Connected to PC via RS232
   PC Software :  Hyper terminal @ 9600 baud
               No Parity,1 Stop Bit,
               Flow Control = NONE

*/
#ifndef F_CPU
#define F_CPU 20000000UL
#endif

# define USART_BAUDRATE 9600
# define BAUD_PRESCALE ((( F_CPU) / (16UL * USART_BAUDRATE) )  - 1)

#include <avr/io.h>

//This function is used to initialize the USART
//at a given UBRR value

void USART_Init( unsigned int baud ) // Extracted function from the datasheet
{
    /* Set baud rate */
    UBRR0H = (unsigned char)(baud>>8);
    UBRR0L = (unsigned char)baud;
    /* Enable receiver and transmitter */
    UCSR0B = (1<<RXEN0)|(1<<TXEN0);
    /* Set frame format: 8data, 1stop bit */
    UCSR0C = (3<<UCSZ00);
}

//This function is used to read the available data from USART. This function will wait until data is
//available.
unsigned char USART_Receive( void ) // Extracted function from the datasheet
{
    /* Wait for data to be received */
    while ( !(UCSR0A & (1<<RXC0)) )
    ;
    /* Get and return received data from buffer */
    return UDR0;
}

//This function writes the given "data" to the USART which then transmit it via TX line
void USART_Transmit( unsigned char data ) // Extracted function from the datasheet
{
    /* Wait for empty transmit buffer */
    while ( !( UCSR0A & (1<<UDRE0)) )
    ;
    /* Put data into buffer, sends the data */
    UDR0 = data;
}

int main(void)
{
   //Variable Declaration
   char data;

   USART_Init(BAUD_PRESCALE);

   //Loop forever
   while(1)
   {
      //Read data
      data=USART_Receive();

      /* Now send the same data but but surround it in
      square bracket. For example if user sent 'a' our
      system will echo back '[a]'.
      */

      USART_Transmit('[');
      USART_Transmit(data);
      USART_Transmit(']');
   }
}
If I configure my realterm as such:
- 8bit
- 1 stop
- no flow ctrl
- no parity

And send the char "a" I get "RXQ" in return, where I would be expecting "[a]".
Can someone advise me what I am doing wrong. I have read all options in the datasheet, but I cannot figure out by myself.

Furthermore connecting tx and rx directly give response on the console like "a" or "b".
 

Thread Starter

tudstudent

Joined Jun 6, 2009
20
Yeay, solved. Switched to a ftdi 2232 module (which I thought initially was broken), everything works now.
I get the correct response "[a]"

So the program was correct as well as my register settings
 
Top