In-depth analysis of Keil C51 bus peripheral operation problems

Publisher:玉立风华Latest update time:2011-03-14 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere


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", and then modify the variable in the program and define it as a direct address: #defme MAXl97 XBYTE[Ox8000]
























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.

Reference address:In-depth analysis of Keil C51 bus peripheral operation problems

Previous article:Application of Single Chip Microcomputer in Baud Rate Converter
Next article:Constructing a real-time operating system for 51 single-chip microcomputer

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号