PDA

View Full Version : Comparative advantages?



RussMartin
- 20th September 2006, 07:11
Using PBPro:

Can someone direct me to a clear, concise treatment of the comparative advantages/disadvantages (and hinky things to beware of) of using HSERIN/HSEROUT versus SERIN/SEROUT? (Aside from the fact that the former uses the chip's hardware USART.)

I don't find the mELabs manual especially helpful on this point.

Thanks!

keithdoxey
- 20th September 2006, 09:25
This is something that interests me as well.

For Serout vs Hserout my personal opinion is that there is no real difference if you are sending several characters as Hserout would wait for each character to be sent whereas with a single character using Hserout would allow the program to continue with no delay.

I personally feel that the real advantage of Hardware serial comes with receiving data.

Using Serin you have to wait for each bit to be received and you have virtually no time to deal with the recieved character before the next one starts to arrive, particularly at higher baud rates. If you miss the start of the character then the data is lost.

Using Hserin the reception of characters is done automagically and upto 2 characters can be buffered which allows you more time to deal with received data. eg at 9600 which is approx 960 characters per second you have just over 1ms in which you can be doing other stuff before the next character arrives and if occasionally you take slightly longer than that the buffer should catch the next character to prevent data loss. Of course if you take too long every time then you have a big problem :)

The only real advantage I can see for software vs hardware is that software allows the logic levels to be inverted without any additional hardware but I am going to use a MAX232 for an RS232 interface to ensure proper positive and negative levels and I also plan on providing a USB serial interface using one of the tiny USB modules and these have TTL inputs and outputs so connect directly to the PIC USART

No doubt someone will be along with a better answer and I could be totally wrong which is why I am interested in this as I am currently trying to get about 20 pics talking to a master along with a couple of external serial sources !!!!

Darrel Taylor
- 20th September 2006, 12:28
I think there are 2 real advantages of HSERIN/OUT over the software SERIN/OUT. The first being speed. Due to the overhead involved in timing the signal in software, SERIN/OUT is limited to about 50kbaud, whereas HSER can manage a whopping 1250kbaud. That's 1.25 megabits per second. If you want to be a Top-Gun and have the "Need for Speed", it's the only way you'll get it.

But for me, the biggest advantage of HSERIN/OUT is the ability to withstand interrupts without affecting the timing of the serial data. With SERIN/OUT you'll be lucky to get over 9600 baud without even the quickest interrupts creating havoc with the data. But with HSER, as long as the interrupt takes less that 2 full "Byte periods", everything just cruises right along.

Add on the ability to respond to specific Addresses on an RS485 bus, and detecting framing errors if the signal gets a bit noisy, I think HSER comes up the Winner every time.

Hmmm, "hinky things to beware of".

<img src="http://www.darreltaylor.com/files/MrHanky.jpeg" height=134 width=134></img>

Oh, wait a minute, that's Hanky.

Well, the only problem I've had with HSER, were unchecked buffer overflows, which can cause the entire program to just stop running. And while it's possible to automatically clear the errors with ...

DEFINE HSER_CLROERR 1

It doesn't give you any indication that an error occurred. So you have to be careful with the design of your program. Or resort to reading 1 byte at a time, so that you can check for errors manually before using the HSERIN. And preferably use some form error checking with the use of checksums or something similar if the data absolutely positively MUST be accurate.

But compared with SERIN which has no error checking at all, HSER still Wins.

Oh yeah, one more thing. SERIN has a bit of a problem when the signal idles at the opposite state that it's expected to. For instance, with TRUE rs232, the idle state should be HIGH. If a cable is disconnected or for any other reason the signal idles LOW, SERIN will just sit there. Even if there's a timeout declared in the statement, the whole program freezes. HSER will timeout (if a timeout is declared).
<br>

Payatronico
- 20th September 2006, 14:54
First.- I don't think that the real factor of importance is the speed, with a 20MHz crystal you can make links in 12000 bauds with serin/out, that's because all the times are calculated for a crystal of 4MHz, for me the real deference between them are the space in the memory, serin/out take more memory than hserin/out.

The other thing is that with Hserin/out you can receive parity bits and multiple stop bits, with serin/out you only can receive one stop bit and no parity.

Darrel Taylor
- 21st September 2006, 05:14
I should probably clear something up here. When I refer to SERIN/OUT, it really means SERIN2 and SEROUT2. I don't even consider the Basic Stamp compatible SERIN and SEROUT to be part of PicBasic Pro. There's absolutely no reason to ever use them, unless you just can't get past the thought of using mode names like N2400 or T9600 like you would with a Basic Stamp.

And if you are referring to that form of SERIN/OUT then you are correct. You can't use parity's. However, you also can't go above 9600 baud, no matter what crystal you are using.

But with SERIN2 and SEROUT2, you can get up to 50Kbaud, assuming you have at least a 20mhz crystal, and you can also have either even or odd parity's.

2 Stop bit's are really meaningless, since those stop bit's will be at the "IDLE" polarity. It simply puts one more "bit period" in-between each byte. Kind of like Char Pacing, without the Pacing. If the data is coming in at 8N2, anything setup for 8N1, will receive it just fine.

It is true that HSERIN/OUT uses considerably less program space than SERIN2/SEROUT2, simply because most of the work is being done in hardware. But if program space is the main concern, then you might also consider DEBUGIN/OUT, since it has the smallest code size for "software based" serial routines. It is consequently much faster than SERIN2/SEROUT2, I think you can get up to 115200 baud with debug.

But most important here, is that either SERIN/SEROUT or SERIN2/SEROUT2 is not dependant on the oscillator frequency. The mode numbers are the same for any baud rate. You just have to make sure the oscillator is fast enough to support the desired speed.

A "complete list of the possible mode settings for PICBASIC PRO's Serin2 and Serout2 commands" can be found here
http://www.melabs.com/resources/ser2modes.htm
It also shows the required OSC freq's for each baud rate.

Although I wouldn't really call it a "Complete" list, since there are many other baud rates that can be achieved with mode numbers that are not listed.

To find those other baud rates, you can use the formula that's shown in the PBP manual under SERIN2.