D7 | D6 | < p>D5 | D4 | D3 | D2 | D1 | < p>D0 |
SM0 | SM1 | SM2 | REN | TB8 | RB8 | TI | RI |
9FH | 9EH | 9DH | 9CH | 9BH | 9AH | 99H | 98H |
< /table> Working mode (SM0 SM1)
(1) Mode 0 (SM0 SM1 :0 0): Serial port working mode 0 is shift register I/O mode, which can be connected externally Shift register, an expansion I/O port, can also be connected to an external synchronous I/O device. Sending operation: When a "MOVSBUF, A" instruction is executed, the sending operation is started, the shift pulse is output by TXD, and the data in SBUF is serialized by RXD. Automatically set TI=1 after sending 8-bit data. Request interrupt. To continue sending, TI must be cleared by an instruction. Receiving operation: REN is the serial port receiving permission control bit. When REN=0, reception is prohibited; when REN=1, reception is allowed. When the software sets REN to "1", it starts to input data from the RXD port at the fosc/12 baud rate. When 8 bits of data are received, the interrupt flag RI is set to "1". Before receiving data again, RI must be cleared to 0 by software.
(2) Mode 1 (SM0 SM1: 01): The serial port is a 10-bit universal asynchronous interface. Sending or receiving a frame of data information is 10 bits, including 1 start bit "0", 8 data bits, and 1 stop bit "1". Sending data: Data is output from the TXD port. When data is written into the sending buffer SBUF, the transmitter is started to send. After sending a frame of data, set the interrupt flag TI=1, apply for an interrupt, and notify the CPU that the next data can be sent. Receiving data: first make REN=1 (allowing to receive data), the serial port receives data from RXD, when the sampling transitions from 1 to 0, confirm that it is the start bit "0", and then start to receive a frame of data. When finishing a frame of data, set the interrupt flag RI=1, apply for an interrupt, and notify the CPU to take the received data from SBUF.
(3) Mode 2 (SM0 SM1: 10): The serial port is an 11-bit asynchronous communication interface. Sending or receiving a frame of information includes 1 start bit "0", 8 data bits, 1 programmable bit, and 1 stop bit "1". Sending data: Before sending, according to the communication protocol, the software sets TB8 to "parity bit" or "data identification bit", and then writes the data to be sent into SBUF to start the transmitter. The sending process is started by executing any instruction with SBUF as the destination register, load 8 bits of data into SBUF, and at the same time load TB8 to the 9th bit of the transmit shift register, and then from TXD (P3.1) The port outputs one frame of data. Receiving data: first set REN=1 to enable the serial port to receive status, and at the same time clear RI to "0". Then, according to the status of SM2 and the status of the received RB8, it is determined whether the serial port will set RI=1 after the information arrives, and apply for an interrupt to notify the CPU to receive data. When SM2=0, regardless of whether RB8 is “0” or “1”, set RI=1, and this serial port will receive the information sent. When SM2=1 and RB8=1, it means that in the case of multi-machine communication, the received information is an "address frame". At this time, set RI=1, and the serial port will receive the address sent. When SM2=1 and RB8=0, it means that in the case of multi-machine communication, the received information is a "data frame", but it is not sent to the slave. At this time, RI is not set to "1", so it is received in SBUF The data frame will be lost.
(4) Mode 3 (SM0 SM1 :1 1): It is an 11-bit asynchronous communication mode with variable baud rate. Except for the difference in baud rate, the other modes are the same as Mode 2 .
Each control bit
SM0, SM1
——Serial port working mode control bit
SM0 | SM1 | < p>Working method | Function | Baud rate |
0 | 0 | Method 0 | 8-bit synchronous shift register | Crystal frequency/12 |
0 | 1 | Method 1 | 10 Bit UART | variable |
1 | 0 | Method 2 | < td width="157">11-bit UART
Crystal frequency/64 or Crystal frequency/32 | < /tr>
1 | 1 | Method 3 | 11-bit UART | Variable |
SM2
——Multi-machine communication control bit
Multi-machine communication works in mode 2 and mode 3. The SM2 bit 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 bit of data (RB8) is 1 will the received first 8 bits of data be sent to SBUF and set Bit RI issues an interrupt request, otherwise the received data will be discarded. When SM2=0, regardless of whether the 9th bit is 0 or 1, the data will be sent to SBUF and an interrupt request will be issued.
When working in mode 0, SM2 must be 0.
REN
——Reception allowed bit
REN is used to control the permission and prohibition of data reception, when REN=1, reception is allowed, when REN=0 , Reception is prohibited.
TB8
——Send data bit 8
In mode 2 and mode 3, TB8 is to be sent—that is, the 9th data bit. In multi-machine communication, this bit must also be transmitted, and it represents the transmitted address or data. TB8=0 is data, and TB8=1 is address.
RB8
——Receive data bit 8
In mode 2 and mode 3, RB8 stores the 9th bit of data received to identify the reception Data characteristics.
TI
——Send interrupt flag bit
Addressable flag bit. In mode 0, after sending the 8th bit, it is set by hardware. In other modes, it is set by hardware before sending or stop bit. Therefore, TI=1 means the end of frame transmission, and TI can be cleared to “0” by software.
RI
——Receive interrupt flag bit
Addressable flag bit. After receiving the 8th bit of data, this bit is set by hardware. In other working modes, this bit is set by hardware. RI=1 means the frame reception is completed.
In the serial port interrupt processing, TI and RI need to be cleared to "0" by software. After the hardware is set, it is impossible to automatically clear to 0. In addition, ES=0 is required to prevent The interruption occurs.