STC89C52 series microcontroller internal resources - serial communication

Publisher:EtherealEssenceLatest update time:2020-04-26 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Computer communication is the combination of computer technology and communication technology to complete the information exchange between computers and external devices or between computers. It can be divided into two categories: parallel communication and serial communication.


Parallel communication usually involves transmitting each bit of a data byte simultaneously using multiple data lines.

Parallel communication has simple control and fast transmission speed; however, due to the large number of transmission lines, the cost is high for long-distance transmission and it is difficult for all receivers to receive simultaneously.


Serial communication is the process of dividing data bytes into bits and transmitting them one by one on a transmission line.

Characteristics of serial communication: fewer transmission lines, low cost for long-distance transmission, and the ability to utilize existing equipment such as telephone networks, but data transmission control is more complex than parallel communication.


Basic concepts of serial communication

1. Asynchronous communication and synchronous communication

1. Asynchronous communication

Asynchronous communication refers to the process in which the sending and receiving devices use their own clocks to control the sending and receiving of data. In order to coordinate the sending and receiving of both parties, the clocks of the sending and receiving devices are required to be as consistent as possible.

Asynchronous communication is transmitted in units of characters (frames). The gaps (time intervals) between characters are arbitrary, but each bit in each character is transmitted at a fixed time. That is, the characters are not necessarily related by an integer multiple of the "bit interval", but the distances between each bit in the same character are all integer multiples of the "bit interval".


Data format for asynchronous communication:

Characteristics of asynchronous communication: It does not require strict consistency of the clocks of the sender and receiver, is easy to implement, and has low equipment overhead, but each character must be added with 2 to 3 bits for start and end bits, and there are intervals between frames, so the transmission efficiency is not high.


2. Synchronous communication

In synchronous communication, the sender's clock directly controls the receiver's clock so that both parties are completely synchronized. At this time, the distance between the bits of the transmitted data is an integer multiple of the "bit interval", and 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 synchronization of the sender and the receiver can be achieved in two ways.  

External synchronization Self-synchronization


Serial communication transmission direction


Simplex means that data can only be transmitted in one direction and reverse transmission is not possible.

Half-duplex means that data transmission can be carried out in both directions, but it needs to be done in time.

Full-duplex means that data can be transmitted in both directions simultaneously.

Common error checking in serial communication 


1. Parity check

When sending data, the 1 bit following the data bit is the parity bit (1 or 0). When odd parity is used, the sum of the number of "1"s in the data and the number of check bits "1" should be an odd number; when even parity is used, the sum of the number of "1"s in the data and the number of check bits "1" should be an even number. When receiving characters, the number of "1"s is checked. If it is inconsistent, it means that an error has occurred during the data transmission process.

2. Code and verification

The code and checksum is that the sender sums the data block (or XORs each byte) to generate a one-byte checksum character (checksum) and appends it to the end of the data block. The receiver receives the data and sums the data block (except the check byte) (or XORs each byte), and compares the result with the sender's "checksum". If they match, there is no error. Otherwise, it is considered that an error occurred during the transmission process.

3. Cyclic Redundancy Check

This type of verification is a cyclic verification between valid information and check bits through some mathematical operations, which is often used for transmission of disk information, integrity verification of storage areas, etc. This verification method has strong error correction capability and is widely used in synchronous communications.


Transmission rate

The bit rate (baud rate) is the number of bits of binary code transmitted per second, and the unit is: bit/second (bps). For example, if 240 characters are transmitted per second, and each character format contains 10 bits (1 start bit, 1 stop bit, 8 data bits), the bit rate (baud rate) at this time is: 10 bits × 240/second = 2400 bps


89C52 serial port

89C52 serial port structure

There are two physically independent receive and transmit buffers SBUF, which occupy the same address 99H; the receiver is a double buffer structure; the transmit buffer, because the CPU is active during transmission, will not cause overlap errors.


89C52 serial port control register

SM0 and SM1 are the working mode selection bits, and four working modes can be selected:

SM2, multi-machine communication control bit, is mainly used in mode 2 and mode 3. When the receiver's SM2=1, the received RB8 can be used to control whether to activate RI (when RB8=0, RI is not activated and the received information is discarded; when RB8=1, the received data enters SBUF and activates RI, and then the data is read from SBUF in the interrupt service). When SM2=0, regardless of whether the received RB8 is 0 or 1, the received data can enter SBUF and activate RI (that is, RB8 does not have the function of controlling RI activation at this time). By controlling SM2, multi-machine communication can be achieved. In mode 0, SM2 must be 0. In mode 1, if SM2=1, RI is set to 1 only when a valid stop bit is received.

