Explain the baud rate setting problem of serial communication

51 single-chip microcomputer has serial communication capability. Its serial interface (UART) is a full-duplex communication interface that enables easy serial or multi-machine serial communication with other microcontrollers. With the popularization of IBM-PC and the rapid development of single-chip applications, the combination of these two kinds of microcomputers is becoming more and more widely, which makes it have both strong real-time control functions and strong data processing capabilities. Played the strengths of different models. In this application, both parties use the RS-232C standard serial communication interface to achieve communication between two or more machines.

In the microcomputer communication of different models, a well-known problem is that both parties communicating should adopt the same transmission rate, that is, both parties should use the same transmission baud rate, which is one of the important conditions for ensuring communication success. In the following, only some discussion on the setting of the baud rate in communication is made for your reference.

In the IBM - PC / XT system, equipped with an asynchronous communication adapter, the board has an INS8250 asynchronous communication interface. The baud rate setting on the PC is achieved by initializing the 8250. In the 8250 port register, 3FSH and 3F9H are set to the lower 8 bits and the upper 8 bits of the baud rate factor, respectively. The factor ranges from 1 to 65 535 and divides the input clock (1. 843 2 MHz) to produce a baud rate generator clock (BAUDOUT) of 16 times the baud rate.

After the 8250 is initialized with the baud rate factor preset, the baud rate generator can generate the specified baud rate.

The baud rate of 51 MCU serial communication varies with the choice of serial port operation mode. It is related to the oscillation frequency of the system, the SMOD bit of the power control register PCON, and the setting of the timer T1.

When the serial port operates in mode O, the baud rate is fixed and its size is foSC/12. In serial port mode 2, the baud rate is only fixed to two types, which is related to the value of the SMOD bit: when SMOD-O, the baud rate is foSC/64; when SMOD=1, the baud rate is foSC /32. The baud rate of serial port modes 1, 3 is variable, and it depends mainly on the overflow rate of timer 1 except for the value of SMOD bit. When Timer 1 is used as the baud rate generator for serial communication, the baud rate can be determined by:

Baud rate = T1 overflow rate / n

Where: n=32 or 16, corresponding to SMOD=O or 1 in PCON. The TI overflow rate depends on the count rate and the timing time constant, ie

T1 overflow rate = count rate / 256-x

At this time, T1 works in mode 2, that is, 8-bit automatic loading mode. This way you can avoid resetting the initial value of the timing. Where x is the initial count value loaded in TH1 and TL1. The count rate of Timer 1 is related to the selection of the timer mode of operation. When T1 is selected as the timing mode, its counting input pulse is the internal clock signal, that is, the register value is incremented by 1 every machine cycle, and each machine cycle is 12 oscillation cycles, so the counting rate is fosc/12. If the system fosc=12 MHz, the T1 count rate is fosc/12=1 MHz. From this

For example, assume that the baud rate requirement for both serial communications is 9 600 b/s. The system oscillation frequency fosc=11. 59 MHz. When SMOD is selected as 1, the initial values ​​of TH1 and TL1 are calculated as follows:

When this value is set to TH, TL1, the actual transmission rate generated by the baud rate generator is

In this case, the PC and the MCU can perform normal reception and transmission.

According to the above conditions, if the system uses a 12 MHz system clock, according to the above formula, z≈250 or 249 (FAH or F9H) can be obtained. At this time, when FAH or F9H is placed in THi and TL1, the actual transmission rate is 10 416.67 or 8 928.57 b/s, and the error is -8.5% or +7%, respectively.

Practice has shown that PCs and microcontrollers cannot achieve normal reception and transmission under such conditions.

The above calculations show that in serial communication, when the transmission rate is specified, the setting of the baud rate plays an important role in the oscillation frequency used by the system, which can directly affect the success and failure of communication. In the above example, when the dual-machine agreed to a transmission rate of 9600 b/s, if the system oscillation frequency is 12 MHz or 6 MHz, the baud rate of the PC is 9 600 b/s, and the actual baud rate of the microcontroller is greater than (or less than) 9 600 b/s. Then the baud rate error is 8.5%. That is, if the PC transmits one bit of data in 101.17 μs, the microcontroller receives one bit of data in 96 μs. In the process of receiving one frame of data, a wrong code is generated due to the accumulation of errors.

We know that in the serial receiving mode (1, 2, 3) of the microcontroller, the CPU continuously samples the received data (RxD) at a sampling rate of 16 times the baud rate. Once a negative transition from 1 to 0 is detected, The divide-by-16 counter is immediately reset so that the full-scale flip is exactly aligned with the edge of the input bit. The divide-by-16 counter divides the time of each received bit into 16 parts. In the middle three positions, ie, the state of 7, 8, and 9, the bit detector samples the value of the RxD end, and determines the received by the voting mode of 3 and 2 The data bits. These three states theoretically correspond to the middle segment of each bit. If the transmission rates of the transmitting end and the receiving end are inconsistent, the sampling offset will occur. The error of this transmission rate does not cause misalignment or missing code within the allowable range; however, when the error exceeds the allowable range, misalignment occurs, and the received data is repeatedly received, thereby generating a received data error.

Lana Series

Nanning Ousibang Information Technology Co., Ltd. , https://www.ousibangvape.com