Serial Change HW Baud

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

Moderator: Benj

Post Reply
User avatar
Alan_37
Posts: 179
Joined: Sun May 01, 2016 8:36 pm
Has thanked: 51 times
Been thanked: 54 times
Contact:

Serial Change HW Baud

Post by Alan_37 »

Hi, I am trying to change the serial baud rate dynamically
but it seems that it does not work maybe I am doing something wrong
been trying all day with no luck.

I bet this bug is also present on V8, I am using Atmega 328P but will be also needing this for Atmega 2560
Would be happy if at least there is a quick workaround for this like some C code would be great until
someone finds the time to fix this.

I am monitoring the serial output using the below-attached code
Attachments
Ser_Mux_V1.fcfx
(16.24 KiB) Downloaded 215 times

User avatar
Alan_37
Posts: 179
Joined: Sun May 01, 2016 8:36 pm
Has thanked: 51 times
Been thanked: 54 times
Contact:

Re: Serial Change HW Baud

Post by Alan_37 »

Hi This sure is a Bug , but I just found a way around this

Just use a C Code, Tested this and works fine on Atmega 328P.

// Baud Rate 2400:
UBRR0L = 0xA0;
UBRR0H = 0x01;

// Baud Rate 4800:
UBRR0L = 0xCF;
UBRR0H = 0x00;

// Baud Rate 9600:
UBRR0L = 0x67;
UBRR0H = 0x00;

// Baud Rate 14400:
UBRR0L = 0x44;
UBRR0H = 0x00;

// Baud Rate 19200:
UBRR0L = 0x33;
UBRR0H = 0x00;

// Baud Rate 28800:
UBRR0L = 0x22;
UBRR0H = 0x00;

// Baud Rate 38400:
UBRR0L = 0x19;
UBRR0H = 0x00;

// Baud Rate 57600:
UBRR0L = 0x10;
UBRR0H = 0x00;

// Baud Rate 76800:
UBRR0L = 0x0C;
UBRR0H = 0x00;

// Baud Rate 115200:
UBRR0L = 0x08;
UBRR0H = 0x00;
Last edited by Alan_37 on Tue Oct 13, 2020 11:57 pm, edited 1 time in total.

mnf
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Serial Change HW Baud

Post by mnf »

Thanks Alan, that's a useful workaround

Martin

User avatar
Alan_37
Posts: 179
Joined: Sun May 01, 2016 8:36 pm
Has thanked: 51 times
Been thanked: 54 times
Contact:

Re: Serial Change HW Baud

Post by Alan_37 »

Hi Martin, you are welcome had to dig deep into the datasheet for this :)

It is also good to mention that this will work only on Channel 1 ( uart 0 )
for Channel 2 ( uart 1 ) use registers UBRR1L and UBRR1H instead, values are the same.

below is a simple Macro I am going to use maybe someone else will find it handy
Attachments
Change_Baud_Rate.FCM
(5.69 KiB) Downloaded 203 times

mnf
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Serial Change HW Baud

Post by mnf »

Hi Alan,

I'd be impressed if an Arduino can manage 1000000 baud!!

Martin

User avatar
Alan_37
Posts: 179
Joined: Sun May 01, 2016 8:36 pm
Has thanked: 51 times
Been thanked: 54 times
Contact:

Re: Serial Change HW Baud

Post by Alan_37 »

Hi Martin

You are right for sure there is no need for high-speed data transfer
will delete the last 3 .

mnf
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Serial Change HW Baud

Post by mnf »

Hi Alan,

I did a version for the Arduino Mega (4 UARTs) - taking a slightly different approach. I used a lookup table rather than a switch to calculate the required values (note that using a LUT instead of the C table I used would push the data into ROM saving a few bytes of RAM)

But - how to generalise (ie allow it to compile on a 328p (one UART) or a Mega (with many!)
SetBaud.fcm
(1.08 KiB) Downloaded 204 times
Martin

User avatar
Alan_37
Posts: 179
Joined: Sun May 01, 2016 8:36 pm
Has thanked: 51 times
Been thanked: 54 times
Contact:

Re: Serial Change HW Baud

Post by Alan_37 »

Hi Martin

Thanks for taking the time to look at this and write the macro.

Regarding your question on "how to generalize" I think that we should not try to reinvent the wheel
here cos the bug is in the AVR_CAL_UART.c

Don't get me wrong, I appreciate your work but for now, a simple workaround is enough
work should be done on the Cal file so we can have the bug fixed.

Thanks Again

Alan

mnf
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Serial Change HW Baud

Post by mnf »

Yes, I think you are right - a fix, is/will be hopefully in the pipes..

I've just been working with a Nextion display - programming it works at 961200 baud. Cool (well fast and cool)!

Martin

Post Reply