title: Serial Connection ATMEGA328p
body: <p>I wrote up a function to connect via UART and print a string for debugging purposes. But my logic does not add up somehow... I see the line "Random Number" printed in the console but blazingly fast... no matter what I add for a _delay_ms value, it is not usable. Did I miss out on anything?</p>
  
  <p>Why is my delay function not having any influence on the output on the serial terminal?</p>
  
  <pre><code>void initUSART(void) {
  
      #define BAUDRATE ((F_CPU) / (BAUD * 8UL)-1) // Set Baud Rate Value for UBRR
  
      // Set register
      UBRR0H = (BAUDRATE &gt;&gt; 8);
      UBRR0L = BAUDRATE;
      UCSR0A |= (1 &lt;&lt; U2X0);  
      // Enable USART transmitter and receiver
      UCSR0B = (1 &lt;&lt; TXEN0) | (1 &lt;&lt; RXEN0);
      // Set 8 data bits and 1 stop bit
      UCSR0C = (1 &lt;&lt; UCSZ01) | (1 &lt;&lt; UCSZ00);
  }
  
  void transmitByte(uint8_t data) {
      // Wait for empty transmit buffer
      loop_until_bit_is_set(UCSR0A, UDRE0);
      UDR0 = data;
  }
  
  void printString(const char myString[]) {
      uint8_t i = 0;
      while (myString[i]) {
          transmitByte(myString[i]);
          i++;
      }
  }
  
  int main(void)
  {
      setup();    
      randomSeed(adcRead(0));
  
      while (1) 
      {   
          printString("Random Number:\n");
          _delay_ms(100000);
      }
  
      return (0);
  }
  </code></pre>
  
  <p>When I use <code>\r\n</code>at the end of the string, the output gets really strange:</p>
  
  <p><a href="https://i.stack.imgur.com/fySZH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fySZH.png" alt="enter image description here"></a></p>
  
  <p>When I try this test code everything works as expected, the LED blinks every second. I really don't see where the difference is, as it is the same function.</p>
  
  <pre><code>                                                       /* Blinker Demo */
  
  // ------- Preamble -------- //
  #include &lt;avr/io.h&gt;                        /* Defines pins, ports, etc */
  #include &lt;util/delay.h&gt;                     /* Functions to waste time */
  
  
  int main(void) {
  
    // -------- Inits --------- //
    DDRB |= 0b00000001;            /* Data Direction Register B:
                                     writing a one to the bit
                                     enables output. */
  
    // ------ Event loop ------ //
    while (1) {
  
      PORTB = 0b00000001;          /* Turn on first LED bit/pin in PORTB */
      _delay_ms(1000);                                           /* wait */
  
      PORTB = 0b00000000;          /* Turn off all B pins, including LED */
      _delay_ms(1000);                                           /* wait */
  
    }                                                  /* End event loop */
    return 0;                            /* This line is never reached */
  }
  </code></pre>