After reading an article titled "Keil C51's Continuous Reading Method for the Same Port" (original text) in the "Experience Exchange" column of the 10th issue of "Microcontrollers and Embedded System Applications" magazine in 2005, the author believes that the article did not conduct an in-depth and accurate analysis of this issue. The two solutions mentioned in the article are not direct and simple. The author believes that this is not a problem that Keil C51 cannot handle continuous reading and writing of a port, but a problem of not being familiar enough with the use of Keil C51 and not being detailed enough in the design, so this article was written.
This article proposes three solutions to the problems mentioned in the original article that are different from the original one. Each method is more direct and simpler than the method mentioned in the original article, and the design is more standardized. (No criticism intended, please forgive the original author)
1 Problem Review and Analysis
The original article mentioned: In actual work, when the same port is read repeatedly and continuously, the Keil C5l compilation does not achieve the expected results. The original author analyzed the assembly program compiled by C and found that the second read statement of the same port was not compiled. But unfortunately, the original author did not analyze the reason for not being compiled, but hurriedly used some less standardized methods to test two solutions.
For this problem, it is easy to find out by reading the manual of Keil C51: KellC51 compiler has an optimization setting, and different optimization settings will produce different compilation results. Generally, the default compilation optimization setting is set to 8 levels of optimization, and the actual maximum can be set to 9 levels of optimization:
①Dead code elimination.
②Data overlaymg.
③Peephole optimization.
④Register variables.
⑤Common subexpression elimination.
⑥Loop rotation.
⑦Extended Index Access 0ptimizing.
⑧Reuse Common. Entry Code.
⑨Common Block Subroutines.
The above problems are caused by KeiI C5l compilation optimization. Because the original program defines the peripheral address as follows:
unsigned char xdata MAX197_at_Ox8000;
_at_ is used to define the variable MAX197 to the external expansion RAM address Ox8000. Therefore, the Keil C51 optimization compiler naturally thinks that repeating the second read is useless, and the result of the first read is enough, so the compiler skips the second read statement. At this point, the problem is clear at a glance.
2 Solution
It is easy to come up with a good solution based on the above analysis.
2.1 The simplest and most direct solution
The program does not need to be modified at all. Just set the compilation optimization selection of Keil C5l to 0 (no optimization).
Select Target in the project window, then open the "Options for Target" setting dialog box, select the "C5l" tab, and select "0: Costant folding" in "Code Optimiztaion" for "Level". After compiling again, you will find that the compilation result is:
CLR MAXHBEN
MOV DPTR, #M. AXl97
MOVX A, @DPTR
MOV R7.
A MOV down8
. R7
SETB MAXHBEN
MOV DPTR, #MAXl97
MOVX A, @DPTR
MOV R7.
A MOV uD4. R7
Both read operations are compiled.
2.2 The best method
Tell Keil C51 that this address is not a general extended RAM, but a connected device with "volatile" characteristics, and each read is meaningful.
You can modify the variable definition and add the "volatile" keyword to indicate its characteristics:
unsigned char volatile xdata MAXl97_at_Ox8000;
you can also include the system header file in the program: "#include
In this way, Keil C51 settings can still retain high-level optimization, and in the compilation results, the same two reads will not be skipped by optimization.
2.3 Hardware solution
In the original text, the data of MAXl97 is directly connected to the data bus, and the address bus is not used. A port line is used to select the high and low bytes. A very simple modification method is to use an address line to select the high and low bytes. For example: connect P2.0 (A8) to the HBEN pin (pin 5 of MAXl97) connected to the original P1.0, and define the operation addresses of the high and low bytes in the program respectively:
unsigned char volatile xdata MAXl97_L_aI_Ox8000;
unsigned char volatile xdata MAXl97 H at 0. x8100;
Change the original program:
MAXHBEN=O; //Read the lower 8 bits
down8=MAXl97:
MAXHBEN=1; //Read the upper 4 bits
up4=MAXl97:
to the following two sentences:
down8=MAXl97_L; //Read the lower 8 bits
up4=MAXl97_H; //Read the upper 4 bits
3 Summary
After long-term testing and improvement and the actual use of a large number of developers, Keil C51 has overcome most of the problems, and its compilation efficiency is also very high. For general use, it is difficult to find any problems. The author has roughly studied the results of Keil C51 optimization compilation, and I admire the wisdom of Keil C51 designers very much. The assembly code generated by some C program compilation is even better and more concise than the code written directly in assembly by general programmers. By studying the assembly code generated by KeilC51 compilation, it is very helpful to improve the level of assembly language programming.
From the problems in this article, it can be seen that when encountering problems in the design, you must not be blinded by the surface phenomenon and do not rush to solve them. You should carefully analyze and find out the cause of the problem, so that you can solve the problem fundamentally and thoroughly. There will be no unnecessary interference, which prevents the occurrence of data inconsistency.
Previous article:Application of Single Chip Microcomputer in Baud Rate Converter
Next article:Constructing a real-time operating system for 51 single-chip microcomputer
- Popular Resources
- Popular amplifiers
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
- CMD file analysis in CCS
- GD32E231 incense machine control board
- SPI interface oled screen driver based on pic18f25k22 microcontroller
- Understanding of USB-Serial, USB to Serial, USB-UART, PL2303, CH340, etc.
- [RVB2601 Creative Application Development] Design of a Farmland Pest Control Device
- [RVB2601 Creative Application Development] 2. Light up the LED
- Level shifter reverse circuit
- Please advise, is it necessary to consider the safety power when calculating the LED resistance?
- [RVB2601 creative application development] u8g2+IP
- What are the screw holes in this package for?