[Communication protocols commonly used in embedded IoT: UART, RS-232, RS-422, RS-485, CAN, IIC, SPI]

1. Basic knowledge of serial communication

1. Common serial communication standards

  • At present, the common serial communication interface standards are RS-232, RS-422, RS-485 and so on. In addition, SPI (Serial Peripheral Interface), I 2 C (Integrated Integrated Circuit) and CNA (Controller Area Network) communication are also serial communication.

2. Common level signals and their electrical characteristics

  1. TTL level
logic 1 logical 0
enter >=2.0V <=0.8V
output >=2.4V <=0.4V

Low noise tolerance, about 0.4V, MCU chip pins are all TTL level
2. CMOS level

logic 1 logical 0
enter >=0.7V <=0.3V
output >=0.8V <=0.1V

Noise tolerance higher than TTL level, Vcc is the supply voltage
3. RS-232 level

Level signal name logic 1 logical 0
RS-232 -15V ~ -3V 3V ~ 15V

The COM port of the PC is RS-232 level
4. USB level

Level signal name logic 1 logical 0
USB level (Vd+- Vd-)>= 200mV (Vd-- Vd+)>= 200mV

Using differential voltage, 4-wire system: Vcc, GND, D + , D -

2. UART (Universal Asynchronous Receiver Receiver) Protocol

It is a general serial and asynchronous communication bus. The bus has two data lines, which can realize full-duplex transmission and reception. It is often used for communication between the host and auxiliary equipment in embedded systems .

1. UART message frame format

insert image description here

  • Idle bit : When no data is transmitted, the default is logic 1, which is a high level;
  • Start bit : first issue a logic "0", indicating the beginning of the message frame;
  • Data bit : Immediately after the start bit, it can be composed of 5 to 8 bits, usually 8 bits are transmitted as a byte. The low-order bits of the data are sent first, and then the high-order bits of the data are sent;
  • Parity bit : Immediately after the data bit (optional), so that the number of "1" digits should be even (even check) or odd (odd check), check whether the data transmission is correct;
  • Stop bit : it is the sign of the end of message transmission, it can be 1-bit, 1.5-bit, 2-bit high level, since the data is timed on the transmission line, and each device has its own clock, it is likely to be in communication There is a small out of sync between the two devices. So the stop bit not only indicates the end of the transfer, but also provides the computer with an opportunity to correct the clock synchronization.
  • Baud rate : is a measure of the data transfer rate, indicating the number of bits transmitted per second. For example, if the baud rate of the serial port is set to 9600, it means that 9600 bits of data are transmitted in 1s, and the time to transmit each bit is 1s / 9600 ≈ 104us, so as to distinguish the data transmitted by each bit in the message frame;

If "Hi" is transmitted from device A to device B through the serial port, the parameters are: baud rate 9600, no parity bit, stop bit 1 bit.
Query the ASCLL table to know that the binary representation of 'H' corresponds to 0100 1000, and the corresponding to 'i' The binary representation is 0110 1001, and the sending effect is shown in the following figure:
insert image description here
device A and device B follow the agreed baud rate, device A starts from start, and sends a bit to device B every 104us, and device B receives a bit to device A every 104us. A bit of data, thereby realizing data transmission.
Disadvantages : UART generally uses TTL signal directly to represent 0 and 1, but TTL signal has poor anti-interference ability, data is prone to errors during transmission; and the communication distance of TTL signal is also very short;

Three, RS-232, RS-422, RS-485 communication protocol

The UART only defines the timing of the signals, not the electrical characteristics of the interface.
The RS-232, RS-422 and RS-485 standards were originally developed and published by the Electronic Industries Association of America.

1. RS-232 protocol

  1. At present, the RS-232 interface of industrial control generally only uses three lines of RXD, TXD and GND, and the PC generally adopts the DB-9 connector;insert image description here
  2. characteristic:
  • Working mode: single-ended (unbalanced);
  • Number of nodes: point-to-point communication (1 receiving and 1 sending);
  • Maximum transmission distance: 50ft ( 50 * 0.3048 = 15.24 m);
  • Maximum transmission rate: 20kbit/s;
  • Connection mode: point-to-point ( full duplex );
  • Electrical characteristics: -3V ~ -15V means logic 1, 3V ~ 15V means logic 0;
    common chips have max232, SP232 and other
    disadvantages : short communication distance, low speed, and only point-to-point communication, unable to build a multi-computer communication system, and easy Information transmission errors caused by external electrical interference.

2. RS-422 protocol

