Pages: [1] 2   Go Down
Author Topic: Arduino Uno Serial Problems (57600 baud is broken)  (Read 9936 times)
0 Members and 1 Guest are viewing this topic.
Canada
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
I Love Arduino!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I tried to switch my Duemilanove for a Uno and I have been experiencing serial communication issues.

I am using OS X 10.6.
I bought my Arduino Uno from Sparkfun.
The Arduino is connected by USB directly to the computer.
I am using the Arduino IDE 0021.

Here is the sample code:

Code:
void setup() {
  Serial.begin(57600);
}

void loop() {
  while ( Serial.available( ) > 0 ) {
    byte serialByte = Serial.read();
    Serial.print(serialByte);
  }
}

When the baud is set to 57600 the Arduino Uno returns gibberish. For example, if I send the bytes 240 1 247, the Arduino Uno returns 248 224 and nothing else!

If I set the baud to 115200, the Arduino Uno returns the proper values.

Anybody else experiencing this?

Logged

Thomas Ouellet Fredericks

0
Offline Offline
Faraday Member
**
Karma: 13
Posts: 2857
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What are you using to communicate with your Arduino? Are you using the built-in serial monitor? Whatever you are using, you must make sure to set its baud rate to match whatever you give to Serial.begin(). It sounds like you are changing the baud rate in your sketch but not in the serial monitor.

--
The Quick Shield: breakout all 28 pins to quick-connect terminals
Logged

Canada
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
I Love Arduino!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I tested with the serial monitor, Pure Data and Processing. I also switched boards with the same code. The Duemilanove runs the code fine, the Arduino Uno does not.

