Diecimila can't handle 115200 baud input?

Hi,
With even the most basic serial read-then-print program, I get occasional corrupt data if I try to send more than a few characters at 115200 baud in the Arduino 0011 app. Switch the program and the serial console down to any lower baud rate, and it works fine.

void setup(){
  Serial.begin(115200);
}

void loop(){
  int avail;
  while (1) {
    for (avail = Serial.available() ; avail > 0 ; avail--) {
      char current = Serial.read();
      Serial.print(current);
    }
  }
}

Type a bunch of "a"s, here's what gets printed:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaXX???aaXXXX?aXX??aaXX?aaaaaa

I've tried multiple computers, similar effect. Do others have this problem?

Hello.

I've never tried running an Arduino at 16 MHz, but I think I can shed some theoretical light on your problems. If you look at page 199 of the mega168 datasheet you can see that you have a -3.5% error when trying to communicate at 115.2k baud using a 16 MHz clock. What this means is that you are communicating with a true baud rate of 111.11 kbps while your target is communicating with a 115.2k baud (or maybe your target has a baud rate error in the opposite direction, making the real error even worse). If you work out the math you can figure out how long it will take you to get out of sync with your target. You might be able to avoid this loss of synchronization by keeping your communication packets smaller than this maximum packet size and then implementing a short delay to allow your target to resynchronize with you.

The maximum recommended receiver error for the mega168 is +/-2.0% for 8-bit character communication, so you're technically exceeding the mega168 spec by going with a baud that yields a 3.5% error.

  • Ben

Hi Ben,
Thanks for the reply. I did see that page in the datasheet and even tried setting baud rates +/- 3.5% of 115.2K. I guess I am missing some basic theory to understand why that shouldn't compensate for the problem.

I wish there were clear directions for the changes that would need to be made if I wanted to run a different speed crystal after programming on the arduino board...

There's some information here: http://www.arduino.cc/en/Hacking/OtherHardware. Running at arbitrary clock speeds probably requires changes to the core. This isn't a huge priority since most people are using the 16 MHz clock, but patches are welcome. If you try it out and have more details to add to that page, that would also be great.

Hi Ben,
Thanks for the reply. I did see that page in the datasheet and even tried setting baud rates +/- 3.5% of 115.2K. I guess I am missing some basic theory to understand why that shouldn't compensate for the problem.

The reason why you can't compensate for the problem on the Arduino is that the baud rate error of -3.5% is the best you can do. The closest you can come to 115.2k while using a 16 MHz clock is 111.1k, so there isn't any way to compensate. The only compensation you might be able to make is on the non-Arduino side, if that side has more flexibility when it comes to setting the baud rate.

Internally, the Arduino baud is determined by the 11-bit UBRR register. The formulas relating UBRR to baud are:

baud = Fosc / (16 * (UBRR + 1)) = 16 MHz / (UBRR + 1) = 1,000,000 / (UBRR + 1)
UBRR = (Fosc / (16 * baud)) - 1

So UBRR = 7 gives a baud rate of 125k, UBRR = 8 gives a baud rate of 111.1k, and UBRR = 9 gives a baud rate of 100k.

  • Ben