title: Broken data read from AVR USART transmission
body: <p>I have an atmega162 chip on stk500 dev board connected to ubuntu with serial cable.
  On atmega, I initialize usart and write some bytes. On Ubuntu side, <em>something</em> comes out of the pipe, but it's definitely not what was sent...</p>
  
  <p>To be more precise: For each byte sent, I get a series of about 6 or 7 bytes, each of them with either 0x00 or 0xC0 values.</p>
  
  <p>Relevant snippets of my code:</p>
  
  <p>compile flags:</p>
  
  <pre><code>CFLAGS = -g
  CFLAGS += -mmcu=atmega162
  CFLAGS += -W -Wall -Wshadow
  CFLAGS += -Os
  CFLAGS += -mcall-prologues
  CFLAGS += -finline-limit=10
  CFLAGS += -mno-interrupts
  CFLAGS += -ffunction-sections
  CFLAGS += -DF_CPU=7372800ULL
  </code></pre>
  
  <p>usart functions:</p>
  
  <pre><code>void Serial0Init(int baud)
  {
          unsigned int    ubrr;
  
          ubrr = ((F_CPU+8*baud)/(16*baud))-1;
          // Baud rate
          UBRR0H = (unsigned char)(ubrr&gt;&gt;8);
          UBRR0L = (unsigned char)ubrr;
          UCSR0A &amp;= ~(1 &lt;&lt; U2X0); // U2X off
          // Transmission settings
          UCSR0C = (1&lt;&lt;URSEL0)|(3&lt;&lt;UCSZ00);       // 8N1
          UCSR0B = (1&lt;&lt;RXEN0)|(1&lt;&lt;TXEN0);
  }
  
  unsigned char Serial0CheckTxReady()
  {
          return (UCSR0A&amp;_BV(UDRE0));     // nonzero if transmit register is ready to receive new data.
  }
  
  void Serial0Write(unsigned char data)
  {
          while (Serial0CheckTxReady()==0)        // while NOT ready to transmit
                  {}
          UDR0 = data;
  }
  </code></pre>
  
  <p>main code:</p>
  
  <pre><code>Serial0Init(9600);
  Serial0Write('!');
  </code></pre>
  
  <p>I receive data with simple python script:</p>
  
  <pre><code>import serial
  import os
  
  port = serial.Serial('/dev/ttyS0', 9600)
  
  print 'Reading from serial...'
  while True:
    c = port.read()
    print c, ord(c)
  </code></pre>
  
  <p>I double checked byte size settings and baud rate calculations, and everything seems okay... Any ideas what I'm doing wrong?</p>