USART Protocol

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

I am writing a code to send data over 2 AVR controllers board while one will only send other will receive and process the data received.


void usart_tx (char data)
{
    UDR = data;
    while(UCSRA.UDRE == 0);
}
unsigned char usart_rx (void)
{
    unsigned char data;
    unsigned int count = 0x00;
    while(UCSRA.RXC == 0 && count < 5000)
    {
        count++;
        delay_us(100);
    }
    if(count == 5000)
    {
        data = 0x00;
    }
    else
    {
        data = UDR;
    }
    return data;
}

Now my concern is that in one controller when I am calling usart_rx() followed by further processing of received data the other controller might be calling usart_tx()

So I would have a data loss. How can I protect it from occuring?[/code]

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

1. Make you reception of data interrupt driven. There are tutorials here on AVRfreaks dealing wwith this.

2. Implement a buffer in software to handle "bursts" of received data while the receiving AVR is busy with other things. (Google, and search AVRfreaks, for "ring buffer" , "circular buffer" and similar for the details.) The ISR writes into the buffer and the main() app reads out of the buffer.

3. Perhaps implement "flow control" over separate signal lines or in software. Which one to choose depends on several things: Do you have the extra AVR pins for doing it the "hardway way"? Will your payload data allow to reserve two unique transmitted byte values for doing it the soft way"? I am fairly sure that e.g. Wikipedia has articles dealing with flow control..

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]