From 6ded0e1c8dc9596e86512d269ace36c5bc5a7a5e Mon Sep 17 00:00:00 2001 From: BlackMark Date: Sun, 28 Jul 2019 19:20:03 +0200 Subject: [PATCH] Fixed rounding error in baud rate calculation --- hardware0.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hardware0.hpp b/hardware0.hpp index 5a054b5..5c5c606 100644 --- a/hardware0.hpp +++ b/hardware0.hpp @@ -55,7 +55,9 @@ struct HardwareAbstraction { template static constexpr auto calcBaud() { - return static_cast((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 {