Page 1 of 1

Serial Change HW Baud

Posted: Mon Oct 12, 2020 10:59 am
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

Re: Serial Change HW Baud

Posted: Tue Oct 13, 2020 9:42 pm
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;

Re: Serial Change HW Baud

Posted: Tue Oct 13, 2020 10:05 pm
by mnf
Thanks Alan, that's a useful workaround

Martin

Re: Serial Change HW Baud

Posted: Tue Oct 13, 2020 10:45 pm
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

Re: Serial Change HW Baud

Posted: Tue Oct 13, 2020 11:31 pm
by mnf
Hi Alan,

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

Martin

Re: Serial Change HW Baud

Posted: Tue Oct 13, 2020 11:57 pm
by Alan_37
Hi Martin

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

Re: Serial Change HW Baud

Posted: Wed Oct 14, 2020 2:33 pm
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 216 times
Martin

Re: Serial Change HW Baud

Posted: Wed Oct 14, 2020 8:54 pm
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

Re: Serial Change HW Baud

Posted: Fri Oct 16, 2020 7:37 pm
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