The principle of serial communication and USB to serial communication
The principle of serial communication and USB to serial communication
The principle of serial communication
The concept of serial communication (SerialCommuniicaTIons) is very simple, the serial port sends and receives bytes by bit (bit).
Although slower than byte-by-byte parallel communication, serial ports can use one wire to send data at the same time as another wire to receive data.
It is simple and enables long-distance communication. For example, when IEEE488 defines the parallel communication state, it is stipulated that the total length of the device line shall not exceed 20 meters, and the length between any two devices shall not exceed 2 meters; for serial ports, the length can be up to 1200 meters.
Typically, serial ports are used for the transmission of ASCII characters. Communication is done using 3 wires, which are ground, send, and receive. Because serial communication is asynchronous, the port can send data on one wire while receiving data on the other.
Other lines are used for handshake, but are not required. The most important parameters of serial communication are baud rate, data bits, stop bits and parity. For two ports communicating, these parameters must match.
The principle of serial communication and USB to serial communication
A, Baud rate: This is a parameter that measures the transmission rate of symbols. It refers to the change in unit time after the signal is modulated, that is, the number of times the carrier parameter changes per unit time, for example, 240 characters are transmitted per second, and each character format contains 10 bits (1 start bit, 1 Stop bit, 8 data bits), the baud rate at this time is 240Bd, and the bit rate is 10 bits * 240 bits/second = 2400bps. Generally, the modulation rate is greater than the baud rate, such as Manchester encoding). Typically phone lines have baud rates of 14400, 28800 and 36600. The baud rate can be much larger than these values, but the baud rate and distance are inversely proportional. High baud rates are often used for communication between instruments placed in close proximity, a typical example being the communication of GPIB devices.
B, Data Bits: This is a parameter that measures the actual data bits in the communication. When a computer sends a packet, the actual data is often not 8 bits, the standard values are 6, 7 and 8 bits. How to set it depends on the information you want to send. For example, the standard ASCII code is 0 to 127 (7 bits). The extended ASCII code is 0 to 255 (8 bits). If the data uses simple text (standard ASCII), then each packet uses 7 bits of data. Each packet refers to a byte, including start/stop bits, data bits and parity bits. Since the actual data bits depend on the choice of communication protocol, the term "packet" refers to any communication situation.
C, stop bit: used to represent the last bit of a single packet. Typical values are 1, 1.5 and 2 bits. Since the data is timed on the transmission line, and each device has its own clock, it is likely that there will be a small out-of-sync between the two devices in the communication. 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. The more bits available for stop bits, the greater the tolerance for different clock synchronizations, but the slower the data transfer rate.
D, parity bit: a simple error detection method in serial communication. There are four error detection modes: even, odd, high and low. Of course, no check digit is also possible. For the case of even and odd parity, the serial port will set the parity bit (one bit after the data bit), and use a value to ensure that the transmitted data has even or odd logic high bits. For example, if the data is 011, then for even parity, the parity bit is 0, ensuring that the number of bits that are logically high is an even number. If it is odd parity, the parity bit is 1, so there are 3 logic high bits. The high and low bits do not really check the data, simply set the logic high or logic low check. This allows the receiving device to know the state of a bit and have the opportunity to determine if noise is interfering with the communication or if the transmitted and received data are out of sync.
Overview of RS232
On our computer, there is usually a 9-pin serial interface. This serial interface is called RS232 interface, which is related to UART communication. However, since notebook computers do not have 9-pin serial ports, communication with microcontrollers is becoming more and more common. for using the USB virtual serial port.
The RS232 interface has a total of 9 pins, which are defined as: 1. Carrier detect DCD; 2. Receive data RXD; 3. Send data TXD; 4. Data terminal ready for DTR; 5. Signal ground wire SG; 6. Data preparation Good DSR; 7. Request to send RTS; 8. Clear to send CTS; 9. Ringing prompt RI. We want this serial port to communicate with our microcontroller, we only need to care about the 2-pin RXD, 3-pin TXD and 5-pin GND.
Although the names of these three pins are the same as the names of the serial ports on our microcontroller, they cannot directly communicate with the microcontroller. Why? As we learn more and more, we have to slowly realize that not all circuits are 5V for high level and 0V for low level. For the RS232 standard, it is an inverse logic, also called negative logic. Why is it called negative logic? Its TXD and RXD voltage, -3V ~ -15V voltage represents 1, +3 ~ +15V voltage represents 0. A low level represents a 1, and a high level represents a 0, so it is called negative logic. Therefore, the 9-pin RS232 serial port of the computer cannot be directly connected to the single-chip microcomputer, and a level conversion chip MAX232 is required to complete the
This chip can convert the standard RS232 serial port level into the UART0V/5V level that our microcontroller can recognize and withstand. From here, everyone seems to understand a little bit. In fact, the RS232 serial port and the UART serial port have the same protocol type, but the level standard is different, and the MAX232 chip plays the role of a middleman. It converts the UART level. into RS232 level, and also convert the RS232 level into UART level, so as to realize the communication connection between the standard RS232 interface and the microcontroller UART.
USB to serial communication
With the development of technology, there is still a lot of use of RS232 serial communication in the industry, but in the application of commercial technology, the USB to UART technology has been gradually replaced by the RS232 serial port, and most notebook computers have no serial port. So what should we do to realize the communication between the microcontroller and the computer?
We only need to add a USB to serial port chip on the circuit to successfully realize the conversion between the USB communication protocol and the standard UART serial communication protocol. On our development board, we use the CH340T chip.
We need to use jumper caps to short the middle and lower pins together. The circuit of CH340T on the right is very simple. After the power supply and crystal oscillator are connected, the DP and DM of pins 6 and 7 are respectively connected to the 2 data pins of the USB port, and pins 3 and 4 are connected to our microcontroller through jumpers. TXD and RXD go up.
A 4148 diode is added to the 3-pin position of the CH340T circuit, which is a little trick. Because the STC89C52 single-chip microcomputer needs to be cold-started when downloading the program, that is, click the download first and then power on. Although the VCC of the single-chip microcomputer is controlled by the switch, because the 3-pin of the CH340T is an output pin, if there is no such diode, when the MCU after the switch is powered off, the 3-pin of the CH340T and the MCU's P3.0 (ie RXD ) pins are connected together, and a current will flow into the post-stage circuit through this pin and charge the capacitor of the post-stage, resulting in a voltage of a certain range in the post-stage. Although this voltage value is only about two or three volts, it may affect the normal cold start. After adding a diode, on the one hand, it does not affect the communication, and on the other hand, it can also eliminate this bad effect. This place can be used as a temporary understanding. If you make this kind of circuit yourself, you can refer to it.
IO port simulates UART serial communication
UART serial port baud rate, commonly used values are 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200 and other rates.
The IO port simulation UART serial communication program is a simple demonstration program. We use the serial port debugging assistant to send a data. After the data is incremented by 1, it returns automatically.
Serial port debugging assistant, here we directly use the serial port debugging assistant that comes with the STC-ISP software. First, let me tell you about the use of the serial port debugging assistant, as shown in Figure 11-6.
The first step is to select the serial port assistant menu, the second step is to select the hexadecimal display, the third step is to select the hexadecimal send, and the fourth step is to select the COM port. This COM port should be the same as the COM port in the device manager of your computer The port is the same, the baud rate is set according to our program, and the duration of a data bit in our program is 1/9600 second, then the baud rate selected here is 9600, the parity bit is N, and the data bit is 8 , stop bit 1.
The essence of the serial debugging assistant is to use the UART communication interface on the computer to send data to our microcontroller, and it can also receive the data sent by our microcontroller to the debugging assistant interface.
Because of the first contact with communication technology, I will explain the following IO analog serial communication program. You can read the program while reading my explanation, and thoroughly understand the underlying principle first.
uart module introduction
The IO port simulates serial communication, so that everyone can understand the essence of serial communication, but our microcontroller program needs to constantly detect and scan the data received by the IO port of the microcontroller, which takes up a lot of the running time of the microcontroller. At this time, some smart people will think, in fact, we are not very concerned about the process of communication, we only need a result of communication, and finally get the received data. In this way, we can make a hardware module inside the single-chip microcomputer, and let it automatically receive data. After receiving the data, just notify us. There is such a UART module inside our 51 single-chip microcomputer. The special function registers are configured.
The structure of the UART serial port of the 51 single-chip microcomputer is composed of three parts: the serial port control register SCON, the sending and receiving circuits. Let's first understand the serial port control register SCON.
For the four modes of the serial port, mode 1 is the most commonly used, which is the 1-bit start bit, 8-bit data bit and 1-bit stop bit we mentioned earlier. Next, we will introduce the working details and usage of mode 1 in detail. As for the other three modes, they are similar to this. When you really need to use it, you can check the relevant information.
When we use the IO port to simulate serial communication, the baud rate of the serial port is reflected by the interrupt of timer T0. In the hardware serial port module, there is a special baud rate generator used to control the speed of sending and receiving data. For the STC89C52 microcontroller, this baud rate generator can only be generated by timer T1 or timer T2, but not by timer T0, which is a completely different concept from our simulated communication.
If you use timer 2, you need to configure additional registers. The default is to use timer 1. We mainly use timer T1 as the baud rate generator to explain the content of this chapter. The baud rate generator in mode 1 must use timing. Mode 2 of the timer T1, that is, the automatic reload mode, the calculation formula of the reload value of the timer is:
UART serial program:
In general, the basic steps for us to write a serial communication program are as follows:
1. Configure the serial port as mode 1.
2. Configure timer T1 as mode 2, that is, automatic reload mode.
3. Calculate the initial values of TH1 and TL1 according to the baud rate. If necessary, use PCON to double the baud rate.
4. Open the timer control register TR1 and let the timer run.
Special attention should be paid here, that is, when using T1 as the baud rate generator, do not enable the interrupt of T1.
Let's first take a look at the program code when the IO port simulates serial communication directly to using the hardware UART module, and see if the program is much simpler, because most of the work is done by the hardware module for us. The program function is exactly the same as the IO port simulation.
UART serial cable
Serial communication cable
UART serial communication cable
USB to serial communication cable
While there are many factors to consider when designing an overall system, terminal blocks are an optimal solution for complex electrical system connections. With a variety of color options and configurations, Premier Cable’ terminal blocks offer a range of options to meet your design challenges.
CAN-bus has been widely used in various automation control systems. For example, CAN-bus has incomparable advantages in various fields such as automotive electronics, automatic control, smart buildings, power systems, and security monitoring.
M12 encoding types are A encoding, B encoding, D encoding and X encoding. A-code, B-code and X-code are some of the earliest developed and longest-available M12 connectors. The latest M12 coding types currently under development are K coding for AC and L coding for PROFINET DC.
The three different sizes of NMEA 2000 certified DeviceNet standard cabling are "micro," "mid," and "mini."
The Premier Cable Starter Kits provide everything you need to get to create a basic NMEA 2000 network from scratch.
DeviceNet_network is a fieldbus network protocol based on Controller Area Network (CAN). In the DeviceNet network, the terminal resistor plays the role of compensation and protection for signal transmission. The function of the terminal resistor is to eliminate signal reflection and interference and ensure the signal transmission quality.