Low Baud Rate speeds- How to set up using C code
Moderator: Benj
-
- 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
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
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
-
- Valued Contributor
- Posts: 740
- Joined: Fri Jun 06, 2014 3:53 pm
- Has thanked: 185 times
- Been thanked: 204 times
- Contact:
Re: Low Baud Rate speeds- How to set up using C code
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
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
-
- 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
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
Hopefully someone will answer to this and solve the issue.
Regards
George
-
- 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
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
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 245 times
-
- 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
Hi Stefan,
Thanks for your help.
I tried to add the C code (4 lines you mentioned) but i get an error(see attachment).
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
Thanks for your help.
I tried to add the C code (4 lines you mentioned) but i get an error(see attachment).
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
-
- 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
Hi George
Can you post your program?
I guess I forgot something... bits
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
Can you post your program?
I guess I forgot something... bits
For some baudrate you need a 16bit value. In a 8bit Pic you need two register for this value.TXSTAbits.BRGH=0;
BAUDCONbits.BRG16=0;
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
-
- 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
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
Is there anything else i have to configure to have it working ?
Thanks
George
-
- 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
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
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
-
- 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
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
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
-
- 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
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
Yes, something like this:
TXSTAbits.BRGH=0;
SPBRGbits.BRG16=0;
SPBRGH=0;
SPBRG=32;
regards
Stefan
https://docs.rs-online.com/a168/0900766b81382af8.pdf
-
- 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
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. Regards
George
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. Regards
George
-
- 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
Hi George,
How about?
How about
Code: Select all
BAUDCONbits.BRG16=0;
Martin
-
- 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
Hi Martin
Thanks a lot
I think you are right;
regards
Stefan
Thanks a lot
I think you are right;
Code: Select all
TXSTAbits.BRGH=0;
BAUDCONbits.BRG16=0;
SPBRGH=0;
SPBRG=32;
Stefan
-
- 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
Hi Martin and Stefanmedelec35 wrote: ↑Tue Feb 09, 2021 8:36 pmHi George,
How about?Code: Select all
BAUDCONbits.BRG16=0;
no luck with this code, i am getting error on C code again.
-
- 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
Hi George
remove BAUDCONbits.BRG16=0; // You don't need it necessarily.
just
;
regards
Stefan
remove BAUDCONbits.BRG16=0; // You don't need it necessarily.
just
Code: Select all
TXSTAbits.BRGH=0;
SPBRG=32
regards
Stefan
-
- 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
worked fine!
Thanks Again Stefan, i hope one day i can "payback" this to all you guys.
Thanks Again Stefan, i hope one day i can "payback" this to all you guys.