3 Gordon Drive, P.O.Box 1347 Rockland, Maine 04841 U.S.A.
Find Tools for Your Chip


 

© 2004 Avocet Systems, Inc.
Call Us Today at 207-596-7766 ("Picton Press")
Avocet Systems, Inc. : The Complete Solution for Embedded Systems Development Tools
Embedded Update
Autobauding

One of our friends recently asked about automatically determining the baud rate of an incoming serial stream (thanks, Wally!).

First, let's paint a picture of the problem: Your product connects to a terminal or other computer via an RS-232 link. Marketing wants to support all sorts of baud rates. It seems silly to make the user set a DIP switch or otherwise configure the system to the rate - isn't there a way for your embedded code to figure out the exact rate of the incoming serial stream?

If you can make one rule, that the user must type a character after powering things up, then it's easy to build autobauding software. If the remote device is a computer instead of a terminal, and that computer is running your proprietary software, life is even easier as you can simply makes sure the first thing the software does is to send a character.

A single character is all you need to determine the baud rate of the serial data. The procedure is as follows:

Program the UART's baud rate generator to the highest rate you handle (say, 38,400 baud). Watch the UART's Data Ready bit. When asserted, the character has arrived. Now start a loop that counts how many characters arrive in the next quarter second or so. That's right... even though only one character gets transmitted, your UART will think more than one is received (for the case where the transmission speed is not matched to the receive speed).

Think about it: If a space (which is mostly zeroes) is the initial character, and if the sender operates at, say, 9600 baud, while the receiver runs at 38400, the UART will get all of the bits needed to assemble a character when the space is only about 1/4 transmitted. It will immediately trip into operation again... and again... and again... until the character is completely transmitted.

The UART's response is only a function of time. When it gets a start bit, it fully expects to be done with the character when the time for 8 data bits and one stop elapses. At 38,400 baud this is around 10*(1/38400) sec, or 260 microseconds (depending on your selections for parity and all). But, the 9600 baud space will transmit for about 10*(1/9600), or better than a millisecond.

That single space character looks like about 4 (scrambled) characters to the UART. At other baud rates, the number of characters counted will differ.

In our (extensive) experiments we've found that the number detected is very stable.

To complete the algorithm, empirically determine the counts expected for a space at each baud rate, and write code that compares the count received to an anticipated value for each baud rate.

It's a good idea to compare to a range of values to add margin to your design. If the UART sees 4 characters at 9600 and 8 at 4800, use the in-between value -- 6 -- as the selection criterion.