The MCS-51 microcontroller has a full-duplex serial communication port inside, namely the serial receive and transmit buffer (SBUF). These two physically independent receivers and transmitters can both receive and transmit data. However, the receive buffer can only read but not write, while the transmit buffer can only write but not read. Their addresses are 99H. This communication port can be used for network communication, serial asynchronous communication, and synchronous shift register. If a level converter is added to the input and output pins of the transmission port, a standard RS-232 interface can be easily formed. Let's introduce them separately.
[1]. Basic concepts
Data communication transmission mode Commonly used data communication transmission modes include simplex, half-duplex, full-duplex and multiplex.
Simplex mode: Data is transmitted in only one fixed direction. Therefore, this transmission mode has limited uses and is often used for serial port printing data transmission and data collection between simple systems.
Half-duplex mode: Data can be transmitted in both directions, but not simultaneously. In actual applications, a certain protocol is used to implement the receive/transmit switch conversion.
Full-duplex mode: allows both parties to transmit data in both directions at the same time, but the lines and equipment of full-duplex transmission mode are generally more complicated.
Multiplexing mode: the above three transmission modes all use the same line to transmit a frequency signal. In order to make full use of line resources, you can use a multiplexer or multi-channel hub, and adopt frequency division, time division or code division multiplexing technology to achieve resource sharing on the same line. We call it multiplexing transmission mode.
Two forms of serial data communication Asynchronous communication In this communication mode, the receiver and transmitter have their own clocks, and their work is asynchronous. Asynchronous communication uses a frame to represent a character, and its content is as follows: a start bit, followed by a number of data bits. Figure 2 is the data format for transmitting 45H.
Synchronous communication In the synchronous communication format, the transmitter and receiver are controlled by the same clock source. In order to overcome the asynchronous communication, each frame of characters must be added with a start bit and a stop bit, which takes up the transmission time. In situations where a large amount of data is required to be transmitted, the speed is much slower. The synchronous transmission mode removes these start bits and stop bits, and only sends a synchronization header (character) mark when transmitting a data block.
The synchronous transmission mode is faster than the asynchronous transmission mode, which is its advantage. However, the synchronous transmission mode also has its disadvantages, that is, it must use a clock to coordinate the work of the transceiver, so its equipment is also more complicated.
The transmission rate of serial data communication There are two concepts for serial data transmission rate, namely the number of bits transferred per second (bps ) and the number of symbols per second ( baud rate ). In communications with a modem, the baud rate is related to the modulation rate.
[2]. MCS-51 serial port and control registers
Serial port control registers
The structure of the serial port register of the MCS-51 single-chip microcomputer is shown in Figure 3. SBUF is the transceiver buffer of the serial port. It is an addressable special register, which contains the receiver and transmitter registers, and can realize full-duplex communication. However, these two registers have the same address (99H). The serial data transmission of MCS-51 is very simple. Data can be sent by writing data to the transmit buffer. Data can be received by reading data from the receive buffer.
In addition, it can be seen from the figure that a level of input shift register is added before the receive buffer. The purpose of this structure of MCS-51 is to avoid data frame overlap when receiving data to avoid errors. Some literature calls this structure a double buffer structure. However, this setting is not required when sending data, because the CPU is active when sending, and this phenomenon is impossible.
Serial Communication Control Register In the previous section, we have analyzed the SCON control register, which is an addressable special register used for serial data communication control. The unit address is 98H and its structure format is as follows:
Table 1 SCON register structure
SCON
D7
D6
D5
D4
D3
D2
D1
D0
SM0
SM1
SM2
REN
TB8
RB8
TI
RI
Bit Address
9F
9E
8DH
9CH
9BH
9AH
99H
98H
Below we introduce the functions of each control bit as follows: (1). SM0, SM1: serial port working mode control bits. SM0, SM1 working mode 00 mode 0 01 mode 1 10 mode 2 11 mode 3
(2). SM2: multi-machine communication control bit. Multi-machine communication works in mode 2 and mode 3. SM2 is mainly used in mode 2 and mode 3. In the receiving state, when the serial port works in mode 2 or 3 and SM2=1, only when the 9th data bit (RB8) is 1, the first 8 bits of data received are sent to SBUF, and RI is set to issue an interrupt request, otherwise the received data will be abandoned. When SM2=0, no matter whether the 9th data bit is 0 or 1, the data is sent to SBUF and an interrupt request is issued. When working in mode 0, SM2 must be 0.
(3). REN: enable reception bit. REN is used to control the permission and prohibition of data reception. When REN=1, reception is allowed, and when REN=0, reception is prohibited.
(4). TB8: Send/receive data bit 8. In mode 2 and mode 3, TB8 is to be sent, that is, the 9th data bit. This bit is also transmitted in multi-machine communication, and it represents the address or data to be transmitted. When TB8=0, it is data, and when TB8=1, it is address.
(5). RB8: Receive data bit 8. In mode 2 and mode 3, RB8 stores the 9th received data bit to identify the characteristics of the received data.
(6). TI: Transmit interrupt flag. Addressable flag. In mode 0, it is set by hardware after the 8th data bit is sent. In other modes, it is set by hardware before the send or stop bit. Therefore, TI=1 indicates that the frame transmission is completed, and TI can be cleared to "0" by software.
(7). RI: Receive interrupt flag. Addressable flag. After the 8th data bit is received, this bit is set by hardware. In other working modes, this bit is set by hardware. RI=1 indicates that the frame reception is completed.
Power management register PCON PCON is mainly a special register set for power control of CHMOS type microcontroller. The unit address is 87H and its structure format is as follows:
Table 2 PCON power management register structure
PCON
D7
D6
D5
D4
D3
D2
D1
D0
Bit Notation
SMOD
-
-
-
GF1
GF0
PD
IDL
In CHMOS type microcontrollers, except for the SMOD bit, all other bits are dummy. SMOD is the serial port baud rate multiplication bit. When SMOD = 1, the serial port baud rate is doubled. The system reset defaults to SMOD = 0.
The interrupt enable register IE has been described in the previous section. Here we will reiterate the bit ES that affects the serial port. ES is the serial interrupt enable control bit. ES=1 enables serial interrupts, and ES=0 disables serial interrupts.
Table 3 IE interrupt enable control register structure