在运用keil进行8051单片机编程的时,常常需求进行肯定地址进行拜访.特别是对硬件操作,如DA AD采样,LCD液晶操作,打印操作.等等.
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_关键字
直接在数据界说后加上_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 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