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
Serial Change HW Baud
Moderator: Benj
Re: Serial Change HW Baud
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;
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.
Re: Serial Change HW Baud
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

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 262 times
Re: Serial Change HW Baud
Hi Martin
You are right for sure there is no need for high-speed data transfer
will delete the last 3 .
You are right for sure there is no need for high-speed data transfer
will delete the last 3 .
-
- Valued Contributor
- Posts: 1208
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 440 times
Re: Serial Change HW Baud
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!)
Martin
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!)
Martin
Re: Serial Change HW Baud
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
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
-
- Valued Contributor
- Posts: 1208
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 440 times
Re: Serial Change HW Baud
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
I've just been working with a Nextion display - programming it works at 961200 baud. Cool (well fast and cool)!
Martin