a better way of receiving data through UART

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

hello avrfreaks,

 

please have a look at this function for receiving data through UART that i have written:

unsigned char UARTRx()
{
 unsigned char ch;
 while(Rxread1==Rxwrite1);
 ch=Rxbuffer[Rxread1];
 Rxbuffer[Rxread1++]='\0';
 if(Rxread1==BUFFER_SIZE) Rxread1=0;
 return ch;
}

is there any better way to read data from the UART?

 

i should mention that i am using c.

 

any suggestions are greatly appreciated.

 

This topic has a solution.

Long live my MASTER.

Last Edited: Fri. Aug 7, 2015 - 09:27 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I like the "lightweight ring buffer" code from Dean Camera as a part of LUFA:

 

http://www.fourwalledcubicle.com... (doc)

http://www.fourwalledcubicle.com... (code - yup, it's all in this .h file)

 

While the read_ptr==write_ptr is one way to check for "nothing in the buffer" I think a lot of people would favour a ring buffer that also keeps a count of how many bytes are in the buffer (because a full buffer can also look like read==write otherwise!). Then the wait simply becomes "while(bytes_in_buffer() == 0);"

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

abbas14 wrote:
is there any better way to read data from the UART?

That doesn't actually have anything to do with the UART at all - it's just reading from your software ring buffer!

 

One optimisation is to ensure that the size is a power of 2 ...

 

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks a lot.

Long live my MASTER.

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

While the read_ptr==write_ptr is one way to check for "nothing in the buffer" ...

Unless the app does >>nothing<< but process incoming characters from the UART, that is a "blocking read" function and will sit there forever waiting for the next character to arrive.  No display updates; no button press recognition; no servicing of external chips; ...

 

IMO/IME not much use in a "real" microcontroller app, unless there is indeed a way to "peek" to be sure there is something in the buffer before invoking the getchar.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

real firmware uses interrupts and a ringbuffer :)

Then getchar return false if the rb is empty

Keith Vasilakes

Firmware engineer

Minnesota