The Flash and Ram resources of the microcontroller are limited.

In general, the Flash and Ram resources of the microcontroller are limited. For this reason, we must try to extract all of its resources and optimize its performance. Then we should optimize the program as much as possible, and try to follow the following. What time is it.

1 Use as small a data type as possible

Can use unsiged without signing;

Can use char without int;

Can not be used without floating;

It is possible to use bit operations without counting.

2 use self-addition, decrement instructions

Usually, self-adding, decrementing instructions, and compound assignment expressions (such as a-=1 and a+=1) can generate high-quality program code. The compiler can usually generate instructions such as inc and dec. For instructions such as a=a+1 or a=a-1, many C compilers generate two to three bytes of instructions.

3 reduce the intensity of the operation

You can replace the original complex expression with an expression that has a small amount of computation but the same function.

(1) Remainder operation

N= N %8 can be changed to N = N &7

Note: Bit operations can be completed in one instruction cycle, and most of the C compiler's "%" operations are done by calling subroutines, which are long in code and slow in execution. Usually, only the remainder of the 2n square is required, and the bit manipulation method can be used instead.

(2) Square operation

N=Pow(3,2) can be changed to N=3*3

Note: In a microcontroller with a built-in hardware multiplier (such as the 51 series), the multiplication operation is much faster than the square operation, because the square of the floating point number is realized by calling the subroutine, and the subroutine of the multiplication operation is larger than the square operation. The subroutine code is short and the execution speed is fast.

(3) Using displacement instead of multiplication

N=M*8 can be changed to N=M<<3

N=M/8 can be changed to N=M>>3

Note: Usually if you need to multiply or divide by 2n, you can use the shift method instead. If multiplied by 2n, the left-shifted code can be generated, and the multiplication and division subroutine is called by multiplying by other integers or dividing by any number. Using the shift method to get the code is more efficient than calling the code generated by the multiplication and division subroutine. In fact, as long as it is multiplied or divided by an integer, the result can be obtained by shifting.

For example, N=M*9 can be changed to N=(M<<3)+M;

(4) The difference between self-reduction and self-reduction

For example, the delay functions we usually use are implemented by self-adding.

Void DelayNms(UINT16 t)

{

UINT16 i,j;

For(i=0;i

Define MAX(A,B) {(A)>(B)?(A):(B)}

Description: The difference between a function and a macro function is that the macro function takes up a lot of space, and the function takes up time. Everyone needs to know that the function call is to use the system stack to save the data. If there is a stack check option in the compiler, generally the assembly header will be embedded in the function header to check the current stack. At the same time, the CPU should also be in the When the function is called, the current scene is saved and restored, and the push and pop operations are performed. Therefore, the function call requires some CPU time. The macro function does not have this problem. The macro function is only embedded in the current program as pre-written code, and does not generate function calls, so it only takes up space. This phenomenon is especially prominent when the same macro function is frequently called.

4. Use the algorithm appropriately

If there is an arithmetic problem, ask for the sum of 1~100.

As a programmer, we will not hesitate to click the keyboard to write the following calculation method:

UINT16 Sum(void)

{

UINT8 i,s;

For(i=1;i<=100;i++)

{

s+=i;

}

Return s;

}

Obviously everyone will think of this method, but the efficiency is not satisfactory, we need to use our brains to solve problems and improve the efficiency of computing.

UINT16 Sum(void)

{

UINT16 s;

s=(100 *(100+1))>>1;

Return s;

}

The result is obvious, the same results different calculation methods, the operating efficiency will be greatly different, so we need to maximize the mathematical efficiency of the program.

5. Replace the array with a pointer

In many cases, pointer arithmetic can be used instead of array indexes, which often produces fast and short code. Compared to array indexes, pointers generally make code faster and take up less space. The difference is more pronounced when using multidimensional arrays. The following code works the same, but the efficiency is different.

UINT8 szArrayA[64];

UINT8 szArrayB[64];

UINT8 i;

UINT8 *p=szArray;

For(i=0;i<64;i++)szArrayB=szArrayA;

For(i=0;i<64;i++)szArrayB=*p++;

The advantage of the pointer method is that after the address of szArrayA is loaded into pointer p, only p increments are required in each iteration. In the array index method, complex operations based on the i-valued array subscript must be performed in each loop.

6. Casting

The first essence of the C language is the use of pointers. The second essence is the use of coercion. Proper use of pointers and casts not only provides program efficiency, but also makes the program more concise, because coercion plays an important role in C programming. The status of the following, there will be five more typical examples as a explanation.

Example 1: Converting a signed byte integer to an unsigned byte integer

UINT8 a=0;

INT8 b=-3;

a=(UINT8)b;

Example 2: In big endian mode (the 8051 series MCU is big endian mode), the array a[2] is converted to an unsigned 16-bit integer value.

Method 1: Use the displacement method.

UINT8 a[2]={0x12,0x34};

UINT16 b=0;

b=(a[0]<<8)|a[1];

Result: b=0x1234

Method 2: Force type conversion.

UINT8 a[2]={0x12,0x34};

UINT16 b=0;

b= (UINT16 )a; //Forced conversion

Result: b=0x1234

Example 3: Save the structure data content.

Method 1: Save one by one.

Typedef struct _ST

{

UINT8 a;

UINT8 b;

UINT8 c;

UINT8 d;

UINT8 e;

}ST;

ST s;

UINT8 a[5]={0};

Sa=1;

Sb=2;

Sc=3;

Sd=4;

Se=5;

a[0]=sa;

a[1]=sb;

a[2]=sc;

a[3]=sd;

a[4]=se;

Result: The contents stored in array a are 1, 2, 3, 4, 5.

Method 2: Force type conversion.

Typedef struct _ST

{

UINT8 a;

UINT8 b;

UINT8 c;

UINT8 d;

UINT8 e;

}ST;

ST s;

UINT8 a[5]={0};

UINT8 p=(UINT8 )&s;//Forced conversion

UINT8 i=0;

Sa=1;

Sb=2;

Sc=3;

Sd=4;

Se=5;

For(i=0;i

Define Perror(FUN) printf("Err:%s %s %d: %s", FILE, func, LINE, FUN) linux perror function implementation, here added the wrong file location, where the function, throwing an error call The function FUN.

The usage of macro and ##

Define STR(s) #s

Define CONS(a, b) int(a##e##b)

Printf(STR(vck));//output vck

Printf("%d", CONS(2,3)); //2e3 output 2000

These methods are commonly used and will inevitably make your code more concise and efficient!


Soft Starter

Soft Starter,Ac Motor Soft Starter,Soft Starter For Machinery,3-Phase Soft Starter

Zhejiang Kaimin Electric Co., Ltd. , https://www.ckmineinverter.com