通信协议
[[7. UART]]
[[8.SPI]]
[[9.I2C]]
在编程中,字符以Ascil码的形式通过通信协议传输:[[2.C语言Char和string]]
Communication
Intro
Definition
Communication between electronic devices is like communication between humans. Both sides need to speak the same language
In electronics, these languages are called communication protocols.(计算机通信协议)
Importance
A Controller Area Network (CAN bus) is a vehicle bus standard designed to allow microcontrollers and devices to communicate with each other’s applications without a host computer
the LIN bus (Local Interconnect Network) standard has been introduced to complement CAN for non-critical subsystems such as air-conditioning and infotainment, where data transmission speed and reliability are less critical.
Common protocols
- Serial Peripheral Interface (SPI)
- Inter-Integrated Circuit (I2C)
- Universal Asynchronous Receiver/Transmitter (UART) driven communication
Principle
- Electronic devices talk to each other by sending bits of data through wires physically connected between devices.
- A bit is like a letter in a word, a bit is binary and canonly be a 1 or 0.
- Bits are transferred from one device to another by quick changes in voltage.
- In a system operating at 5 V, a 0 bit is communicated as a short pulse of 0 V, and a 1 bit is communicated by a short pulse of 5 V.
- The bits of data can be transmitted either in parallel or serial form.
Types
Simplex(单工
Uni-directional flow of data
In only one direction
Half duplex(半双工
Bi-directional flow of data (one direction at a time.)
In both direction, but only one TX and one RX at a time
Full duplex(全双工
Bi-directional flow of data in both directions at the same time (simultaneous flow).
Both sides can transmit and receive in the same time
Serial VS Parallel communication
The bits of data can be transmitted either in parallel or serial form.
Parallel
In parallel communication, the bits of data are sent all at the same time, each through a separate wire. The parallel transmission of the letter “C” in binary (01000011) is shown as:
多线并行
好处
- The process of sending several bits as a whole, on a link with several parallel
channels. - It requires a separate channel for each bit to _ be transmitted
- A parallel link use simpler hardware as there is no need for a serializer/ deserializer.
- Usually used for very short distances.Sender
坏处
- Clock skew: Clock signal arrives at differnet components at differnet time
- Crosstalk: It the transmitted signal is badly affected by another nearby signal, when electronmagnetic energy from one cable leaves an imprint on adjacent cables
Crosstalk is a type of noise signal that corrupts the actual signal while transmission through the communication medium.
Crosstalk is mainly induced because of coupling between different signals transmitted using parallel adjacent cables.
Crosstalk is caused due to electrostatic or electromagnetic induction.
Feature
- Very simple.
- Crosstalk places an upper limit on the length of a parallel data connection (usually shorter than a serial connection).
- Clock skew between different channels.
- Low data rate compared to a serial connection for long distances. (Due to the last two reasons).
- Has a cable cost higher than a serial connection.
Serial
In serial communication, the bits are sent one by one through a single wire. The serial transmission of the letter “C” in binary (01000011) is shown:
单线输出波形
- The process of sending data bit by bit sequentially, over a single channel between sender and receiver.
- For correct data transmission, there has to be some form of synchronization between transmitter and receiver.
- Cost of cable and synchronization difficulties make parallel communication impractical.
Features
- To reduce the cost of an IC package by reducing the number of pins used for communication between different IC’s, instead of using parallel
communication. - Clock skew between different channels is not an issue (for unclocked asynchronous serial communication links).
- A serial connection requires fewer interconnecting cables and hence occupies less space.
- Serial links can be clocked considerably faster than parallel links, and achieve a higher data rate.
- Used for all long- haul communication and most computer networks
Bit rate and baud rate
Bit rate is the number of binary bits per second
Baud rate is the number of symbol changes per second
N = log2(m)
where N *= Number of bits required to represent signal or voltage levels
* m = *number of signal or voltage levels
* Example: To represent 4 voltage levels you required at least 2 bits. Put m = 4 in above formula , you will get N as 2.
Similarly to represent 16 voltage levels, 4 bits are required (log2(16))
Bit rate
Bit rate refers to Number of data bits transferred per second. Unit of Bit rate is bits per second(bps)
每秒传送的比特(bit)数。单位为 bps(Bit Per Second),比特率越高,每秒传送数据就越多
Baud rates(传输速率,波特率)
Baud rate refers to number of signal or voltage level changes per second. Unit of baud rate is bauds per second. For example baud rate is 9600 means 9600 signal level changes are happening within a second
UART的波特率是指每秒传输的二进制位数(单位bps),比如9600bps,意思就是每秒钟可以传输9600个位(bit)。
例如:设字符传输的速率为120字符/秒,而每1个字符为10位(bit),那么传送的波特率为:10位/字符 * 120 字符/秒 = 1200 /秒 = 1200bps。那么每1位二进制位(bit)的传送时间:
T = 1/1200 = 0.833ms
UART要求发送与接收两个UART的波特率配置相同。如果发送与接收波特率不同,相差很大,接收端采样点跨过多个电平,造成接收丢失,或者造成波特率不匹配
常见的波特率有9600、115200、128000、256000等
1 Baud = log2M (bit/s)
其中M是信号的编码级数。
也可以写成:Rbit = Rbaud log2M(Rbit:比特率;Rbaud:波特率)
Protocol
The sender and receiver must agree on a set of rules(protocol) on:
- when data transmission begins and ends
- The used bit rate and data packaging format
SPI, I2C, and UART are slower than protocols like USB, Ethernet, Bluetooth, and WiFi, but they’re a lot more simple and use less hardware
SPI, I2C, and UART are ideal for communication between microcontrollers and between microcontrollers and sensors where large amounts of high speed data don’t need to be transferred.
SYNCHRONIZATION
For asynchronous transmission:
- A start bit with the value o indicate the beginning of each word, then eight data bits are sent bit by bit, and finally a stop bit with the value 1to indicate the end of the word.
Both the transmitter and receiver use the same baud rate
For svnchronous transmission:
Synchronization is done using a clock line
- A clock line from one terminal (master) to the other terminal (slave) makes the synchronization.
- Another line is used for data transmission between master and slave(s).
- If the master communicates with many peripheral ICs using the same data and clock lines, a (slave select) line is used to determine which slave to communicate with.
How
Notes about Synchronous transmission: Preferred for short-distance transmission.
- Uses its communication time more efficiently because, after the transmitter and receiver are synchronized, theyonly
transmit/receive data. - Not used For long-distance transmission as sending the clock signal with the data signal is not an appropriate solution for the additional costs and clock skew.
- In some cases, it’s impossible to use synchronoustransmission and we have to use asynchronous communication Ex. remote control.