The use of variables in Keil C51

Publisher:liliukanLatest update time:2011-07-21 Keywords:Keil  C51  Variable Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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]

3 Necessity of using variable storage mode in Keil C51
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:
c.JPG
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:
d.JPG

[page]

4.6 Ambiguous variables
In standard C, if you want to use an ambiguous variable, you can only use enumeration types. For example,
e.JPG
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:
f.JPG
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:
g.JPG

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.

Keywords:Keil  C51  Variable Reference address:The use of variables in Keil C51

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

C51 MCU addressing structure
I have been using uc/os recently and need to learn assembly
[Microcontroller]
C51 MCU addressing structure
How to use bit field function in C51?
Define a structure like this:   typedef struct   {     uchar DC0_ALA:1; //Power supply 0 alarm     uchar DC1_ALA:1; //Power supply 1 alarm     uchar AC_ALA:1; //Power failure alarm     uchar UN_H_ALA:1; //Same frequency channel machine unlock alarm     uchar UN_L_ALA:1; //Inter-frequency channel machine unlock alarm  
[Microcontroller]
Variable and constant positioning problem in Keil
Variable location: char tab1 _at_ 0x200; Assignment: Assign values ​​in the function, such as tab1 =0x01; Constant location and initialization: Create a new TABLE.C, write char code table ={initial value}; KEIL location: Select option-->BL51 Locate, write in the CODE: column such as: ?CO?TABLE(0x7000) In thi
[Microcontroller]
Use of scatter loading files under KEIL
The concept of scattered loading is clearly introduced in Chapter 11 of the book "ARM Architecture and Programming".   A scatter file (i.e., a scatter file with a suffix of .scf) is a text file that specifies How does the ARM connector allocate the storage addresses of RO, RW, ZI and other data when generating an
[Microcontroller]
Manual counter design circuit diagram based on C51 single chip microcomputer
  Use AT89S51 single-chip microcomputer to make a manual counter. Connect a touch switch to the P3.7 pin of the AT89S51 single-chip microcomputer as a manual counting button. Connect a common cathode digital tube to the P2.0 - P2.7 of the single-chip microcomputer as the unit digit display of 00-99 counting. Connect a
[Microcontroller]
Manual counter design circuit diagram based on C51 single chip microcomputer
C51 Programming 25-Application (MCU and computer realize WiFi communication)
This article implements data communication between the microcontroller and the computer using the ESP-01S wifi module.  Set the baud rate of the WiFi module  Since the default baud rate of the ESP-01S wifi module is 115200, and the baud rate of the 51 microcontroller is usually set at 9600, it is necessary to set
[Microcontroller]
C51 Programming 25-Application (MCU and computer realize WiFi communication)
DS1302 clock chip C51 driver
/********************************************** Program name: DS1302 clock chip C51 driver Brief description: read_clockS function is used to read clock data    Call Set_time to adjust the time and write it to DS1302    sel is an adjustment flag, and its value can be changed by external buttons ***********************
[Microcontroller]
How to use keil to determine the cold start and hot start of ARM
Microprocessor: LPC2114 Compilation environment: Keil MDK V4.10 Ideas: The reset of the microcontroller system is often divided into cold start and hot start. The so-called cold start is also known as the power-on reset. After the cold start, the contents of the RAM inside and outside the chip are random, usuall
[Microcontroller]
How to use keil to determine the cold start and hot start of ARM
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号