51單片機(jī)C語言學(xué)習(xí)筆記4:keil C51絕對地址訪問
C51提供了三種訪問絕對地址的方法:
1.絕對宏:
在程序中,用“#include”即可使用其中定義的宏來訪問絕對地址,包括:
CBYTE、XBYTE、PWORD、DBYTE、CWORD、XWORD、PBYTE、DWORD
具體使用可看一看absacc.h便知
例如:
#include
#defineADstartXBYTE[0x8000]//總線方式訪問AD
#defineADLXBYTE[0x8000]
#defineADHXBYTE[0x8001]
rval=CBYTE[0x0002];指向程序存貯器的0002h地址
rval=XWORD [0x0002];指向外RAM的0004h地址
2. _at_關(guān)鍵字
直接在數(shù)據(jù)定義后加上_at_ const即可,但是注意:
(1)絕對變量不能被初使化;
(2)bit型函數(shù)及變量不能用_at_指定。
例如:
idata struct link list _at_ 0x40;指定list結(jié)構(gòu)從40h開始。
xdata char text[25b] _at_0xE000;指定text數(shù)組從0E000H開始
提示:如果外部絕對變量是I/O端口等可自行變化數(shù)據(jù),需要使用volatile關(guān)鍵字進(jìn)行描述,請參考absacc.h。
3.連接定位控制
此法是利用連接控制指令code xdata pdata data bdata對“段”地址進(jìn)行,如要指定某具體變量地址,則很有局限性,不作詳細(xì)討論。
附:(c51)
/*--------------------------------------------------------------------------
ABSACC.H
Direct access to 8051, extended 8051 and Philips80C51MX memory areas.
Copyright (c) 1988-2002 Keil Elektronik GmbH and Keil Software, Inc.
All rights reserved.
--------------------------------------------------------------------------*/
#ifndef __ABSACC_H__
#define __ABSACC_H__
#define CBYTE ((unsigned char volatile code *) 0)
#define DBYTE ((unsigned char volatile data *) 0)
#define PBYTE ((unsigned char volatile pdata *) 0)
#define XBYTE ((unsigned char volatile xdata *) 0)
#define CWORD ((unsigned int volatile code *) 0)
#define DWORD ((unsigned int volatile data *) 0)
#define PWORD ((unsigned int volatile pdata *) 0)
#define XWORD ((unsigned int volatile xdata *) 0)
#ifdef __CX51__
#define FVAR(object, addr) (*((object volatile far *) (addr)))
#define FARRAY(object, base) ((object volatile far *) (base))
#define FCVAR(object, addr) (*((object const far *) (addr)))
#define FCARRAY(object, base) ((object const far *) (base))
#else
#define FVAR(object, addr) (*((object volatile far *) ((addr)+0x10000L)))
#define FCVAR(object, addr) (*((object const far *) ((addr)+0x810000L)))
#define FARRAY(object, base) ((object volatile far *) ((base)+0x10000L))
#define FCARRAY(object, base) ((object const far *) ((base)+0x810000L))
#endif
#endif
附:(c166)
/*--------------------------------------------------------------------------
ABSACC.H
Direct access to 166 memory areas for C166/EC++ Version 5.
Copyright (c) 1992-2004 Keil Elektronik GmbH and Keil Software, Inc.
All rights reserved.
--------------------------------------------------------------------------*/
#ifndef __ABSACC_H__
#define __ABSACC_H__
#if (__MODEL__ == 0)
#define MVAR(object, addr) (*((object volatile *) (addr)))
#define MARRAY(object, base) ((object volatile *) (base))
#else
#define MVAR(object, addr) (*((object volatile far *) (addr)))
#define MARRAY(object, base) ((object volatile far *) (base))
#define HVAR(object, addr) (*((object volatile huge *) (addr)))
#define HARRAY(object, base) ((object volatile huge *) (base))
#define XVAR(object, addr) (*((object volatile xhuge *) (addr)))
#define XARRAY(object, base) ((object volatile xhuge *) (base))
#endif
#endif
評論