Skip to content

Commit e7170e9

Browse files
egilkvfacchinm
authored andcommitted
Fix bug in baudrate compensation. (#44)
* Fix bug in baudrate compensation. The code is now in accordance to the data sheet. The previous code would in effect either apply twice the compensation, or apply no compensation, depending on the polarity of the compensation. * Improve readability of expression for baudrate compensation. It is now exactly the same as in the documentation. The net result is the same as the previous version. * Since we have code to correct for errors in F_CPU to generate the right baudrate, then we should use F_CPU, the specified CPU frequency as basis, not the F_CPU with whatever corrections.
1 parent dee1cc2 commit e7170e9

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

cores/arduino/UART.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,15 @@ void UartClass::begin(unsigned long baud, uint16_t config)
154154
uint8_t oldSREG = SREG;
155155
cli();
156156

157-
baud_setting = (((8 * F_CPU_CORRECTED) / baud) + 1) / 2;
157+
baud_setting = (((8 * F_CPU) / baud) + 1) / 2;
158158
// Disable CLK2X
159159
(*_hwserial_module).CTRLB &= (~USART_RXMODE_CLK2X_gc);
160160
(*_hwserial_module).CTRLB |= USART_RXMODE_NORMAL_gc;
161161

162162
_written = false;
163163

164164
int8_t sigrow_val = SIGROW.OSC16ERR5V;
165-
baud_setting *= (1024 + sigrow_val);
166-
baud_setting /= (1024 - abs(sigrow_val));
165+
baud_setting += (baud_setting * sigrow_val) / 1024;
167166

168167
// assign the baud_setting, a.k.a. BAUD (USART Baud Rate Register)
169168
(*_hwserial_module).BAUD = (int16_t) baud_setting;

0 commit comments

Comments
 (0)