We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 205d8b2 commit 5146822Copy full SHA for 5146822
megaavr/libraries/Wire/src/utility/twi.c
@@ -204,9 +204,12 @@ void TWI_MasterSetBaud(uint32_t frequency) {
204
t_rise = 1000;
205
}
206
207
- uint32_t baud = ((F_CPU / 1000 / freq_khz) - (((F_CPU * t_rise) / 1000) / 1000) / 1000 - 10) / 2;
208
- TWI0.MBAUD = (uint8_t)baud;
209
-
+ // uint32_t baud = ((F_CPU / 1000 / freq_khz) - (((F_CPU * t_rise) / 1000) / 1000) / 1000 - 10) / 2;
+ // TWI0.MBAUD = (uint8_t)baud;
+ // Prevent an integer overflow that can break the baud rate!
210
+ // Arduino megaAVR #90.
211
+ uint32_t baud = (F_CPU / frequency - F_CPU / 1000 / 1000 * t_rise / 1000 - 10) / 2;
212
+ TWI0.MBAUD = (uint8_t)baud;
213
214
215
/*! \brief TWI write transaction.
0 commit comments