Understanding Communication Protocols: UART, TTL, RS232, RS422, RS485, CAN, USB
Follow “Technical Training” focused on automation education for 14 yearsUART, I2C, RS485… These communication methods are widely used, but their explanations can be ambiguous. Therefore, I feel it is necessary to organize them for clarification and differentiation. The article is a bit long, so I suggest saving it for later reference when needed.UART Universal Asynchronous Receiver-TransmitterThe UART port refers to a physical interface form (hardware).UART is asynchronous, full-duplex serial bus. It is much more complex than synchronous serial ports. There are two lines, one TXD for transmission and one RXD for reception.UART’s serial data transmission does not require a clock signal for synchronization; instead, it relies on predefined configurations between the transmitting and receiving devices.For both the transmitting and receiving devices, the serial communication configuration should be set to be identical.Start bit: Indicates the beginning of data transmission, with a logic level of “0”.Data bits: Possible values are 5, 6, 7, 8, 9, representing the number of bits transmitted. Generally, the value is 8, as one ASCII character is 8 bits.Parity bit: Used by the receiver to verify the received data. The number of bits with a value of “1” can be even (even parity) or odd (odd parity) to check the correctness of data transmission. This bit is optional.Stop bit: Indicates the end of a data frame. The logic level is “1”.If simulating a UART bus with general I/O ports, one input port and one output port are required.I2C BusThe I2C bus is a synchronous, half-duplex bidirectional 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, sending data, and generating the required synchronous clock pulses. The slave device waits for commands from the master and responds by receiving commands.Both 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 with general I/O ports for bidirectional transmission, one input/output port (SDA) and one output port (SCL) are required.SPI Serial Peripheral InterfaceThe SPI bus is a synchronous, full-duplex bidirectional 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 enable communication, SPI has a total of four signal lines:
(1) Master Out Slave In (MOSI): The signal line for transmitting data from the master device to the slave device, also known as slave input (SI/SDI).(2) Master In Slave Out (MISO): The signal line for transmitting data from the slave device to the master device, also known as slave output (SO/SDO).(3) Serial Clock (SCLK): The signal line for transmitting clock signals.(4) Slave Select (SS): The signal line for selecting 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 state of the initial level of the clock signal; CPOL = 0 indicates that the initial state of the clock signal is low, while CPOL = 1 indicates that the initial level of the clock signal is high. CPHA indicates the clock edge at which data is sampled; CPHA = 0 indicates sampling on the first clock edge, while CPHA = 1 indicates sampling on the second clock edge.Comparison of UART, SPI, and I2C① I2C has fewer lines 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 less for long-distance communication.② SPI is simpler to implement, while UART requires a fixed baud rate, meaning the interval between two data bits must be equal, while SPI does not have this requirement because it is a clocked protocol.③ I2C is slightly slower than SPI, with a more complex protocol, but it requires fewer connections than standard SPI.④ A UART frame can transmit 5/6/7/8 bits, while I2C must be 8 bits. Both I2C and SPI transmit from the highest bit.⑤ SPI uses chip select signals to choose slaves, while I2C uses addresses to select slaves.RS232 Serial CommunicationThere are two transmission lines and one ground line. The levels are negative logic:-3V~-15V logic “1”, +3V~+15V logic “0”.RS-232 serial communication can transmit distances of about 15 meters. It supports bidirectional transmission and full-duplex communication, with a transmission rate of up to 20kbps.The following image shows the definitions of the DB9 male and female connectors, with the most commonly used signals being RXD, TXD, and GND.TTL and RS-232 ConversionMicrocontroller interfaces generally use TTL levels. If connecting to a device with RS-232 levels, a TTL to RS232 conversion module is required. For example, the MAX232 chip can be used for conversion.RS422 Serial CommunicationRS-422 has four signal lines: two for transmission, two for reception, and one ground line. It supports 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 CommunicationRS-485 uses balanced transmission and differential reception, providing the capability to suppress common-mode interference.It uses two-wire half-duplex transmission, with a maximum rate of 10Mb/s, and the logic levels are determined by the voltage difference between the two wires, improving interference resistance and allowing for long transmission distances (from tens of meters to thousands of meters).+2V~+6V logic “1”, -2~-6V logic “0”.TTL to RS-485 conversion is common, for example using MAX485, with the reference circuit shown below:RE pin: Receiver output enable (active low).DE pin: Transmitter output enable (active high). It can be controlled directly through the MCU’s I/O port.TTLThe serial port referred to in embedded systems generally means the UART port. It has four pins (Vcc, GND, RX, TX), using TTL levels.The COM port in a PC refers to the serial communication port, also known as a serial port. It has nine pins and uses RS232 levels.The serial port and COM port refer to physical interface forms (hardware), while TTL, RS-232, and RS-485 refer to voltage standards (electrical signals).The communication diagram between a microcontroller and a PC is shown below:CAN BusCAN stands for Controller Area Network, a serial communication network that enables distributed real-time control. The CAN bus is complex and intelligent, primarily used for automotive communication.The CAN bus network is mainly hung on CAN_H and CAN_L, with each node achieving serial differential signal transmission through these two lines. To avoid signal reflection and interference, a 120-ohm terminating resistor should 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 up to 10 kilometers (at speeds below 5Kbps), and speeds can reach 1Mbps (for distances less than 40M).CAN Logic LevelsCAN bus uses the “AND” rule for bus arbitration, where 1&0 equals 0, thus calling 0 dominant and 1 recessive.From the potential perspective, since the high potential is defined as 0 and low potential as 1, when signals are sent simultaneously, the actual signal appears as high potential, visually resembling 0 covering 1, hence 0 is dominant and 1 is recessive.USB Communication Serial BusThe 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 that of serial ports.The two data lines use differential transmission, meaning that both data lines are required to transmit one bit, thus it is half-duplex communication, allowing only sending or receiving at the same time.USB specifies that if the voltage level remains unchanged, it represents logic 1; if the voltage level changes, it represents logic 0.USB to TTLGenerally, USB to serial conversion uses the CH340G chip.Serial communication is simpler than USB because serial communication does not have protocols.SD CardThe SD card is a type of storage card that can be used as a memory card for mobile phones.In embedded systems, communication between microcontrollers and SD cards can occur in two modes:1. SPI bus communication mode;2. SD bus communication mode.It is worth noting that in the SD bus mode, there are four data lines; in the SPI bus mode, there is only one data line (MOSI and MISO cannot read data simultaneously, nor can they write data simultaneously); thus, in embedded systems, communication between microcontrollers and SD cards using the SD bus mode is several times faster than using the SPI bus mode.1-WIRE Bus1-Wire was introduced by Dallas (达拉斯) company, and it is an 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, usually transmitting data at rates below 100Kbit/s.The 1-Wire line port is typically an open-drain or tri-state gate port, so a pull-up resistor Rp is generally required, usually chosen to be 5K~10KΩ.Main applications include: identification of printing cartridges or medical consumables; identification and authentication of printed circuit boards, accessories, and peripherals.DMA Direct Memory AccessDMA is a hardware module within STM32 that operates independently of the CPU, transferring data between peripheral devices and memory, freeing the CPU and significantly 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 transfer speeds, which is also a highlight of the ARM architecture – DMA bus control.DMA corresponds to a highway, characterized by its dedicated and high-speed nature. If DMA is not used, the goal can still be achieved, but it will take a longer time.EthernetEthernet is currently the most widely used local area network technology.Ethernet interfaces can be divided into protocol layers and physical layers.The protocol layer is implemented by a single module called the MAC (Media Access Layer) controller.The physical layer consists of two parts: the PHY (Physical Layer) and the transceiver.Many motherboard southbridge chips now include Ethernet MAC control functions but do not provide physical layer interfaces. Therefore, a PHY chip must be externally connected to provide the Ethernet access channel.The function of the network transformer is:To couple differential signals, enhancing anti-interference capability;To isolate different voltage levels of different devices at the network end, isolating DC signals.Ethernet interface reference circuit:—END—Source: Internet, copyright belongs to the original author, please delete if infringing