Here are some more strange results (when using Arduino's serial monitor set to the proper baud rate) with the Arduino Uno and the same code:

When I type "a" (send)
I receive "a"

When I type "ab" (send)
I receive "ab"

When I type "abc" (send)
I receive "abì"

When I type "abcdefg" (send)
I receive "ab,V–"

If I switch the baud to 115200 in the code AND in the serial monitor, all the messages sent are identical to the ones received. There definitely is a problem with 57600.

Logged

Thomas Ouellet Fredericks

Austin, TX
Offline Offline
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am seeing the same thing.

Similar setup: OSX 10.6, Uno, 0020 (instead of 0021).  At 57600 there tends to be garbage with 2 or more characters.  No issues at 115200.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

0
Offline Offline
Faraday Member
**
Karma: 13
Posts: 2857
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That is interesting. It does have all the hallmarks of a not-quite-right-baudrate issue though. I don't suppose you have an oscilloscope that you can connect to the RXD serial line (digital pin D0) to accurately measure the bit width? It should be 17.36 microseconds.

--
The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, light sensor, potentiometers, pushbuttons
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6378
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I see the same problem.  I can get it to work better by commenting out the fancy code in HardwareSerial.cpp that tries to get "better" bitrates by using U2X mode...

(interestingly, the other direction seems to work fine.  I don't see any errors in the byte stream sent TO my mac...)
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6378
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I wrote a sketch to dump the UART config.  It is using u2x mode for 57600, and it DOES seem to have correct values for the bit rate generator...

Hmm.  Now that the Uno has an ATmega chip for the USB/Serial converter, is THAT firmware (LUFA) using the same algorithm for picking bitrates as the arduino firmware?  This sort of thing could be explained if the 8u is picking a divisor/setting with +n% error, while the 328 is picking a divisor with -m% error...  Having them MATCH is more important than actually being close to the specified bit rate...
Code:
void setup()
{
  Serial.begin(57600);
  Serial.print("UCSR0A: ");
  Serial.println(UCSR0A, BIN);
  Serial.print("UCSR0B: ");
  Serial.println(UCSR0B, BIN);
  Serial.print("UCSR0C: ");
  Serial.println(UCSR0C, BIN);
  Serial.print("UBRR0H:L: ");
  Serial.print(UBRR0H, HEX);
  Serial.print(UBRR0L, HEX);


}

void loop()
{
  byte newstatus = 0;
  int c = Serial.read();
  while (c > 0) {
    if (UCSR0A & 0x3C) {
      Serial.print("Error: ");
      Serial.println(UCSR0A, HEX);
    }
    newstatus = 1;
    Serial.print(c, HEX);
    Serial.print(" ");
    if (c == '\r') {
      Serial.println();
    }
    delay(1);
    c = Serial.read();
  }
  if (newstatus) {
    Serial.println();
    Serial.print("UCSR0A: ");
    Serial.println(UCSR0A, BIN);
    Serial.print("UCSR0B: ");
    Serial.println(UCSR0B, BIN);
    Serial.print("UCSR0C: ");
    Serial.println(UCSR0C, BIN);
    Serial.print("UBRR0H:L: ");
    Serial.print(UBRR0H, HEX);
    Serial.println(UBRR0L, HEX);
  }
}
« Last Edit: November 08, 2010, 03:19:51 am by westfw » Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6378
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In fact, it looks like the 8u firmware has special case code for 57600 that DOES configure its uart "opposite" the way the 328 is configured.  I can't tell whether that is a bug, or something that was done specifically to address 57600bps issues with an earlier Arduino; there's a comment there about being compatible with the 57600bps bootloader on 328, but... the 328 bootloader doesn't RUN at 57600 any more.  And if it is talking about the current Arduino code, it looks like the tests are backward (it picks u2x for everything except 57600 !?)
Logged

Canada
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
I Love Arduino!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Should a bug report be filed? If so, where?
Logged

Thomas Ouellet Fredericks

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6378
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have submitted
  http://code.google.com/p/arduino/issues/detail?id=394
Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 506
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does it work better if you use Serial.begin(57599) or other numbers around 57600? This should trick the 328 and 8u2 into using the same parameters.

I am not having any problems with 57600 baud myself but I had a similar issue with 300 baud. I had to use Serial.begin(298) to get that speed to work.

I am using an Arduino Uno on Ubuntu 10.10 64 bit and Windows XP. An older (FTDI) Arduino Mega works fine at 300 baud on both systems.

Logged


SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6378
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Does it work better if you use Serial.begin(57599) or other numbers around 57600?
Good idea!  Serial.begin(57850) seems to improve things...
Code:
For baud=057770, nonu2x_baud = 058823, u2x_baud = 057142 Use U2x, divisor = 34
For baud=057780, nonu2x_baud = 058823, u2x_baud = 057142 Use U2x, divisor = 34
For baud=057790, nonu2x_baud = 058823, u2x_baud = 057142 Use U2x, divisor = 34
For baud=057800, nonu2x_baud = 058823, u2x_baud = 057142 Use U2x, divisor = 34
For baud=057810, nonu2x_baud = 058823, u2x_baud = 057142 Use U2x, divisor = 34
For baud=057820, nonu2x_baud = 058823, u2x_baud = 057142 Use U2x, divisor = 34
For baud=057830, nonu2x_baud = 058823, u2x_baud = 057142 normal, divisor = 16
For baud=057840, nonu2x_baud = 058823, u2x_baud = 057142 normal, divisor = 16
For baud=057850, nonu2x_baud = 058823, u2x_baud = 057142 normal, divisor = 16
For baud=057860, nonu2x_baud = 058823, u2x_baud = 057142 normal, divisor = 16
For baud=057870, nonu2x_baud = 058823, u2x_baud = 057142 normal, divisor = 16
« Last Edit: November 08, 2010, 11:57:25 pm by westfw » Logged

0
Offline Offline
God Member
*****
Karma: 24
Posts: 587
Always making something...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This same issue happened when using Teensy to communicate with '328 based Arduino clones.  I wrote about it here:

http://dorkbotpdx.org/blog/paul/teensy_as_benito_at_57600_baud

The original Arudino bootloader never tried to use the 2X mode, so it communicated at 58824 baud, not 57600.  That's +2.124% error, which is just barely within 8 bit serial tolerance.  In other words, all those Duemilanove boards were just barely working when in bootloader mode.

Dean probably tested the 8u2 firmware for Uno with a Duemilanove running the original bootloader at 58824 baud, instead of optiboot.  Just as I encountered, if you use the "best" baud rate in 2X mode, which is 57143 (-0.794% error), then you can't communicate with the original Arduino bootloader.
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 9
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What about just using this for the baud rate calculation (for Serial.begin())?
Code:
 bool use_u2x = true;

#if F_CPU == 16000000UL
  // hardcoded exception for compatibility with the bootloader shipped
  // with the Duemilanove and previous boards and the firmware on the 8U2
  // on the Uno and Mega 2560.
  if (baud == 57600) {
    use_u2x = false;
  }
#endif
Logged

0
Offline Offline
Faraday Member
**
Karma: 7
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What about just using this for the baud rate calculation (for Serial.begin())?

What does that do to communications between 56kbaud peripherals that are working correctly and the ATmega?

Breaking the core to work around a broken bootloader is a bad idea.

-j
Logged

Pages: [1] 2   Go Up
Jump to: