---
+ Post New Thread
Results 1 to 6 of 6
  1. #1
    Newbie level 3
    Points: 169, Level: 2

    Join Date
    Jan 2012
    Posts
    3
    Helped
    0 / 0
    Points
    169
    Level
    2

    USART communication errors (ATmega16)

    Hey,

    I've got a problem with USART communication on ATmega16, I'm using USB-RS232 converter (AVR-CDC), with newest drivers and firmware, but simple echo doesn't work :( atmega is clocked by 11.0592MHz crystal, so there should be no errors.

    Here's the code:
    Code:
    #define FOSC 11059200
    #define BAUD 9600
    #define MYUBRR FOSC/16/BAUD-1
     
    #include <avr/io.h>
    #include <avr/interrupt.h>
    #include <util/delay.h>
    #include <inttypes.h>
     
    void USART_Init(unsigned int);
    void USART_Transmit(unsigned char data);
    unsigned char USART_Receive(void);
     
    unsigned char a;
     
    int main(void)
    {
    	USART_Init (MYUBRR);
     
        while(1)
        {
    		a=USART_Receive();
    		USART_Transmit(a);
        }
    }
     
    void USART_Init(unsigned int ubrr)
    {
    	/* Set baud rate */
    	UBRRH = (unsigned char)(ubrr>>8);
    	UBRRL = (unsigned char)ubrr;
    	/* Enable receiver and transmitter */
    	UCSRB = (1<<RXEN)|(1<<TXEN);
    	/* Set frame format: 8data, 1stop bit */
    	UCSRC |= (1 << URSEL) | (1 << UCSZ1) | (1<<UCSZ0);
    }
     
    void USART_Transmit(unsigned char data)
    {
    	/* Wait for empty transmit buffer */
    	while (!( UCSRA & (1<<UDRE)));
    	/* Put data into buffer, sends the data */
    	UDR = data;
    }
     
    unsigned char USART_Receive(void)
    {
    	/* Wait for data to be received */
    	while (!(UCSRA & (1<<RXC)));
    	/* Get and return received data from buffer */
    	return UDR;
    }
    and a sample communication (LED display is connected to atmega, so i know what it is receiving):

    Code:
    sent      atmega      received 
    00000000   00000000   00000000 
    00000001   01111111   01000000 
    00000010   00111111   01100000 
    00000011   01111110   00100000 
    00000100   00011111   01110000 
    00000101   01111101   01000001 
    00000110   00111110   00110000 
    00000111   01111100   00010000 
    00001000   00001111   01111000
    Thanks in advance for any help!

    •   Alt30th January 2012, 22:53

      advertising

        
       

  2. #2
    Full Member level 3
    Points: 1,548, Level: 8

    Join Date
    May 2009
    Posts
    172
    Helped
    24 / 24
    Points
    1,548
    Level
    8

    Re: USART communication errors (ATmega16)

    Two questions:
    1. If you disconnect the ATmega and connect Rx and Tx on the PC (?) do you receive what you transmit?
    2. If you reconnect the ATmega, what do you see on Rx and Tx using an oscilloscope?

    I think you should enable the UART after setting the frame format, not before.



    •   Alt31st January 2012, 07:30

      advertising

        
       

  3. #3
    Newbie level 3
    Points: 169, Level: 2

    Join Date
    Jan 2012
    Posts
    3
    Helped
    0 / 0
    Points
    169
    Level
    2

    Re: USART communication errors (ATmega16)

    1. Yes, echo on USB-RS232 converter is correct
    2. Unfortunately I don't have an oscilloscope...

    Enabling the UART after setting the frame format didn't solve the problem



    •   Alt31st January 2012, 11:59

      advertising

        
       

  4. #4
    Full Member level 3
    Points: 1,548, Level: 8

    Join Date
    May 2009
    Posts
    172
    Helped
    24 / 24
    Points
    1,548
    Level
    8

    Re: USART communication errors (ATmega16)

    Try to borrow an oscilloscope if possible.

    Verify that the setup is done EXACTLY as Atmel describe in some application note.



  5. #5
    Newbie level 3
    Points: 169, Level: 2

    Join Date
    Jan 2012
    Posts
    3
    Helped
    0 / 0
    Points
    169
    Level
    2

    Re: USART communication errors (ATmega16)

    okay it's working now, I was using USB-RS232 converter which consists of ATtiny2313 and MAX3232, when I connected RXD and TXD directly to ATtiny2313, not using MAX3232, everything started to work properly, thanks for your help!



  6. #6
    Full Member level 3
    Points: 1,548, Level: 8

    Join Date
    May 2009
    Posts
    172
    Helped
    24 / 24
    Points
    1,548
    Level
    8

    Re: USART communication errors (ATmega16)

    Shit happens!


    1 members found this post helpful.

+ Post New Thread
Please login