51 considerations for defining variables in different Memory Spaces (Data xdata bdata)

Source: Internet
Author: User

1. The data zone has a small space, so only variables that are frequently used or require high computing speed are placed in the Data zone, such as the Count value in the for loop.

2. It is best to place local variables in the Data zone.

Because the space of local variables can be overwritten (the space of local variables of a function is released after exiting the function, and is overwritten by the local variables of other functions), the memory usage can be improved. Except for static local variables, the memory usage is the same as that of global variables;

3. Make sure that no function is called in your program.

When a function is not called in Keil C, the compiler considers it as an interrupt function. The space of the local variables used in the function is not released, that is, it is processed like the global variables. Keil C is stupid, but no way.

4. The logical flag variables encountered in the program can be defined in bdata, which can greatly reduce the memory usage.

There are 16 bits in the 51 series of chips, including 8*16 = 128 logical variables. The definition method is bdatabit ledstate, but the bit type cannot be used in arrays or struct.

5. other variables that are not frequently used and do not require high computing speed are placed in the xdata area.

6. If you want to save data space, you must use the large mode to put all variables with undefined memory locations in the xdata zone. Of course, it is best to specify the memory type for all variables.

7. When a pointer is used, specify the memory type pointed to by the pointer.

In C51, General pointers pointing to the memory type are not defined to occupy 3 bytes, while the specified pointer pointing to the data area only occupies 1 byte. The specified pointer pointing to the xdata area occupies 2 bytes. If the pointer P points to the data area, it should be defined as char data * P ;. You can also specify the memory storage type of the pointer, for example, char data * xdata P ;. It means that the pointer P points to the data zone variable, which is stored in the xdata zone.

Single Chip MicrocomputerFor beginnersData,Xdata, Bdata, idata, code, is not very clear, because there is no such thing in the C language of the microcomputer, it is unacceptable to come out at once, especially friends who do not have basic hardware. this is also the place where mistakes are often made when writing programs. See the following table.
Ordinary 51 SeriesSingle Chip MicrocomputerStorage Space Resource Allocation

Space name

Address range

Description

Data D: 00h ~ 7fh In-chip RAM direct addressing Area
Bdata D: 20 h ~ 2fh In-chip RAM-bit addressing Area
Idata I: 00h ~ FFH In-chip RAM indirect addressing Area
Xdata X: Running H ~ Ffffh 64 K off-chip RAM data Zone
Code C: Running H ~ Ffffh 64 kB internal and external ROM code Zone
Bank0 ~ Bank31 B0: Running H ~ Ffffh
:
:
B31: Running H ~ Ffffh
Code area, up to 32x64 kB ROM can be expanded

The following table shows the new 80C51Single Chip MicrocomputerAllocation of Extended Space

Space name

Address range

Description

Data D: 00h ~ 7fh In-chip RAM direct addressing Area
Bdata D: 20 h ~ 2fh In-chip RAM-bit addressing Area
Idata I: 00h ~ FFH In-chip RAM indirect addressing Area
Xdata X: Running H ~ Ffffh 64 kB General off-chip RAM data Zone
Hdata X: Running H ~ Ffffffh 16 MB extended RAM data Zone
Code C: Running H ~ Ffffh 64 k General Internal and External ROM code area
Hconst (ecode) C: Running H ~ Ffffffh 16 MB extended Rom constant zone (available as code zone for dallas390)
Bank0 ~ Bank31 B0: Running H ~ Ffffh
:
:
B31: Running H ~ Ffffh
Code area, up to 32x64 kB ROM can be expanded

Single Chip MicrocomputerMedium keywordData, Idata,Xdata, Pdata differencesData:
Fixed refers to the first 128 Ram records of 0x00-0x7f, which can be directly read and written using ACC, with the fastest speed and the smallest generated code.

Idata:
Fixed refers to the first 256 Ram records of 0x00-0xff, of which the first 128 andData128 is the same, but the access method is different. Idata is accessed using pointers similar to those in C. The statement in the Assembly is mox acc, @ Rx. (It is not important to add: idata in C performs pointer access well)

Xdata:
External extended RAM refers to the external 0x0000-0xffff space, which is accessed by dptr. Pdata: The External extended RAM's 256-byte-low, read-write when the address appears on the A0-A7, read-write with movx ACC, @ Rx. This is special, and the C51 seems to have this bug. We recommend that you use it less. But it also has its advantages. The specific usage is an intermediate issue, which is not mentioned here.

Role of startup. A51
Like assembly, initialization of the variables and arrays defined in C is carried out in startup. A51. If you define a global variable with a value, such as unsigned charDataXxx = "100"

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.