Detailed Analysis of KEIL C51 Code Optimization

In-depth analysis of Keil C51 bus peripheral operation issues.

1 Problem Review and Analysis

In actual work, encountering repeated consecutive reads of the same port, the Keil C51 compiler did not achieve the expected results. Analyzing the assembly program generated by the C compiler found that the second read statement of the same port was not compiled.

For this issue, it is easy to find in the Keil C51 manual: the Keil C51 compiler has an optimization setting, and different optimization settings will produce different compilation results. Generally, the default compilation optimization setting is set to level 8, while the highest can be set to level 9:

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 issues are precisely due to Keil C51 compiler optimizations. Because in the original program, the peripheral address is directly defined as follows:

unsigned char xdata MAX197 _at_ 0x8000

Using _at_ to define the variable MAX197 at the external extended RAM specified address 0x8000. Therefore, the Keil C51 optimizer naturally considers that the second read is unnecessary, and directly uses the result of the first read, thus skipping the second read statement. At this point, the problem becomes clear.

2 Solutions

From the above analysis, it is easy to propose good solutions.

2.1 The simplest and most direct method

No modifications are needed to the program; just set the Keil C51 compiler optimization choice to 0 (no optimization). Select the Target in the project window, then open the “Options for Target” dialog, select the “C51” tab, and set the “Level” in “Code Optimization” to “0: Constant folding.” After recompiling, you will find 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 method

Inform Keil C51 that this address is not ordinary extended RAM, but a connected device with “volatile” characteristics, making each read meaningful. You can modify the variable definition to include the “volatile” keyword:

unsigned char volatile xdata MAX197 _at_ 0x8000;

You can also include the system header file in the program: “#include<absacc.h>”, and then modify the variable in the program, defining it as a direct address:

#define MAX197 XBYTE[0x8000]

In this way, the Keil C51 settings can still retain high-level optimization, and the compilation result will also not skip the two reads.

2.3 Hardware Solution

In the original text, the data of MAX197 is directly connected to the data bus, while the address bus is not used, and a port line is used to select the operation of high and low bytes. A simple modification method is to use an address line to select the operation of high and low bytes. For example: connect P2.0(A8) to the HBEN pin (pin 5 of MAX197) originally connected to P1.0. In the program, define the operation addresses for high and low bytes:

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 low 8 bits

MAXHBEN =1;

up4=MAX197;//Read high 4 bits

to the following two sentences:

down8= MAX197_L;//Read low 8 bits

up4=MAX197_H;//Read high 4 bits

3 Conclusion

Keil C51 has overcome most issues through long-term testing, improvements, and practical use by many developers, and its compilation efficiency is also very high. For general use, it is difficult to find any problems. I have roughly studied the optimization results of Keil C51 and greatly admire the wisdom of the designers of Keil C51, as some assembly code generated by compiling C programs is even better and more concise than code written directly in assembly by general programmers. Studying the assembly code generated by Keil C51 is very helpful for improving the level of writing assembly language programs.

The issues presented in this article show that when encountering problems in design, one should not be blinded by superficial phenomena or rush to solve them; instead, one should analyze carefully to identify the root cause of the problem, which is the only way to fundamentally and thoroughly solve the issue.

Detailed Analysis of KEIL C51 Code Optimization

Leave a Comment