
【Communication Interface Background Knowledge】
Methods of Communication Between Devices
Generally speaking, the methods of communication between devices can be divided into parallel communication and serial communication. The differences between parallel and serial communication are shown in the table below.
Classification of Serial Communication
1. According to the data transmission direction, it can be divided into:
-
Simplex: Data transmission only supports data transmission in one direction;
-
Half-duplex: Allows data to be transmitted in both directions. However, at any given time, data is allowed to be transmitted in only one direction, which is actually a switching direction simplex communication; it does not require independent receiving and sending ends, both can be combined to use one port.
-
Full-duplex: Allows data to be transmitted simultaneously in both directions. Therefore, full-duplex communication is a combination of two simplex communication methods, requiring independent receiving and sending ends.
2. According to the communication method, it can be divided into:
-
Synchronous communication: Transmits with clock synchronization signals. For example: SPI, IIC communication interfaces.
-
Asynchronous communication: Does not carry clock synchronization signals. For example: UART (Universal Asynchronous Receiver Transmitter), single bus.
In synchronous communication, a signal line is used to transmit signals above the sending and receiving devices, and both parties coordinate and synchronize data under the clock signal. For example, it is usually agreed that both parties will sample the data line on the rising or falling edge of the clock signal.
In asynchronous communication, clock signals are not used for data synchronization; instead, some synchronization signal bits are interspersed directly in the data signal, or the subject data is packaged and transmitted in the format of data frames. Both parties also need to agree on the data transmission rate (i.e., baud rate), etc., for better synchronization. Common baud rates include 4800bps, 9600bps, 115200bps, etc.
In synchronous communication, the content transmitted by the data signal is mostly valid data, while in asynchronous communication, various identifiers of the data frame will be included, so synchronous communication is more efficient, but the clocks of both parties in synchronous communication allow for very little error; a slight clock error can lead to data corruption, while asynchronous communication allows for larger clock errors.
Common Serial Communication Interfaces
【Basics of STM32 Serial Communication】
STM32 has two types of serial communication interfaces: UART (Universal Asynchronous Receiver Transmitter) and USART (Universal Synchronous and Asynchronous Receiver Transmitter). For the large-capacity STM32F10x series chips, there are 3 USART and 2 UART.
UART Pin Connection Method
-
RXD: Data input pin, data reception;
-
TXD: Data output pin, data transmission.
For the connection between two chips, the two chips share a GND, and TXD and RXD are cross-connected. Here, the cross-connection means that chip 1’s RxD connects to chip 2’s TXD, and chip 2’s RXD connects to chip 1’s TXD. In this way, the two chips can communicate at TTL level.
If the chip is connected to a PC (or host), in addition to sharing ground, it cannot be directly cross-connected like this. Although both the PC and the chip have TXD and RXD pins, the PC (or host) usually uses RS232 interfaces (usually DB9 packaging), so they cannot be directly cross-connected. The RS232 interface has 9 pins (or pins), which are usually obtained by level conversion from TxD and RxD. Therefore, to enable direct communication between the chip and the PC’s RS232 interface, the chip’s input and output ports must also be level converted to RS232 type, and then cross-connected.
After level conversion, the voltage standards of the chip’s serial port and RS232 are different:
-
The microcontroller’s voltage standard (TTL level): +5V represents 1, 0V represents 0;
-
The RS232 voltage standard: +15/+13 V represents 0, -15/-13 represents 1.
The communication structure diagram between devices in the RS-232 communication protocol standard serial port is as follows:
Therefore, the communication between the microcontroller’s serial port and the PC’s serial port should follow the connection method below: Between the microcontroller’s serial port and the RS232 port provided by the host, a level conversion circuit (such as the Max232 chip in the diagram below) is used to achieve conversion between TTL level and RS232 level.
Introduction to RS232 Serial Port
The 9-pin interface at the back of a desktop computer is the COM port (serial port), widely used in industrial control and data acquisition. In the above image, the rightmost is the serial port interface collectively referred to as the RS232 interface, which is common DB9 packaging.
Only two pins participate in the communication process.
-
Pin 2: Computer’s input RXD
-
Pin 3: Computer’s output TXD. Through pins 2 and 3, full-duplex (simultaneous send and receive) serial asynchronous communication can be achieved.
-
The microcontroller’s P3 port has two multiplexed interfaces RXD and TXD. This is the transceiver port for serial communication of the microcontroller, and it should be connected correctly to the computer’s TDX RDX. Note: The voltage standards of the microcontroller and RS232 are different.
The microcontroller’s voltage standard TTL level: +5V represents 1, 0V represents 0.
The RS232 voltage standard: +15/+13 V represents 1, -15/-13 represents 0.
Thus, the communication between the microcontroller and the PC serial port should follow the connection method below:
Between the microcontroller and the RS232 port provided by the host, a level conversion circuit (the Max232 chip in the diagram above) is used to achieve conversion between TTL level and RS232 level. The connection method between the PC serial port and the microcontroller serial port is shown in the diagram:

Note these two DB9: DB91 is on the computer, DB92 is soldered onto the microcontroller experiment board.
The meaning of the cross-connection here is that DB91’s RXD connects to DB92’s TXD.
DB92’s RXD connects to DB91’s TXD, thus cross-connected. If the computer has no RS232 port but only a USB port, a serial port adapter can be used to convert to a serial port.
At this time, it is necessary to install the serial port driver program on the computer’s host.
Note that this driver program drives the PL2303 chip (inside the large head in the image above) to convert RS232 information into USB information.
The diagram below shows the internal structure of the above image:
Using serial communication is simpler than USB because serial communication has no protocol, making it convenient and simple.
Characteristics of STM32’s UART
-
Full-duplex asynchronous communication;
-
Fractional baud rate generator system, providing accurate baud rate. Sending and receiving share programmable baud rate, up to 4.5Mbits/s;
-
Programmable data word length (8 bits or 9 bits);
-
Configurable stop bits (supports 1 or 2 stop bits);
-
Configurable DMA multi-buffer communication;
-
Separate transmitter and receiver enable bits;
-
③ Transmission complete flag;
-
Multiple interrupt sources with flags, triggering interrupts;
-
Others: Parity control, four error detection flags.
Serial Communication Process
The data packet for serial communication is transmitted from the sending device through its TXD interface to the receiving device’s RXD interface; the packet format for both parties must be consistent for successful data transmission.
The parameters that need to be defined for asynchronous communication in STM32 are: start bit, data bits (8 or 9 bits), parity bit (9th bit), stop bits (1, 1.5, or 2 bits), baud rate settings.
The data packet for UART serial communication is structured in frames, with a common frame structure being: 1 start bit + 8 data bits + 1 parity bit (optional) + 1 stop bit. As shown in the diagram below:
The parity bit can be either odd or even, which is a simple method for error checking. Odd parity means that the total number of 1s in the frame, including data bits and parity bit, must be odd; even parity means that the total number of 1s must be even.
In addition to odd (odd) and even parity (even), there can also be: 0 parity (space), 1 parity (mark), and no parity (noparity). 0/1 parity: Regardless of the content of the valid data, the parity bit is always 0 or 1.
UART (USART) Block Diagram
This block diagram is divided into upper, middle, and lower parts. This article briefly describes the content of each part; for specific details, refer to the “STM32 Chinese Reference Manual”.
The upper part of the block diagram shows that data enters the receive shift register from RX, then enters the receive data register, and is finally read by the CPU or DMA; data from the CPU or DMA is passed to the send data register, then enters the send shift register, and is finally sent out through TX.
However, both sending and receiving of UART need to be controlled by baud rate; how is baud rate controlled?
This leads us to the lower part of the block diagram, where there is an incoming arrow for the receive shift register and send shift register, both connecting to the receiver control and transmitter control. This means that although asynchronous communication does not have clock synchronization signals, clock signals are provided internally to control the serial port. The receiver clock and transmitter clock are controlled by the same control unit, meaning they share a baud rate generator.It can also be seen how the receiver clock (generator clock) and USRRTDIV are calculated.
Disclaimer: This article is sourced from the internet. Reproduction is for learning reference only and does not represent the views of this account. This account is not responsible for any infringement of its content, text, or images.