[MCU] UART, I2C, SPI, TTL, RS232, RS422, RS485, CAN, USB, SD card, 1-WIRE, Ethernet and other common communication methods

In the development of single-chip microcomputer, UART, I2C, RS485, etc. are commonly used, here is a brief introduction

UART Universal Asynchronous Receiver Transmitter

The UART port refers to a form of physical interface (hardware).
Insert image description here
UART is an asynchronous (referring to not using clock synchronization and relying on frame length for judgment), full-duplex (transmitting and receiving can be performed at the same time) serial port bus. It is much more complicated than synchronous serial port. There are two wires, one TXD for sending and one RXD for receiving. The serial data transmission of UART does not need to use a clock signal to synchronize the transmission, but relies on the predefined configuration between the sending device and the receiving device. For the sending device and the receiving device, the serial communication configuration of the two should be set to Exactly the same.

Insert image description here

Start bit: Indicates the start of data transmission, the level logic is "0".

Data bit: The possible values ​​are 5, 6, 7, 8, 9, which means that these bits of data are transmitted. The general value is 8, because an ASCII character has 8 bits.

Parity bit: used by the receiver to verify the received data. The number of "1" bits is an even number (even parity) or an odd number (odd parity) to verify the correctness of the data transmission. This bit is not needed when using it.

Stop bit: indicates the end of a frame of data. The level logic is "1".

If a general-purpose IO port is used to simulate the UART bus, one input port and one output port are required.

I2C bus

The I2C bus is a synchronous, half-duplex, bidirectional, two-wire serial bus. It consists of two buses: the serial clock line SCL and the serial data line SDA.

SCL line - responsible for generating synchronization clock pulses.

SDA Line - Responsible for transferring serial data between devices.

This bus can connect multiple I2C devices to the system. A device connected to the I2C bus can function as either a master or a slave. Communication is carried out by addressing the device and broadcasting. For details, refer to the link: IIC details
Insert image description here
. The master device is responsible for controlling communication, sending data and generating the required synchronization clock pulses by initializing data transmission. The slave device waits for commands from the master device and responds to command reception.

Both master and slave devices can act as sending or receiving devices. Regardless of whether the master device is used as a sending device or a receiving device, the synchronous clock signal can only be generated by the master device.

If a general IO port is used to simulate the I2C bus and realize bidirectional transmission, an input and output port (SDA) and an output port (SCL) are required.

SPI serial peripheral interface

The SPI bus is a synchronous, full-duplex, bidirectional 4-wire serial interface bus. It is a system composed of "single master + multiple slaves".

In the system, as long as only one master device is active at any time, there can be multiple SPI master devices. It is often used to realize communication between AD converter, EEPROM, FLASH, real-time clock, digital signal processor and digital signal decoder.

Insert image description here
In order to achieve communication, SPI has a total of 4 signal lines, which are:

Master Out Slave In (MOSI): The signal line that transmits data from the master device to the slave device, also called slave device input (Slave Input/Slave Data In, SI/SDI).

Master In Slave Out (MISO): The signal line that transmits data from the slave device to the master device, also called slave device output (Slave Output/Slave Data Out, SO/SDO).

Serial Clock (SCLK): The signal line that transmits the clock signal.

Slave Select (SS): Used to select the signal line of 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 represents the state of the initial level of the clock signal. CPOL of 0 represents the initial state of the clock signal. is low level, and 1 indicates that the initial level of the clock signal is high level. CPHA indicates which clock edge the data is sampled on. A CPHA of 0 indicates that the data is sampled on the first clock changing edge, while a CPHA of 1 indicates that the data is sampled on the second clock changing edge.

Insert image description here

UART, SPI, I2C comparison

I2C has fewer lines and is more powerful than UART and SPI, but it is also technically more troublesome because I2C requires bidirectional IO support and uses pull-up resistors, which have weak anti-interference capabilities and are generally used on chips on the same board. Communication between computers is rarely used for long-distance communication.

The SPI implementation is simpler. UART requires a fixed baud rate, which means that the intervals between two bits of data must be equal, while SPI does not matter because it is a clocked protocol.

The speed of I2C is a little slower than SPI, and the protocol is a little more complicated than SPI, but there are fewer connections than standard SPI.

UART can transmit 5/6/7/8 bits in one frame, and I2C must be 8 bits. Both I2C and SPI transmit from the highest bit.

SPI uses the chip select signal to select the slave, and I2C uses the address to select the slave.

Insert image description here

RS232 serial communication

There are two transmission lines and one ground wire. The levels are negative logic:
-3V to -15V logic "1", +3V to +15V logic "0".
The transmission distance of RS-232 serial communication is about 15 meters. It can achieve two-way transmission, full-duplex communication, and the transmission rate is as low as 20kbps.
The figure below is the definition of DB9 male and female. Generally, the most commonly used signals are RXD, TXD, and GND.
Insert image description here

TTL and RS-232 conversion

The microcontroller interface is generally TTL level, if you connect 232 level peripherals, you need to add a TTL to RS232 module. As shown in the figure below, the chip MAX232 can be used for conversion.
Insert image description here

RS422 serial communication

RS-422 has 4 signal wires: two sending, two receiving and one ground wire, which is full-duplex communication.
It has a master device, and the rest are slave devices, and the slave devices cannot communicate with each other, so RS-422 supports point-to-many two-way communication.

Insert image description here

RS485 serial communication

