In embedded development, interfaces such as UART, I2C, and RS485 are commonly used. Understanding them may be ambiguous, so this article organizes them. The article is lengthy, with an estimated reading time of about 10 minutes.
UART (Universal Asynchronous Receiver-Transmitter)
The UART port refers to a type of physical interface (hardware).
UART is an asynchronous, full-duplex serial bus. It is much more complex than a synchronous serial port. There are two lines: one TXD for transmission and one RXD for reception. Recommended article: STM32 and 51 Microcontroller Serial Communication Example.
Serial data transmission with UART does not require a clock signal for synchronization; instead, it relies on a predefined configuration between the sending and receiving devices.
Both the sending and receiving devices must have their serial communication configurations set to be exactly the same.
Start bit: Indicates the beginning of data transmission, with a logic level of “0”.
Data bits: Possible values are 5, 6, 7, 8, or 9, representing the number of bits of data transmitted. The usual value is 8, as one ASCII character is 8 bits.
Parity bit: Used by the receiver to check the received data, checking whether the number of “1” bits is even (even parity) or odd (odd parity) to verify the correctness of data transmission. This bit can be omitted if not needed.
Stop bit: Indicates the end of a frame of data. The logic level is “1”.
If simulating a UART bus using general-purpose I/O ports, one input port and one output port are required.
I2C Bus
The I2C bus is a synchronous, half-duplex, two-wire serial bus. It consists of two lines: the serial clock line SCL and the serial data line SDA.
SCL line – responsible for generating synchronous clock pulses.
SDA line – responsible for transmitting serial data between devices.
This bus can connect multiple I2C devices to the system. Devices connected to the I2C bus can act as either master or slave devices.
The master device controls communication by initializing data transmission and generating the required synchronous clock pulses. The slave device waits for commands from the master device and responds to receive commands.
Both the master and slave devices can act as sending or receiving devices. Regardless of whether the master device is sending or receiving, the synchronous clock signal can only be generated by the master device.
If simulating an I2C bus using general-purpose I/O ports and achieving bidirectional transmission, one input/output port (SDA) and one output port (SCL) are required.
SPI (Serial Peripheral Interface)
The SPI bus is a synchronous, full-duplex, four-wire serial interface bus. It consists of a system of “one master device + multiple slave devices”.
In the system, as long as only one master device is activated at any time, multiple SPI master devices can exist. It is commonly used for communication between AD converters, EEPROMs, FLASH, real-time clocks, digital signal processors, and digital signal decoders.
To achieve communication, SPI has four signal lines:
-
Master Out Slave In (MOSI): The signal line used to transmit data from the master device to the slave device, also known as Slave Input (SI/SDI).
-
Master In Slave Out (MISO): The signal line used to transmit data from the slave device to the master device, also known as Slave Output (SO/SDO).
-
Serial Clock (SCLK): The signal line for transmitting clock signals.
-
Slave Select (SS): The signal line used to select the slave device, active low.
The working timing mode of SPI is determined by the phase relationship between CPOL (Clock Polarity) and CPHA (Clock Phase). CPOL indicates the initial level state of the clock signal, where CPOL = 0 means the initial state is low, and CPOL = 1 means the initial state is high. CPHA indicates at which clock edge to sample the data. CPHA = 0 means sampling data at the first clock transition, while CPHA = 1 means sampling data at the second clock transition.
Comparison of UART, SPI, and I2C
-
The I2C bus has fewer wires and is more powerful than UART and SPI, but it is technically more complicated because I2C requires support for bidirectional I/O and uses pull-up resistors, making it less resistant to interference. It is generally used for communication between chips on the same board and is less frequently used for long-distance communication.
-
SPI is simpler to implement, while UART requires a fixed baud rate, meaning the interval between two bits of data must be equal, whereas SPI does not have this requirement as it is a clocked protocol.
-
I2C is slightly slower than SPI, the protocol is more complex than SPI, but it requires fewer connections than standard SPI.
-
UART can transmit 5/6/7/8 bits in a frame, while I2C must transmit 8 bits. Both I2C and SPI transmit from the highest bit.
-
SPI uses a chip select signal to select the slave, while I2C uses an address to select the slave.
RS232 Serial Communication
The transmission line consists of two wires and one ground wire. The logic level is negative:
-3V~-15V logic “1”, +3V~+15V logic “0”.
RS-232 serial communication can transmit distances of about 15 meters. It supports bidirectional transmission, full-duplex communication, with a transmission rate of up to 20kbps.
The following diagram shows the definitions of DB9 male and female connectors, where the most commonly used signals are RXD, TXD, and GND.
TTL and RS-232 Conversion
Microcontroller interfaces typically use TTL levels. If connecting to an RS-232 level peripheral, a TTL to RS-232 conversion module is needed. As shown below, the MAX232 chip can be used for conversion.
RS422 Serial Communication
RS-422 has four signal lines: two for transmission, two for reception, and one ground wire, supporting full-duplex communication.
It has one master device and the rest are slave devices. Slave devices cannot communicate with each other, so RS-422 supports point-to-multipoint bidirectional communication.
RS485 Serial Communication
RS-485 uses balanced transmission and differential reception, thus having the ability to suppress common-mode interference.
It uses two-wire half-duplex transmission, with a maximum rate of 10Mb/s. The logic level is determined by the voltage difference between the two wires, enhancing interference resistance and allowing long-distance transmission (from dozens of meters to thousands of meters).
+2V~+6V logic “1”, -2~-6V logic “0”.
TTL to RS-485 conversion is common, for example using the MAX485 chip, as shown in the reference circuit below.
RE pin: Receiver output enable (active low).
DE pin: Transmitter output enable (active high). Can be directly controlled through the MCU’s I/O port.
TTL
In embedded systems, the term “serial port” generally refers to the UART port. It has 4 pins (Vcc, GND, RX, TX) using TTL levels.
The COM port in a PC refers to the serial communication port, abbreviated as serial port, which has 9 pins using RS232 levels.
The serial port and COM port refer to the physical interface (hardware), while TTL, RS-232, and RS-485 refer to voltage standards (electrical signals).
The schematic diagram of communication between a microcontroller and a PC is shown below:
CAN Bus
CAN stands for Controller Area Network, which is a type of serial communication network capable of achieving distributed real-time control. The CAN bus is complex and intelligent. It is mainly used for automotive communication, related articles: Detailed Explanation of CAN Bus.
The CAN bus network mainly consists of CAN_H and CAN_L, and each node transmits signals serially and differentially through these two lines. To avoid signal reflection and interference, a 120-ohm terminating resistor needs to be connected between CAN_H and CAN_L.
Each device can act as either a master or a slave. The communication distance of the CAN bus can reach 10 kilometers (at speeds lower than 5Kbps), with speeds up to 1Mbps (communication distance less than 40M).
CAN Voltage Logic
The CAN bus uses the “AND” rule for bus arbitration, where 1&0 equals 0, so 0 is called dominant and 1 is recessive.
From a voltage perspective, since high voltage is defined as 0 and low voltage as 1, when signals are sent simultaneously, the actual appearance is high voltage, which visually appears as 0 covering 1, hence 0 is dominant and 1 is recessive.
USB Communication Serial Bus
The USB interface has at least four wires, two of which are data lines, and all USB data transmission is completed through these two lines. Its communication is much more complex than serial ports.
The two data lines use differential transmission, meaning that two data lines must work together to transmit one bit, hence it is half-duplex communication, where only sending or receiving can occur at the same time.
The USB standard states that if the voltage level remains unchanged, it represents logic 1; if the voltage level changes, it represents logic 0.
USB to TTL
Generally, USB to serial conversions use the CH340G chip.
Serial communication is simpler than USB because serial communication does not have a protocol.
SD Card
An SD card is a type of memory card that can be used as a memory card for mobile phones.
In embedded systems, communication between a microcontroller and an SD card can be done in two modes:
-
SPI Bus Communication Mode
-
SD Bus Communication Mode
It is worth noting that in SD bus mode, there are four data lines; in SPI bus mode, there is only one data line (MOSI and MISO cannot read or write data simultaneously); thus, communication between a microcontroller and an SD card in SD bus mode is several times faster than in SPI bus mode.
1-WIRE Bus
1-Wire was introduced by Dallas Semiconductor and is a type of asynchronous half-duplex serial transmission. It uses a single signal line to transmit both clock and data, and the data transmission is bidirectional.
The data transmission rate of the single wire is generally 16.3Kbit/s, with a maximum of 142 Kbit/s, and typically operates at rates below 100Kbit/s.
The 1-Wire line port is typically an open-drain or tri-state port, so a pull-up resistor (Rp) is generally required, usually chosen to be between 5K and 10KΩ.
It is mainly used for identifying printer cartridges or medical consumables, as well as for identifying and authenticating printed circuit boards, components, and peripherals.
DMA (Direct Memory Access)
DMA is a hardware module within the STM32 that operates independently of the CPU, transferring data between peripheral devices and memory, thus freeing the CPU and greatly improving its efficiency.
It can access peripherals and memory at high speeds, with transfers not controlled by the CPU, and it supports bidirectional communication. Therefore, using DMA can significantly improve data transmission speeds, which is a highlight of the ARM architecture – DMA bus control.
DMA is analogous to a highway, characterized by its dedicated and high-speed features. While it is possible to achieve the same objectives without using DMA, it would take a longer time to do so.
Ethernet
Ethernet is currently the most widely used local area network technology.
As we know, the Ethernet interface can be divided into protocol layer and physical layer.
The protocol layer is implemented by a single module called the MAC (Media Access Controller).
The physical layer consists of two parts: PHY (Physical Layer) and transceiver.
Currently, many motherboard southbridge chips include Ethernet MAC control functionality but do not provide a physical layer interface. Therefore, an external PHY chip is needed to provide the Ethernet access channel.
The role of the network transformer includes:
-
Coupling differential signals, providing stronger anti-interference capability
-
Isolating different voltage levels of different devices at the network end, isolating DC signals
The reference circuit for the Ethernet interface is shown below.
From the Information Technology Person Information Technology Person WeChat Official Account