AVR MCU C Language Programming Skills

Publisher:andyliow1980Latest update time:2013-01-10 Source: 21IC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Reference address:AVR MCU C Language Programming Skills

Previous article:AVR MCU TC0 fast PWM
Next article:Features of C language for AVR microcontrollers

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号