Autodetect baud rate?

I’m trying to set up some inter-controller communications using the UART (on an ATmega16), and want to be able to automatically detect the baud rate being used (similar to the Pololu serial motor controllers). How do the pololu controllers do this? So far, the only solution I can see is to tie the RX pin to another I/O pin, and poll the state of the I/O pin to determine the transmission speed (using a “start” byte, like the pololu protocol). Is this the correct way to do this?


Just use the RX pin as a regular input line for the first byte. Time the first byte, then set up the UART receiver.

- Jan

huh. Simple, yet brilliant. But I would still need some sort of standard “Start byte”, or “AT” command, so that I know what value to expect.

And, of course, I only go into “rate detection mode” immediately after a RESET. Which means that once the baud rate is set, if the uC on the other end of the line wants to change the baud rate, they have to reset me.

(which, come to think of it, is exactly how the pololu motor controllers work…)

The “pololu protocol”, as I like to call it is partially documented, in bits and pieces, in various product manuals (like how to configure the controller using device type 0x02, or the special 5-byte high-accuracy protocol for device type 0x03). Is there some document that gathers the entire protocol in one place? I would love reading it to get some ideas (if it’s not copyright or something)…

You could build a “change baud rate” command into your system if you don’t want to reset to change baud rate.

The serial protocol we use specifies only the 0x80 start byte followed by a 7-bit device ID and 7-bit data values. All devices look for 0x80 followed by a device ID they understand. If they understand the device ID, they look at the subsequent data; otherwise, they wait for a new 0x80. Therefore, the concept of “the entire protocol” doesn’t really apply since each device can have its own protocol beyond the first two bytes.

- Jan