2013年8月30日 星期五

Data Types & Data Pointer

一直以來,我總是看過就算了
這些基本的項目.像是Data pointer,Data type之類
但...我每次都會遺忘.故還是做些整理吧
--------------------------------------------------------------- 



以下是keil c中所有的資料類別



The Cx51 Compiler provides several basic data types you may use in your C programs. The compiler supports the standard C data types as well as several data types that are unique to the Cx51 platform.
Data TypesBitsBytesValue Range
bit1
0 to 1
signed char81-128 — +127
unsigned char810 — 255
enum8 / 161 or 2-128 — +127 or -32768 — +32767
signed short int162-32768 — +32767
unsigned short int1620 — 65535
signed int162-32768 — +32767
unsigned int1620 — 65535
signed long int324-2147483648 — +2147483647
unsigned long int3240 — 4294967295
float324±1.175494E-38 — ±3.402823E+38
double324±1.175494E-38 — ±3.402823E+38
sbit1
0 or 1
sfr810 — 255
sfr161620 — 65535
Note
  • The bit, sbit, sfr, and sfr16 data types are not provided in ANSI C. They are unique to the Cx51 Compiler.
其中的 sbit,sfr,sfr16是用於The 8051 provides 128 bytes of memory for Special Function Registers (SFRs). SFRs are bit, byte, or word-sized registers that are used to control timers, counters, serial I/O, port I/O, and peripherals. Refer to Special Function Registers for more information.
----------------------------------------------------------------------
接下來是其資料所可存於的位址

1.程式記憶体, 使用MOVC 來間接(由R0/1,DPTR)存取,不可直接存取,code
Program Memory

Program (CODE) memory is read only; it cannot be written to. Program memory may reside within the 8051 MCU, it may be external, or it may be both, depending upon the 8051 derivative and the hardware design.
  • The 8051 architecture supports up to 64K Bytes of program memory. However, program space can be expanded using code banking.
  • Some devices offer a larger code space.
  • Program code, including all functions and library routines, is stored in program memory.
  • Constant variables may also be stored in program memory.
  • The 8051 executes programs stored in program memory only.
  • Program memory may be accessed from your C programs using the code memory type specifier.
Note
  • Program memory may be accessed indirectly using the MOVC instruction. It may not be accessed directly.
2.內部資料記憶体,其開頭128 byte,可直接'間接存取.後128b(與SFR同位址),
故只能間接存取.(分別),idata,data,bdata
Internal Data Memory
Internal data memory resides within the 8051 MCU and is read/write. Up to 256 bytes of internal data memory are available depending upon the 8051 derivative. The first 128 bytes of internal data memory are both directly and indirectly addressable. The upper 128 bytes of data memory (from 0x80 to 0xFF) can be addressed only indirectly (this address space, when accessed directly, is mapped to SFRs). There is also a 16 byte area starting at 20h that is bit-addressable.
Access to internal data memory is very fast because it can be accessed using an 8-bit address. However, internal data memory is limited to a maximum of 256 bytes.
Internal data can be broken down into three distinct memory types: data, idata, and bdata.
  • The data memory specifier always refers to the first 128 bytes of internal data memory. Variables stored here are accessed using direct addressing.
  • The idata memory specifier refers to all 256 bytes of internal data memory; however, this memory type specifier code is generated by indirect addressing which is slower than direct addressing.
  • The bdata memory specifier refers to the 16 bytes of bit-addressable memory in the internal data area (20h to 2Fh). This memory type specifier allows you to declare data types that may also be accessed at the bit level.