The RS-422 standard defines a balanced communication interface, which changes the single-ended communication method of the RS-232 standard. Differential voltages are used for signal transmission on the bus. The transmission rate is increased to 10Mbit/s, the transmission distance is as long as 4000ft=1219.2m (when the rate is lower than 100kbit/s), and up to 10 receivers are connected to the operating bus.

  1. characteristic:
  • Working mode: differential (balanced);
  • Number of nodes: point-to-multipoint communication (1 sending and 10 receiving);
  • Maximum transmission distance: 4000ft ( 4000 * 0.3048 = 1219.2m );
  • Maximum transmission rate: 10Mbit/s;
  • Connection mode: point-to-multipoint ( four-wire system, full duplex );
  • Electrical characteristics: 2V ~ 6V means logic 1, -2V ~ -6V means logic 0;
    Disadvantages : There are few connected devices, and the anti-interference ability is not strong, which cannot fully meet the industrial needs.
    Commonly used chips are max232, SP232, etc.

2. RS-485 protocol

The RS-485 standard operates to connect multiple transceivers, that is, it has multi-station capability and increases multi-point and two-way communication capabilities.

  1. characteristic:
  • Working mode: differential (balanced);
  • Number of nodes: point-to-multipoint communication (1 sending and 32 receiving);
  • Maximum transmission distance: 4000ft ( 4000 * 0.3048 = 1219.2m );
  • Maximum transmission rate: 10Mbit/s;
  • Connection method: multi-point to multi-point ( two-wire system, half-duplex );
  • Electrical characteristics: 2V ~ 6V means logic 1, -2V ~ -6V means logic 0;
    commonly used chips include max485, SP3072EEN, etc. The typical application circuit of SP3072EEN transceiver chip is as follows
    insert image description here
    : The effect diagram of one master and multiple slaves:insert image description here

The Modbus application layer communication protocol developed on the basis of the RS-485 bus network can refer to the detailed explanation of the sensor network application development (intermediate) network protocol message

Four, CAN communication protocol

CAN (Control Area Network, Controller Area Network) was developed by the German company Bosch in 1983. It was first used in the monitoring of the internal control system of the car and the data communication between the actuators. It is currently one of the most widely used fieldbuses in the world. .

1. The CAN bus has the following main characteristics:

  • Long data transmission distance (up to 10km );
  • High data transfer rate (maximum data transfer rate 1Mbit/s );
  • Have an excellent arbitration mechanism;
  • Use filters to implement multi-address data frame delivery;
  • Realize remote data request by means of remote control frame ;
  • With error detection and processing functions;
  • With automatic data retransmission function;
  • The faulty node can automatically leave the bus without affecting the normal operation of other nodes on the bus;

2. CAN technical specifications and standards

  • The ISO 11898 standard CAN communication data transmission rate is 125kbit/s~1Mbit/s , which is suitable for high-speed communication application scenarios; while the ISO 11519 standard CAN communication data transmission rate is below 125kbit/s , suitable for low-speed communication application scenarios.
  • The CAN technical specification mainly defines the physical layer (part), data link layer and transport layer (part) in the OSI basic reference model.insert image description here

3. CAN bus message signal and network topology

