Page 1 of 1

AVR Baud rate calculation is incorrect

Posted: Fri Feb 03, 2012 2:58 am
by JetForMe
It needs to be done in floating point, and rounded, not truncated. Here's what it is currently (in os/hal/platforms/AVR/serial_lld.h):

Code: Select all

/**
 * @brief   Macro for baud rate computation.
 * @note    Make sure the final baud rate is within tolerance.
 */
#define UBRR(b) (((F_CPU / b) >> 4) - 1)


and here's what I'm using:

Code: Select all

/**
 * @brief   Macro for baud rate computation.
 * @note    Make sure the final baud rate is within tolerance.
 */
#define UBRR(b)      ((((double) F_CPU / (double) b) / 16.0) - 0.5)

/**
 * @brief   Macro for baud rate computation when U2Xn == 1.
 * @note    Make sure the final baud rate is within tolerance.
 */
#define UBRR2(b)   ((((double) F_CPU / (double) b) / 8.0) - 0.5)

Re: AVR Baud rate calculation is incorrect

Posted: Sat Feb 04, 2012 3:40 pm
by Giovanni
I added your version too.

Giovanni