Page 1 of 1

UART with two differnt baudrates

Posted: Fri Nov 17, 2017 7:20 pm
by Dirk Bubley
Hello FC community,

I try to find a way to in order to reconfigure the Hardware UART baud rate with in the running Programm.
Device is a PIC 18F series microcontroller.

Normally I setup/define in FC the baudrate i the UART properties. Of course this works fine.

In my new a design I want to start with 9600Baud and at a later stage in the Programm I want to Change to finally 115000Baud.

Has anyboda a idea how to achieve this.

BR
Dirk

Re: UART with two differnt baudrates

Posted: Mon Nov 20, 2017 11:53 am
by Benj
Hi Dirk,

Is there a ChangeHWBaud macro available for the RS232 component? This should allow you to change the baud rate on the fly.

Re: UART with two differnt baudrates

Posted: Mon Nov 20, 2017 8:51 pm
by Dirk Bubley
Hi Ben,

your are so right!! Great help!! Sometimes it is hard to see things that are in front of you/me.

ChangeHWBaud
Changes the hardware UART baud rate allowing for dynamic speed changes.
Parameters
BYTE NewBaud
0=1200, 1=2400, 2=4800, 3=9600, 4=19200, 5=38400, 6=57600, 7=115200



Is there a way to define acustomize baud rate too. Like for example 50000.

THX
Dirk

Re: UART with two differnt baudrates

Posted: Tue Nov 21, 2017 11:18 am
by Benj
Hello Dirk,

Currently custom baud rates are not supported but you can add support for them by editing the CAL file.

First copy the file "Flowcode 6\CAL\PIC\PIC_CAL_UART.c" to your desktop or another place where you can edit the file.

Open the file using a text editor such as notepad.

Near the top of the file you will find this section of code.

Code: Select all

	#define MX_HARD_BAUD_1200	(((MX_CLK_SPEED / 1200) - 8) / 16)		//Was - 16 but had issues with rounding down
	#if (MX_HARD_BAUD_1200 > 255)
		#undef MX_HARD_BAUD_1200
		#define MX_HARD_BAUD_1200	(((MX_CLK_SPEED / 1200) - 8) / 64)		//Was - 16 but had issues with rounding down
		#define MX_HARD_SLOW_1200	1
	#else
		#define MX_HARD_SLOW_1200	0
	#endif
Change to add your new baud rate in the calculation.

Code: Select all

	#define MX_HARD_BAUD_1200	(((MX_CLK_SPEED / 50000) - 8) / 16)		//Was - 16 but had issues with rounding down
	#if (MX_HARD_BAUD_1200 > 255)
		#undef MX_HARD_BAUD_1200
		#define MX_HARD_BAUD_1200	(((MX_CLK_SPEED / 50000) - 8) / 64)		//Was - 16 but had issues with rounding down
		#define MX_HARD_SLOW_1200	1
	#else
		#define MX_HARD_SLOW_1200	0
	#endif
Save and copy the file back to the "Flowcode 6\CAL\PIC\" directory, you might want to make a backup of the file first so you can go back.

Then when calling the ChangeHWBaud pass the parameter as 0 to invoke the new overridden 1200 speed setting.

Hope this helps.

Re: UART with two differnt baudrates

Posted: Tue Nov 21, 2017 12:15 pm
by Dirk Bubley
Hi Ben,

very good. Many thanks this is the Point I was looking for. :D

BR

Dirk

Re: UART with two differnt baudrates

Posted: Sat Jan 08, 2022 5:09 pm
by George_B
Hello All!

I am trying to use 4Mhz crystal on a 18F4523 and 1200 baud rate on UART and the code is not working correctly.

I have successfully use custom baudrate of 1200 on the same device but the crystal frequency was 19.660800 Mhz and the C code at the beginning of the program was as below:

TXSTAbits.BRGH=0;
BAUDCONbits.BRG16=0;
SPBRGH=0;
SPBRG=255; //this value is for 1200baud rate




What is the settings for 4Mhz crystal on a 18F4523 and 1200 baud rate on UART?

I tried to use the table bellow but the program is not working correctly.



Screenshot_3.jpg
Screenshot_3.jpg (84.04 KiB) Viewed 4765 times


Thanks in advance
George

Re: UART with two differnt baudrates

Posted: Sat Jan 08, 2022 6:44 pm
by medelec35
You don't need code for 1200 as that is not a custom rate, you just choose 1200 within UART properties.
Just make sure the timings are correct with a one-sec flasher test first.

Re: UART with two differnt baudrates

Posted: Sat Jan 08, 2022 9:55 pm
by George_B
medelec35 wrote:
Sat Jan 08, 2022 6:44 pm
You don't need code for 1200 as that is not a custom rate, you just choose 1200 within UART properties.
Just make sure the timings are correct with a one-sec flasher test first.
thank you for your reply.

I tried to set the baudrate at 1200 in UART properties but it is not working. Note that i want to use software defined pins and not the default hardware pins. Does this make the difference and the program is not working at 1200?


Regards
George

Re: UART with two differnt baudrates

Posted: Sat Jan 08, 2022 11:34 pm
by medelec35
George_B wrote:
Sat Jan 08, 2022 9:55 pm
Note that i want to use software defined pins and not the default hardware pins. Does this make the difference and the program is not working at 1200?
Yes, on V8 it does.
Getting the baud to work at the correct rate for software on all targets is very time-consuming.
For example, my target device is 16F1937.
I have set the software baud to 1200. The measure baud is 917 = 23.6% error.
New to V9, there is a UART software (timer) component that uses a timer interrupt to generate a baud which is bang on the money.
With V9 even though 1200 is a standard baud rate, you will need to use custom to get it working.
A tip I always used to determine the UART baud rate.
Send a string with U it must be in upper case.
measure any single pulse width.
Baud = 1/singe pulse duration.
I did that for 4MHz and with a custom baud set at 1719, the baud is 1251 on hardware.
You will be best having a slightly faster baud as if you have any interrupts, the baud will be slowed down.