There are many ways to achieve serial communication. For example, according to the requirements of the communication protocol, you can use the method of writing a program to complete the reception and transmission of data characters in serial communication, but this method is more troublesome; in order to quickly, easily and effectively achieve serial communication, the PC series and its compatible machines can use a programmable asynchronous serial communication interface chip to execute the asynchronous serial communication protocol. The core of this asynchronous communication interface chip is a large-scale integrated communication component called a universal asynchronous receiver/transmitter, or UART (Universal Asynchronous Receiver/Transmitter).
1. 8051 realizes the principle of multi-computer communication
The three control registers SCON (Serial Controller), PCON (Power Controller) and IE (Interrupted Enhanced) related to 8051 serial communication are used to set four different communication modes and define the baud rate. Its serial port working mode 3 is a 9-bit data asynchronous communication mode with variable baud rate. The sending or receiving of a frame of data is 11 bits: 1 start bit, 8 data bits, 1 additional check bit and 1 stop bit. The additional 9th bit of data is programmable. Using this controllable 9th bit of data, multi-machine communication can be achieved.
2. Communication principle between PC and 8051
The core of the PC's serial communication adapter is the programmable asynchronous receiver and transmitter UART8250 chip. The 8250 has 10 addressable registers for the CPU to read/write to achieve communication with the outside world, formulate communication protocols and provide communication status information.
The serial channel of the 8051 microcontroller is a full-duplex serial communication port, which can realize both dual-machine communication and multi-machine communication. When the serial port works in mode 2 or mode 3, if the multi-machine communication control bit SM2 of the serial control register SCON is set to "1" by software, it is multi-machine mode; if SM2 is "0", it is 9-bit asynchronous communication mode.
When multiple machines communicate, the frame format sent by 8051 is 11 bits, of which the 9th bit is the send data bit TB8 in SCON, which is the flag for sending address (TB8=1) or sending data (TB8=0) during multi-machine communication. The corresponding bit of the serial frame format is automatically loaded during serial transmission. At the receiving end, the 9th bit of a frame of data is loaded into the receive data bit RB8 of SCON. The receiver determines whether to generate a serial interrupt flag based on the status of RB8 and SM2, so that it can respond or not to the serial interrupt, thus realizing the serial interrupt.
The serial communication of the PC is completed by the interface chip 8250. It is a universal asynchronous receiver/transmitter, which is specially designed for data communication of Intel's microprocessor. It receives parallel data from the CPU, then converts it into continuous serial data and sends it out through the transmitter's shift register. Although the 8250 does not have the multi-machine communication function and cannot generate TB8 or RB8, the 8250 can be used flexibly to complete the above functions with software. The 8250 can send a variety of word lengths, with a maximum of 11 bits in a frame. Compared with the frame format sent by the 8250, the difference is only in the 9th bit, that is, the 9th bit sent by the 8250 of the PC is an odd/even check bit, rather than the corresponding address/data bit flag. Software programming can be used to make the odd/even check bit of the 8250 form the correct address/data flag.
3﹒Multi-machine communication control between PC and 8051
Although the 8250 itself does not have the multi-machine communication function of the 8051 series microcontrollers, the 8250 can meet the communication requirements of the 8051 microcontroller through software.
The port address range of 8250 is 3F8H~3FEH or 2F8H~2FEH. The port addresses of its internal registers are shown in Table 1.
Table 1 Port addresses of 8250 internal registers
Serial number |
name |
Port Address |
Input/output form |
1 |
Data transmission holding register |
3F8H |
Output |
2 |
Data receiving register |
3F8H |
enter |
3 |
Baud rate factor register (low bit) |
3F8H |
Output |
4 |
Baud rate factor register (high bit) |
3F9H |
Output |
5 |
Interrupt Control Register |
3F9H |
Output |
6 |
Interrupt Identification Register |
3FAH |
enter |
7 |
Communication line control register |
3FBH |
Output |
8 |
MODEM Control Register |
3FCH |
Output |
9 |
Communication line status register |
3F |
enter |
10 |
MODEM Status Register |
3FEH |
enter |
8250 can send 11-bit data frames, which consist of 1 start bit, 8 data bits, 1 parity bit and 1 stop bit. Its format is as follows:
Start bit |
D0 |
D1 |
D2 |
D3 |
D4 |
D5 |
D6 |
D7 |
Parity bit |
Stop bits |
The typical data format of 8051 microcontroller communication is:
Start bit |
D0 |
D1 |
D2 |
D3 |
D4 |
D5 |
D6 |
D7 |
TB8 |
Stop bits |
TB8 is a programmable bit that distinguishes data frames from address frames by setting it to 0 or 1.
Comparing the above two data formats, we can see that their data bit lengths are the same, and the only difference is the parity bit and TB8. If the parity bit of 8250 can be programmed by software so that it is "1" when sending addresses and "0" when sending data, then the parity bit of 8250 completely simulates the TB8 bit of multi-machine communication of single-chip microcomputers. The method is to write a specific control word into the communication line control register of 8250.
A careful study of the DS bit function of the serial card's communication line control register 3FBH shows that when the serial port is initialized, D5=1 and D3=1 of 3FB are set, while D4=0 is set when sending addresses and D4=1 is set when sending data. This implements the function of TB8 in 8051 without having to adjust it every time. This method not only saves software expenses but also improves communication speed.
By setting the line control register (LCR) of 8250, 8250 can be made very flexible. To achieve multi-machine communication between 8250 and 8051, the key is to control its line status so that its data transmission format is consistent with that of 8051. According to the structural characteristics of the line control register of 8250, the following options can be made in programming:
If the parity bit of the frame sent by 8250 is required to be 1, just execute
MOV DX, 3FBH
MOV AL, 2BH
OUT DX,AL
These three statements, the frame format is:
Start bit |
D0 |
D1 |
D2 |
D3 |
D4 |
D5 |
D6 |
D7 |
1 |
Stop bits |
If you want the parity bit of 8250 to be 0, just execute
MOV DX, 3FBH
MOV AL, 3BH
OUT DX,AL
These three statements, the frame format is:
Start bit |
D0 |
D1 |
D2 |
D3 |
D4 |
D5 |
D6 |
D7 |
0 |
Stop bits |
Obviously, the former can be used as an address frame in multi-machine communication, while the latter can be used as a data frame.
4﹒ Setting non-standard baud rate of PC
In most cases, the 8051 microcontroller system clock uses a 6MHz quartz crystal oscillator. The baud rate of its serial port is determined by its internal timer TH1 (8 bits). The specific calculation formula is:
Baud=(fosc﹡2SMOD)/(32﹡12﹡(256-TH1))=(15625﹡2SMOD)/(256-TH1)
Where SMOD is programmable, and different values of TH1 determine different baud rates.
If you use BASIC or directly call ROM BIOS INT14 (serial port interrupt), you can only set a few standard baud rates. Under this standard baud rate, it is difficult for 8051 to achieve, such as a baud rate of 4800, which cannot be achieved for a microcontroller using a 6MHz crystal oscillator. However, in actual applications, it is unlikely to select a crystal oscillator just to meet the standard baud rate requirements. On the other hand, under the premise of ensuring reliable communication, it is always hoped that the communication speed is as fast as possible. Therefore, non-standard baud rates can be obtained by directly programming the divisor latch of 8250.
Assume that the calculated baud rate for multi-machine communication is 2400. Since it cannot be realized by a single-chip microcomputer, it can be designed as 1953. Set TH1=248 and SMOD=0 on the single-chip microcomputer, and set the divisor equal to 59 on the PC. In this way, multi-machine communication can be realized.
5. Programming of 8250
For 8250 only, the steps for asynchronous serial communication programming are as follows:
Step 1: Set the communication protocol, such as baud rate, parity check mode, data format, data byte length, etc.
Step 2: Read the status of the communication line (or MODEM) to determine whether communication is possible;
step3: send (or receive) a byte;
step4: Repeat step2 and step3 until the communication is completed.
When interrupts are enabled, after the CPU sends (or receives) a byte, it does not need to constantly query the controller status, but can turn to perform other tasks. When the interrupt signal INT4 occurs and responds, the third step of step 2 and step 3 above can be processed.
When using 8250 for serial communication, it must first be initialized, that is, the baud rate, the data format used for communication, whether to use interrupts, whether to perform self-test operations, etc. After initialization, communication can be carried out in program query mode or interrupt mode.
The initialization of 8250 is generally divided into three steps:
Step 1: Set the baud rate (assuming it is 1200)
MOV AL, 80H
MOV DX, 3FBH
OUT DX, AL
MOV AL, 60H
MOV DX, 3FBH
OUT DX,AL
MOV AL, 00H
MOV DX,3F9H
OUT DX,AL
Step 2: Set the communication data format (assuming 7 data bits, 1 stop bit, even parity)
MOV AL, 1AH
MOV DX, 3FBH
OUT DX,AL
Step 3: Set the operation mode
MOV AL, 03H
MOV DX, 3FCH
OUT DX, AL; interrupt output is not allowed
MOV DX, 3FCH
MOV AL, OBH
OUT DX, AL; Enable interrupt output
MOV AL, 13H
MOV DX, 3FCH
OUT DX, AL; self-test working mode
Step 4: Set the interrupt enable register
MOV AL, 00H
MOV DX,3F9H
OUT DX,AL
6. Innovation of this paper
The innovations of this paper are: first, using a programmable asynchronous serial communication interface chip to execute the asynchronous serial communication protocol to achieve communication; second, directly programming the divisor latch of 8250 to obtain a non-standard baud rate.
References:
[1] Hong Jiaping. Communication between PC and MCU under Windows environment. Beijing: Microcomputer Information, No. 3, 2005, pp. 70-73.
[2] Li Chaoqing. PC and MCU Data Communication Technology. Beijing: Beijing University of Aeronautics and Astronautics Press, 2002.12
[3] Yao Zhijiang et al. A complete implementation of a synthesizable VHDL model of 8051 MCU. Beijing: Computer Applications, 2000 (20)
[4] Li Guangdi. Fundamentals of Single Chip Microcomputers. Beijing: Beijing University of Aeronautics and Astronautics Press, 2002.12
Previous article:Research on PLC Based on AT89C55 Single Chip Microcomputer
Next article:How to perform co-simulation of SPICE models and 8051 MCU in NI Multisim
Recommended ReadingLatest update time:2024-11-16 17:45
- 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
- 11 years of experience in microwave and radio frequency manufacturing
- I need a QFN24 packaged MCU with two ADCs, two serial ports, low power consumption, in stock, GD Zhaoyi Innovation has recommendations...
- Design and implementation of high-speed real-time speech recognition system based on DSP
- What is strong current & weak current? What is the difference between the two? I didn't expect the truth to be like this! ~
- EEWORLD University Hall----Live Replay: Microchip's latest SAM and PIC32 microcontroller software development platform - MPLAB? Harmony V3 introduction
- WiFi will soon face challenges from other technologies or will be replaced
- Help, I have a smart hardware combination of STM32 and SIM800C. I have produced 100 of them and run more than 70 of them. About 7 of them have crashed.
- AD9144-FMC-EBZ ADI data transfer board four-channel digital-to-analog converter evaluation board module conversion
- A40i Platform Application Notes - Huawei - ME909S-4G Module Porting Application
- The news about the Agilex M series has been released for some time, but I haven’t seen any information about the specific pin packaging, etc.?