AVR Baud rate calculation is incorrect

ChibiOS public support forum for topics related to the Atmel AVR family of micro-controllers.

Moderator: tfAteba

Post Reply
JetForMe
Posts: 99
Joined: Mon Jan 31, 2011 8:12 am

AVR Baud rate calculation is incorrect

Post 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)
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: AVR Baud rate calculation is incorrect

Post by Giovanni »

I added your version too.

Giovanni
Post Reply