Abstract: This article introduces the implementation ideas, methods, communication protocols and C51 program platform of batch processing commands for communication between PC and MCS-51 microcontroller. It provides a new method for the design of serial communication program between PC and microcontroller.
Keywords: Batch processing MCS-51 microcontroller communication protocol
The serial communication between the PC and the MCS-51 microcontroller is basically one-to-one, that is, every time the PC sends a command to the microcontroller, it waits for a return message from the microcontroller. This may waste a lot of time in the communication process between the PC and the microcontroller. For this reason, after practice, the author has designed a system that can execute commands sent by the PC to the microcontroller in batches. Just like the batch commands in the DOS operating system, the information can be processed in batches continuously. As a result, the system speed is certainly improved. This article provides part of the platform C51 software source code for your reference.
1 Communication protocol
(1) Communication data format
PC→MCS-51 microcontroller data format is listed in Table 1.
Table 1
Stx | Len | Comma | Data[0]…Data[N-1] | Edc | Etx |
start | length | Order | data body | Check code | end code |
0x60 | N+2(2Byte) | (2Bytes) | Stx^Len^CommandH^ CommandL^data[0]…Data[N-1] |
0X03 |
MCS-51 microcontroller→PC data format is listed in Table 2.
Table 2
Stx | Len | Status | Data[0]…Data[N-1] | Edc | Etx |
start | length | Order | data body | Check code | end code |
0x60 | N+2(2Byte) | 2(byte) | Stx^Len^CommandH^ CommandL^data[0]..Data[N-1] |
0X03 |
(2) Initial communication parameters
Between PC and microcontroller (9600, n, 8, 1)That is, the baud rate is 9600 baud, 1 start bit, 8 data bits, 1 stop bit, and no parity.
(3) Data sending and receiving method
All data is sent as binary single bytes.
(4) Sending data sequence
Send synchronization code (1Byte): Stx=0X60
Send the length of subsequent bytes (2Byte): Len=2+N
Send Len subsequent data bytes (N+2 Byte): CommandH, CommandL, Data[0]…Data[N-1]
Send verification code (1Byte): Edc=Stx^CommandH^ CommandL^Data[0]^…Data[N-1]
Send end code (1Byte): Etx=0X03
2 Single command COMMAND[n]
COMMAND[n] format:
Command | Data[0]~-Data[N-1] |
XXXXH | data body |
Generally, the length of a single command is controlled within 255.
The data sequence sent by the PC to the microcontroller is:
STX, LENH, LENL, COMMANDH, COMMANDL, DATA...DATA, EDC, ETX
3 Thinking methods of batch processing
(1) Batch command BATCH_COMMAND (0X0000)
PC format:
Command | Data[0] | Data[1]~-Data[i] | Data[i+1]… | ---…Data[N] |
0x0000 | NUM number of commands | Flag,COMMAND[0] | Flag,COMMAND[i] | Flag,COMMAND[n] |
NUM - the number of subsequent commands;
Flag - the flag that needs to be returned (00 means data is to be returned,Others do not need to return data).
COMMAND[i]=LEN, subcommand body
Function: Batch execution function.
Return status and data: Determine whether to return data according to the Flag flag, and return data in the order of sending commands.
(2) Commands to control batch transfer
Function: No data or status is returned, and the lower computer is controlled to jump.
① Control BATCH_If (0X0001) for judgment and conditional execution of commands
Function: When the microcontroller encounters this command, it performs judgment. If the result is logically true, it executes the first command following BATCH_If; otherwise, it executes the second command following BATCH_If.
PC format:
Command | Data1 | Data2~-Datai | Dataj… |
0x0001 | num | EQUAL/NOT | VALUE |
num: Returns the number of data from right to left. When it is 0, it is the low-order byte of the status data;
EQUAL/NOT - 00 means equal, 1 means unequal;
VALUE – Numeric value.
Returns: None.
② Control BATCH_Break (0x0002) to jump out of the loop command
Function: Used to jump out of the BATCH_While loop.
PC format:
Command |
0x0002 |
Returns: None.
③ Control BATCH_While (0x0003) to loop and execute the commands in the loop body
Function: cyclically execute the commands in the loop body.
PC format:
Command | Data1 |
0x0003 | Num (number of commands in the loop) |
Returns: None.
④ Control BATCH_Return (0x0004) to exit the packaging program block
Function: Exit the packaging program block.
PC format:
Command |
0X0004 |
Returns: None.
(3) Precautions
◇ BATCH _While loop cannot be used nested;
◇ BATCH _Break can only be used within the body of the BATCH _While loop;
◇ Commands that need to obtain returned data immediately should generally not be placed in batch processing.
(4) Examples of PC commands that control the transfer of packaging programs
The code transferred from the PC to the microcontroller: BATCH_ COMMAND
Reader_Version(): assumed to be 0X1001
The actual code downloaded from the PC is (HEX format):
60 28 00 00 06 01 00 03 00 00 05 01 00 04 00 01 00 00 03 01 00
02 00 02 01 00 05 00 01 05 00 61 01 00 02 00 04 00 00 02 10 01 Check code 03
4 Program flow
Each program flow is shown in Figure 1 to Figure 4.
5 parts of source code
Due to space limitations, only part of the source program is provided here.
(1) Main program
The main program first receives the data, analyzes the data, and distinguishes between batch processing and single commands; then executes the command; and finally returns the results and uploads them to the PC. The program list can be found in the supplementary version of this journal's website (website address: http://www.dpj .com.cn).
(2) Batch processing program
The batch processing program distinguishes the control transfer commands with flags, executes the commands by interpreting the command sequence, and finally packages the results according to the communication data format and uploads them to the PC. The program list can be found in the supplementary version of this journal's website.
Conclusion
This article explores the software programming of serial communication between PC and microcontroller to realize the processing of batch information data communication. I believe that this new idea and method will develop and be applied to actual embedded systems soon.
Previous article:Implementing CAN bus communication using 87C196NT microcontroller
Next article:Design of slave device communication adapter based on DeviceNet bus
- Popular Resources
- Popular amplifiers
- Molex leverages SAP solutions to drive smart supply chain collaboration
- Pickering Launches New Future-Proof PXIe Single-Slot Controller for High-Performance Test and Measurement Applications
- CGD and Qorvo to jointly revolutionize motor control solutions
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Nidec Intelligent Motion is the first to launch an electric clutch ECU for two-wheeled vehicles
- Bosch and Tsinghua University renew cooperation agreement on artificial intelligence research to jointly promote the development of artificial intelligence in the industrial field
- GigaDevice unveils new MCU products, deeply unlocking industrial application scenarios with diversified products and solutions
- Advantech: Investing in Edge AI Innovation to Drive an Intelligent Future
- CGD and QORVO will revolutionize motor control solutions
- 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
- A brief history of channel coding
- Connected cars, where is the future?
- What PCB design practice materials are there?
- EEWORLD University ---- Live playback: The most important component of the analog world - Signal chain and power supply: USB Type-C? PD special session
- Problems connecting Bluetooth module with CC2541
- Matter Development Guide (VI): Network Configuration and Lighting-App Examples
- Based on FOC5.3 library IHM07 self-made board BLDC/PMSM motor drive: schematic/device library/reference program/training materials/delivery...
- 【EasyARM-RT1052 Review】+ SDRAM Memory Management
- Usage of MSP430F5438 internal delay function
- MCU Selection