Computer serial communication Basics

Source: Internet
Author: User

With the wide application of multi-microcomputer systems and the popularization of computer network technology, computer communication functions become more and more important.Computer communication refers to information exchange between a computer and an external device or between a computer and a computer.
CommunicationParallel Communication and serial communicationTwo methods. Information exchange in Multi-microcomputer systems and modern measurement and control systems mostly adopts serial communication.

Computer CommunicationIt is a combination of computer technology and communication technology to exchange information between a computer and an external device or between a computer and a computer. It can be divided into two categories: Parallel Communication and serial communication.
Parallel CommunicationGenerally, data bytes are transmitted simultaneously using multiple data lines.

Parallel CommunicationSimple Control and fast transmission speed; due to the large number of transmission lines, long-distance transmission is costly and it is difficult for the receiver to receive data at the same time.

Serial CommunicationThe data is transmitted one by one on a transmission line in the form of dividing data bytes into one digit.

Serial CommunicationFeatures: Low transmission lines, low cost for long-distance transmission, and the use of telephone networks and other ready-made devices, but data transmission control is more complex than parallel communication.

Basic concepts of Serial Communication

I. asynchronous communication and synchronous communication
1. Asynchronous Communication
Asynchronous CommunicationCommunicationThe sender and receiver use their respective clocks.Controls the process of sending and receiving data. In order to make the sending and receiving coordination between the two parties, the clock of the sending and receiving devices should be as consistent as possible.

Asynchronous CommunicationYesTransmitted in units of characters (frames,The gap (time interval) between characters is arbitrary, but each character is transmitted at a fixed time, that is, the characters do not necessarily have an integer multiple of the "bit interval",Within the same characterThe distance between each other is an integer multiple of the "bit interval.

Asynchronous Communication Data Format:

Features of Asynchronous Communication: Do not require strict consistency between the sending and receiving sides of the clock, implementation is easy, the device overhead is small, but each character must be appended 2 ~ The three bits are used for the starting and ending bits, and there are still intervals between frames, so the transmission efficiency is not high.

2. synchronous communication
During synchronous communication, a sender's clock must be set up to directly control the receiver's clock so that both parties can achieve full synchronization. At this time, the distance between the bit transmitted data is an integer multiple of the bit interval. At the same time, there is no gap between the transmitted characters, that is, the bit synchronization relationship is maintained, and the character synchronization relationship is also maintained. The sender can synchronize the receiver in two ways.

In this case, data blocks are treated as data streams, and sequence 01111110 is used as the start and end signs. To avoid confusion caused by sequence 01111110 in the data stream, the sender always inserts an additional 0 in each of the five consecutive 1 s in the data stream it sends; when the receiver detects 5 consecutive 1 s and then returns 0 s, the 0 s are deleted.
Typical bit-oriented synchronization protocols such as ISO's Advanced Data Link Control Procedure HDLC and IBM's synchronization Data Link Control Procedure SDLC.
Synchronous CommunicationIt is characterized by a specific bit combination of "01111110" as the start and end signs of the frame. The transmitted frame data can be any bit. Therefore, the transmission efficiency is high, but the hardware is more complex than the asynchronous communication.

Ii. Transmission Direction of Serial Communication
1. Ticket
The ticket is for exponential data transmission.Only one direction, Cannot implement reverse transmission.
2. Half Duplex
Half Duplex is exponential Data TransmissionIt can be in two directions.
3. Full Duplex
Full Duplex is exponential data availableBidirectional transmission at the same time.

Iii. signal modulation and demodulation
ExploitationModulator)SetDigital signal to analog signalAnd then sent to the communication line, and then the demodulator receivesAnalog Signal to Digital Signal. Because the communication is bidirectional, the modem and the modem are merged into one device, which is the modem.

Iv. Serial communication error Verification
1. Parity
When sending data, the first digit trailing by the Data bit is the parity bit (1 or 0 ). During odd verification, the sum of the number of "1" in the data and the number of "1" in the check bit should be an odd number. During even verification, in the data, the sum of the number of "1" and the number of "1" of the check bit should be an even number. When receiving characters, check the number of "1". If any inconsistency is found, it indicates an error occurred during data transmission.
2. Code and Verification
Code and verification means that the sender sums the sent data blocks (or each byte excludes or), generates a byte verification character (verification) and attaches it to the end of the data block. When receiving data, the receiver sums (or each byte excludes or) the data block (except the validation byte) and compares the obtained result with the sender's "checksum", no error occurs, otherwise, an error occurs during transmission.
3. cyclic redundancy check
This type of verification is a mathematical operation to realize cyclic verification between valid information and check bit. It is often used for the transmission of disk information and the Integrity Verification of the storage area. This verification method has strong error correction capability and is widely used in synchronous communication.

