在利用keil进行8051单片机编程的时,常常需要进行绝对地址进行访问.特别是对硬件操作,如DA AD 采样 ,LCD 液晶操作,打印操作.等等.
C51提供了三种访问绝对地址的方法:
1. 绝对宏:
在程序中,用“#include<absacc.h>”即可使用其中定义的宏来访问绝对地址,包括:
CBYTE、XBYTE、PWORD、DBYTE、CWORD、XWORD、PBYTE、DWORD
具体使用可看一看absacc.h便知
例如:
#include<absacc.h>
#define ADstart XBYTE[0x8000] //总线方式访问AD
#define ADL XBYTE[0x8000]
#define ADH XBYTE[0x8001]
rval=CBYTE[0x0002];指向程序存贮器的0002h地址
rval=XWORD [0x0002];指向外RAM的0004h地址
2. _at_关键字
直接在数据定义后加上_at_ const即可,但是注意:
(1)绝对变量不能被初使化;
(2)bit型函数及变量不能用_at_指定。
例如:
idata struct link list _at_ 0x40;指定list结构从40h开始。
xdata char text[25b] _at_0xE000;指定text数组从0E000H开始
提示:如果外部绝对变量是I/O端口等可自行变化数据,需要使用volatile关键字进行描述,请参考absacc.h。
3. 连接定位控制
此法是利用连接控制指令code xdata pdata \data bdata对“段”地址进行,如要指定某具体变量地址,则很有局限性,不作详细讨论。
附:(c51)
/*--------------------------------------------------------------------------
ABSACC.H
Direct access to 8051, extended 8051 and Philips 80C51MX 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
转载自:
http://www.dz863.com/Microprocessors/MCS-8051/keil-C51-CBYTE.htm
参考资料:
1)Cx51 User's Guide: XBYTE