Incorrectly calculating UART Baud Rate

A forums to allow bugs and problems with Flowcode v7 to be reported and resolved.

Moderator: Benj

Post Reply
Neon27
Posts: 6
Joined: Mon Aug 02, 2010 9:18 pm
Has thanked: 1 time
Been thanked: 2 times
Contact:

Incorrectly calculating UART Baud Rate

Post by Neon27 »

MCU: PIC18F67K22-I/PT
FREQ: 64 MHz

It appears that Flowcode V7 is incorrectly calculating the UART (EUSART) Baud Rate when I compile code with a baud rate lower than 4 kHz.
I believe that the issue might stem from a possible Flowcode miscalculation of the 'SPBRGH2' register.
Here's the Microchip baud calculation...

Solving for SPBRGHx:SPBRGx:
X = ((FOSC/Desired Baud Rate)/64) – 1
= ((64000000/4000)/64) – 1
= 249

249 fits easily into the SPBRG2 register (with SPBRGH2 register set to 0)

If I use a baud rate lower than 4 kHz, I get random baud rates (measured by my o-scope).
For example...

Solving for SPBRGHx:SPBRGx:
X = ((FOSC/Desired Baud Rate)/64) – 1
= ((64000000/2400)/64) – 1
= 416

I'm assuming that rollover of 'SPBRG2' is occurring, and 'SPBRGH2' is not being updated within Flowcode.
I get a host of random (higher) baud rates if I choose anything lower than 4 kHz. Here are some examples of rates I captured with my o-scope:

Flowcode: O-Scope:
240 Hz = 14.0 KHz
600 Hz = 7.62 KHz
700 Hz = 6.72 KHz
800 Hz = 4.42 KHz
900 Hz = 11.5 KHz
1000 Hz = 4.31 KHz
1500 Hz = 6.49 KHz
2000 Hz = 4.10 KHz
2360 Hz = 5.95 KHz
2400 Hz = 6.22 KHz
2600 Hz = 7.81 KHz
3000 Hz = 12.8 KHz
3500 Hz = 33.3 KHz

4000 Hz = 4.00 KHz
4800 Hz = 4.79 KHz
240000 Hz = 236. KHz
Attachments
003.jpg
003.jpg (87.45 KiB) Viewed 5790 times
002.jpg
002.jpg (115.09 KiB) Viewed 5790 times
001.jpg
001.jpg (42.29 KiB) Viewed 5790 times
Last edited by Neon27 on Thu Apr 18, 2019 4:25 pm, edited 1 time in total.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Incorrectly calculating UART Baud Rate

Post by Benj »

Hello,

Please can you try the 1 second flasher test to see if everything is setup correctly.
https://www.matrixtsl.com/wikiv7/index. ... ED_flasher

Neon27
Posts: 6
Joined: Mon Aug 02, 2010 9:18 pm
Has thanked: 1 time
Been thanked: 2 times
Contact:

Re: Incorrectly calculating UART Baud Rate

Post by Neon27 »

Thank you kindly for the assistance!

I appreciate the suggestion, buy my code is working 100% (I've attached a pic of my project)

My MCU is:
- driving an LCD display
- communicating with several I2c devices
- Driving 3 RGB LED's (WS2811)
- Converting an analog signal

As I mentioned, all baud rates above 4 kHz work 100%. I've verified this using my digital oscilloscope.

- I'm using Hardware UART Channel 1 (RC6, RC7) set to 9600 Hz (working fine. I measured it on my oscilloscope at exactly 9.6 kHz)
- I'm using Hardware UART Channel 2 (RG1, RG2) set to 2400 Hz (this one isn't working. It will only produce baud rates accurately above ~4 kHz)
Attachments
001.jpg
001.jpg (169.65 KiB) Viewed 5786 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Incorrectly calculating UART Baud Rate

Post by Benj »

Aha ok sorry I see. Yes slower baud rates will be tricky as we currently only use the 8-bit baud mode.

You should be able to modify the registers using a C code icon to allow the lower baud rates.

Call the C code after you have initialised the UART or the initialise will override the register values.

Replace xxx with the number of the UART peripheral.

Code: Select all

TXSTAxxxbits.BRGH = 0;          //Low speed Baud rate
BAUDCONxxxbits.BRG16 = 1;    //Switch to 16-bit BAUD
Then do a quick calc to get the value for the baud registers. FOSC / (16 x BAUD)

Code: Select all

SPBRGxxx = CALCVAL;
SPBRGHxxx = CALCVAL >> 8;

Neon27
Posts: 6
Joined: Mon Aug 02, 2010 9:18 pm
Has thanked: 1 time
Been thanked: 2 times
Contact:

Re: Incorrectly calculating UART Baud Rate

Post by Neon27 »

Thanks Ben! :mrgreen:
I uploaded the code and it works like a charm!

Here's what I ended up using:

TXSTA2bits.BRGH = 0;
BAUDCON2bits.BRG16 = 1;
SPBRG2 = 129;
SPBRGH2 = 6;

With my 4xPLL OSC running at 64MHz, I'm getting a Baud rate of 2403 kHz (0.12% error)

Post Reply