RS-485 uses balanced transmission and differential reception, so it has the ability to suppress common mode interference.
Using two-wire half-duplex transmission, the maximum rate is 10Mb/s. The level logic is determined by the level difference of the two wires, which improves the anti-interference ability and has a long transmission distance (tens of meters to thousands of meters).
+2V to +6V logic "1", -2 to -6V logic "0".

It is very common to convert TTL to RS-485, such as MAX485, the reference circuit is as follows
Insert image description here
RE pin: receiver output enable (active low).
DE pin: Transmitter output enable (active high).
It can be controlled directly through the IO port of the MCU.

TTL

The serial port mentioned in the embedded generally refers to the UART port. 4 pins (Vcc, GND, RX, TX), use TTL level.
The COM port in the PC is the serial communication port, referred to as the serial port. 9 Pins, using RS232 level.

Insert image description here

Serial port and COM port refer to the physical interface form (hardware). TTL, RS-232, and RS-485 refer to level standards (electrical signals).
Insert image description here
The schematic diagram of the communication between the microcontroller and PC is as follows:

Insert image description here

CAN bus

CAN is the abbreviation of Controller Area Network, which is a serial communication network that can realize distributed real-time control. The functions of the CAN bus are complex and intelligent. Mainly used for automotive communications, related articles: Detailed explanation of CAN bus.
The CAN bus network is mainly connected to CAN_H and CAN_L. Each node implements serial differential transmission of signals through these two lines. In order to avoid signal reflection and interference, a 120-ohm terminal resistor needs to be connected between CAN_H and CAN_L.

Insert image description here
Each device can be either a master device or a slave device. The communication distance of the CAN bus can reach 10 kilometers (the rate is less than 5Kbps), and the speed can reach 1Mbps (the communication distance is less than 40M).

Insert image description here
The CAN level logic
CAN bus uses the "wired AND" rule for bus punching. 1&0 is 0, so 0 is called dominant and 1 is recessive.
From the potential point of view, because the high potential is 0 and the low potential is 1, when the signal is sent out at the same time, it actually appears to be a high potential. From the perspective of the phenomenon, it is like 0 covering 1, so 0 is called dominant and 1 is recessive.
Insert image description here

USB communication serial bus

The USB interface has at least four wires, two of which are data wires, and all USB data transmission is completed through these two wires. Its communication is much more complicated than the serial port.
The two data lines use differential transmission, which means that two data lines need to cooperate to transmit one bit. Therefore, it is half-duplex communication and can only send or receive at the same time.
USB stipulates that if the voltage level does not change, it represents a logic 1; if the voltage level changes, it represents a logic 0.
Insert image description here

USB to TTL

Generally, the CH340G chip is used to convert USB to serial port.

Insert image description here

Serial communication is simpler than USB because there is no protocol for serial communication.

SD card

SD card is a memory card that can be used as a memory card in mobile phones.

In embedded, there are two modes of communication between microcontroller and SD card:

SPI bus communication mode


Insert image description here
It is worth noting that in the SDIO bus communication mode, there are 4 data lines in the SD bus mode; there is only one data line in the SPI bus mode (MOSI and MISO cannot read data at the same time, nor can they write data at the same time); in this way, in the embedded system , When the microcontroller communicates with the SD card, the SD bus mode is several times faster than the SPI bus mode.

Insert image description here

1-WIRE bus

1-Wire was launched by the Dallas (Dallas) Company in the United States and is an asynchronous half-duplex serial transmission. A single signal line is used to transmit both clock and data, and data transmission is bidirectional.

Insert image description here
The data transmission rate of a single bus is generally 16.3Kbit/s, and the maximum can reach 142 Kbit/s. Usually, the data transmission rate is below 100Kbit/s.
The 1-Wire line port is an open-drain or tri-state gate port, so it is generally necessary to add a pull-up resistor Rp, usually 5K~10KΩ. It is mainly used
in: identification of printing cartridges or medical consumables; printed circuit boards, accessories and peripherals Identification and authentication of equipment.

DMA direct memory access

DMA is a hardware module in STM32. It is independent of the CPU and transmits data between peripheral devices and memory, freeing the CPU and greatly improving the efficiency of the CPU.

Insert image description here
It can access peripherals and memory at high speed, and the transmission is not controlled by the CPU, and it is two-way communication. Therefore, the use of DMA can greatly increase the data transmission speed, which is also a bright spot of the ARM architecture - DMA bus control.
DMA corresponds to a highway, dedicated and high-speed. If you don't use DMA, you can also achieve the goal, but the time to achieve the goal is relatively long.

EthernetEthernet

Ethernet is currently the most commonly used LAN technology.

As we all know, the Ethernet interface can be divided into protocol layer and physical layer.

The protocol layer is implemented by a single module called MAC (Media Access Layer) controller.

The physical layer consists of two parts, namely PHY (Physical Layer) and transmitter.

At present, the south bridge chips of many motherboards already include Ethernet MAC control functions, but do not provide physical layer interfaces. Therefore, an external PHY chip is required to provide an Ethernet access channel.

Insert image description here
The functions of a network transformer are:

Coupled differential signals, stronger anti-interference ability

The transformer isolates different levels of different devices at the network cable end and isolates DC signals.

Ethernet interface reference circuit, as shown in the figure below.

Insert image description here

Guess you like

Origin blog.csdn.net/apythonlearner/article/details/132550032