[51 single chip microcomputer] Keil C51 the use of variables in the detailed

Source: Internet
Author: User

Introduction
8051 Core SCM is a universal monolithic microcomputer, which occupies a large market share in China. Keil has been the most successful in the study of the use of C language for 51-core microcontroller. Due to the particularity of the storage structure of the 51-core microcontroller, the use of variables in Keil C51 differs from standard C. The correct use of variables facilitates the acquisition of efficient target code. Here is a detailed description of how variables are used in Keil C51.

1 The relationship between CPU storage structure and variables
Variables need to have storage space, the different storage space makes the variable work efficiency is also different.
Typical operating environment of standard C is 8086 (including IA-32 series) kernel, its storage structure is internal CPU registers, external memory, register access speed is much higher than the memory access speed. In standard C, variables that are not specifically defined are placed in memory, and registers can be used to force variables to be stored in registers, which can be chosen for more efficient use of particularly frequent and infrequent variables.
In contrast, the 51-core microcontroller storage structure is a bit strange, it has 3 storage space: Program memory space (on-chip, off-chip), off-chip data memory space (64KB), on-chip data memory and special function register space. It has no real register, and 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, using variables in Keil C51 differs greatly from standard C.

2 Keil C51 Variable analysis
Keil C51 supports most of the original variable types in standard C, but adds a variety of storage types to these variables, as well as some variables that are not standard C.
2. 1 Keil C51 new variable storage type
The format of the variables defined in Keil C51 is as follows:
[Storage kind] Data type [storage type] variable name table;
where [storage type] is not in standard C, there are 6 types of [storage type], respectively, as follows:
①data. The variables are stored in a directly addressable data memory on-chip. With this storage mode, the variable is accessed the fastest in the target code.
②bdata. The variables are stored in the on-chip addressable data memory. In the target code, a variable can be conveniently bit-handled, the same as data when no bit processing occurs.
③idata. The variables are stored in an on-chip, indirectly addressable data memory. In the 52 kernel, when the on-chip direct addressing data memory is not enough, 128 bytes of indirect addressable data memory can be used, the access speed is generally slower than data, but has the largest on-chip data memory space, in the 51 kernel because there is no separate indirect addressing data memory area, idata and the database is not different.
④xdata. The variables are stored in an off-chip data memory. Only "MOVX A, @DPTR" and "[email protected],a" directives are used to access variables in the target code, with the slowest access, but the maximum storage space (64KB).
⑤pdata. The variables are stored in the first page (00H~FFH) in the off-chip data storage. The target code can use "MOVX A, @Ri" and "[email protected],a" Instructions to access variables, access speed is the same as xdata, storage space is 256 bytes.
⑥code. Store the variable in program memory. Only the MOVC instruction can be used to access variables in the target code, because the variables are stored in program memory and are non-volatile and read-only.
2. 2 Keil C51 new pointer variable storage type
The pointer variable in Keil C51 is in the following form:
data type [data store type]*[pointer storage type] identifier;
Where [data store type] and [pointer storage type] are not in standard C. [Data storage type] Defines the amount of space stored by the data (that is, the addressable object), which defines the space stored by the pointer itself. If you do not use [data store type], the pointer is a generic pointer and takes up 3 bytes, and if you use the data store type, the pointer is a memory-based pointer and takes up to two bytes.
2. 3 Keil C51 New variable type
bit: Bitwise variable. Stored on the on-chip data memory on a bit of the addressable byte (20H~2FH), this variable in real-time control has a high practical value.
SFR: Special function Register variable. Stored on-chip special function registers, which can be used to read and write special function registers.
Sbit: Special function Register bit variable. A bit that is stored on the on-chip special function register, which can be divisible by 8, is used to read and write the addressable bits of a special function register.
Sbitl6:16 bit special function register variable. This variable type is rarely used on a 2-byte low address stored on-chip special function registers.
These new variable types in the Keil C51 do not support array and pointer operations.
3 Necessity of using variable storage mode in Keil C51
In Keil C51, the storage mode of a variable is an option, and if you do not use this option, the Keil C51 is automatically allocated at compile time. However, this approach has the following drawbacks:
① system does not know the frequency of use of various variables, it is possible to use the variable frequency of a slow access to the on-chip storage, and the use of high-frequency variables using on-chip storage, so that the operating efficiency of the program is reduced;
② when using the pointer addressing, because do not know how to store the Address object, had to use the general pointer, in the Keil C51 the general pointer to occupy one or two bytes, and the use of the storage method to judge, increased addressing operation time.
If you can define a variable while defining its storage type, you can efficiently use the storage space of a 51-core microcontroller to obtain high-quality target code.

