Skip to content

Commit 5146822

Browse files
authored
Update twi.c
1 parent 205d8b2 commit 5146822

File tree

1 file changed

+6
-3
lines changed
  • megaavr/libraries/Wire/src/utility

1 file changed

+6
-3
lines changed

megaavr/libraries/Wire/src/utility/twi.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,12 @@ void TWI_MasterSetBaud(uint32_t frequency) {
204204
t_rise = 1000;
205205
}
206206

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-
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+
// 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;
210213
}
211214

212215
/*! \brief TWI write transaction.

0 commit comments

Comments
 (0)