原创 C51对绝对地址的操作

2009-5-9 14:34 2653 3 3 分类: MCU/ 嵌入式

51单片机C语言学习笔记4:keil C51绝对地址访问



51单片机C语言学习笔记4keil C51绝对地址访问<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />


在利用keil进行8051单片机编程的时,常常需要进行绝对地址进行访问.特别是对硬件操作,DA AD 采样 ,LCD 液晶操作,打印操作.等等.
C51
提供了三种访问绝对地址的方法:
1. 绝对宏:
   
在程序中,用include<absacc.h>”即可使用其中定义的宏来访问绝对地址,包括:
CBYTEXBYTEPWORDDBYTECWORDXWORDPBYTEDWORD
具体使用可看一看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];
指向外RAM0004h地址


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 <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />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

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
3
关闭 站长推荐上一条 /3 下一条