datasheets.com EBN.com EDN.com EETimes.com Embedded.com PlanetAnalog.com TechOnline.com   UBM Tech
UBM Tech

# Use a PIC for automatic baud-rate detection

Ross Fosler, Microchip Technology, Chandler, AZ - August 22, 2002

Automatic baud-rate detection is desirable in many applications. Microchip's (www.microchip.com) standard USART module that the company embeds in most of its PIC microcontrollers lends itself to a simple and easily implemented automatic baud-detection scheme. The PIC18FXX2 data sheet defines the following baud rate in bits per second:

Equation 1

where X is the value for the baud-rate generator and FOSC is the input clock frequency.

Figure 1 represents a general-case signal typically seen on the RX pin of a PIC microcontroller. The time, p, is the number of instruction cycles from the end of the start bit to the beginning of the stop bit. This definition allows you to relate baud rate, B, to the total time it takes for the RX pin on the microcontroller to see eight bits of data. Eight is a convenient figure for a binary machine. Basically,

Equation 2

The term (FOSC/4) is the instruction rate of a PIC microcontroller. The term 8/B is the eight bit-times that the RX pin sees. Relating the two baud-rate equations,

Equation 3

The simplified result leads to a simple equation that you can easily implement on a PIC microcontroller. Count the total numbers of instruction cycles for eight bit-times. Divide the result by 32. (In other words, shift the count right five times.) Add a rounding bit. Then, decrement the value by one. Finally, load the SPBRG register with the result to synchronize the PIC microcontroller to the incoming baud rate. It is important to select the right control signal so that the microcontroller samples the correct number of bits. For this implementation, the signal in Figure 2 is an ideal control signal. You take measurements from the rising edge, and the pulse is symmetric. Figure 3 shows the simplified program flow for the baud-rate detection.

It is useful to know the valid frequency range for a baud rate, SPBRG value, and tolerable error. The following equation defines error as follows:

Equation 4

where B is the desired baud rate and BC is the calculated or actual baud rate. Substituting Equation 1 for BC and using algebra leads to the following result:

Equation 5

E is the error used to determine the maximum and minimum frequencies for a chosen baud rate and SPBRG value. For example, a good value for E would be 62%. Evaluating Equation 5 for the high and low limits of error E yields a valid oscillator operating range. For most SPBRG values, common baud rates, and the most common clock frequencies, operating ranges overlap each other from one SPBRG to the next. Thus, the automatic baud-detection scheme synchronizes with the source for most of the common conditions. However, some errors and clock frequencies never have a valid SPBRG (X) value.

To approach this problem, you must compare the maximum frequency for an SPBRG value with the minimum frequency of the next SPBRG. The value at which they're equal is the border between continuous and discontinuous operation for any given input frequency. The following two equations express this equality and the continuity barrier:

Equation 6

Equation 7

Thus, for any given frequency and a defined error, the automatic baud-detection scheme always generates a good SPBRG value if it is above XLOW. Of course, you must select the frequency and baud rate such that SPBRG is less than or equal to 255, the largest value that SPBRG supports. For example, for a 2% error, the lowest SPBRG value before certain clock frequencies become a problem is 25. A bootloader is an excellent example of an application for this baud-rate-detection scheme. The simple implementation uses minimal resources. It synchronizes to a baud rate within one transmitted byte, and you can most likely successfully synchronize it to any standard baud rate, especially 9600 bps.

Is this the best Design Idea in this issue? Select at www.edn.com.