What happens if you read UDR0 multiple times?

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.

 

 

 

Attachment(s): 

This topic has a solution.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Oct 6, 2020 - 11:41 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The forum doesn't allow the opening post to be marked as the answer to the question - but it is.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Oct 6, 2020 - 11:42 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Interesting result.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That is really interesting!

Not exactly what I would have expected, but understandable.

David