57600 baud rate inaccuracy in arduino uno

Hi all,

I need to establish connection between a PC and arduino uno, using RS232. I have a shield for the RS232 but the problem is that the PC's baud rate is 57600 (I can't change that) and when I use the line "serial.begin(57600)" the actual baud rate (after sniffing with logic analyzer) is NOT 57600, there's an inaccuracy of 5% or even more.
Now, according to the datasheet (of atmega328p) page 241, is f_osc will be at 1.8432MHz, the accuracy
at that baud rate should be much better. I believe I can achieve this number with the correct code but I don't know that code...

Any suggestions?

(deleted)

The normal Arduino oscillator frequency is 16Mhz and the data sheet suggests an error of either 2.1% or -0.8% at 57600 baud.

Are you running an Atmega 328 at 1.84.... MHz? Does the Arduino IDE know that that is what you are doing?

I have never had any problem communicating with my laptop at different baud rates using 16MHz and 8MHz Atmega 328s.

...R

spycatcher2k:
Try it as it is, I've interfaced with a lot of equipment at 57600 BAUD with no issues.

I can't try it as is because it's not good enough for my needs.
Have you ever sniffed your sent data?
As I said, if I set it as 57600, I get about 59000 which is not good, the PC can't "see" the arduino.

Robin2:
The normal Arduino oscillator frequency is 16Mhz and the data sheet suggests an error of either 2.1% or -0.8% at 57600 baud.

Are you running an Atmega 328 at 1.84.... MHz? Does the Arduino IDE know that that is what you are doing?

I have never had any problem communicating with my laptop at different baud rates using 16MHz and 8MHz Atmega 328s.

...R

I know about normal arduino and no, of course I'm not using 1.84MHz, this is a frequency from the datasheet.
2.1% is a lot.

yehudabch:
I know about normal arduino and no, of course I'm not using 1.84MHz, this is a frequency from the datasheet.
2.1% is a lot.

Depends, this is asyncronous communications after all, so what is the accepatable error ?

The receiver, based on the start bit transition, starts sampling in the middle of the bit, so assuming a further 10 bits to be sampled, what is the allowable frequency differance before the sampling gets to one of the bit edges ?

yehudabch:
I know about normal arduino and no, of course I'm not using 1.84MHz,

So why did you mention it?

What frequency is your Arduino working at?

...R

(deleted)

yehudabch:
Hi all,

I need to establish connection between a PC and arduino uno, using RS232. I have a shield for the RS232 but the problem is that the PC's baud rate is 57600 (I can't change that) and when I use the line "serial.begin(57600)" the actual baud rate (after sniffing with logic analyzer) is NOT 57600, there's an inaccuracy of 5% or even more.
Now, according to the datasheet (of atmega328p) page 241, is f_osc will be at 1.8432MHz, the accuracy
at that baud rate should be much better. I believe I can achieve this number with the correct code but I don't know that code...

Any suggestions?

Do you have F_CPU set correctly in your "boards.txt" file?

I just ran my little AVR baud rate calculator utility and got this:

[b]Arduino Crystal and Baud Rate Calculator
----------------------------------------
Enter crystal frequency in Hz. or MHz.
(i.e. 16 or 16.0 or 16000000 are all OK)

Crystal frequency: 1.8432
Desired baud rate: 57600

Effective F_CPU:       1.843200 MHz.
UBRR  register :      3 (0x0003)
UBRRH register :      0   (0x00)
UBRRL register :      3   (0x03)
UCSRA U2X bit  :    set      (1)
Desired baud   :  57600
Actual baud    :  57600.000
Percent error  : 0.000%[/b]

As you can see, your crystal is a perfect integer divider for standard baud rates (the percent error is zero).

(edit to add): I was under the impression you were using a 1.84 MHz crystal!!!

For a 16 MHz crystal, this is what you get for an Arduino:

[b]Arduino Crystal and Baud Rate Calculator
----------------------------------------
Enter crystal frequency in Hz. or MHz.
(i.e. 16 or 16.0 or 16000000 are all OK)

Crystal frequency: 16
Desired baud rate: 57600

Effective F_CPU:     16.000000 MHz.
UBRR  register :     34 (0x0022)
UBRRH register :      0   (0x00)
UBRRL register :     34   (0x22)
UCSRA U2X bit  :    set      (1)
Desired baud   :  57600
Actual baud    :  57142.857
Percent error  : -0.794%


[/b]

f_osc will be at 1.8432MHz, the accuracy at that baud rate should be much better. I believe I can achieve this number with the correct code but I don't know that code...

You can't change Fosc just by changing code; you have to change the physical crystal/resonator attached to the chip.
18.432MHz would probably be a better choice, but you'd probably still need to change a lot of other Arduino code.

With the usual 16MHz resonator, 57.6kbps should be THAT far off (about 2.1%, whereas async communications will normally allow a combined error of up to 5%.) WormFood AVR BRG calculator

Now, it turns out that the Ardunino code contains a hack that causes 57600bps to be LESS accurate than it could be, in the name of backward compatibility. If the normal bitrate isn't close enough, try using:

   Serial.begin(57601);

Since we are talking about 10 bit asynchronous communication, cannot image why you needing super tight timing.

.

westfw:

   Serial.begin(57601);

That's interesting. Never occurred to me that one could use any value, but I see the manpage says this:

For communicating with the computer, use one of these rates: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. You can, however, specify other rates - for example, to communicate over pins 0 and 1 with a component that requires a particular baud rate.

Never occurred to me that one could use any value

The actual rates get very "quantized" at higher speeds. The actual bit rates near 57.6kbps are 60606, 58824, 57143, and 55556. It's only the "hack" that causes "57600" to select 58824, while "57601" will select 57134.

krupski:
Do you have F_CPU set correctly in your "boards.txt" file?

I just ran my little AVR baud rate calculator utility and got this:

In Reply #4 the OP told us he is NOT using 1.84xxx MHz.

However he has not told us what oscillator frequency he is using.

...R

westfw:
You can't change Fosc just by changing code; you have to change the physical crystal/resonator attached to the chip.
18.432MHz would probably be a better choice, but you'd probably still need to change a lot of other Arduino code.

With the usual 16MHz resonator, 57.6kbps should be THAT far off (about 2.1%, whereas async communications will normally allow a combined error of up to 5%.) WormFood AVR BRG calculator

Now, it turns out that the Ardunino code contains a hack that causes 57600bps to be LESS accurate than it could be, in the name of backward compatibility. If the normal bitrate isn't close enough, try using:

   Serial.begin(57601);

57600 baud with a 16.0 crystal comes out very close:

[b]Enter crystal frequency: 16
Crystal frequency is     16.000000
Enter serial baud rate : 57600

F_CPU MHz :     16.000000
UBRR  reg :     34 (0x0022)
UBRRH reg :      0   (0x00)
UBRRL reg :     34   (0x22)
U2X bit   :    SET
Baud      :  57600
Actual    :  57142.857
Pct error :     -0.794%
[/b]