AVR MCU C Language Programming Skills
1. Choose the right algorithm and data structure. You should be familiar with the algorithm language and know the advantages and disadvantages of various algorithms. For specific information, please refer to the corresponding reference materials. There are many computer books that introduce them. Replacing the slower sequential search method with a faster binary search or random 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. . It is also important to choose a suitable data structure. For example, if you use a large number of insertion and deletion instructions in a bunch of randomly stored numbers, it will be much faster to use a linked list. Arrays and pointer statements have a very close relationship. Generally speaking, pointers are more flexible and concise, while arrays are more intuitive and easy to understand. For most compilers, the code generated by using pointers is shorter and more efficient than that of arrays. But in Keil, on the contrary, the code generated by using arrays is shorter than that of using pointers. .
2. Use the smallest data type possible. If you can define a variable with a character type (char), do not use an integer type (int); if you can define a variable with an integer type, do not use a long int; if you can avoid using a floating point type (float), do not use a floating point type (float). Of course, do not exceed the scope of the variable after defining it. If you assign a value that exceeds the scope of the variable, the C compiler will not report an error, but the program will run incorrectly, and such errors are difficult to find. In ICCAVR, you can set the use of printf parameters in Options, try to use basic parameters (%c, %d, %x, %X, %u, and %s format specifiers), use less long integer parameters (%ld, %lu, %lx, and %lX format specifiers), and try not to use floating point parameters (%f). The same applies to other C compilers. If other conditions remain unchanged, using the %f parameter will increase the amount of generated code and reduce the execution speed. 3. Use self-increment and self-decrement instructions. Usually, the use of self-increment and self-decrement instructions and compound assignment expressions (such as a-=1 and a+=1, etc.) can generate high-quality program codes. Compilers can usually generate instructions such as inc and dec. When using instructions such as a=a+1 or a=a-1, many C compilers will generate two to three bytes of instructions. The codes generated by the above writing methods are the same in ICCAVR, GCCAVR, IAR and other C compilers applicable to AVR chips, and can also generate high-quality inc and dec codes.
4. Reduce the intensity of operations. You can replace the original complex expressions with expressions with small amount of operations but the same functions. As follows: (1) Remainder operation. a=a%8; can be changed to: a=a7; Note: Bit operation only needs one instruction cycle to complete, while most C compilers call subroutines to complete the "%" operation, which has long code and slow execution speed. Usually, if you only need to find the remainder of 2n squares, you can use bit operation methods instead. (2) Square operation 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 square operations, because the square of floating-point numbers is implemented by calling subroutines. In AVR microcontrollers with built-in hardware multipliers, such as ATMega163, multiplication operations only need 2 clock cycles to complete. Even in AVR microcontrollers without built-in hardware multipliers, the subroutine for multiplication operations has shorter code and faster execution speed than the subroutine for square operations. If you want to calculate the cube, such as: a=pow(a,3.0); change it to: a=a*a*a; the efficiency improvement will be more obvious. (3) Use shifting to implement multiplication and division operations 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 left shift code, and multiply by other integers or divide by any number, call the multiplication and division subroutines. 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 you multiply or divide by an integer, you can use the shifting method to get the result, such as: a=a*9 can be changed to: a=(a<<3)+a
5. Loop (1) For some tasks that do not require loop variables to participate in the calculation, they can be placed 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 grouped together and placed in an initialization program called init. (2) Delay function: The commonly used delay function is in the form of self-increment: void delay (void) { unsigned int i; for (i=0;i<1000;i++) ; } Change it to a self-decrement delay function: void delay (void) { unsigned int i; for (i=1000;i>0;i--) ; } The delay effects of the two functions are similar, but almost all C compilers generate 1 to 3 bytes less code for the latter function than for the former, because almost all MCUs have instructions for branching to 0, and the latter method can generate such instructions. The same is true when using a while loop. Using a self-decrement instruction to control the loop will generate 1 to 3 fewer letters of code than using a self-increment instruction to control the loop. However, when there are instructions to read and write arrays through the loop variable "i" in the loop, the array may exceed the bounds when using the pre-decrement loop, so be careful. (3) While loop and do...while loop When using the while loop, there are two loop forms: unsigned int i; i=0; while (i<1000) { i++; //user program} or: unsigned int i; i=1000; do i--; //user program while (i>0); Of these two loops, the length of the code generated after compiling using the do...while loop is shorter than that of the while loop.
6. Table lookup is generally not used in programs for very complex operations, 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 both time and resources, table lookup should be used as much as possible, and the data table should be placed in the program storage area. If it is difficult to directly generate the required table, try to start it at the beginning to reduce the workload of repeated calculations during program execution.
7. Other methods, such as using online assembly and storing strings and some constants in program memory, are beneficial to optimization.
Previous article:AVR MCU TC0 fast PWM
Next article:Features of C language for AVR microcontrollers
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!
- 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
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- GD32E231 DIY Part 3: LED Smooth Dimming Principle
- Far away, yet close at hand
- The power cord is too long and the power-on voltage is too high. Should I use a TVS tube?
- What is the essence of GND in the circuit?
- EEWORLD University - i.MX Linux Development Practical Guide
- Encoder Problems
- Introduction to IC Packaging and Testing Process
- TI C6000 Optimization Advanced: Loops are the most important!
- A zero voltage platform appears on the secondary side of the forward converter, causing large oscillations. What might be the cause? How can I solve it?
- DIY a ESP32 game console based on MicroPython