PDA

View Full Version : Baud rate calculation



Demon
- 20th July 2006, 19:56
Hi,

I have a 16F876A transmitting via HSEROUT to a 18F4550 with a 20MHz crystal and I'd like to use the maximum speed possible.
http://ww1.microchip.com/downloads/en/DeviceDoc/39582b.pdf

I select High Speed (BRGH=1) on table 10.1 on p.115, so I only have the option of SYNC=0 (asynchronous). Asynchronous table 10-3 on p.116 gives a KBAUD rate of 312.5 for a 20 MHz speed and a SPBRG value 0.

I have absolutely no idea how they got from this formula FOSC/(16 (X + 1)) using the settings I chose. Anyways, how does this relate with the HSER_BAUD command? KBAUD means kilobaud in my mind, that would mean 312.5 x 1000, that value seems waaaaaay too high for me. I wasn't expecting anything higher than 115200 or so based on experience with modems.

I checked the datasheet for the 18F4550 (table 20-3 on p.245) and that's exactly the top end:
http://ww1.microchip.com/downloads/en/DeviceDoc/39632c.pdf

So now I'm confused on what setting to take to get the 2 chips working reliably. I'd really like to understand this 'cause I might have to change chip later.

Robert
:)

mister_e
- 20th July 2006, 22:19
Look at that...
http://www.picbasic.co.uk/forum/attachment.php?attachmentid=316&d=1118138371

Yeah the same placed on another website but for PBP... from the same guy ;)

Darrel Taylor
- 20th July 2006, 22:31
I was just going to mention your calculator Steve. Beat me too it.

Robert,

I think you're looking at the wrong table. 10-3 is for BRGH=0. For BRGH=1, see table 10-4.

@ 20mhz, with BRGH=1, and SPBRG= 0, the baud rate is 1250K, that's 1.25 million bits per second. And the formula works with that one.
<br>

Demon
- 21st July 2006, 01:31
Crap, and I had checked my work too. Getting old... :)

Steve's utility gave this (real cool, can check calculations instantly, saves me from typing that into an Excel sheet):
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_SPBRG 10 ' 115200 Bauds

I'm not a fan of all-encompassing hex values like that, I like to know what is happening. Is there a significant difference in performance if I set the bits manually like this?
DEFINE HSER_SPBRG 10 ' 115200 Bauds
RCSTA.7 = 1 ' SPEN Serial port enable
RCSTA.4 = 1 ' CREN Continuous receive enable
TXSTA.5 = 1 ' TXEN Transmit enable
TXSTA.4 = 0 ' SYNC Asynchronous communication
TXSTA.2 = 1 ' BRGH High speed baud rate

I like comments, it helps make up for a miserable memory. I can toggle stuff ON and OFF easily that way. I usually ignore default values, but I make exceptions for important and relevant settings like SYNC in the example above.

Robert
:)

Demon
- 21st July 2006, 20:12
Steve, any chance of letting the baud rate utility accept 1250000? That is a valid speed according to the datasheets, but the utility does not accept it.

Robert
:)

Demon
- 21st July 2006, 20:39
These values work in the utility:
FOSC=20
SPBRG=0
20/16(0+1) = 1.250 = 1250 KBAUD


These don't:
FOSC=48
SPBRG=0
48/16(0+1) = 3.000 = 3000 KBAUD

Don't you hate 18F MCUs?

Robert
:)

mister_e
- 21st July 2006, 21:32
nope i don't hate them... it's more a matter of updating it to fit with the EUSART.... as already asked a while back.

Do you really need it as this fast is always my question ;)

I'll do the EUSART thing one day.

For now, i'd modified the baud restriction. try it... use it to your own risk and all the usual disclaimer stuff. Didn't test it in real environement so, if it works, it works case not... too bad ;)

Demon
- 21st July 2006, 22:42
...Do you really need it as this fast is always my question...


Well, I don't know exactly how fast I need it the USART to go. All I know is that it has to be fast enough so that I can transfer 2 bytes from the 16F to the 18F without losing the USB connection.

I had some difficulty in coming up with a baud rate that was compatible between a 16F @ 20MHz and a 18F @ 48MHz. Since I can't change the FOSC for the 18F because of USB, I did the next best thing and adjusted the FOSC on the 16F. I used 16 MHz which is a multiple of 48, I figured I could come up with a compatible SPBRG value that way.

Sure enough, SPBRG=0 @ 16 MHz and SPBRG=2 @ 48 MHz both give 1000 KBAUD. It's not the maximum of 1250, but it's close. Now I have to make some tests later and see if the USB connection holds up.

Robert
:)

mister_e
- 21st July 2006, 22:50
use the calc and get the lower error % is one of the best trick i can say. Now uncheck the BRGH=1 box to compare both results. Sometime BRGH=1 give a better error % sometimes not. it's all depend of the OSC speed and the BRGH setting. This one of the reason i did this calc. Probably i could add an auto error % feature wich will compare both BRGH=1 and BRGH=0 results and give the best of it. As now it just provide the best error % for the BRGH you choose.

Using your example, both gave 0% error percentage.. couldn't be better assuming a real 16MHZ and a real 48MHZ
HTH

Demon
- 21st July 2006, 22:56
Thanks Steve, the new "improved" version works great!

Robert
:D

mister_e
- 21st July 2006, 23:00
How dare you edit the title bar? mon esti'd'chien sale :) LMAO!

Great to know it's working

Demon
- 21st July 2006, 23:01
Paint Brush is my friend.

I was wondering if you'd notice.

Robert
:D

mister_e
- 21st July 2006, 23:02
Hehe... yes i do ;)