2.80c51 serial port structure
2.1. Introduction to internal structure
The internal structure of the serial port of the AT89S51 microcontroller is shown in the figure below. It has two physically independent receive and send buffers SBUF (belonging to special function registers), which can send and receive data at the same time. The send buffer can only be written but not read, and the receive buffer can only be read but not written. The two buffers share a special function register byte address (99H).
TXD (P3.0) sends data Transmit (tx) Data abbreviation
RXD (P3.1) receives data Receive (rx) Data Abbreviation
There are two control registers for the serial port: special function registers SCON and PCON. The functions of each bit of these two special function registers are introduced below.
2.1.1、Serial port control register SCON
Serial port control register SCON, byte address 98H, bit addressable, bit address 98H ~ 9FH. SCON is a special function register, used to set the serial port working mode, receive and send control to set the status flag, the format is shown in the figure below. (Set to 0 if not used, generally the default is 0)
The following describes the functions of each unit in SCON.
(1) SM0, SM1: Serial port 4 operating mode selection bits.
The four working modes corresponding to the two-bit encoding of SM0 and SM1 are shown in the table below.
Table 4 working modes of serial port
Note: Method 1 is the most commonly used, take this as an example!
(2) SM2: multi-machine communication control bit.
Because multi-machine communication is carried out in mode 2 and mode 3, the SM2 bit is mainly used in mode 2 or mode 3. When the serial port receives in mode 2 or mode 3,
If SM2=1, RI is set to 1 only when the received 9th bit of data (RB8) is 1, an interrupt request is generated, and the first 8 bits of received data are sent to SBUF; when the received 9th bit of data (RB8) is 0, the first 8 bits of received data are discarded.
When SM2=0, no matter whether the 9th bit is 1 or 0, the first 8 bits of data are sent to SBUF, and RI is set to 1, generating an interrupt request. (RB8 does not have the function of activating RI)
In mode 1, if SM2 = 1, RI (receiving controller) will be activated only when a valid stop bit is received.
In mode 0, SM2 must be 0.
(3) REN: Enable serial receive bit.
Set or cleared by software.
REN=1, allows the serial port to receive data.
REN=O, prohibit the serial port from receiving data.
(4) TB8: The 9th data bit sent.
In mode 2 and mode 3, TB8 is the 9th bit of data to be sent, and its value is set to 1 or cleared to 0 by software. In dual-machine serial communication, TB8 is generally used as a parity bit; in multi-machine serial communication, it is used to indicate whether the host sends an address frame or a data frame. TB8=1 is an address frame, and TB8=0 is a data frame.
In modes 0 and 1, this bit is unused (0)
(5) RB8: The 9th data bit received.
When operating in Mode 2 and Mode 3, RB8 stores the 9th bit of received data. In Mode 1, if SM2 = 0, RB8 is the received stop bit. In Mode 0, RB8 is not used.
(6) TI: Send interrupt flag
When the serial port works in mode 0, TI is set to 1 by hardware when the 8th bit of serial data is finished. In other working modes, TI is set to 1 when the serial port starts to send the stop bit. TI = 1, indicating that a frame of data has been sent. The status of the TI bit can be queried by software, and an interrupt can also be requested. After the CPU responds to the interrupt, the next frame of data to be sent is written to SBUF in the interrupt service routine. TI must be cleared to 0 by software.
(7) RI: Receive interrupt flag
When the serial port works in mode 0, RI is set to 1 by hardware when the 8th bit of data is received. In other working modes, this bit is set to 1 when the serial port receives the stop bit. RI = 1, indicating that a frame of data has been received, and an interrupt is requested, requiring the CPU to take data from the receiving SBUF. The status of this bit can also be queried by software. RI must be cleared to 0 by software.
All bits in SCON can be cleared or set to 1 by bit operations.
2.1.2 Special Function Register PCON
The byte address of the special function register PCON is 87H and cannot be bit-addressed. The format of PCON is shown in the figure below.
Among them, only the highest bit SMOD is related to the serial port.
SMOD bit: baud rate selection bit.
When SMOD=1, the baud rate is doubled compared to when SMOD=0, so the SMOD bit is also called the baud rate multiplication bit.
2.2、80c51 serial port working mode
There are 4 ways determined by the encoding of the two bits SM0 and SM1!
2.2.1、Method 1、
Only method 1 is introduced.
The format for transmitting a frame of data is shown in the figure, where:
TXD is the transmitting pin, and RXD is the receiving pin. A frame is 10 bits, 1 start bit, 8 data bits (first low and then high), and 1 stop bit. The baud rate is determined by the overflow rate of T1 or T2.
After sending or receiving a frame of data, the hardware sets TI=1 or RI=1 to request an interrupt from the CPU; but the interrupt flag must be cleared (set to 0) by software, otherwise the next frame of data cannot be sent or received.
(1) Send:
The CPU executes a write SBUF instruction, starts the serial port transmission, and writes 1 to the 9th bit of the output shift register. After sending the start bit, under the action of each shift pulse, the output shift register shifts one bit to the right, and 0 is shifted to the left. When the highest bit of the data is moved to the output bit, the left side of the originally written 9th bit 1 is all 0. After the detection circuit detects this condition, it makes the control circuit shift for the last time, /SEND and DATA are invalid, the stop bit is sent, and a frame ends, and TI=1 is set.
(2) Receiving:
After REN = 1, reception is allowed. The receiver samples RXD at 16 times the selected baud rate.
The terminal level, when a negative jump is detected, the receiver is started, and 1FFH is written into the input shift register (9 bits). Since there is a slight error in the clock frequency of the receiving and sending sides, the receiving controller samples RXD in 16 equal parts of the one-bit transmission time, and takes the same value of at least 2 of the 7, 8, and 9 samples as the received value. The receiving bit enters from the right side of the shift register, and 1 is shifted out from the left. When the leftmost is the start bit 0, it means that 8 bits of data have been received, and then the last shift is made to receive the stop bit. The first 8 bits of the received 9-bit data are loaded into SBUF, the ninth bit enters RB8 and RI=1 is set, and an interrupt is requested to the CPU.
2.2.2 Baud rate calculation
In serial communication, the sender and receiver must agree on the rate at which data is sent or received. The serial port of the microcontroller can be programmed in four ways through software.
Modes 0 and 2 have fixed baud rates.
Modes 1 and 3 have variable baud rates, determined by the timer overflow rate.
Baud rate calculation formulas for various methods;
1) Mode 0 baud rate = fosc/12
2) Mode 2 baud rate = 2^smod*fosc/64
3) The baud rate of mode 1 and 3 = 2^smod*fosc/32
Determined by the overflow rate of T1 or T2 and the SMOD bit
The overflow rate of the timer is determined by the TH1 count value in the timer.
T1 timer overflow rate = fosc / [12*(256-TH1)]
In the single-chip microcomputer, the most commonly used crystal oscillator frequencies are 12MHZ and 11.0592MHZ, so the baud rate selected is relatively stable. The commonly used baud rates and the relationship between the parameters are shown in the table below.
1) Using T1:
Baud rate = 2^smod*T1 timer overflow rate/32, T1 is mode 2 (this refers to the timer working mode, automatic reload 8 bits) //T1 timer overflow rate is determined by TH1
T1 timer overflow rate = fosc / [12*(256-TH1)]
Example: Given fosc=6MHz, SMOD=0, and the baud rate is set to 2400, find the initial count value TH1 of T1.
Baud rate = 1/((12/fosc)*(256-X))/32 = fosc/12*32(256-X)
(256-X)=dark/2400/384=6M/2400/384;256-X~=6.5104
TH1~=250=FAH can only be calculated approximately.
If fosc=11.0592MHz, then 256-X=11.0592M/2400/384=4068/384=12 TH1=F4H; it can be calculated accurately, and other commonly used standard baud rates can also be calculated correctly. Therefore, this crystal frequency is the most commonly used.
If SMOD = 1, the baud rate is doubled for the same initial value of X.
2) Using T2:
In the 52-type single-chip microcomputer, the baud rate generator selection of serial port mode 1 and 3 is determined by the TCLK and RCLK bits.
Determine whether it is T1 or T2. If TCLK=1, the transmitter baud rate comes from T2, otherwise from T1. If RCLK=1, the receiver baud rate comes from T2, otherwise from T1.
The baud rate generated by T2 has nothing to do with SMOD. The minimum unit of T2 timing = 2/fosc. The overflow pulse of T2 is divided by 16 and used as the sending or receiving pulse of the serial port.
Baud rate = (1/((2/fosc)(65536-X)))/16 = fosc/(32(65536-X))
Example: Given fosc = 11.0592MHz, find X when baud rate = 2400
2400=11059200/(32(65536-X)) 65536-X=144 X=65392=FF70H
2.2.3. Initialization before using the serial port
Mainly set the timer 1 that generates the baud rate, serial port control and interrupt control. The specific steps are as follows:
1. Determine the working mode of T1 (program the register of TMOD)
2. Calculate the initial value of T1 and install TH1.TL1
3. Start T1 (TR1 bit in TCON during programming)
4. Determine the serial port control (program the SCON register, this register can be bit-addressed, i.e. 0x??, or directly operated, such as: SM1=?, SM0=?)
5. When the serial port works in interrupt mode, interrupt settings must be performed (programming IE.IP registers)
Previous article:Simple LCD display mobile program
Next article:The program gradually incorporates modular exercises -
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- New Year, gifts, and heights are all here! AMS OSRAM invites you to play games and win New Year gifts!
- Seven Rules for Measuring Oscilloscope Noise
- "Recommend Chinese chips" + domestic FPGA
- [Rawpixel RVB2601 Creative Application Development] Running Routine HELLOWORLD
- EEWORLD University ---- Microwave Technology Basics
- How to deploy and install docker service on arm platform
- Solar photovoltaic grid-connected power generation and its inverter control
- [SAMR21 New Gameplay] 6. Graphical Programming - Breathing Light 2
- MS3142 can replace A3906
- 【Radio Waves】What is the source of Bluetooth? Does it have any radiation that may affect your health? Is it safe?