Low Baud Rate speeds- How to set up using C code

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 7.

Moderator: Benj

Post Reply
George_B
Posts: 128
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 51 times
Been thanked: 19 times
Contact:

Low Baud Rate speeds- How to set up using C code

Post by George_B »

Hi all! I hope you are all well.



My question is (as the title says) how to set up lower baud rate speeds than 9600(flowcode default) using C code for device 18F4680. I would like to be able to use baud rate values of 1200 , 2400 and 4800.


It is not very clear to me what is the way we calculate and configure the parameters using C code.


Thanks in advance for anyone who will spend some time to answer this.


Best Regards
George

chipfryer27
Valued Contributor
Valued Contributor
Posts: 617
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Low Baud Rate speeds- How to set up using C code

Post by chipfryer27 »

Hi

Is there a reason you cannot use the UART component? It appears to facilitate the lower speeds you are after and also has a custom setting. I admit I haven't used that chip before, or the UART at lower than 9600 so I can't say it will actually work.

Regards

George_B
Posts: 128
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 51 times
Been thanked: 19 times
Contact:

Re: Low Baud Rate speeds- How to set up using C code

Post by George_B »

Hi, i tried to use the lower values baud rate but it is actually not working on hardware. There should be something missing in the flowcode's background..

Hopefully someone will answer to this and solve the issue.



Regards
George

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times
Contact:

Re: Low Baud Rate speeds- How to set up using C code

Post by stefan.erni »

Hi Georg

I hope it will help.

You can read in the manual from page 216 - 229 how to calculate the value for SPBRG SPBRGH

The C Code has to be after the init the Uart! You will "overwrite" the part with the speed for the uart from FC

REGISTER 18-1: TXSTA: TRANSMIT STATUS AND CONTROL REGISTER
bit 2 BRGH: High Baud Rate Select bit
Asynchronous mode:
1 = High speed
0 = Low speed

REGISTER 18-3: BAUDCON: BAUD RATE CONTROL REGISTER
bit 3 BRG16: 16-bit Baud Rate Register Enable bit
1 = 16-bit Baud Rate Generator – SPBRGH and SPBRG
0 = 8-bit Baud Rate Generator – SPBRG only (Compatible mode), SPBRGH value ignored

SPBRGH EUSART Baud Rate Generator Register High Byte
SPBRG EUSART Baud Rate Generator Register Low Byte

regards

Stefan
You can insert a C Code with 4 line like this.
//This is the C-Code:
TXSTA.BRGH=0;
BAUDCON.BRG16=1;
SPBRGH=0;
SPBRG=32;
Attachments
PIC18F2585_2680_4585_4680-data-sheet-30009625D.pdf
(3.52 MiB) Downloaded 220 times

George_B
Posts: 128
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 51 times
Been thanked: 19 times
Contact:

Re: Low Baud Rate speeds- How to set up using C code

Post by George_B »

Hi Stefan,

Thanks for your help.

I tried to add the C code (4 lines you mentioned) but i get an error(see attachment).
Sim_Error.jpg
Sim_Error.jpg (106.19 KiB) Viewed 9780 times
I am using 19.660800 Mhz Crystal and i want to set the baud rate value at 1200.

As far as i read in the manual i can only calculate the value of SPBRG. The formula i used for getting the SPBRG value is :

((FOSC/Desired Baud Rate)/64) – 1 = ((19660800/1200)/64)-1 = 255

I can't understand how to calculate the value for SPBRGH.


If you can give me a hand here i will be more than thankful.

Regards
George

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times
Contact:

Re: Low Baud Rate speeds- How to set up using C code

Post by stefan.erni »

Hi George

Can you post your program?

I guess I forgot something... bits
TXSTAbits.BRGH=0;
BAUDCONbits.BRG16=0;
For some baudrate you need a 16bit value. In a 8bit Pic you need two register for this value.
You can choose if you just use a 8 bit Value (in SPBRG)

