In another thread, MrKendo wrote:
I'm not sure what happens if you read UDR0 multiple times
in reply, I wrote:
If it's double-buffered, and the 2 buffers are full, you'd get the two (potentially different) received bytes.
Not sure what happens if you re-read UDR when no new byte has been received, though ...
See: https://www.avrfreaks.net/commen...
So I thought I'd try it and see:
while (1) { while ( !(UCSR0A & (1<<RXC0)) ) { // Wait for RXC } // At this point, RXC0 must be set - so there must be at least one byte to read from UDR0; // Read & display the UDR0 plus RXC0 and DOR0 (overrun) bits multiple times... usart_txString( "\r\nRXC0: " ); usart_txByte( (UCSR0A & (1<<RXC0)) ? '1' : '0' ); usart_txString( "; DOR0: " ); usart_txByte( (UCSR0A & (1<<DOR0)) ? '1' : '0' ); usart_txString( "; UDR0: " ); usart_txByte( UDR0); usart_txString( "\r\nRXC0: " ); usart_txByte( (UCSR0A & (1<<RXC0)) ? '1' : '0' ); usart_txString( "; DOR0: " ); usart_txByte( (UCSR0A & (1<<DOR0)) ? '1' : '0' ); usart_txString( "; UDR0: " ); usart_txByte( UDR0); usart_txString( "\r\nRXC0: " ); usart_txByte( (UCSR0A & (1<<RXC0)) ? '1' : '0' ); usart_txString( "; DOR0: " ); usart_txByte( (UCSR0A & (1<<DOR0)) ? '1' : '0' ); usart_txString( "; UDR0: " ); usart_txByte( UDR0); usart_txString( "\r\nRXC0: " ); usart_txByte( (UCSR0A & (1<<RXC0)) ? '1' : '0' ); usart_txString( "; DOR0: " ); usart_txByte( (UCSR0A & (1<<DOR0)) ? '1' : '0' ); usart_txString( "; UDR0: " ); usart_txByte( UDR0); usart_txString( "\r\nRXC0: " ); usart_txByte( (UCSR0A & (1<<RXC0)) ? '1' : '0' ); usart_txString( "; DOR0: " ); usart_txByte( (UCSR0A & (1<<DOR0)) ? '1' : '0' ); usart_txString( "; UDR0: " ); usart_txByte( UDR0); usart_txString( "\r\nRXC0: " ); usart_txByte( (UCSR0A & (1<<RXC0)) ? '1' : '0' ); usart_txString( "; DOR0: " ); usart_txByte( (UCSR0A & (1<<DOR0)) ? '1' : '0' ); usart_txString( "; UDR0: " ); usart_txByte( UDR0); usart_txString( "\r\n" ); // Pause - allow the user to type some characters _delay_ms(3000); }
That's the important bit - full code attached.
And the result, running on an ATmega328P XPlained Mini and logged by TeraTerm, is:
[2020-10-06 12:25:04.974] Hello, ATmega328P XPlained Mini world [2020-10-06 12:25:33.960] A [2020-10-06 12:25:33.965] RXC0: 1; DOR0: 0; UDR0: A [2020-10-06 12:25:33.994] RXC0: 0; DOR0: 0; UDR0: [2020-10-06 12:25:34.025] RXC0: 0; DOR0: 0; UDR0: [2020-10-06 12:25:34.056] RXC0: 0; DOR0: 0; UDR0: [2020-10-06 12:25:34.087] RXC0: 0; DOR0: 0; UDR0: [2020-10-06 12:25:34.118] RXC0: 0; DOR0: 0; UDR0: [2020-10-06 12:25:40.926] B [2020-10-06 12:25:40.930] RXC0: 1; DOR0: 0; UDR0: B [2020-10-06 12:25:40.959] RXC0: 0; DOR0: 0; UDR0: A [2020-10-06 12:25:40.991] RXC0: 0; DOR0: 0; UDR0: A [2020-10-06 12:25:41.022] RXC0: 0; DOR0: 0; UDR0: A [2020-10-06 12:25:41.053] RXC0: 0; DOR0: 0; UDR0: A [2020-10-06 12:25:41.083] RXC0: 0; DOR0: 0; UDR0: A [2020-10-06 12:25:46.606] C [2020-10-06 12:25:46.610] RXC0: 1; DOR0: 0; UDR0: C [2020-10-06 12:25:46.640] RXC0: 0; DOR0: 0; UDR0: B [2020-10-06 12:25:46.671] RXC0: 0; DOR0: 0; UDR0: B [2020-10-06 12:25:46.702] RXC0: 0; DOR0: 0; UDR0: B [2020-10-06 12:25:46.732] RXC0: 0; DOR0: 0; UDR0: B [2020-10-06 12:25:46.763] RXC0: 0; DOR0: 0; UDR0: B [2020-10-06 12:25:51.686] D [2020-10-06 12:25:51.690] RXC0: 1; DOR0: 0; UDR0: D [2020-10-06 12:25:51.720] RXC0: 0; DOR0: 0; UDR0: C [2020-10-06 12:25:51.751] RXC0: 0; DOR0: 0; UDR0: C [2020-10-06 12:25:51.782] RXC0: 0; DOR0: 0; UDR0: C [2020-10-06 12:25:51.813] RXC0: 0; DOR0: 0; UDR0: C [2020-10-06 12:25:51.843] RXC0: 0; DOR0: 0; UDR0: C [2020-10-06 12:26:00.463] E [2020-10-06 12:26:00.467] RXC0: 1; DOR0: 0; UDR0: E [2020-10-06 12:26:00.497] RXC0: 0; DOR0: 0; UDR0: D [2020-10-06 12:26:00.528] RXC0: 0; DOR0: 0; UDR0: D [2020-10-06 12:26:00.558] RXC0: 0; DOR0: 0; UDR0: D [2020-10-06 12:26:00.590] RXC0: 0; DOR0: 0; UDR0: D [2020-10-06 12:26:00.621] RXC0: 0; DOR0: 0; UDR0: D [2020-10-06 12:26:00.910] 1234567 [2020-10-06 12:26:03.653] RXC0: 1; DOR0: 0; UDR0: 1 [2020-10-06 12:26:03.683] RXC0: 1; DOR0: 0; UDR0: 2 [2020-10-06 12:26:03.713] RXC0: 1; DOR0: 1; UDR0: 7 [2020-10-06 12:26:03.744] RXC0: 0; DOR0: 0; UDR0: 2 [2020-10-06 12:26:03.775] RXC0: 0; DOR0: 0; UDR0: 2 [2020-10-06 12:26:03.806] RXC0: 0; DOR0: 0; UDR0: 2
Lines in the red colour are sent from the terminal;
Lines in the blue colour are received by the terminal.