Serial communication is the main communication method between the terminal and the host. The communication baud rate is generally 1800, 4800, 9600 and 19200. There are many types of terminals, and there are many choices for their communication rates. How does the host determine the communication rate of the terminal? This article gives a simple and easy method: set the host's receiving baud rate (take 9600 baud as an example), the terminal sends a specific character (take carriage return as an example), and the host can determine the terminal's communication baud rate based on the received character information. This article describes this method in detail.
1 Basic method
The ASCII value of the carriage return character is 0x0D. During serial communication, a start bit and a stop bit are added, and the bit transmission order is generally to transmit the low bit first and then the high bit. At this time, the binary representation of the carriage return character is:
Figure 1 The bit sequence of the carriage return character
The transmission time of a binary bit in serial communication (denoted as T) depends on the baud rate of the communication. The transmission time of a binary bit at 9600 baud is twice that of a binary bit at 19200 baud, that is: 2*T
19200
=T
9600.
Therefore, the transmission time of one bit at 9600 baud can transmit two bits at 19200 baud. Similarly, the time of transmitting two bits at 9600 baud can only transmit one bit at 4800 baud. The host sets the receiving baud rate to 9600. The host can only correctly receive the characters sent by the terminal at 9600 baud. Sending baud rates higher or lower than 9600 will cause errors in the characters received by the host. When the receiving baud rate is 9600 and the terminal sends carriage returns at different baud rates, the binary sequence received by the host is shown in Table 1.
It can be seen from Table 1 that except for the two special cases at 19200 and 1800 baud, the binary sequences in other cases are all transformations of the binary sequences at 9600 baud. Take the first ten bits and match them to the bits at 9600 baud. Ignore the data frame error caused by the lack of the stop bit '1' and represent the received characters in byte form (as shown in the rightmost column of Table 1). For example: when the transmission rate is 1200 baud and the receiving rate is 9600 baud, the host receives the byte 0x80 instead of the correct carriage return character 0x0D. Because the bytes received at different transmission rates (9600, 4800, 2400, 1200) are different, the transmission baud rate can be determined by judging the received characters.
When the transmission baud rate is 19200, its transmission speed is exactly twice the receiving speed (9600 baud), so the two bits at the transmitter will be regarded as one by the receiver. Depending on the different serial interface hardware, the two binary bit combinations of '01' and '10' may be regarded as '1' or '0'. Fortunately, only bits 0 to 4 have such ambiguity. The following bits are all '1' because they are all stop bits. Therefore, the upper half byte of the character received at a transmission rate of 19200 baud is 0xF. The lower half byte may be one of several values, but it will not be 0x0 because there are two adjacent '1's in 0x0D, which will produce at least one '1' in the lower half byte. Therefore, the entire byte is of the form 0xF?, and the lower half byte is not 0.
Table 1 Binary sequences at different baud rates
Baud rate |
The received binary bit sequence |
Byte representation |
19200 |
0 1 0 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1
0xF?
9600
0 1 0 1 1 0 0 0 0 1
0x0D
4800
0 0 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1
0xE6
2400
0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1
0x78
1800
0 0 0 0 0 x 1 1 1 1 x 0 0 0 0 0 1 1 1 1
0xE0
1800
0 0 0 0 0 x 1 1 1 1 x 0 0 0 0 0 1 1 1 1
0xF0
1200
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0
0x80
600
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
0x00
300
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x00
150
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x00
110
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x00
When the transmission rate is 1800 baud, because
T
1800
= T
9600
* 16/3,
and 16/3 is not an integer, the state transition time of the binary bit at the receiving end does not correspond to 9600 baud, causing the state to change during a bit reception cycle at the receiving end. This is the case with the sixth bit (represented as x) given in Table 1. Because x may be seen as '1' or '0', the byte received when the transmission rate is 1800 baud may be 0xE0 or 0xF0. The same problem also exists when the baud rate is 3600 and 7200, and the same method can be used, but the number of uncertain bits will increase, and more types of bytes need to be detected. The transmission rates of 3600 baud and 7200 baud are almost never used, so this problem is not serious. As long as the transmission baud rate is between 1200 and 19200, we can uniquely determine the baud rate by receiving a character. [page]
2 Low baud rate detection
When the transmission rate is lower than 1200 baud, the bytes received by the receiver are all 0x00, so it can only be determined that the rate is lower than 1200 baud, and it is impossible to get more information. To solve this problem, the next byte of information can be received at a rate of 9600 baud. When the transmission rate is 600 baud or lower, the transmission time of one bit is longer than the reception time of the entire byte at 9600 baud. Therefore, every jump from '1' (stop bit) to '0' (start bit) at the transmitter will make the receiver think that a new byte has started. Table 2 shows the binary sequence of the carriage return character at the receiver at a transmission rate of 600 baud or lower (only the first few bits are given).
Table 2: How to receive carriage return characters at low baud rates
Baud rate |
9600 baud binary serial |
Time difference
|
Time difference
|
600 |
16 0\'s 16 1\'s 16 0\'s
32
3.33ms
300
32 0\'s 32 1\'s 32 0\'s
64
6.66ms
150
64 0\'s 64 1\'s 64 0\'s
128
13.33ms
110
87 0\'s 87 1\'s 87 0\'s
174
18.13ms
75
128 0\'s 128 1\'s 128 0\'s
256
26.66ms
50
192 0\'s 192 1\'s 192 0\'s
384
4 0.00ms
At 600 baud, the first transition from '1' to '0' occurs immediately after initialization. This transition causes the receiver to receive the byte 0x00. The second transition occurs (16+16)*T 9600 seconds after initialization, which causes the receiver to think that another byte has been received. The reception time of a binary bit is T 9600 , so the serial interface circuit will indicate that the first byte has been received 10* T 9600 seconds after the first transition , and the second byte has been received (16+16+10)* T 96 00 seconds after. Therefore, at 600 baud, the time difference between the first byte and the second byte is (16+16+10-10)* T 9600 = 32* T 9600 seconds. The third column of Table 2 shows this time difference expressed as the number of T 9600s . Because T 9600 = 1/9600 seconds = 104.16 milliseconds, the real time difference between the two bytes received can be obtained by multiplication. The time difference for different sending baud rates is shown in the last column of Table 2. With this time difference information, the baud rate at a low transmission rate can be determined: measure the receiving time difference between the first and second bytes, and then find out which baud rate has the closest time difference in the time difference constant table (Table 2), which corresponds to the terminal sending baud rate. Even if the measured time difference has some errors, the baud rate can generally be determined correctly.
3 Implementation method
Through the above analysis, various baud rates can be determined by sending and receiving information of carriage return characters. The pseudo code of the algorithm implementation is given at the end of this article. Application practice proves the effectiveness of this method.
; Pseudo code to determine what baud rate a transmitter is at,
on the b asis of a single
; RETURN (0x0D) character received from it.
Initialise receive baud rate to 9600
Wait for Byte to be received
IF Byte = 0x00 THEN
Start Timer
REPEAT
UNTIL (Timer > 50 ms OR New Byte Received)
CASE Timer IN
1 ms
-
4 ms: 600 Baud
5 ms-10 ms: 300 Baud
11 ms-15 ms: 150 Baud
16 ms-22 ms: 110 Baud
23 ms-32 ms: 75 Baud
33 ms-49 ms: 50 Baud
ELSE: Timed out; reset
END CASE;
ELSIF Byte >= 0xF1 THEN
19200 Baud
ELSE
CASE Byte IN
0x0D: 9600 Baud
0xE6: 4800 Baud
0x78: 2400 Baud
0xE0,0xF0: 1800 Baud
0x80: 1200 Baud
ELSE: Line noise; reset
END CASE
END IF■
References:
[1]Zhao Yijun et al. Single-chip microcomputer interface technology[M]. Beijing: People's Posts and Telecommunications Press, 1989.
[2]Liu Li. Software and hardware technology reference[M]. Beijing: Xueyuan Press, 1993.
[3]Zhang Shiyi. Digital signal processing[M]. Beijing: Beijing Institute of Technology Press, 1987
Previous article:Serial port expansion circuit from RS232 to RS422 based on programmable logic array
Next article:Mixed Logic Level Interface Technology
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- EEWORLD University ---- ARM (IMX6U) bare metal video tutorial (punctual atom)
- EEWORLD University Hall----Texas Instruments field transmitter output interface/fieldbus solution
- Design of Portable Weather Instrument Based on MSP430 Microcontroller
- EEWORLD University ---- Top 5 Simple Electronic projects
- Thank you for being here, thank you EEWORLD Admin-okhxyyo and Orange Kai
- Recommended solutions to reduce Wi-Fi interference
- The MCU cannot run in DC-DC mode of BLUENRG-1
- Update the image
- Vote: Which of these TI star products do you like?
- Temperature detection [What is the device sensor you are most comfortable using]?