If you set the bit BAUDCONbits.BRG16=0 you don't have to care about SPBRGH (H is for higher byte)

BRG16: 16-bit Baud Rate Register Enable bit
1 = 16-bit Baud Rate Generator – SPBRGH and SPBRG
0 = 8-bit Baud Rate Generator – SPBRG only (Compatible mode), SPBRGH value ignored

regards

Stefan

George_B
Posts: 128
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 51 times
Been thanked: 19 times
Contact:

Re: Low Baud Rate speeds- How to set up using C code

Post by George_B »

ok the compile to hex now does not returns any error. However the MCU is only sending data out(correctly at 1200 baud rate) and not receiving data .Note that i am using Rx INTerrupt to read the incoming data.

Is there anything else i have to configure to have it working ?


Thanks
George

George_B
Posts: 128
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 51 times
Been thanked: 19 times
Contact:

Re: Low Baud Rate speeds- How to set up using C code

Post by George_B »

Thanks to Stefan i manage to have the program working at 1200 baudrate.

The trick was to change( use grater than 1) the timeout value from UART Receive component.



I wish you all be well and health.


Regards
George

George_B
Posts: 128
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 51 times
Been thanked: 19 times
Contact:

Re: Low Baud Rate speeds- How to set up using C code

Post by George_B »

Hi again,


In my previous posts, with the help of Stefan, i manage to set the baud rate at 1200 on a 18F4680 device.

I am trying to set the same baud rate value on a 16F88 device but i am getting an error for the C code.


Should i use any other code than this i used for 18F device?



Thanks
George

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times
Contact:

Re: Low Baud Rate speeds- How to set up using C code

Post by stefan.erni »

Hi Georg

Yes, something like this:

TXSTAbits.BRGH=0;

SPBRGbits.BRG16=0;

SPBRGH=0;
SPBRG=32;


regards

Stefan

https://docs.rs-online.com/a168/0900766b81382af8.pdf

George_B
Posts: 128
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 51 times
Been thanked: 19 times
Contact:

Re: Low Baud Rate speeds- How to set up using C code

Post by George_B »

Hi Stafan, thank you once again for helping me out.

I got this error after compiling with the code you provided.

I am looking the datasheet so i can familiarise myself and learn about it.
Screenshot_2.jpg
Screenshot_2.jpg (20.63 KiB) Viewed 9495 times
Regards
George

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Low Baud Rate speeds- How to set up using C code

Post by medelec35 »

Hi George,
How about

Code: Select all

BAUDCONbits.BRG16=0;
?
Martin

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times
Contact:

Re: Low Baud Rate speeds- How to set up using C code

Post by stefan.erni »

Hi Martin
Thanks a lot
I think you are right;

Code: Select all

TXSTAbits.BRGH=0;
BAUDCONbits.BRG16=0;
SPBRGH=0;
SPBRG=32;
regards

Stefan

George_B
Posts: 128
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 51 times
Been thanked: 19 times
Contact:

Re: Low Baud Rate speeds- How to set up using C code

Post by George_B »

medelec35 wrote:
Tue Feb 09, 2021 8:36 pm
Hi George,
How about

Code: Select all

BAUDCONbits.BRG16=0;
?
Hi Martin and Stefan

no luck with this code, i am getting error on C code again. :(
Screenshot_3.jpg
Screenshot_3.jpg (27.2 KiB) Viewed 9459 times

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times
Contact:

Re: Low Baud Rate speeds- How to set up using C code

Post by stefan.erni »

Hi George

remove BAUDCONbits.BRG16=0; // You don't need it necessarily.
just

Code: Select all

TXSTAbits.BRGH=0;

SPBRG=32
;

regards

Stefan

George_B
Posts: 128
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 51 times
Been thanked: 19 times
Contact:

Re: Low Baud Rate speeds- How to set up using C code

Post by George_B »

worked fine!


Thanks Again Stefan, i hope one day i can "payback" this to all you guys.

Post Reply