title: How can I implement interrupt for serial USART communication for ATmega328P/Arduino Nano?
body: <p>I have a small project for school that requires me to load data in the EEPROM of an <a href="https://en.wikipedia.org/wiki/ATmega328#Family" rel="nofollow noreferrer">ATmega328P</a> through the USART serial communication. I'm going to figure out the EEPROM read/write myself.</p>
  <p>I have problems sending data using interrupts. Basically, I want to have the <a href="https://en.wikipedia.org/wiki/List_of_Arduino_boards_and_compatible_systems#Official" rel="nofollow noreferrer">Arduino Nano</a> loop through code and when I send something through the USART serial communication using the serial monitor of the Arduino IDE, an interruption will occur and the data that was send will be saved in a variable.</p>
  <p>My <em>clock</em> is 16 MHz, with a baud rate of 9600; and as I've said I'm using the Arduino IDE.</p>
  <p>Here is what I've tried so far:</p>
  <pre><code>#define USART_BAUDRATE 9600
  #define MYUBRR (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
  
  void setup() {
  
     UCSR0B = (1 &lt;&lt; RXEN0) | (1 &lt;&lt; TXEN0);   // Turn on the transmission and reception circuitry
     UCSR0C = (1 &lt;&lt; UCSZ00) | (1 &lt;&lt; UCSZ01); // Use 8-bit character sizes
  
     UBRR0H = (MYUBRR &gt;&gt; 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
     UBRR0L = MYUBRR; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
  
     UCSR0B |= (1 &lt;&lt; RXCIE0); // Enable the USART Receive Complete interrupt (USART_RXC)
  
     sei(); // Enable the Global Interrupt Enable flag so that interrupts can be processed
  }
  
  // Interrupt on receive
  ISR (USART_RXC_vect)
  {
     char ReceivedByte;
     ReceivedByte = UDR0; // Fetch the received byte value into the variable &quot;ByteReceived&quot;
     UDR0 = ReceivedByte; // Echo back the received byte back to the computer
  }
  
  // Use the eeprom() function to read/write to EEPROM
  void loop() {
  
  }
  </code></pre>
  <p>I've copied and adapted the code <a href="https://www.avrfreaks.net/forum/tut-soft-using-usart-interrupt-driven-serial-comms?page=all" rel="nofollow noreferrer">from this site (first post)</a>.</p>
  <p>(In the UCSR0C register, there is no URSEL bit that is mentioned in this post - the datasheet does not mention it and it gives me an error when I try to use it.)</p>
  <p>But it seems that the interruption (as it is presented in the last part of the post) is not working for me. I've tested to see if I can read and write in the serial monitor using the following code:</p>
  <pre><code>while ((UCSRA &amp; (1 &lt;&lt; RXC)) == 0) {}; // Do nothing until data have been received and is ready to be read from UDR
  ReceivedByte = UDR; // Fetch the received byte value into the variable &quot;ByteReceived&quot;
  
  while ((UCSRA &amp; (1 &lt;&lt; UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it
  UDR = ReceivedByte; // Echo back the received byte back to the computer
  </code></pre>
  <p>But this makes the controller wait for a data to be sent through the serial monitor, and then it prints it in the serial monitor. I do not want this behaviour.</p>
  <p>Here is the datasheet that I've used to check my code: <a href="http://ww1.microchip.com/downloads/en/devicedoc/atmel-42735-8-bit-avr-microcontroller-atmega328-328p_datasheet.pdf" rel="nofollow noreferrer">ATmega328P datasheet</a></p>
  <p>Did I do something wrong? Did I forget something in my implementation of USART serial communication interrupt? What is actually wrong with my implementation? why is it not working?</p>