1, using short variables
One of the most basic ways to increase code efficiency is to reduce the length of variables. When programming in C, we are all accustomed to using the int type for loop control variables. This is a huge waste for 8-bit microcontrollers. You should carefully consider the range of possible values ​​for the variables you declare and select the appropriate ones. The type of variable, it is clear that often used variables should be unsigned char, only occupy a byte.
2, using unsigned types
Why use an unsigned type? The reason is that the 8051 does not support symbolic operations, and the program should not use external code that contains signed variables. In addition to selecting variable types based on their length, you also need to consider whether or not the variables will be used for negative numbers. If your program can not need negative numbers then define variables as unsigned types.
Avoid floating point pointers
Using 32-bit floating-point numbers on 8-bit operating systems is not worth the effort. You can do this, but it will waste a lot of time, so when you want to use floating-point numbers in the system, you have to ask yourself whether it is necessary, you can eliminate floating-point pointers by increasing the numerical order of magnitude and using integer operations. Processing ints and longs is much more convenient than dealing with doubles and floats. Your code will execute faster without having to wire up modules that handle floating-point pointers. If you must use floating-point hands, you should use the 80320 of the Siemens 80517 and Dallas Semiconductor, which are already logarithmic, to handle optimized microcontrollers. If you have to add floating point pointers to your code, then your code length will increase the speed of the program execution will be slower. If the floating-pointer operation can be interrupted, you must ensure that either the floating-pointer operation is not used in the interrupt or the interrupt pointer is pushed onto the stack using the fpsave instruction before the interrupt program is executed. Use the fprestore instruction to resume the pointer after the interrupt program is executed. Another way is to disable the use of interrupts when you want to use floating-point arithmetic such as sin(), and enable it after the execution of the arithmetic program.
4, using the bit variable
For some flags bits should be used instead of unsigned char, which will save your memory, you don't have to waste 7-bit memory, and bit variables only require one processing cycle to access them in RAM.
5, use local variables instead of global variables
Defining a variable as a local variable is more efficient than a global variable. The compiler allocates storage space for the local variables in the internal storage area, and allocates the storage space for the global variables in the external storage area. This reduces your access speed. The reason for avoiding the use of global variables is that you must adjust the use of global variables during the processing of your system, because in interrupt systems and multitasking systems, more than one process uses global variables.
6. The way to allocate internal memory for variables is to reduce the length of variables. When programming in C, we are all accustomed to using the int type for loop control variables. This is a huge waste for 8-bit microcontrollers. You should carefully consider the range of possible values ​​for the variables you declare and select the appropriate ones. The type of variable, it is clear that often used variables should be unsigned char, only occupy a byte.
2, using unsigned types
Why use an unsigned type? The reason is that 8051 does not support
When you use pointers in your program, you specify the type of pointers to determine which area they point to, such as the XDATA or CODE fields, so that your code will be more compact because the compiler does not have to determine the memory pointed to by the pointer because you Explained.
8, use the order
For some simple operations, such as variable looping, the compiler provides some commands for the user. Many commands are directly related to assembly instructions. Others are more complicated and compatible with ANSI. All these commands are reentrant functions. You can do any Locally-invoked calls which correspond to the single-byte cyclic shift instructions RL A and RR A are _crol_ circular shift left and _cror_ (circular right shift). If you want to cyclically shift variables of type int or long, the call will be more complicated and the execution time will be longer for _irol_, _iror_ for int types, and _lrol_, _lror_ for long types. In C, there is also a _testbit_ command like the JBC instruction in the assembly. If the parameter is set, it will return 1; otherwise, it will return 0. This command is very useful for checking the flag, and makes the C code more readable. The sexual order will be directly converted into JBC instructions.
#include void serial_intr(void) interrupt 4 {if (!_testbit_(TI)) { // Whether it is sending interrupt P0=1; // flipping P0.0_nop_(); // waiting for one instruction cycle P0=0;.. .}if (!_testbit_(RI)) {test=_cror_(SBUF, 1); // Rotate data in SBUF // right by one...}}
9, use the macro substitution function
For small pieces of code, like enabling certain circuits or reading data from latches, you can make the program more readable by using macros instead of functions. You can define the code in a macro, which looks more like Like functions. When the compiler encounters a macro, it substitutes the previously defined code to replace the macro. The name of the macro should describe the operation of the macro. When you need to change the macro, you only need to modify the macro definition.
#define led_on() {\led_state=LED_ON; \XBYTE[LED_CNTRL] = 0x01;}#define led_off() {\led_state=LED_OFF; \XBYTE[LED_CNTRL] = 0x00;}#define checkvalue(val) \( (val <MINVAL || val > MAXVAL) ? 0 : 1 )
Macros make it easier to access multi-level structures and arrays. Macros can be used to replace complex statements that are often used in programs to reduce the amount of typing you do and to have better readability and maintainability.
10, memory mode
C51 provides three memory modes for storing variables, process parameters, and assigning reentrant functions to the stack. You should try to use the small memory mode. Few applications need to use the other two modes, as there are large reentrant function stack systems. In general, if the amount of memory required by the system is less than the number of internal RAM, it should be compiled in a small storage mode. In this mode, the DATA section is the default storage section for all internal variables and global variables. All parameter passing occurs in the DATA section. If functions are declared as reentrant functions, the compiler allocates space for them in the internal RAM. The advantage of this mode is that the data access speed is very fast, but only 120 bytes of storage space is available for your use. There are a total of 128 bytes, but at least 8 bytes are used by the register bank. You need to Open up enough stack for program calls. If your system has 256 bytes or less of external RAM you can use compressed storage mode. As a result, if not stated, the variables will be allocated in the PDATA section. This mode will increase the amount of RAM you can use. The data store outside the XDATA section will still be very fast, and the parameter passing of the variables will be internal. RAM, so that the storage speed will be faster, the data of the PDATA segment is addressed indirectly through R0 and R1, faster than using DPTR. In the large storage mode, the default storage area for all variables is the XDATA segment. Keil C uses the internal register set as much as possible for parameter transfer. The number of parameters that can be passed in the register set is the same as the compression storage mode. The re-entry function's simulation stack will have the slowest access to the XDATA segment data in XDATA, so Carefully consider where the variables should be stored so that the data storage speed is optimized.
11, mixed storage mode
Keil allows mixed storage mode, which is very useful in large storage mode. In large memory mode, some processes require high data transfer speeds. I defined the process in a small memory mode register, which allows the compiler to allocate memory for the process's local variables in the internal RAM and ensure that all parameters are passed through the internal RAM. Although the code length after compiling with mixed mode does not change much, this effort is worthwhile just as if you could declare the process as a small mode in large mode. You can declare the process as if it were in small mode. Compression mode or large mode, which is generally used in the process that requires a lot of storage space, so that the local variables in the process will be stored in the external storage area, you can also allocate variables in the XDATA segment through the variable declaration in the process .
12, runtime
The runtime library provides a lot of short and simple functions, you can use them very easily, and it's hard for you to write better code yourself. It is worth noting that some functions in the library are not reentrant functions. If they are interrupted when these functions are executed, and they are called in the interrupt program, unexpected results will be obtained. And this kind of error is difficult to find, it is best to prohibit the use of these function interrupts.
Pcb Board,Circuit Board,Pcb Electronics,Custom Pcb
Cixi Zhongyi Electronics Factory , https://www.zybreadboard.com