Atmega 644 & UART - strange output

Discussion in 'Embedded Systems and Microcontrollers' started by tudstudent, Mar 12, 2015.

  1. tudstudent

    Thread Starter Member

    Jun 6, 2009
    20
    1
    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 (Text):
    1.  
    2. /*
    3.  
    4. A simple program to demonstrate the use of USART of AVR micro
    5.  
    6. Hardware:
    7.    ATmega644 @ 20MHz
    8.  
    9.    Suitable level converter on RX/TX line
    10.    Connected to PC via RS232
    11.    PC Software :  Hyper terminal @ 9600 baud
    12.                No Parity,1 Stop Bit,
    13.                Flow Control = NONE
    14.  
    15. */
    16. #ifndef F_CPU
    17. #define F_CPU 20000000UL
    18. #endif
    19.  
    20. # define USART_BAUDRATE 9600
    21. # define BAUD_PRESCALE ((( F_CPU) / (16UL * USART_BAUDRATE) )  - 1)
    22.  
    23. #include <avr/io.h>
    24.  
    25. //This function is used to initialize the USART
    26. //at a given UBRR value
    27.  
    28. void USART_Init( unsigned int baud ) // Extracted function from the datasheet
    29. {
    30.     /* Set baud rate */
    31.     UBRR0H = (unsigned char)(baud>>8);
    32.     UBRR0L = (unsigned char)baud;
    33.     /* Enable receiver and transmitter */
    34.     UCSR0B = (1<<RXEN0)|(1<<TXEN0);
    35.     /* Set frame format: 8data, 1stop bit */
    36.     UCSR0C = (3<<UCSZ00);
    37. }
    38.  
    39. //This function is used to read the available data from USART. This function will wait until data is
    40. //available.
    41. unsigned char USART_Receive( void ) // Extracted function from the datasheet
    42. {
    43.     /* Wait for data to be received */
    44.     while ( !(UCSR0A & (1<<RXC0)) )
    45.     ;
    46.     /* Get and return received data from buffer */
    47.     return UDR0;
    48. }
    49.  
    50. //This function writes the given "data" to the USART which then transmit it via TX line
    51. void USART_Transmit( unsigned char data ) // Extracted function from the datasheet
    52. {
    53.     /* Wait for empty transmit buffer */
    54.     while ( !( UCSR0A & (1<<UDRE0)) )
    55.     ;
    56.     /* Put data into buffer, sends the data */
    57.     UDR0 = data;
    58. }
    59.  
    60. int main(void)
    61. {
    62.    //Variable Declaration
    63.    char data;
    64.  
    65.    USART_Init(BAUD_PRESCALE);
    66.  
    67.    //Loop forever
    68.    while(1)
    69.    {
    70.       //Read data
    71.       data=USART_Receive();
    72.  
    73.       /* Now send the same data but but surround it in
    74.       square bracket. For example if user sent 'a' our
    75.       system will echo back '[a]'.
    76.       */
    77.  
    78.       USART_Transmit('[');
    79.       USART_Transmit(data);
    80.       USART_Transmit(']');
    81.    }
    82. }
    83.  
    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".
     
  2. tudstudent

    Thread Starter Member

    Jun 6, 2009
    20
    1
    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
     
    ErnieM likes this.
Loading...