V. Transfer Rate and transmission distance
1. Transmission Rate
Bit RateYesNumber of digits that transmit binary code per secondUnit: bit/second (BPS ). For example, if 240 characters are transferred per second, and each character format contains 10 characters (one start bit, one stop bit, and eight data bits), the bit rate is:
10 bits x 240/S = 2400 bps
2. Relationship between transmission distance and transmission rate
The maximum distance between serial information bit streams directly transmitted by a serial interface or terminal is related to the transmission rate and the electrical characteristics of the transmission line. When the transmission line uses a non-balanced shielded twisted pair with a 50pf capacitor every 0.3 m (about 1 feet), the transmission distance decreases as the transmission rate increases. When the bit rate exceeds 1000 BPS, the maximum transmission distance decreases rapidly. For example, when the bit rate exceeds 9600 bps, the maximum distance decreases to only 76 m (about 250 feet ).

Serial Communication Interface Standard
1. RS-232C Interface
RS-232C is EIA (American Electronics Industry Association) 1969 revision of RS-232C standards. The RS-232C defines the physical interface standard between the data terminal device (DTE) and the data communication device (DCE.
1. Mechanical Properties
The RS-232C interface requires the use of 25-pin connectors, the connector size and the arrangement position of each pin are clearly defined. (Yangtou)

 

 

6. Problems of using RS-232C Interface
1. Short Transmission Distance and low transmission rate
The RS-232C bus standard is limited by the allowable capacitance value, the transmission distance is generally not more than 15 meters (line conditions are not more than dozens of meters ). The maximum transfer rate is 20 kbps.
2. horizontal offset
RS-232C bus standards require both sides to send and receive co-location. When the communication distance is large, the potential difference between the receiving and receiving sides is great. There is a large local current at the signal location and the voltage drop is generated.
3. Poor anti-interference capability
The RS-232C uses single-ended input and output during level conversion when interference and noise are mixed in normal signals during transmission. In order to improve the signal-to-noise ratio, RS-232C bus standards have to adopt relatively large voltage swing.

2. RS-422A Interface

RS-422AThe output drive is a dual-ended balanced drive.. If one line is in the logical "1" state, the other line is in the logical "0" state, which is twice the voltage magnification when the single-ended unbalanced drive is used.Differential CircuitIt can pick up valid signals from ground interference, and the difference receiver can distinguish the potential difference of more than 200mV. If interference and noise are mixed in the transmission process, the interference and noise can be offset by the difference amplifier. Therefore, the effects of ground interference and electromagnetic interference can be greatly reduced or avoided.RS-422A transfer rate (90 Kbps), transmission distance up to 1200 meters.

RS-485 is a point to multi-point communication interface, generally usedTwisted Pair wires. Ordinary PC generally does not contain RS485 interface, so use RS-232C/RS-485 converter. For single-chipMaxcomputeTo complete the level conversion of TTL/RS-485. In the RS-485 communication system composed of computer and single chip microcomputer, the lower computer is composed of single chip microcomputer system, the upper computer is a general PC, is responsible for monitoring the operation status of the lower computer, and the State Information of the centralized processing, displays the working status of the lower computer and the working status of the controlled equipment on the industrial site in graphic mode. In the system, the identification of each node (including the upper computer) is achieved by setting different station addresses.

Serial Port of 80C51

80C51 serial port structure

There are two physically independent receiving and sending buffer sbuf, which occupy the same address 99 h; the receiver is a dual Buffer structure; the sending buffer, because the CPU is active during sending, there will be no overlap errors.

Control Register of 80C51 serial port

SM2, multi-host communication control bit, Mainly used in methods 2 and 3. WhenWhen the receiver's SM2 is 1, you can use the received rb8 to control whether to activate the RI.(If rb8 is set to 0, the Ri is not activated and the received information is discarded. If rb8 is set to 1, the received data enters sbuf and the RI is activated. In this way, data is read from sbuf In the interrupted service ).When SM2 is 0, no matter the received rb8 is 0 and 1, the received data can be transferred to sbuf and the RI is activated.(In this case, rb8 does not have the function to control Ri activation ). By controlling SM2, you can achieve multi-host communication.
When the mode is 0, SM2 must be 0. In method 1, if SM2 is 1, Ri is set to 1 only when valid Stop bits are received.
Ren, allows serial receiving bits. If the software sets Ren = 1, the serial port is started to receive data. If the software sets Ren = 0, receiving is prohibited.
Tb8, in method 2 or method 3, is the ninth digit for sending data, You can use software to define its role. It can be used as the parity bit of the data, or as the flag of the address frame/data frame in multi-machine communication.
In Methods 0 and 1, this bit is not used.
Rb8, in method 2 or method 3, is the ninth bit of received dataAs the parity bit or the flag of the address frame/data frame. In method 1, if SM2 = 0, rb8 is the stop bit received.
Ti, sending interrupt flag. When the method is 0, when the serial sending of 8th-bit data ends, or in other ways, when the serial sending Stop bit starts, the internal hardware enables Ti to set 1 and sends an interrupt request to the CPU. In the interrupt service program, the software must be cleared to 0, cancel the interrupt application.
Ri, receiving interrupt flag. When the method is 0, when the serial receiving of 8th-bit data ends, or in other ways, when the serial receiving of the stop bit is in the middle, the internal hardware sets Ri to 1 and sends an interrupt request to the CPU. In the interrupt service program, you must clear it with the software to cancel the interrupt application.

Only one smod in pcon is related to the serial port operation:

Smod (pcon.7) baud rate multiplier bit. When the serial port type is 1, 2, or 3, the baud rate is related to smod. When the smod is 1, the baud rate is doubled. When reset, smod = 0.

Working Method of 80C51 serial port

Ii. method 1
Method 1 is a 10-bit asynchronous communication port. Txd is the data transmission pin, rxd is the data receiving pin, and transmits a frame of data format. One start bit, eight data bits, and one stop bit.

2. Input in method 1

When the software sets Ren to 1, the receiver samples the rxd pin level at the 16th rate of the selected baud rate. If the input level of the rxd pin is detected to have a negative hop, the start position is valid, move it into the input shift register and start receiving the rest of the frame information. During the receiving process, the data is moved from the right of the input shift register. When the start bit is moved to the far left of the input shift register, the control circuit performs the last shift. When rI = 0 and SM2 = 0 (or the halted bits are 1), load the first eight bits of the received 9 bits into the receiving sbuf, 9th bits (Stop bits) Enter rb8, and the parallel Ri is 1. The request to the CPU is interrupted.

Iii. method 2 and method 3
Asynchronous Communication Port for 11-bit data in Mode 2 or 3. Txd is the data transmission pin, and rxd is the data receiving pin.

When Mode 2 and Mode 3 are used, the start position is one bit, the data is nine bits (including the 9th bits appended with one bits, the tb8 in scon when sending, And the rb8 when receiving), and the stop position is one bits, one frame of data is 11 bits. The baud rate of method 2 is fixed to 1/64 or 1/32 of the crystal oscillator frequency. The baud rate of method 3 is determined by the overflow rate of the timer T1.

Iv. Calculation of baud rate
In serial communication, the receiving and receiving sides must specify the rate at which data is sent or received. The software can program the single-chip microcomputer serial port as four working modes. The baud rate of Mode 0 and Mode 2 is fixed, and the baud rate of Mode 1 and Mode 3 is variable, it is determined by the overflow rate of timer T1.
Correspond to the four working modes of the serial portThree baud rates. Since the input shift clock comes from different sources, the baud rate calculation formulas for different methods are also different.
Method 0 baud rate = fosc/12
Method 2 baud rate = (2 smod/64) · fosc
Method 1 baud rate = (2 smod/32) · (T1 overflow rate)
Method 3 baud rate = (2 smod/32) · (T1 overflow rate)

When T1 is used as the baud rate generator, the most typical usage is to enable T1 to work in the 8-bit Timer mode that is automatically loaded (that is, Mode 2, and tcon tr1 = 1, to start the timer ). The overflow rate is determined by the Count value in Th1.
T1 overflow rate = fosc/{12 × [256-(Th1)]}
In the application of single-chip microcomputer, the commonly used crystal oscillator frequencies are 12 MHz and 11.0592 MHz. Therefore, the baud rate is relatively fixed. The Common Serial Port baud rate and the relationship between parameters are shown in the table.

Before the serial port works, it should be initialized. It is mainly set to generate a baud rate timer 1, serial port control and interrupt control. The procedure is as follows:
A. OKHow T1 works(Programming tmod registers );
B. ComputingInitial Values of T1, Load Th1 and TL1;
C,Start T1(Tr1 bit in programming tcon );
D. OKSerial Port Control(Programming scon registers );
When the serial port is interrupted, it must be interrupted (programming IE, IP register ).

Example of single chip microcomputer serial port application

In a computer-based measurement and control system, data is often transmitted through serial communication. The serial port of 80C51 single-chip microcomputer provides extremely convenient conditions for communication between computers. The serial port of single-chip microcomputer can also be used to conveniently expand the keyboard and display, which is very convenient for simple applications. This section only describes the application of single-chip microcomputer serial port in communication.

I. Point-to-point communication
1. Hardware connection

2. multi-host communication
1. Hardware connection

The multi-host system composed of single-chip microcomputer usually adopts a bus-type master-slave structure. The so-called master-slave model, that is, one of several single-chip computers is the host, and the other is the slave machine, which must be subject to the scheduling and control of the host. The serial port Mode 2 and Mode 3 of 80C51 single-chip microcomputer are suitable for the master-slave communication structure. Of course, when different communication standards are adopted, the corresponding level conversion is also required, and sometimes the signal must be photoisolated. In the actual multi-machine application system, RS-485 serial standard bus is often used for data transmission.

2. Communication Protocol
A. All the slave machine's SM2 position 1 is in the Receiving address frame state.
B. The host sends an address frame, where 8 bits are addresses and 9th bits are the identification mark of addresses/data. This position 1 indicates that this frame is an address frame.
C. After the slave receives the address frame, the received address is compared with the address of the local host. For the slave with the same address, make your SM2 position 0 (to receive the data frame subsequently sent from the host), and send the address back to the host as a response; for the slave with the same address, keep SM2 = 1 and ignore the data frame sent from the host later.
D. After sending data from the slave machine, send a frame checksum and set the 9th-bit (tb8) to 1 as the mark of the end of transmitting data from the slave machine.
E. When the host receives data, it first determines the data receiving mark (rb8). If rb8 = 1, it indicates that the data transmission is completed and the frame checksum is compared. If it is correct, the correct signal is sent back for 00 H, this signal command resets the slave machine (that is, waiting for the address frame again); If the checksum fails, it sends 0ffh, and the command resends data from the slave machine. If the received frame's rb8 = 0, save the data to the buffer and prepare to receive the next frame information.
F. After the host receives the reply address from the slave machine, check whether the address matches. If the address does not match, send a reset signal (tb8 = 1 in the data frame). If the address matches, clear tb8, start sending data.
G. After receiving the reset command from the slave machine, return to the listening address (SM2 = 1 ). Otherwise, the system starts receiving data and commands.

3. Applications
A. The host sends the address contact signals: 00 h, 01 H, 02 h ,... ... (That is, the address of the slave device), the address FFH is the command for each slave machine reset, that is, the recovery SM2 = 1.
B. The host command code is: 01 H. The host command receives data from the host; 02 h. The Host Command sends data from the host. The rest will be treated as 2 hours.

Rrdy = 1: indicates that the slave is ready to receive the message.
Trdy = 1: indicates that the slave is ready for sending.
Err = 1: indicates that the commands received from the server are invalid.
Programs are divided into host programs and slave programs. It is agreed that the data transmitted at a time is 16 bytes. Take the slave machine with the 01h address as an example.

Exercise:

1. When the host computer sends 1 to the microcontroller, the buzzer generates a sound at a frequency of MS, 2 at a frequency of MS, 3 at a frequency of MS, and 4 at a time of idle buzzer.
2. Send any byte of data from the computer at 2400bps. When the MCU receives the data, it adds a serial number before the data and sends it together with the data to the computer. When the serial number exceeds 255, it returns to zero.
3. Send any number between 0 and in hexadecimal notation. When the MCU dynamically displays it on the digital tube, the baud rate is customized.
4. Use ad to collect analog signals at a 1Hz frequency, convert them into numbers, and send them to the computer at a 1200bps for display on the computer.
5. press the first line of the matrix keyboard to send with ipvbps, and the second line to send with 2400bps, and the third line to send with BPS, and the fourth line to send with 9600pbs, 15, 16.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.