4 How to use Keil C51 variables
4. 1 Global variables and static local variables
Global variables are typically used in multiple functions and are valid throughout the run of the program, although static local variables are used only in one function, but are also valid during the entire program run. For these variables, you should choose the data type, so that in the target code can be accessed with direct addressing instructions, to obtain the highest access speed, improve the efficiency of the program. For example, a global variable of n_g, which is often used in multiple functions, can be defined as:
unsigned int data n_g;//use the "MOV xxh, ..." command when assigning values to N_g
4. 2 Arrays (both global and local)
The definition array is typically used with the Idata storage type, and the "[email protected]" directive is used for indirect addressing in the target code. If there are too many elements in the factor group and the compile times are wrong, you can use pdata and xdata storage types instead.
It is not significant to define an array as a data storage type, since using an array means that you want to be able to access an array element based on an argument. As defined x[100], is generally to be able to use X (i is a variable) to access, so that in the target code must use the question-address, so the array does not need to use the data storage type, even if the use of data storage type, in the target code will still use the indirect addressing instructions. An array is defined as a idata storage type that uses an on-chip data storage space that can only be indirectly addressed when the 52 kernel is used and the on-chip data memory is insufficient. In this way, the processing speed is not reduced and the available storage space is expanded.
4. 3 Data for Table of inquiry
This type of data is characterized by the need to always remain constant and read-only when used, so it should be defined as code. For example, a glyph table:
<ignore_js_op>
Global or local code variables are stored without distinction.
4. 4 non-static local variables
A non-static local variable is used only within a function, and the variable is freed when the function exits.
If the system uses small storage mode, for these variables can not be stored in the description, by the compiler software itself according to the optimal principle, because only in the function of non-static local variables, it is possible to use the work register R0~R7, which will be faster and more saving storage space. For example:
unsigned char i,j;//The system uses R0~R7 storage 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 being defined as PDAGTA or XData mode when it decides to reduce productivity.
4. 5 Hands
As mentioned earlier, there are 2 storage types when defining pointer variables: Data storage type, description of the storage type of the addressed object, pointer storage type, indicating the storage type of the pointer itself. When the data store type is xdata, the pointer itself occupies 2 bytes, and when the data store type is pdata and the on-chip storage type such as Idata, the pointer itself consumes 1 bytes, and if the data store type is not described, the pointer itself consumes 3 bytes. Therefore, when using pointers in KeilC51, you should define the data store type as much as possible, but pay particular attention to the fact that the data store type in the pointer must be the same as the storage type of the object being addressed. Pointers are frequently used, and are constantly being set up, modified, and used, so that their own storage type should choose the data type. For example, when an array is defined, its storage type is defined, and the storage type of the array is added to the data type of the pointer later when it is addressed with a pointer. Here's how:
<ignore_js_op>
4. 62 Semantic variables
In standard C, if you want to use a ambiguity variable, you can only use the enumeration type. Such as:
<ignore_js_op>
When the above program is used in Keil C51, the variable t has only 0 and 12 states, but still occupies one byte in the target code. This process is a waste of storage resources, but also extended processing time, which is not much of a problem for the 8086 kernel, but in the limited resources, the speed is not high in the 51 kernel can not be considered. The following methods can be used in Keil C51:
<ignore_js_op>
The effect is exactly the same, but in the target code, the variable t occupies only 1 bits (that is, 1/8 bytes), and because the 51 kernel microcontroller instruction system has bit processing instructions, the generated target code consumes less memory and runs faster.
4. 7 Special function Register variables (including bit variables)
In special function registers, accumulator A, register B, stack pointer sp, and data pointer dptr are used by the system and are not available to users in C51. Other special function registers can be defined as a variable with SFR, where the address can be divisible by 8, you can also use BSFR to define a bit variable. By accessing these variables, you can read and write the special function register and its addressable members to achieve the purpose of operating the microcontroller internal hardware. For standard 51-core microcontroller, the header file is reg51. H, reg52. These special function register variables have been defined in H or other header files, and users can include this header file with # include and then use it. Now many of the 51 core-compatible microcontroller expands more special function registers, which need to be defined by the user, the method can refer to the use of the device instructions.
4. 8 External Data Memory variables
If set to pdata and XData storage type, the variables will be stored in the off-chip data memory. These two storage types have the slowest access and are not forced to use them. When using these two storage types, be careful to save the original data or the final result as much as possible, minimizing the number of accesses to it, and the intermediate results that require frequent access do not use it.
4. 9 Additional hardware with external data memory address extensions
Other hardware, which is extended outside the microcontroller, is generally borrowed from an external data memory address and is represented as an external data memory unit. For these hardware, you can use pointers to read and write operations. For example:
<ignore_js_op>
Conclusion
The variables in Keil C51 increase the storage type, which is slightly more complex than standard C when used. In Keil C51, the variable storage type is different, the time required to access the variable is different, because the C51 kernel SCM resource is small, the speed is slow, the influence of variable storage type on the system work speed can not be neglected. On the basis of understanding the relationship between variable and SCM storage structure, according to the application requirements of variables, reasonable choice of variable storage type, can get higher work efficiency on the same hardware.

[51 single chip microcomputer] Keil C51 the use of variables in the detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.