µC detects transmission rate of RS-232 interface

-September 30, 1999

RS-232 is the most common serial interface in the PC world. Most RS-232 interfaces communicate with the receiver at a fixed transmission rate, such as 9600 baud. But what happens if the transmitter operates with different transmission rates? Different transmission rates require the receiver to detect the rate and adjust the software to the new communication speed. The following description of how a receiver detects the transmission rate of an RS-232 interface does not describe the implementation of a receive-and-transmit routine. Instead, it describes a system consisting of a transmitter and a receiver. The transmitter (for example, a PC) transmits a character to the receiver. The receiver, a low-cost µC, detects the transmission rate and adjusts its software according to the new rate. The theory of implementation is simple.

The transmitter sends a calibration value to the receiver. The receiver measures the time to receive the bits of the calibration value. Based on this measurement, the receiver calculates the transmission time of 1 bit. The method uses this time for the baud-rate generator. The trick is to measure the time of the incoming bit stream and calculate the average time to receive 1 bit. This implementation of an autobaud routine assumes that the receiver knows the bit sequence of the calibration value and that the receiver knows when to calibrate. The technique uses a PIC16C54B µC. The µC connects to a PC via a MAX232 chip. The PC sends the calibration character to the µC. We chose the ASCII value of "?" because of the bit sequence (00111111). The autobaud routine measures the time to receive the ones in the bit stream and then divides the time by six. The result is the time the routine takes to receive or transmit 1 bit.

Because the PIC16C54B has no hardware USART, a software routine measures the timing of the bit sequence. Listing 1 gives the source code of the autobaud routine. The calibration character contains one start bit, one stop bit, and no parity bit. For time measurement, the technique uses a 16-bit counter, which provides a range of transmission speeds. In the first part of the routine, the software initializes the counter and an autobaud-status register, AUTOB_STATUS. The register stores information about whether the incoming signal is too slow or too fast for the autobaud routine. You can use this information to check whether the calibration process is successful. After the initialization, the autobaud routine looks for the start bit, which is a logic-one-to-zero transition. After detecting the start bit, the autobaud routine looks for the reverse transition. Following detection of this transition, the routine starts measuring the time, using the 16-bit software counter. The software increments the low byte of the 16-bit counter until the counter overflows.

When an overflow occurs, the high byte of the 16-bit counter increments by one. This process continues until either a change from logic one to zero occurs or the high byte of the counter overflows. In either of these cases, the routine sets a flag in AUTOB_STATUS to indicate that the incoming signal is fast or slow. Otherwise, the software calculates the transmission time of 1 bit. This time generates the baud rate for the transmit or receive routine. These routines need the transmission time of 1 bit either for generating a delay for bit sampling or for bit transmission. The software calculates the transmission time for 1 bit by dividing the measured time by the number of transmitted ones in the calibration value. In the case of the calibration value "?," it's necessary to divide the measured time by six. Dividing by six entails shifting the 16-bit counter/register three times to the right while drawing zeros from the left. After the division, the routine divides the bit time by two, to calculate the transmission of half a bit. This time figure serves in the receive routine to place the bit sampling in the middle of a bit. The division by two entails a simple shift of the 16-bit counter by one position to the left. The program stores the results of this operation in two registers: AUTOHALF_LOW and AUTOHALF_HIGH.

Once the program completes this calculation, it's necessary to adjust the transmission time of 1.5 bits to the software overhead. This adjustment involves subtracting the number of instruction cycles it takes to execute either the transmit or the receive routine. After the subtraction, the software verifies whether the result is smaller than zero. If so, the incoming signal is too fast, and the routine sets an error flag in the AUTOB_STATUS register. After the adjustment, the software verifies whether the incoming signal is too fast by verifying that the value of the 16-bit counter is zero. If the incoming signal is not too fast, the autobaud routine returns to the operating system. (DI #2418).

Loading comments...

Write a Comment

To comment please Log In