Avoid Arduino HardwareSerial?

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

So, I am working on a Arduino Mega 2560 for a project that will be making use of 3 of the USART connections. I was originally using the HardwareSerial library. I knew there were certain libraries to avoid such as digitalwrite for time critical programs but I didn't think this applied to the Serial libraries until I was recently told that it is. Is this true? I've been trying to learn the names of all the registers and what they do and trying to write my own library but it's taking up alot more time than I had hoped. Should I keep trying to write my own Serial library or was my friend mistaken and the Arduino Serial libraries are fine. Also I think one of the serial connections will have to be interrupt driven, is this even possible using the Arduino libraries or would that one have to be customized as well for my use? Thanks for any help and sorry if this is posted somewhere else, I couldn't find anything pertaining to this except some tutorial explaining how to start my own.

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

BrisTheLimit wrote:
I was recently told ...

Who told you?

 

How did they justify it?

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. Jul 7, 2015 - 06:32 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The hardware serial is actually using interrupt driven ring buffers. It's about the best implementation you could hope for when using UART (guarantees you never miss anything and transmission just occurs "in the background") so I don't know who told you this but like Andy I'd love to hear their justification!

or was my friend mistaken and the Arduino Serial libraries are fine

This.

will have to be interrupt driven, is this even possible using the Arduino libraries

As I say, one of the joys of Arduino is that on this occasion they made the right choice at implementation and decided to already implement it using interrupts for both Tx and Rx. Saves you a LOT of work!

 

Also think about it - there are 100's of 1,000's of Arduino applications out there - a large proportion will be using the serial. Apart from your mate did you ever here anyone complain? (same cannot be said of pinMode and digitalWrite - we complain about that all the time!).

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

It really wasn't an argument. It was more of a "I believe it's no good and you should double check that" kind of thing. The only one I was aware of was the pinMode/digitalWrite but I don't have a ton of experience programming microcontrollers. So It made sense that there could be more Arduino libraries to avoid than just those two. Thanks for your help though.

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

I suggest that you just try a library for yourself. Come to your own conclusions.
Most "core" libraries are very efficient. Likewise third party libraries written for specific Shields can use hard code wiring. e.g. can assume that the SPI pins or I2C pins are connected to your external electronics.
It is when people are determined to use inappropriate pins that third parties have to bit bash GPIO pins instead of using the proper hardware. Convenient yes, efficient no.
David.