After reading an article titled "Keil C51's method of continuously reading the same port" (original text) in the "Experience Exchange" column of "Microcontroller and Embedded System Application" magazine, issue 10, 2005, the author believes that the article does not provide 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 that the user is not familiar enough with the use of Kei1 C51 and the design is not detailed enough, so this article is written.
This article proposes three different solutions to the problems mentioned in the original article. 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 was read repeatedly and continuously, the Keil C51 compilation did not achieve the expected results. The original author analyzed the assembly program compiled by C and found that the second read statement for the same port was not compiled. Unfortunately, the original author did not analyze the reason for the failure to compile, but hurriedly used some less standardized methods to test out two solutions.
For this problem, it is easy to find out by reading the manual of Keil C51: KeilC51 compiler has an optimization setting. Different optimization settings will produce different compilation results. Generally, the default compilation optimization setting is set to level 8 optimization, and the actual maximum can be set to level 9 optimization:
1. Dead code elimination.
2. Data overlaying.
3. Peephole optimization.
4. Register variables.
5. Common subexpression elimination.
6. Loop rotation.
7. Extended Index Access Optimizing.
8. Reuse Common Entry Code.
9. Common Block Subroutines.
The above problems are caused by Keil C51 compiler optimization. Because in the original program, the peripheral address is directly defined as follows:
unsigned char xdata MAX197 _at_ 0x8000
. The variable MAX197 is defined to the external extended RAM address 0x8000 using _at_. Therefore, Keil C51 optimized compilation naturally thinks that repeating the second read is useless, and the result of the first read can be used directly, so the compiler skips the second read statement. At this point, the problem is clear at a glance.
2 Solution
Based on the above analysis, it is easy to come up with a good solution.
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 C51 to 0 (no optimization). Select the Target in the project window, then open the "Options for Target" setting dialog box, select the "C51" tab, and select "Level" in "Code Optimiztaion" as "0: Costant folding". After compiling again, you will find that the compilation result is:
CLR MAXHBEN
MOV DPTR,#MAX197
MOVX A,@DPTR
MOV R7,A
MOV down8,R7
SETB MAXHBEN
MOV DPTR,#MAX197
MOVX A,@DPTR
MOV R7,A
MOV up4,R7
Both read operations are compiled.
2.2 The best way
is to 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 describe its characteristics:
unsigned char volatile xdata MAX197 _at_ 0x8000;
You can also include the system header file in the program; "#include", and then modify the variable in the program and define it as a direct address:
#define MAX197 XBYTE[0x8000]
In this way, the Keil C51 settings can still retain advanced 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 MAX197 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 MAX197) that was originally connected to P1.0. Define the operation addresses of the high and low bytes in the program respectively:
unsigned char volatile xdata MAX197_L _at_ 0x8000;
unsigned char volatile xdata MAX197_H _at_ 0x8100;
Change the original program:
MAXHBEN = 0;
down8 = MAX197; // read the lower 8 bits
MAXHBEN = 1;
up4 = MAX197; // read the upper 4 bits
to the following two sentences
down8 = MAX197_L; // read the lower 8 bits
up4 = MAX197_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. I admire the wisdom of Keil C51 designers very much. The assembly code generated by the compilation of some C programs is even better and more concise than the code written directly by ordinary programmers in assembly language. By studying the assembly code generated by Kell C51 compilation, it is very helpful to improve the level of assembly language programming.
From the problems in this article, we can see that when we encounter problems in design, we must not be blinded by superficial phenomena and rush to solve them. We should carefully analyze and find out the causes of the problems. Only in this way can we solve the problems fundamentally and thoroughly.
Appendix: Optimization levels and optimization effects in Keil C51 | |
level | illustrate |
0 | Constant merging: The compiler pre-calculates results and replaces expressions with constants whenever possible. This includes running address calculations. |
1 | Dead code removal: Unused code segments are removed. |
2 | Data Coverage: Data and bit segments suitable for static coverage are determined and internally identified. The BL51 connector/locator can select segments that can be covered through global data flow analysis. |
3 | Peephole optimization: Eliminate redundant MOV instructions. This includes unnecessary loads from storage and constant load operations. When storage space or execution time can be saved, replace complex operations with simple operations. |
4 | Register variables: Automatic variables and function parameters are allocated to registers if possible. The storage area reserved for these variables is omitted. |
5 | Global common subexpression elimination: The same subexpression within a function may be calculated only once. Intermediate results are stored in registers and used in a new calculation. |
6 | Loop Optimization: The program optimizes loops if the resulting program code is faster and more efficient. |
7 | Extended index access optimization: Use DPTR for register variables when appropriate. Optimize execution speed and code size for pointer and array access. |
8 | Common tail merging: When a function has multiple calls, some setup code can be reused, thus reducing program size. |
9 | Common Block Subroutines: Detects looping instruction sequences and converts them into subroutines. Cx51 even rearranges the code to get larger looping sequences. |
Previous article:KEIL C51 general register parameter passing rules
Next article:51 MCU local variables occupy RAM problem
Recommended ReadingLatest update time:2024-11-17 06:01
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- How to read electrical control circuit diagrams (Classic best-selling books on electronics and electrical engineering) (Zheng Fengyi)
- MCU C language programming and Proteus simulation technology (Xu Aijun)
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!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- 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
- Discussion on the Dilemma and Improvement Measures of RF Circuit PCB Design
- I am a PhD student
- TMS320C6678 Evaluation Module Core and Device Benchmarks
- Power consumption evaluation of GD32L233CCT6 in different power management modes
- Differential Equal Length
- IAP Function Design of MSP43F149 Series MCU
- C2000 floating point calculation notes - differences between CPU and CLA and error handling techniques
- TMS320F28335GPIO Example - Light up the LED
- Building an assisted driving system algorithm based on FPGA platform
- Why does my MSP430F5529 clock initialization use the high-speed XT1 mode, and XT1HFOFFG is always high?