Introduction
The 8051 core microcontroller is a general-purpose microcontroller that occupies a large market share in China. Keil has been the most successful in using C language for the 51 core microcontroller. Due to the particularity of the storage structure of the 51 core microcontroller, the use of variables in Keil C51 is different from that in standard C. Correct use of variables is conducive to obtaining efficient target code. The following is a detailed introduction to the use of variables in Keil C51.
1 The relationship between CPU storage structure and variables
Variables need storage space, and different storage spaces make the working efficiency of variables different when used.
The typical operating environment of standard C is the 8086 (including IA-32 series) core, whose storage structure is that there are registers inside the CPU and memory outside. The access speed of registers is much higher than the access speed of memory. In standard C, variables without special definitions are placed in memory. Using register can force variables to be stored in registers. For variables that are used frequently and in small quantities, this storage mode can be selected to obtain higher working efficiency.
In contrast, the storage structure of the 51 core microcontroller is somewhat strange. It has three storage spaces: program memory space (64 KB including on-chip and off-chip), off-chip data memory space (64KB), on-chip data memory and special function register space. It does not have real registers. Its registers are actually part of the on-chip data memory (such as R0~R7) and special function registers (such as A, B, etc.). Therefore, the use of variables in Keil C51 is very different from standard C.
2 Keil C51 variable analysis
Keil C51 supports most of the original variable types of standard C, but adds a variety of storage types for these variables, and also adds some variables that are not in standard C.
2.1 Keil C51's new variable storage type
The format of defining variables in Keil C51 is as follows:
[Storage type] Data type [Storage type] Variable name table;
Among them, [Storage type] is not in standard C. There are 6 types of [Storage type], which are introduced as follows:
①data. Store the variable in the on-chip directly addressable data memory. Using this storage mode, the target code has the fastest access speed to variables.
②bdata. Store variables in the on-chip bit-addressable data memory. In the target code, variables can be easily bit-processed, and are the same as data when no bit processing is performed.
③idata. Store variables in the on-chip indirectly addressed data memory. In the 52 core, when the on-chip directly addressed data memory is insufficient, the 128-byte indirectly addressed data memory can be used. The access speed is generally slower than data, but it has the largest on-chip data memory space; in the 51 core, since there is no separate indirect addressable data memory area, idata is no different from data.
④xdata. Store variables in the off-chip data memory. In the target code, only "MOVX A, @DPTR" and "MOVX@DPTR, A" instructions can be used to access variables. The access speed is the slowest, but the storage space is the largest (64KB).
⑤pdata. Store variables in the first page (00H~FFH) of the off-chip data memory. In the target code, you can use the "MOVX A, @Ri" and "MOVX@Ri, A" instructions to access variables. The access speed is the same as xdata, and the storage space is 256 bytes.
⑥code. Store the variable in the program memory. In the target code, you can only use the MOVC instruction to access the variable. The variable is stored in the program memory, which is non-volatile and read-only.
2.2 Keil C51's new pointer variable storage type
The pointer variable format in Keil C51 is as follows:
data type [data storage type] * [pointer storage type] identifier;
among them, [data storage type] and [pointer storage type] are not in standard C. [Data storage type] defines the space for data (i.e. addressing object) storage, and [pointer storage type] defines the space for pointer storage itself. If [data storage type] is not used, the pointer is a general pointer, occupying 3 bytes; if [data storage type] is used, the pointer is a memory-based pointer, occupying 1 to 2 bytes.
2.3 Keil C51's new variable type
bit: bit variable. Stored in a certain bit of the bit-addressable byte (20H~2FH) of the on-chip data memory, this variable has a high practical value in real-time control.
sfr: Special function register variable. Stored in the on-chip special function register, used to read and write the special function register.
sbit: Special function register bit variable. Stored in a certain bit of the bit-addressable byte (address divisible by 8) of the on-chip special function register, used to read and write the bit-addressable bit of the special function register.
sbitl6: 16-bit special function register variable. Stored in the low address of 2 consecutive bytes of the on-chip special function register, this variable type is rarely used.
The above new variable types in Keil C51 do not support array and pointer operations. [page]
In Keil C51, the storage mode of variables is an option. If this option is not used, Keil C51 will automatically perform optimal allocation during compilation. However, this processing method has the following disadvantages:
① The system does not know the frequency of use of various variables. It is possible that the variable with high frequency of use uses the off-chip storage method with slow access speed, while the variable with high frequency of use uses the on-chip storage method, which reduces the running efficiency of the program;
② When using pointer addressing, since the storage method of the addressing object is unknown, general pointers have to be used. In Keil C51, general pointers take up 1 to 2 bytes more, and the storage method must be judged when used, which increases the addressing operation time.
If the storage type can be defined while defining the variable, the storage space of the 51 core microcontroller can be used efficiently to obtain high-quality target code.
4 How to use Keil C51 variables
4.1 Global variables and static local variables
Global variables are generally used in multiple functions and are valid during the entire program execution. Although static local variables are only used in one function, they are also valid during the entire program execution. For these variables, you should try to choose data type, so that you can use direct addressing instructions to access them in the target code, get the highest access speed, and improve the efficiency of the program. For example, a global variable n_g that stores the number of people is often used in multiple functions. It can be defined as follows:
unsigned int data n_g; // Use the "MOV XXH, ..." instruction to assign values to n_g
4.2 Arrays (including global and local)
Arrays are generally defined using the idata storage type, and the "MOV@Ri" instruction is used for indirect addressing in the target code. If an error is reported during compilation due to too many array elements, you can use pdata and xdata storage types instead.
It is not very meaningful to define an array as a data storage type, because since you use an array, you want to be able to access the array elements based on a certain independent variable. For example, when defining X[100], it is generally for the purpose of being able to use X[i] (i is a variable) to access it, so indirect addressing must be used in the target code, so there is no need to use the data storage type for arrays. Even if the data storage type is used, indirect addressing instructions must still be used in the target code. The array is defined as idata storage type. When using 52 cores and the on-chip data memory is insufficient, the on-chip data storage space that can only be addressed indirectly will be used. In this way, the processing speed will not be reduced, and the available storage space will be expanded.
4.3 Data for table lookup
The characteristics of this type of data are that it needs to remain unchanged at all times and is read-only when used, so it should be defined as code type. For example, a font table:
there is no difference in storage between global and local code type variables.
4.4 Non-static local variables
Non-static local variables are only used within a function, and the variables are also released when the function is exited.
If the system uses small storage mode, these variables can be stored without description, and the compiler software will decide according to the optimal principle. Because non-static local variables that are only used within a function may use working registers R0~R7, this will be faster and save more storage space. For example:
unsigned char i, j; //The system will use R0~R7 to store i and j as much as possible.
If the system uses compact or large storage mode, these variables should be defined as data storage mode to prevent the system from defining them as pdagta or xdata mode and reducing work efficiency.
4.5 Pointer
As mentioned above, there are two storage types when defining pointer variables: data storage type, which describes the storage type of the addressed object; pointer storage type, which describes the storage type of the pointer itself. When the data storage type is xdata, the pointer itself occupies 2 bytes; when the data storage type is pdata and idata and other on-chip storage types, the pointer itself occupies 1 byte; if the data storage type is not specified, the pointer itself will occupy 3 bytes. Therefore, when using pointers in KeilC51, you should try to define the data storage type, but pay special attention to the fact that the data storage type in the pointer must be consistent with the storage type of the addressed object. Pointers are frequently used, and they need to be constantly set, modified and used, so their own storage type should be selected as data type. For example, when defining an array, its storage type is also defined. When addressing it with a pointer later, the array storage type is added to the pointer data type. The method is as follows:
[page]
4.6 Ambiguous variablesIn standard C, if you want to use an ambiguous variable, you can only use enumeration types. For example,
when the above program is used in Keil C51, although the variable t has only two states, 0 and 1, it still occupies one byte in the target code. This processing method not only wastes storage resources, but also prolongs the processing time. This is not a big problem for the 8086 core, but it cannot be ignored in the 51 core with limited resources and low running speed. The following method can be used in Keil C51:
The effects of these two methods are exactly the same, but in the target code, the variable t only occupies 1 bit (that is, 1/8 byte), and because there are bit processing instructions in the 51 core microcontroller instruction system, the generated target code occupies less memory and runs faster.
4.7 Special function register variables (including bit variables)
Among the special function registers, the accumulator A, register B, stack pointer SP and data pointer DPTR are used by the system and are not provided to users in C51. Other special function registers can be defined as variables using sfr, and the bits whose addresses are divisible by 8 can also be defined as bit variables using bsfr. By accessing these variables, you can read and write the special function registers and their bit-addressable bits, so as to achieve the purpose of operating the hardware inside the microcontroller. For standard 51 core microcontrollers, these special function register variables have been defined in the header files reg51.h, reg52.h or other header files. Users can use #include to include this header file and then use it. Now many 51 core compatible microcontrollers have expanded more special function registers, which require users to define them by themselves. For specific methods, please refer to the device instructions.
4.8 External data memory variables
If set to pdata and xdata storage types, the variables will be stored in the off-chip data memory. These two storage types have the slowest access speed and should not be used unless absolutely necessary. When using these two storage types, pay attention to using them only to save the original data or final results, minimize the number of accesses to them, and do not use them for intermediate results that need to be accessed frequently.
4.9 Other hardware extended with external data memory addresses
Other hardware extended outside the microcontroller generally borrows the external data memory address and is expressed in the form of external data memory units. For these hardware, pointers can be used for read and write operations. For example:
Conclusion
Keil C51 variables have added storage types, which makes them slightly more complicated to use than standard C. In Keil C51, different storage types of variables require different access times. Since the C51 core microcontroller has few resources and slow speed, the impact of variable storage types on system operating speed cannot be ignored. Based on the understanding of the relationship between variables and microcontroller storage structures, according to the program's requirements for the use of variables, reasonably selecting the variable storage type can achieve higher work efficiency on the same hardware.
Previous article:Application of C8051F020 in the Design of SD Card Main Controller
Next article:Application of MCS-51 Single Chip Microcomputer and Wireless Modulator
Recommended ReadingLatest update time:2024-11-16 22:24
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- How to read electrical control circuit diagrams (Classic best-selling books on electronics and electrical engineering) (Zheng Fengyi)
- MCU C language programming and Proteus simulation technology (Xu Aijun)
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
- At the same frequency, the impedance of a large capacitor is smaller than that of a small capacitor, and it has better filtering performance for high frequencies. Why do we need to connect a small capacitor in parallel?
- [GD32E503 Review] Part 5: FreeRTOS Project Creation
- I heard that ST has released a LoRa development board, NUCLEO-WL55JC2. Why can't I find any information about it anywhere in the world?
- MT7603EN Supply
- When stm32 writes MQTT, it subscribes to multiple topics. How should the program distinguish different topics when receiving?
- Application Note - Optimizing Capacitance and AC Impedance Measurements Using the 4200A-SCS Parameter Analyzer
- SPI interface driver based on TI-RTOS and separate control of CS pin
- Prize live broadcast: TI low-power MCU products and Zigbee wireless solutions are now open for registration~
- FPGA_100 Days Journey_VGA Design
- CCS Basics Tutorial