Changing baud rate calculation to always use double speed mode except for 57600 baud at 16 MHz.

http://code.google.com/p/arduino/issues/detail?id=394
This commit is contained in:
David A. Mellis 2010-11-11 23:28:21 -05:00
parent 24e057a343
commit 66755f9bce
1 changed files with 8 additions and 14 deletions

View File

@ -194,22 +194,16 @@ HardwareSerial::HardwareSerial(ring_buffer *rx_buffer,
void HardwareSerial::begin(long baud) void HardwareSerial::begin(long baud)
{ {
uint16_t baud_setting; uint16_t baud_setting;
bool use_u2x; bool use_u2x = true;
// U2X mode is needed for baud rates higher than (CPU Hz / 16) #if F_CPU == 16000000UL
if (baud > F_CPU / 16) { // hardcoded exception for compatibility with the bootloader shipped
use_u2x = true; // with the Duemilanove and previous boards and the firmware on the 8U2
} else { // on the Uno and Mega 2560.
// figure out if U2X mode would allow for a better connection if (baud == 57600) {
use_u2x = false;
// calculate the percent difference between the baud-rate specified and
// the real baud rate for both U2X and non-U2X mode (0-255 error percent)
uint8_t nonu2x_baud_error = abs((int)(255-((F_CPU/(16*(((F_CPU/8/baud-1)/2)+1))*255)/baud)));
uint8_t u2x_baud_error = abs((int)(255-((F_CPU/(8*(((F_CPU/4/baud-1)/2)+1))*255)/baud)));
// prefer non-U2X mode because it handles clock skew better
use_u2x = (nonu2x_baud_error > u2x_baud_error);
} }
#endif
if (use_u2x) { if (use_u2x) {
*_ucsra = 1 << _u2x; *_ucsra = 1 << _u2x;