When designing the 8051 microcontroller application system, programming often requires directly operating the various memory address spaces.
The object code generated after the C51 program is compiled has a floating address, and its absolute address can only be determined after BL51 connection positioning.
In order to directly operate on any specified memory address in the C51 program, the extended keyword "at", pointer, predefined and connection positioning control commands can be used.
Among these methods, I think the simplest and most effective method is to use the "_at_" keyword to specify the absolute address of the variable memory space. The general format is as follows:
[Memory type] Data type identifier_at_ address constant
in:
(1). Memory type: all types that C51 can recognize, such as idata, data, xdata, etc., it is best not to omit them.
(2). Data type: You can use basic types such as int, long, float, etc., of course, you can also use arrays,
For complex data types such as structures, I think generally using unsigned int can solve many problems.
(3).Identifier: It is the name of the variable to be defined, and the programmer decides it himself.
(4). The address constant is the absolute address of the memory to be directly operated and must be located within a valid memory space.
Note: Variables cannot be initialized, they can only be global variables. Generally, do not use them lightly to avoid errors.
Summarize:
1. Use self-increment and self-decrement instructions
Usually, using self-increment, self-decrement instructions and compound assignment expressions (such as a-=1 and a+=1, etc.) can generate high-quality program code. Compilers can usually generate instructions such as inc and dec, and use Instructions such as a=a+1 or a=a-1, many C compilers will generate two to three byte instructions. The codes generated by the above writing methods are the same in ICCAVR, GCCAVR, IAR and other C compilers suitable for AVR single-chip, and they can also generate high-quality inc and dec codes.
2. Look up the table
Generally, very complex operations are not performed in the program, such as multiplication, division and square root of floating point numbers, as well as interpolation operations of some complex mathematical models. For these operations that consume time and resources, table lookup methods should be used as much as possible. , and place the data table in the program storage area. If it is difficult to directly generate the required table, try to start it beforehand to reduce the workload of repeated calculations during program execution.
3. Choose appropriate algorithms and data structures
You need to be familiar with the algorithm language and know the advantages and disadvantages of various algorithms. Replacing the slower sequential search method with the faster binary search or out-of-order search method, and replacing the insertion sort or bubble sort method with quick sort, merge sort or root sort can greatly improve the efficiency of program execution. Choose a suitable one. The data structure is also very important. Generally speaking, pointers are more flexible and concise, while arrays are more intuitive and easy to understand. For most compilers, using pointers generates shorter code and more efficient execution than using arrays. But in Keil the opposite is true, using arrays generates shorter code than using pointers
4. Use the smallest data type possible
Variables that can be defined using character type (char) should not be defined using integer type (int) variables; variables that can be defined using integer type variables should not be defined using long integer type (long int), and floating point type (float) should not be used. ) variables, do not use floating-point variables. Of course, after defining a variable, do not exceed the scope of the variable. If you assign a value beyond the scope of the variable, the C compiler will not report an error, but the program running result will be wrong, and such errors are difficult to find.
5. Reduce the intensity of calculations
You can replace complex expressions with expressions that require less computation but have the same functionality. as follows:
(1), remainder operation.
For example: a=a%8; can be changed to: a=a&7;
Note: Bit operations only require one instruction cycle to complete, while most "%" operations of C compilers are completed by calling subroutines, which results in long code and slow execution speed. Usually, if the only requirement is to find the remainder of 2n square, bit operations can be used instead.
(2), square operation
For example: a=pow(a,2.0); can be changed to: a=a*a;
Note: In microcontrollers with built-in hardware multipliers (such as the 51 series), multiplication operations are much faster than squaring operations, because the squaring of floating point numbers is implemented by calling subroutines. In AVRs with built-in hardware multipliers, In microcontrollers, such as ATMega163, multiplication operations can be completed in only 2 clock cycles. Even in an AVR microcontroller without a built-in hardware multiplier, the subroutine for multiplication operation has shorter code and faster execution speed than the subroutine for square operation.
If it is to find the third power, such as: a=pow(a,3.0); change to: a=a*a*a;
The improvement in efficiency is more obvious.
(3) Use shifting to implement multiplication and division operations
For example: a=a*4;b=b/4; can be changed to: a=a《2;b=b》2;
Note: Usually if you need to multiply or divide by 2n, you can use the shifting method instead. In ICCAVR, if you multiply by 2n, you can generate a left shift code, and when you multiply by other integers or divide by any number, you call the multiplication and division subroutine. The code generated by the shifting method is more efficient than the code generated by calling the multiplication and division subroutines. In fact, as long as it is multiplied or divided by an integer, the result can be obtained by shifting. For example: a=a*9 can be changed to: a=(a<3)+a
6. Cycle
(1), circular language
For some tasks that do not require loop variables to participate in operations, you can put them outside the loop. The tasks here include expressions, function calls, pointer operations, array access, etc. All operations that do not need to be performed multiple times should be gathered together. Put it in an init initialization program.
(2) Delay function:
Commonly used delay functions are in the form of self-adding:
The delay effects of the two functions are similar, but the code generated by almost all C compilers for the latter function is 1 to 3 bytes less than the former code, because almost all MCUs have instructions for 0 transfer. Such instructions can be generated using the latter approach. The same is true when using while loops. Using self-decrement instructions to control the loop will generate 1 to 3 fewer letters of code than using self-increment instructions to control the loop. However, when there are instructions to read and write the array through the loop variable "i" in the loop, when using the pre-decrement loop, the array may go out of bounds, so please pay attention.
(3)while loop and do...while loop
There are two loop forms when using a while loop:
Of the two types of loops, the length of the code generated after compilation using the do...while loop is shorter than that of the while loop.
7. Others
For example, using online assembly and saving strings and some constants in program memory are beneficial to optimization.
Previous article:CS5213 chip characteristics
Next article:Main performance features and application solutions of high-performance motion control chip IRMCF343
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- [RVB2601 Creative Application Development] Handheld Game Console (Bonus-01) Python Program to Convert Files into Arrays
- Blue line appears in vhdl bidirectional bus buffer simulation
- Different backlight structure types in monochrome LCD screens
- 3rd Anniversary
- DSP Flash API Steps
- MSP430 - Timer_A timer interrupt program
- Buck switching power supply MOS tube gate has pwm signal, but cannot conduct normally
- Motor Driver
- Sharing on the serious problem of LM2611 output voltage heating
- How much do you know about the "ADI Inside" behind these popular healthcare products?