3.外接資料記憶体,xdata,pdata(MOVX
External Data Memory
External data memory is read/write. Since external data memory is indirectly accessed through a data pointer register (which must be loaded with an address), it is slower than access to internal data memory.
Several 8051 devices provide on-chip XRAM space that is accessed with the same instructions as the traditional external data space. This XRAM space is typically enabled via dedicated chip configuration SFR registers and overlaps the external memory space.
There may be up to 64K Bytes of external data memory; though, this address space does not necessarily have to be used as memory. Your hardware design may map peripheral devices into the memory space. If this is the case, your program would access external data memory to program and control the peripheral. This technique is referred to as memory-mapped I/O.
The C51 Compiler offers two memory types that access external data: xdata and pdata.
  • The xdata memory specifier refers to any location in the 64K Byte address space of external data memory. The large memory model locates variables in this memory space.
  • The pdata memory type specifier refers to exactly one (1) page (256 bytes) of external data memory. The compact memory model locates variables in this memory space.
Note
  • External data memory may be accessed indirectly using the MOVX instruction. It may not be accessed directly.
4.Far 記憶体,特殊51所支援,可以擴張其extended RAM space , constant ROM space
  (far,const far


Far Memory
Far memory refers to the extended address space of many new 8051 variants. The Cx51 Compiler uses generic 3-byte pointers to access extended memory spaces. Two Cx51 memory types, far and const far, access variables in extended RAM space and constants in extended ROM space.

5.SFR memory,Special Function Register.只能直接存取
SFR Memory
The 8051 provides 128 bytes of memory for Special Function Registers (SFRs). SFRs are bit, byte, or word-sized registers that are used to control timers, counters, serial I/O, port I/O, and peripherals. Refer to Special Function Registers for more information.
Note
  • SFR memory may be accessed directly using the MOV instruction. It may not be accessed indirectly.
 ----------------------------------------------------------------------------------
以上為基本的分類及介紹,接下來是如何去宣告變收,以上述各的組合

1.變數宣告

////////////////////////////////////////////////////////////////////////////////////////////////////////
char data var1;
char code text[] = "ENTER PARAMETER:";
unsigned long xdata array[100];
float idata x,y,z;
unsigned int pdata dimension;
unsigned char xdata vector[10][4][4];
char bdata flags;
/////////////////////////////////////////////////

其規格如下
其黑体字為存放空間,其後為其資料型態
type
變數型態


physical
變數位址
name
unsigned char

datap
 *愈靠近變數名稱為變數位址

2.指摽變數
////////////////////////////////////////
unsigned xdata char * data p;
  1. 指向外部記憶體的指標變數,變數存放在內部記憶體。
  2. unsigned char data * xdata p;
    指向內部記憶體的指標變數,變數存放在外部記憶體。
  3. unsigned char code * xdata p;
    指向程式記憶體的指標變數,變數存放在外部記憶體。
  4. unsigned char code * code p;
    指向程式記憶體的指標變數,變數存放在程式記憶體,指標變數是唯讀,所以必須先設定初始值
  5. unsigned char * xdata p;
    指向任何記憶體的指標變數 (generic pointer),變數存放在外部記憶體。
  6. unsigned char * p;
    指向任何記憶體的指標變數 (generic pointer),變數存放由 memory model 決定。
  7. char (* data fp)(void);
    指向程式記憶體的函數指標變數,變數存放在內部記憶體。函數指標變數只能指向程式記憶體,無法修改喔。 
type
變數型態
located
指向位址
pointerphysical
變數位址
name
unsigned charxdata*datap




3.其它
對於沒有指名其存放位址的變數
ex:char p;
則會依其 Memory Mode


The memory model determines the default memory type to use for function arguments, automatic variables, and declarations that include no explicit memory type. The C51 Compiler provides three memory models:
The following table lists the default memory areas used for each memory model.
Memory
Model
Parameters &
Automatic
Variables
Default
Global
Variables
Default
Constant
Variables
Default
Pointer
Definitions
Default
Pointer
Size
SMALLdatadatadata*3 bytes
COMPACTpdatapdatapdata*3 bytes
LARGExdataxdataxdata*3 bytes
You may specify the memory model on the compiler command line using the SMALL, COMPACT, or LARGE directives or using #pragma in the source file. You may also specify the memory model on a function-by-function basis by adding the memory model to the function declaration.
Note
  • Except in very special select applications, the SMALL memory model generates the fastest, most efficient code.
(*1: http://www.keil.com/support/man/docs/c51/c51_le_memmodels.htm)
(*2 :還需討論的有)
Type Qualifiers const volatile Storage Classes auto register extern static Absolute Variable Location









沒有留言:

張貼留言