title: USART giving unknown values on the atmega 328p
body: <p>I have been unable to solve a simple USART transmit/receive program. The problem is that I used this in the past and worked effortlessly but now it returns in my Linux machine question marks and in windows some boxes and Chinese characters. I don´t understand what changed. I have tried a lot of different things like changing the code and the parts but nothing seems to solve it.</p>
  
  <p>I´m using a AVR atmega 328p, and a pololu usb programmer. I flashed it from the atmel studio in windows and from avrdude in linux.</p>
  
  <p>Here is the code:</p>
  
  <pre class="lang-c prettyprint-override"><code>#define F_CPU 1000000
  #define BAUD 9600
  
  #include &lt;avr/io.h&gt;
  #include &lt;util/delay.h&gt;
  #include &lt;util/setbaud.h&gt;
  
  
  void init_USART(void);
  void transmit_Byte(char data);
  char receive_Byte(void);
  
  int main(void)
  {
      char TEST;
      init_USART();                               /* Initialize USART */
  
      while (1) 
      {
  
          TEST =  receive_Byte();
          transmit_Byte(TEST);
  
      }
  }
  
  
  void init_USART(void)
  {
      UBRR0H = UBRRH_VALUE;                       
      UBRR0L = UBRRL_VALUE;
  
      #if USE_2x
          UCSR0A |= (1 &lt;&lt; U2X0);
      #else
          UCSR0A &amp;= ~(1 &lt;&lt; U2X0);
      #endif
  
      UCSR0B = (1 &lt;&lt; TXEN0) | (1 &lt;&lt; RXEN0);           /* Enable USART 
                                                      transmitt/receive */
      UCSR0C = (1 &lt;&lt; UCSZ01) | (1 &lt;&lt; UCSZ00);         /* 8 data bits, 1 stop 
                                                              bit */ 
  }
  
  void transmit_Byte(char data)
  {
      loop_until_bit_is_set(UCSR0A, UDRE0);           /* Wait for empty 
                                                      transmit buffer */
      UDR0 = data;
  }
  
  char receive_Byte(void)
  {
      loop_until_bit_is_set(UCSR0A, RXC0);
      return(UDR0);
  }
  </code></pre>
  
  <p>This is my first question so I apologize in advanced if I did something wrong.</p>
  
  <p>Thanks a lot for your time! </p>
  
  <p>edit: I have tried a ftdi232 and a mcp2221 </p>
  
  <p>edit 2: <a href="https://i.stack.imgur.com/AMB3S.png" rel="nofollow noreferrer">After addin UL</a> still not as expected but got less unknown characters.</p>
  
  <p>SOLVED: But I don't understand why. I changed the fuse CKDIV8 to 1 and it started working. The datasheet says it comes factory with 8 MHz clock and the CKDIV8=0 so it is set to 1 MHz. So why is it inversed? I also tried previously  <code>clock_prescale_set(clock_div_8);</code> as the datasheet suggest for prescaling and it didn't work neither.</p>
  
  <p>What is the difference between setting the CLDIV8 and the avr/power.h function?</p>