How can microcontrollers improve programming efficiency and optimize programs?

Publisher:电子创新者Latest update time:2024-03-04 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.


Reference address:How can microcontrollers improve programming efficiency and optimize programs?

Previous article:CS5213 chip characteristics
Next article:Main performance features and application solutions of high-performance motion control chip IRMCF343

Latest Microcontroller Articles
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号