The serial port of the MCS-51 microcontroller can be used for network communication, serial asynchronous communication, and can also be used as a synchronous shift register. If a level converter is added to the input and output pins of the serial port, a standard RS-232 interface can be easily formed.
The serial interface of the MCS-51 microcontroller is a full-duplex communication interface. It has two physically independent receive and send buffers SBUF, which can send and receive data at the same time. However, the send buffer can only be written, not read; the receive buffer can only be read, not written. The two buffers share the same address (99H).
Basic concepts of data communication
The transmission modes commonly used in data communication are 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 printing data transmission of serial ports and data collection between simple systems.
- Half-duplex mode: Data can be transmitted in both directions, but not simultaneously. Actual applications use a certain protocol 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, resource sharing function can be achieved on the same line by using a multiplexer or multi-channel hub and adopting frequency division, time division or code division multiplexing technology.
According to the synchronization method, serial data communication has two forms, as shown in Figure 5-5.
- 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 a start bit, followed by several data bits.
- Synchronous communication. In the synchronous communication format, the transmitter and receiver are controlled by the same clock source. In asynchronous communication, each frame of characters must be added with a start bit and a stop bit, which takes up the transmission time. If a large amount of data is required to be transmitted, the speed will be 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.
MCS-51 serial port control register
After completing the serial port initialization, when sending data, use the MOV SBUF, A instruction to write the data to be sent into SBUF, and the CPU will automatically start and complete the output of serial data; when receiving data, use the MOV A, SBUF instruction, and the CPU will automatically read the received data from SBUF.
There are two control registers for controlling the serial interface of the MCS-51 microcontroller - special function registers SCON and PCON, which are used to set the working mode of the serial port, the operating status of receiving/sending, the characteristics of receiving/sending data, the size of the data transmission rate, and the interrupt flag of operation. The format is as follows:
① Serial port control register SCON. The byte address of SCON is 98H, and the bit address (from low to high) is 98H to 9FH. The format of SCON is as follows:
SM0, SM1: Serial port working mode control bits.
00——Mode 0; 01——Mode 1;
10——Method 2; 11——Method 3.
SM2: Multi-machine communication control bit used only for mode 2 and mode 3.
Transmitter SM2 = 1 (requires program control setting).
When it is mode 2 or mode 3:
When the receiver SM2 = 1, if RB8 = 1, it can cause a serial reception interruption; if RB8 = 0, it will not cause a serial reception interruption. When SM2 = 0, if RB8 = 1, it can cause a serial reception interruption; if RB8 = 0, it can also cause a serial reception interruption.
- REN serial receive enable bit: 0 - disable reception; 1 - enable reception.
- TB8: In modes 2 and 3, TB8 is the 9th bit of data to be sent by the transmitter.
- RB8: In modes 2 and 3, RB8 is the 9th bit of data received by the receiver, which happens to come from TB8 of the transmitter.
- TI: Transmit interrupt flag. It must be cleared by software before transmission. During transmission, TI remains at zero level. After a frame of data is sent, it is automatically set to 1 by hardware. If you want to send again, you must clear it by software.
- RI: Receive interrupt flag. Before receiving, it must be cleared by software. During the receiving process, RI remains at zero level. After receiving a frame of data, it is automatically set to 1 by the on-chip hardware. If you want to receive again, you must clear it again by software.
② Power control register PCON. The byte address of PCON is 87H, without bit address, and its format is as follows:
PCON is added to realize power control on MCS-51 microcontrollers with CMOS structure. For MCS-51 series microcontrollers with HMOS structure, except for the 7th bit, the rest are dummy. The 7th bit is related to serial communication and is called SMOD. Its purpose is to double the data transmission rate.
SMOD: Data transmission rate doubling bit. When calculating the data transmission rate of serial modes 1, 2, and 3, 0 means no doubling; 1 means doubling.
The remaining valid bits are described below.
GF1, GF2: general flag bits.
PD: Power-off control bit, 0 indicates normal mode, 1 indicates power-off mode.
IDL: Idle control bit, 0 means normal mode, 1 means idle mode.
In addition to the above two control registers, the ES bit in the interrupt enable register IE is also used as the serial I/O interrupt enable bit. When ES = 1, serial I/O interrupts are enabled; when ES = 0, serial I/O interrupts are disabled. The PS bit in the interrupt priority register IP is used as the serial I/O interrupt priority control bit. When PS = 1, it is set to high priority; when PS = 0, it is set to low priority.
Way of working
The MCS-51 microcontroller can specify four serial port operation modes by setting SM0 (SCON.7) and SM1 (SCON.6) in the serial port control register SCON through software. The serial port operation mode selection is shown in Table 5-2.
Table 5-2 Serial port operation mode selection table
SM0 SM1 |
model |
Function |
Baud rate |
0 0 |
0 |
Synchronous Shift Register |
fOSC/12 |
0 1 |
1 |
8-bit UART |
Variable (T1 overflow rate) |
1 0 |
0 |
9-bit UART |
fOSC/64 or fOSC/32 |
1 1 |
1 |
9-bit UART |
Variable (T1 overflow rate) |
Among them, fosc is the frequency of the oscillator, and UART is the abbreviation of universal asynchronous receiver and transmitter. The following is a further introduction to these four working modes. [page]
1. Method 0
When SM1 and SM0 are set to 00, the serial port works in mode 0, which is also called synchronous shift register output mode. In mode 0, data is serially output or input from the RXD (P3.0) terminal, and the synchronization signal is output from the TXD (P3.1) terminal. The data sent or received is 8 bits, with the low bit first and the high bit last, and there is no start bit and stop bit. The data transmission rate is fixed to 1/12 of the oscillator frequency, that is, one bit of data is transmitted per machine cycle. Mode 0 can be connected to an external shift register to expand the serial port to a parallel port, or it can be connected to an external synchronous input/output device.
Executing any register instruction with SBUF as the destination starts sending.
2. Method 1
When SM1 and SM0 are set to 01, the serial port works in mode 1. Mode 1 is an 8-bit asynchronous communication mode with variable data transmission rate, which is sent by TXD and received by RXD. One frame of data is 10 bits, 1 start bit (low level), 8 data bits (low bit first) and 1 stop bit (high level). The data transmission rate depends on the overflow rate of timer 1 or 2 (1/overflow cycle) and the selection bit SMOD of whether the data transmission rate is doubled.
For a microcontroller with timer/counter 2, when RCLK and TCLK in the T2CON register are set, timer 2 is used as the data rate generator for receiving and sending, and when RCLK=TCLK=0, timer 1 is used as the data rate generator for receiving and sending. The two can also be used crosswise, that is, different data rates are used for sending and receiving.
Similar to Mode 0, the transmission process is initiated by executing any register instruction with SBUF as the destination.
3. Method 2
When SM0 and SM1 are set to 10, the serial port works in mode 2. At this time, the serial port is defined as a 9-bit asynchronous communication interface. This mode can receive or send 11 bits of data, with 11 bits as a frame. One data bit is added compared to mode 1, and the rest is the same. The 9th data, namely D8, is used as parity check or address/data selection. It can be controlled by software. In addition, the SM2 bit in the special function register SCON can make the MCS-51 microcontroller serial port suitable for multi-machine communication. When sending, the 9th data bit is TB8, and when receiving, the 9th data bit is sent to RB8. The data transmission rate of mode 2 is fixed, and there are only two options, 1/64 or 1/32 of the oscillation rate, which can be selected by the highest bit of PCON.
4. Method 3
When SM0 and SM1 are set to 11, the serial port works in mode 3. Mode 3 is similar to mode 2, the only difference is that the data transmission rate of mode 3 is variable. The frame format is the same as mode 2, with 11 bits per frame. Therefore, mode 3 is also suitable for multi-machine communication.
Determining the data transmission rate
The number of bits sent (or received) per second by the serial port is the data transfer rate.
For mode 0, the data transmission rate is fixed to fosc/12. The data transmission rate varies with the frequency of the external crystal oscillator. Commonly used fosc are 12MHz and 6MHz, so the data transmission rates are 1000×103 and 500×103bit/s respectively. In this mode, data will be automatically sent/received at a fixed data transmission rate without any setting.
For mode 2, the data transmission rate is calculated as 2SMOD·fosc/64. When SMOD=0, the data transmission rate is fm/64; when SMOD=1, the data transmission rate is fosc/32. In this mode, after the program controls the state of the SMOD bit, the data transmission rate is determined and no other settings are required.
For mode 1 and mode 3, the data transfer rate is related to the overflow rate of timer 1. The overflow rate of timer 1 is:
Timer 1 overflow rate = Timer 1 overflow times/second
The data transmission rate calculation formula for mode 1 and mode 3 is:
2SMOD/32×T1 overflow rate
According to the different SMOD status bits, the data transmission rate has two types: T1/32 overflow rate and T1/16 overflow rate. Since the setting of T1 overflow rate is convenient, the selection of data transmission rate will be very flexible.
As mentioned before, timer T1 has four working modes. In order to obtain its overflow rate without entering the interrupt service program, T1 is often set to the operating state of working mode 2, that is, the mode of automatically adding 8-bit time constant.
Table 5-3 shows the common data transmission rate setting methods.
Table 5-3 Common data transmission rate setting methods
Data transfer rate/Hz |
fOSC/MHz |
SMOD |
Timer 1 |
||
C/T |
Way |
Reload Values |
|||
Mode 0 maximum: 1M Mode 2 Maximum: 375k Method 1, 3: 62.5k 19.2k 9.6k 4.8k 2.4k 1.2k 110 |
12 12 12 11.0592 11.0592 11.0592 11.0592 11.0592 12 |
X 1 1 1 0 0 0 0 0 |
X X 0 0 0 0 0 0 0 |
X X 2 2 2 2 2 2 1 |
X X FFH FDH FDH FAH F4H E8H 0FEEH |
Serial Communication Example
This is a C51 microcontroller serial port receiving (interrupt) and sending routine, which can be used to test the interrupt reception and query transmission of the 51 microcontroller.
#include#include #define length 4 //data length unsigned char inbuf[length]; unsigned char checksum,counter; bit flag = 0; //data access flag main() { init_serial(); //serial port initialization while (1) { if (flag!=0) //if the data access flag is set, send the read number from the serial port { flag= 0; //clear the data access flag send_string(inbuf,length); //send a string to the serial port } } } void init_serial( void ) { SCON = 0x50; //serial working mode 1, 8-bit asynchronous communication mode TMOD |= 0x20; //timer 1, mode 2, 8-bit automatic reload PCON |= 0x80; //SMOD=1, indicating that the data transmission rate is doubled TH1 = 0xF4; //data transmission rate: 4800 fosc=11.0592MHz IE |= 0x90; //Enable serial interrupt TR1 = 1; //Start timer 1 } void send_char( unsigned char x) { SBUF=x; while (TI== 0 ); TI= 0; } void send_string( unsigned char *s, unsigned int string_length) { unsigned int i= 0; do { send_char(*(s + i)); //Send a character to the serial port i++; } while ( i 127 ) { counter= 0; inbuf[counter]=x; checksum= x- 128; } else { counter++; inbuf[counter]=x; checksum ^= x; if ((counter==(length- 1)) && (!checksum)) { flag = 1; //If the serial port receives data that reaches length and the checksum is correct, //set the data fetch flag} } } }
Previous article:51 MCU and GPS serial communication
Next article:C51 is not strict in writing special function register bit variable definitions
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- Single Voltage Reference vs. Dual Voltage Reference - III
- How to prevent excessive current when controlling multiple uln2003-driven stepper motors?
- DSP28335 Hardware Overview and Registers
- EEWORLD University Hall -- Experts teach you how to DIY a pocket PC
- Chapter 6 Application of SPI - OLED Display
- PCB circuit board questions
- STMicroelectronics SiC products and industrial application guide live data collection
- MSP430 capture device is simple and practical
- Colorful wallet programmed with CircuitPython
- The storage and downloading of time values sent by customers require sum verification or CRC verification