8051 MCU Basics 4: Data Storage Types (data, idata, xdata, code)

Publisher:温馨时光Latest update time:2021-12-02 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Reference source: Keil > Help > uVision Help > Cx51 Compiler User's Guide > Language Extensions > Memory Types


Storage Type

The Cx51 compiler can access all storage areas of the 8051. When declaring a variable, you can specify the allocated storage area, or you can make implicit allocations based on the memory model configuration.

image.png

In actual projects, xdata and code are often used.


Instructions

When declaring/defining variables, add the keywords data, idata, bdata, pdata, xdata, code.


unsigned char data var1;

unsigned char code text[] = "ENTER PARAMETER:";

unsigned char xdata array[100];

unsigned char idata x,y,z;

unsigned char pdata dimension;

unsigned char xdata vector[10][4][4];

unsigned char bdata flags;


When the MCU accesses the internal RAM, the reading and writing speed is faster than the extended RAM.


Therefore, when writing application code, place frequently used variables in internal RAM and less frequently used variables in external RAM.


Variables in Flash can only be read and are saved when power is off.


Variables in RAM can be read and written, but are not saved when power is off.


Detailed description

8051 Memory Structure

insert image description here

data

Data can only declare variables, not functions.


When reading and writing data variables, the MCU uses an 8-bit address to directly address the internal RAM (lower 128 bytes).


The advantage is the fastest reading and writing speed, and the disadvantage is that the available space is too small.


idata

idata can only declare variables, not functions.


When reading and writing idata variables, the MCU indirectly addresses the internal RAM (all 256 bytes) using an 8-bit address.


The accessible space is all 256 bytes of internal RAM, of which the lower 128 bytes are duplicated with the data space, but the access method is different.


bdata

bdata can only declare variables, not functions.


When reading and writing bdata variables, the MCU uses 8 address bits to address/directly address the internal RAM (part of 16 bytes).


The accessible space is 16 bytes of internal RAM, with an address range of 0x20~0x2F, located inside the data space.


pdata

pdata can only declare variables, not functions.


When reading and writing the pdata variable, the MCU uses an 8-bit address to indirectly address the extended RAM (the lower 256 bytes, a page).


xdata

xdata can only declare variables, not functions.


When reading and writing xdata variables, the MCU uses 16-bit addresses to indirectly address the extended RAM (all addresses, up to 64K bytes).


code

code can declare constants or functions.


When reading code constants, the MCU uses a 16-bit address to indirectly address the Flash space (maximum 64K bytes).


When declaring a function, the compiler implicitly adds the code keyword by default, and the user does not need to deal with it.

Reference address:8051 MCU Basics 4: Data Storage Types (data, idata, xdata, code)

Previous article:8051 MCU Basics 3: C51 Memory Model (small, compact and large)
Next article:8051 MCU Basics 6: General Pointers and Directional Pointers

Latest Microcontroller Articles
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号