When I transmit serial data on an ATmega2560 I sometimes get bit stretching, corrupting the characters:
I send character 0x55 twice at a baud rate of 115200; this is the yellow trace in the image. The red trace signals the start and end of the interrupt routine sending the characters.
The first character is fine. The falling edge is the start of the start bit, followed by 8 data bits (LSB to MSB) and then the stop bits (starting at the last rising edge).
The time for the 9 bits (start bit plus 8 data bits) is 78us; consistent with a bit time of 8.68us (115200 baud).
The second character is corrupted: two of the data bits are stretched. The start bit and the other data bits are fine.
This seems to happen randomly.
Is there anything that can cause this?