Scheme autodetects baud rate
Alexander Eisen, State University of New York, Buffalo, New York -- EDN, 7/20/2000
The popularity and easy access of RS-232 ports lend them to many communication projects. You can use a port "as is" or as a tiny parallel port when the exchange uses only control lines. Before the asynchronous serial-data transfer between two devices can take place, you must ensure that both devices are configured to the same data format and transmission rate. Usually, the OS utility or application program on the host computer performs this task, which the OS or a combination of switches selects in the peripheral.The design in Figure 1 and the accompanying software realize the requirement of automatically equalizing the speed of the parties by adapting the bit rate of a µC's serial port to that of a host computer. The host sends a known ASCII code to a µC that estimates the bit rate of the transmission. The µC samples the receiving end of a transmission line. As soon as the line goes low, indicating a start bit, the µC clears the timer. After the line again goes high, the µC reads the timer's value and uses this value to calculate the bit rate of a transmission. The µC uses the calculated bit rate in a timing-delay loop for clocking out the data from the µC to the host computer during transmission and for sampling the incoming data during receiving mode. The time between those falling and rising edges on a transmission line depends, of course, on the transmitted ASCII symbol. Its good practice to use a symbol with 2k –1 zeros on the low side. Then, when you include the start bit, there are 2k trailing zeroes. One timing unit is easier to derive by substituting a division with k right shifts.
The application in Figure 1 uses an 8-bit RISC µP and an ASCII code of 8 (38 hex) to establish a serial communication. Any symbol that ends with 8H also works. The timer increments its value every 4P/FOSC seconds, where P is a prescaler factor and FOSC is the µP's clock frequency. Thus, the timer's value after 4 bits have transmitted during the time between the falling and rising edge, is T=4/BR, is N=FOSC/(BR×P), where BR is the bit rate.
Now you can derive the 1-bit transmission time, which is a reciprocal of a transmission speed, as (N/4)(4P)/FOSC=N×P/FOSC.
Because the µP's timer is only 8 bits long, it is important to pick up the right prescaler factor. N must not exceed the timer's capacity of 255 but should be large enough to cover a certain range with good resolution. For the worst case, which is when the bit rate is at its minimum, P should beP>FOSC/(NMAX×BRMIN).
After you determine all of these timing relationships, you need to design a software loop for a 1-bit delay. One machine cycle for the µP in Figure 1 is 4/FOSC, so the total cycle count for the execution time of a loop should be NP/4. The loop for a bit_clk is 4(k+2) cycles long (Listing 1). Therefore, k=(NP/16)–2. If you choose P=16, k then conveniently becomes N–2. At FOSC=8 MHz, these parameters reliably cover speeds of 2400 to 38,400 bps, or baud rate, which in the case of the serial port is the same thing. (DI #2546)
















