Page 1 of 1

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

Posted: Thu Dec 17, 2020 9:50 pm
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

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

Posted: Thu Dec 17, 2020 10:04 pm
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

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

Posted: Thu Dec 17, 2020 10:10 pm
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

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

Posted: Fri Dec 18, 2020 9:13 am
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;

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

Posted: Fri Dec 18, 2020 4:33 pm
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 12669 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

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

Posted: Fri Dec 18, 2020 4:58 pm
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

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

Posted: Fri Dec 18, 2020 6:03 pm
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

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

Posted: Fri Dec 18, 2020 8:22 pm
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

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

Posted: Tue Feb 09, 2021 9:58 am
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

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

Posted: Tue Feb 09, 2021 10:14 am
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

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

Posted: Tue Feb 09, 2021 10:52 am
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 12384 times
Regards
George

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

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

Code: Select all

BAUDCONbits.BRG16=0;
?

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

Posted: Wed Feb 10, 2021 11:05 am
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

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

Posted: Wed Feb 10, 2021 11:09 am
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 12348 times

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

Posted: Wed Feb 10, 2021 11:15 am
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

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

Posted: Wed Feb 10, 2021 11:34 am
by George_B
worked fine!


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