REN, serial reception enable bit. If software sets REN=1, the serial port starts receiving data; if software sets REN=0, reception is disabled.

TB8, in mode 2 or mode 3, is the ninth bit of the transmitted data, and its function can be defined by software. It can be used as the parity bit of the data, or as the flag bit of the address frame/data frame in multi-machine communication. In mode 0 and mode 1, this bit is not used.

RB8, in Mode 2 or Mode 3, is the ninth bit of the received data and serves as a parity bit or a flag bit for the address frame/data frame. In Mode 1, if SM2 = 0, RB8 is the received stop bit.

TI, transmit interrupt flag. In mode 0, when the serial transmission of the 8th bit of data ends, or in other modes, when the serial transmission of the stop bit begins, the internal hardware sets TI to 1 and sends an interrupt request to the CPU. In the interrupt service program, it must be cleared to 0 by software to cancel the interrupt request.

RI, receive interrupt flag. In mode 0, when the serial reception of the 8th bit of data is completed, or in other modes, when the serial reception is in the middle of the stop bit, the internal hardware sets RI to 1 and sends an interrupt request to the CPU. It must also be cleared to 0 by software in the interrupt service routine to cancel this interrupt request.

There is only one SMOD in PCON that is related to the serial port operation:

SMOD (PCON.7) Baud rate multiplication bit. In serial port mode 1, mode 2, mode 3, the baud rate is related to SMOD. When SMOD=1, the baud rate is doubled. When reset, SMOD=0.


How the serial port works

1. Method 0

In mode 0, the serial port is the input and output mode of the synchronous shift register. It is mainly used to expand the parallel input or output port. Data is input or output by the RXD (P3.0) pin, and the synchronous shift pulse is output by the TXD (P3.1) pin. Both sending and receiving are 8-bit data, with the low bit first and the high bit later. The baud rate is fixed to fosc/12.   

1. Mode 0 output:

2. Mode 0 input:

Method 1

Mode 1 is an asynchronous communication port with 10-bit data. TXD is the data transmission pin, RXD is the data reception pin, and the format of transmitting a frame of data is shown in the figure. There is 1 start bit, 8 data bits, and 1 stop bit.  

1. Mode 1 output:

2. Method 1 input:

When REN is set to 1 by software, the receiver samples the RXD pin level at a rate 16 times the selected baud rate. When a negative jump is detected in the input level of the RXD pin, it means that the start bit is valid, and it is shifted into the input shift register, and the remaining bits of this frame information are received. During the reception process, the data is shifted in from the right side of the input shift register. When the start bit is moved to the leftmost side of the input shift register, the control circuit performs the last shift. When RI=0 and SM2=0 (or the received stop bit is 1), the first 8 bits of the received 9-bit data are loaded into the receiving SBUF, the 9th bit (stop bit) enters RB8, and RI=1 is set to request an interrupt to the CPU.


Method 2 and Method 3:

In mode 2 or mode 3, it is an asynchronous communication port for 11-bit data. TXD is the data transmission pin, and RXD is the data reception pin.

In mode 2 and mode 3, the start bit is 1 bit, the data is 9 bits (including the 1-bit additional 9th ​​bit, which is TB8 in SCON when sending and RB8 when receiving), the stop bit is 1 bit, and a frame of data is 11 bits. The baud rate of mode 2 is fixed to 1/64 or 1/32 of the crystal frequency, and the baud rate of mode 3 is determined by the overflow rate of timer T1.


The difference between Mode 2 and Mode 3 is the baud rate selection method. In both modes, the received stop bit has nothing to do with SBUF, RB8 and RI.


Baud rate calculation

In serial communication, the sender and receiver must agree on the rate at which data is sent or received. The microcontroller serial port can be programmed into four working modes through software, of which mode 0 and

[1] [2]
Reference address:STC89C52 series microcontroller internal resources - serial communication

Previous article:51 MCU Experiment 7: Matrix Buttons
Next article:51 MCU Experiment 11: Serial Communication

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号