From f46cb02828db27891a7fb1967f80dd30a4dd95ad Mon Sep 17 00:00:00 2001 From: Tobias Diedrich Date: Sat, 20 Dec 2025 08:33:34 +0100 Subject: [PATCH] Add rounding to baud rate calculation Without rounding: - Divisor: 33 = (125000000 / 115200 / 32) - RCAP2 value: 65503 - Actual baud rate: 118371 = (125000000 / 32 / 33) - Error: 2.75% With rounding: - Divisor: 34 = ((125000000 / 115200 + 16) / 32) - RCAP2 value: 65502 - Actual baud rate: 114889 = (125000000 / 32 / 34) - Error: 0.27% Should fix https://github.com/logicog/RTLPlayground/issues/48 --- rtlplayground.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rtlplayground.c b/rtlplayground.c index 7beb6b3..51f22f3 100644 --- a/rtlplayground.c +++ b/rtlplayground.c @@ -1649,8 +1649,8 @@ void setup_serial(void) // The RCAP2 registers contain the high/low byte that is loaded into // timer2 when T2 overflows to 0x10000 - RCAP2H = (0x10000 - (CLOCK_HZ / SERIAL_BAUD_RATE / 32)) >> 8; - RCAP2L = (0x10000 - (CLOCK_HZ / SERIAL_BAUD_RATE / 32)) % 0xff; + RCAP2H = (0x10000 - ((CLOCK_HZ / SERIAL_BAUD_RATE + 16) / 32)) >> 8; + RCAP2L = (0x10000 - ((CLOCK_HZ / SERIAL_BAUD_RATE + 16) / 32)) % 0xff; PCON |= 0x80; // Double the Baud Rate