Fixed rounding error in baud rate calculation

This commit is contained in:
BlackMark 2019-07-28 19:20:03 +02:00
parent 00cb9ad13c
commit 6ded0e1c8d

View File

@ -55,7 +55,9 @@ struct HardwareAbstraction<SupportedHardware::ATmega1284P> {
template <uint32_t baudRate> template <uint32_t baudRate>
static constexpr auto calcBaud() static constexpr auto calcBaud()
{ {
return static_cast<uint16_t>((F_CPU / (16 * baudRate)) - 1); // The actual formula is (F_CPU / (16 * baudRate)) - 1, but this one has the advantage of rounding correctly
constexpr auto baudVal = (F_CPU + 8 * baudRate) / (16 * baudRate) - 1;
return baudVal;
} }
struct DataBitsVal { struct DataBitsVal {