When ISO 11898 is static, the level voltage of the two signal lines is about 2.5V ( potential difference 0V ), the state at this time represents 1 (called " recessive level " state), when the voltage value on CAN_H is 3.5 When V and the voltage value on CAN_L is 1.5V, the potential difference between the two lines is 2V , and the state at this time represents a logic 0 (referred to as the " dominant level " state). insert image description here
When the ISO 11519 standard is static, when the voltage value on CAN_H is 4.0V and the voltage value on CAN_L is 1.0V, the potential difference between the two lines is 3.0V , and the state at this time indicates 0 (called " dominant voltage "). " flat " state), when the voltage value on CAN_H is 1.75V and the voltage value on CAN_L is 3.25V, the potential difference between the two lines is -1.50V , the state at this time represents logic 1 (called " recessive level " "condition).

The CAN bus network topology diagram is as follows: The insert image description here
high-speed CAN bus ISO 11898 network is used in the automotive power and transmission system. It is a closed-loop network. The maximum bus length is 40m, and each end has a 120Ω resistance.
The low-speed CAN bus ISO 11519 network is used in the body system. Its two buses are independent and do not form a closed loop. Each bus is required to be connected in series with a 2.2kΩ resistor .
Transmission media can be twisted pair, coaxial cable and optical fiber.

4. Introduction of CAN communication frame

(1) Data frame

Purpose : used to transmit data from the sending unit to the receiving unit

  1. A data frame consists of 7 segments
  • The start of the frame , indicating the start of the data frame and the remote frame, consists of only one "dominant level" bit.
  • The arbitration segment is a segment that indicates frame priority. The arbitration segment of the standard frame consists of 11bit identifier ID and RTR (remote transmission request) bit; the arbitration segment of the extended frame consists of 29bit identifier ID, SRR (substitute remote request bit), IDE bit and RTR.
  • The control segment is a segment indicating the number of bytes of data and reserved bits. The format of the control segment of the standard frame and the extended frame is different.
  • The data segment is used to carry the content of the data. It contains 0~8B of data and is output from the MSB (most significant bit).
  • The CRC segment is a segment used to check whether the frame transmission is erroneous. It consists of a 15-bit CRC sequence and a 1-bit CRC delimiter (for separation).
  • The ACK segment is a segment used to confirm whether the reception is correct. It consists of an ACK slot and an ACK delimiter (for separation), with a length of 2 bits
  • The end of the frame is used to indicate the end of the data frame, which consists of 7 bits of recessive bits.insert image description here
    insert image description here

(2) Remote control frame

Purpose : Used by the receiving unit to request data from the sending unit with the same ID.
insert image description here

(3) Error frame

Purpose : Used to notify other units of errors when errors are detected.

insert image description here

(4) Overload frame

Purpose : Used by the receiving unit to notify the sending unit that it is not ready to receive.
insert image description here

(5) Frame interval

Purpose : A frame used to separate data frames and remote control frames.
There are three constituent elements of the frame interval:
one is the interval, which consists of 3-bit recessive bits.
The second is that the bus is idle, which is composed of recessive levels and has no length limit. The unit to transmit starts accessing the bus only when the bus is idle.
The third is delayed transmission, which consists of 8-bit recessive bits.
insert image description here

Five , I2C communication protocol

1. Introduction to the bus

IIC (Inter-Integrated Circuit) is actually the abbreviation of IICBus, so it should be called the integrated circuit bus in Chinese. It is a serial communication bus that uses a multi-master-slave architecture. It is developed to connect low-speed peripheral devices; the IIC bus has two bidirectional signal lines, a data line SDA for sending and receiving data, and a clock line SCL for synchronizing the clocks of both parties.
insert image description here

2. IIC communication process

  1. The host sends a start signal to enable the bus;
  2. The master sends a byte of data to indicate the slave address and determine the transfer direction of the subsequent master slave ;
  3. Each slave on the IIC bus determines whether the slave address sent by the master is itself, and the matching slave sends a response signal to the master;
  4. The transmitter sends one byte of data;
  5. The receiver sends a reply signal in response to the sender;
  6. ...(loop repeating steps 4 and 5);
  7. After the last communication is completed, the host sends a stop signal to release the bus;

IIC bus send and stop timing diagram:
insert image description here
IIC bus send data timing diagram:
insert image description here

3. Typical timing of IIC

  1. The master sends data to the slave
  2. The slave sends data to the master
    insert image description here
    3. The master first sends data to the slave, and then the slave sends data to the master
    insert image description here

6. SPI communication protocol

1. Introduction to SPI bus

SPI is a high-speed full-duplex synchronous serial communication protocol (one master and multiple slaves) developed by Motorola. A bit similar to IIC, but different from the way IIC gates a slave device. IIC gates the slave by sending the slave address, while SPI gates the slave by pulling down the NSS pin connected to the slave. .

The general application of SPI consists of four pins (one master and multiple slaves):

  • SCLK (Serial Clock): serial clock, issued by the host;
  • MOSI (Maser Output, Slave Input): The host outputs the slave input signal, which is sent by the host;
  • MISO (Master Input, Slave Output): master input slave output signal, sent by slave;
  • NSS (Slave Selected): The selection signal, sent by the host, is generally active low.
    The SPI master-slave connection is shown in the figure:
    insert image description here

2. The process of SPI communication

SPI is a serial communication protocol, but the host occupies significantly more pins than IIC and UART, and the number of host pins will increase with the number of slaves. When the master sends data through the MOSI data line, the slave also transmits data to the master through MISO (receive and receive at the same time), and they are connected in a virtual-like topology. The data is usually shifted out the most significant bit first, and on the clock edge, both the master and the slave are shifted out by one bit, and then output to each other on the transmission line (change the data). On the next clock edge, both the master and slave receivers accept the bit from the transmission line and set it as the new least significant bit (sampled data) of the shift register. After completing such a shift-out-shift-in cycle, the master and slave exchange a bit in the register, and the transfer may last any number of clock cycles. After the transfer is complete, the master stops the clock signal and pulls the NSS strobe high.
insert image description here

Guess you like

Origin blog.csdn.net/a6662580/article/details/122601481