The serial port is a tool for the microcontroller to exchange information with the outside world.
There are two communication modes for 8051 microcontroller:
Parallel communication: data bits are sent or received at the same time. Serial communication: data bits are sent or received sequentially. See the figure below:
Serial communication method:
Asynchronous communication: It uses a start bit to indicate the beginning of a character and a stop bit to indicate the end of a character. The format of each frame is as follows:
In a frame format, there is a start bit 0 first, then 8 data bits, with the low bit first and the high bit last, followed by the parity bit (which can be omitted), and finally the stop bit 1. Using this format to represent characters, characters can be transmitted one by one.
In asynchronous communication, there must be two regulations between the CPU and the peripherals, namely, the character format and the baud rate. The character format is stipulated so that both parties can understand the same string of 0s and 1s as the same meaning. In principle, the character format can be freely determined by both parties in communication, but from the perspective of universality and convenience, it is generally better to use some standards, such as the ASCII standard.
Baud rate is the speed at which data is transmitted, and its definition is the number of bits of binary numbers transmitted per second. For example, if the data transmission rate is 120 characters/s, and each character contains 10 digits as specified above, the transmission baud rate is 1200 baud.
Synchronous communication: In synchronous communication, each character uses a start bit and a stop bit as a mark of the beginning and end of the character, which takes time; so when transferring data blocks, in order to increase the speed, these marks are often removed and synchronous transmission is used. Since the start of data block transfer must be indicated by a synchronization character, and a clock is required to achieve synchronization between the sender and the receiver, the hardware is more complex.
Communication direction: In serial communication, the one-way transmission method in which the communication interface can only send or receive is called simplex transmission; and the two-way transmission of data between two machines A and B is called duplex transmission. Duplex transmission is divided into half-duplex transmission and full-duplex transmission. Half-duplex transmission means that the two machines cannot send and receive at the same time, and can only send or receive information at any time.
2. Serial interface structure of 8051 microcontroller
The 8051 microcontroller serial interface is a programmable full-duplex serial communication interface. It can be used as an asynchronous communication method (UART) to connect to external devices that transmit information serially, or for full-duplex 8051 multi-machine systems using standard asynchronous communication protocols. It can also be used in a synchronous manner to expand I/O ports using TTL or CMOS shift registers.
The 8051 microcontroller communicates with the outside world through the RXD pin (P3.0, serial data receiving end) and the TXD pin (P3.1, serial data transmitting end). SBUF is the serial port buffer register, including the transmit register and the receive register. They have the same name and address space, but there will be no conflict, because one of them can only be read by the CPU, and the other can only be written by the CPU.
Serial port control and status registers
Serial port control register SCON
It is used to define the working mode of the serial port and implement receiving and sending control. The byte address is 98H, and its bit definitions are as follows:
D7
|
D6
|
D5
|
D4
|
D3
|
D2
|
D1
|
D0
|
SM0
|
SM1
|
SM2
|
REN
|
TB8
|
RB8
|
TI
|
RI
|
SM0, SM1: Serial port working mode selection bits, which are defined as follows:
SM0, SM1
|
How it works
|
Functional Description
|
Baud rate
|
0 0
|
Method 0
|
8-bit shift register
|
Fosc/12
|
0 1
|
Method 1
|
10-bit UART
|
variable
|
1 0
|
Method 2
|
11-bit UART
|
Fosc/64 or fosc/32
|
1 1
|
Method 3
|
11-bit UART
|
variable
|
Where fosc is the crystal oscillator frequency
SM2: Multi-machine communication control bit. In mode 0, SM2 must be equal to 0. In mode 1, when (SM2) = 1, RI is set to 1 only when a valid stop bit is received. In mode 2 or mode 3, when (SM2) = 1 and the received ninth bit of data RB8 = 0, RI is set to 1.
REN: Receive enable control bit. It is set by software to enable reception and cleared by software to disable reception.
TB8: is the 9th bit of the data to be sent. In mode 2 or mode 3, the 9th bit of data to be sent is set to 1 or cleared to 0 by software as needed. For example, it can be agreed to be used as a parity bit, or as a flag bit to distinguish address frames or data frames in multi-machine communication.
RB8: The 9th bit of the received data. RB8 is not used in mode 0. In mode 1, if (SM2) = 0, RB8 is the received stop bit. In mode 2 or mode 3, RB8 is the 9th bit of the received data.
TI: Transmit interrupt flag. In mode 0, it is set by hardware when the 8th bit is sent. In other modes, it is set by hardware before the send stop bit. TI setting indicates that a frame of information has been sent, and it also requests an interrupt. You can use software query to obtain information that the data has been sent, or use an interrupt to send the next data. TI must be cleared to 0 by software.
RI: Receive interrupt flag. In mode 0, it is set by hardware after receiving the 8th bit of data. In other modes, it is set by hardware at the middle of receiving the stop bit (see the description of SM2 for exceptions). RI is set to indicate that a frame of data has been received, which can be obtained by query or interrupt. RI must also be cleared to 0 by software.
Special Function Register PCON
PCON is added to realize power control on CHMOS 80C51 microcontroller. The highest bit is SMOD.
How the serial port works
The full-duplex serial port of the 8051 microcontroller can be programmed into four working modes, which are described as follows:
Mode 0 is shift register input/output mode. You can connect an external shift register to expand the I/O port, or connect an external synchronous input/output device. 8-bit serial data is input or output from RXD, and TXD is used to output synchronous pulses.
The output serial data is output from the RXD pin, and the TXD pin outputs the shift pulse. When the CPU writes the data into the transmit register, it starts the transmission immediately, and outputs the 8-bit data from the RXD at a fixed baud rate of fos/12, with the low bit first and the high bit last. After sending a frame of data, the transmit interrupt flag TI is set by hardware.
When the serial port receives in mode 0, first set the control bit REN to allow reception. At this time, RXD is the serial data input terminal, and TXD is still the synchronous pulse shift output terminal. When (RI) = 0 and (REN) = 1 are satisfied at the same time, reception begins. When the 8th bit of data is received, the data is shifted into the receive register, and RI is set by hardware.
The following two figures are the wiring diagrams of mode 0 expansion output and input.
Mode 1 is a 10-bit asynchronous communication interface with variable baud rate. Sending or receiving a frame of information includes 1 start bit 0, 8 data bits and 1 stop bit 1.
Output When the CPU executes an instruction to write data into the transmit buffer SBUF, transmission is started. Serial data is output from the TXD pin, and after a frame of data is sent, TI is set by hardware.
When the input (REN) = 1, the serial port samples the RXD pin. When the sampled jump from 1 to 0 is confirmed to be the start bit 0, it starts to receive a frame of data. Only when (RI) = 0 and the stop bit is 1 or (SM2) = 0, the stop bit enters RB8, and the 8-bit data can enter the receiving register, and the interrupt flag RI is set by hardware; otherwise the information is lost. Therefore, when receiving in mode 1, the RI and SM2 flags should be cleared by software first.
Method 2
Mode 1 is an 11-bit UART mode with a fixed baud rate. Compared with mode 1, it adds a 9th data bit which can be programmed to 1 or 0.
Output: The serial data sent is output from the TXD terminal. One frame of information is 11 bits. The additional 9th bit comes from the TB8 bit of the SCON register, which is set or reset by software. It can be used as a flag bit for address/data information in multi-machine communication, and can also be used as a parity bit for data. When the CPU executes an instruction to write data to SUBF, the transmitter is started to send. After sending a frame of information, the interrupt flag TI is set.
Input: When (REN) = 1, the serial port samples the RXD pin. When the sampled jump from 1 to 0 is confirmed to be the start bit 0, it starts to receive a frame of data. After receiving the additional 9th bit of data, when (RI) = 0 or (SM2) = 0, the 9th bit of data enters RB8, and the 8 bits of data enter the receiving register, and the hardware sets the interrupt flag RI; otherwise, the information is lost. And RI is not set. After another bit of time, regardless of whether the above conditions are met, the receiving circuit is reset and re-detects the jump from 1 to 0 on RXD.
Working method 3
Mode 3 is an 11-bit UART mode with variable baud rate. Except for the baud rate, everything else is the same as Mode 2.
Baud rate selection
As mentioned above, in serial communication, the data transmission rate (baud rate) between the sender and the receiver must be agreed upon. Among the four working modes of the 8051 serial port, the baud rates of modes 0 and 2 are fixed, while the baud rates of modes 1 and 3 are variable and controlled by the overflow rate of timer T1.
Method 0
The baud rate of mode 0 is fixed at 1/12 of the main oscillator frequency.
Method 2
The baud rate of mode 2 is determined by the selection bit SMOD in PCON and can be expressed as follows:
Baud rate = 2 to the power of SMOD divided by 64 and then multiplied by fosc, that is, when SMOD=1, the baud rate is 1/32fosc, when SMOD=0, the baud rate is 1/64fosc
3. Method 1 and Method 3
Timer T1 is used as a baud rate generator, and its formula is as follows:
Baud rate = Timer T1 overflow rate
T1 overflow rate = T1 count rate / number of cycles required to generate overflow
The count rate of T1 depends on whether it works in the timer state or the counter state. When working in the timer state, the count rate of T1 is fosc/12; when working in the counter state, the count rate of T1 is the external input frequency, which should be less than fosc/24. The cycle required to generate overflow is related to the working mode of timer T1 and the preset value of T1.
Timer T1 works in mode 0: the number of cycles required for overflow = 8192-x
Timer T1 works in mode 1: the number of cycles required for overflow = 65536-x
Timer T1 works in mode 2: the number of cycles required for overflow = 256-x
Since mode 2 is an 8-bit timer/counter mode that automatically reloads the initial value, it is most appropriate to use it as a baud rate generator.
When the clock frequency is selected as 11.0592MHZ, it is easy to obtain the standard baud rate, so this is why many microcontroller systems choose this seemingly "strange" crystal oscillator.
The following table lists the commonly used baud rates and initial values when timer T1 works in mode 2.
Common baud rates
|
Fosc(MHZ)
|
SMOD
|
TH1 initial value
|
19200
|
11.0592
|
1
|
FDH
|
9600
|
11.0592
|
0
|
FDH
|
4800
|
11.0592
|
0
|
FAH
|
2400
|
11.0592
|
0
|
F4H
|
1200
|
11.0592
|
0
|
E8
|
Previous article:51 MCU Tutorial from Scratch - MCU Timer/Counter Experiment
Next article:51 MCU Tutorial from Scratch—— 23 MCU Serial Port Communication Programming
Recommended ReadingLatest update time:2024-11-16 16:24
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
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!
- 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
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Chip supply issues
- Do you have more award-winning power banks? If so, please give me one.
- I want to know what this DSA is and why I don't choose a fixed resistance resistor.
- Review Summary: Tuya Sandwich Wi-Fi & BLE SoC NANO Main Control Board
- Comparison between microbit and CLUE
- STM32F3 MCU peripheral components and crystal oscillator selection reference
- Recommend Chinese chips + domestic ARM and DDR particles (products have been verified and available)
- Review summary: Anxinke NB-IoT development board EC-01F-Kit
- MPS gives benefits | Order online at the mall, get a JD card, and 8,000 yuan worth of gifts are waiting for you!
- Description of the implementation process of the 2019 National Undergraduate Electronic Design Competition