以前扩展RAM是用汇编语言访问存储器,好处是定位精准,指哪打哪,坏处就是:程序规模一大就有点难维护了,所以还得改用C实现。
对应于汇编语言用R1,R0/DPTR访问外部RAM,keil C扩展了存储类型,增加了如pdata(等同用movx @Rn访问方式)/xdata(等同于movx @DPTR访问方式)存储类型用于访问片外ram。同时,还提供了绝对地址访问的宏,如PBYTE/XBYTE,查看定义:
#define PBYTE((unsigned char volatile pdata*)0); #define XBYTE((unsigned char volatile xdata*)0);
其实,也还是定义相应存储类型的指针~。
上仿真图和代码前,整理一下keil c提供访问绝对地址的方法:
1._at_定义变量:
变量类型 [存储类型] 变量名 _at_ 常数,指定变量存放在常数所指定的ram位置,注意bit型变量不能使用_at_指定位置;
2.绝对地址访问:
头文件absacc.h提供了绝对地址访问宏,用于字节/字寻址,如
val=XBYTE[0x0000];读片外ram 0x00处内存值
XBYTE[0x0200]=val;写片外ram 0x0200处内存值
扩展ram仿真图:
上段去年用汇编写的访问ram的代码:
ORG 0 SJMP START ORG 10H START: ;1456是要访问的内存地址 MOV DPTR,#1456H; MOV A, #0FFH; ;0xFF移入[1456] MOVX @DPTR,A; CLR A; MOVX A, @DPTR; ;对外部RAM 1456H读出数据到A NOP MOV A, #0A0H; INC DPL MOVX @DPTR,A; ;将0xA0写入外部RAM 1457H NOP ;从外部RAM [1457]H读出数据到A MOVX A, @DPTR; NOP SJMP START ENDc语言版本:
#include最后看下程序运行后6264内部存储的数据:#include #define OFFRAMBASE 0x100 unsigned char xdata offRamVal0 _at_ OFFRAMBASE*2; unsigned char xdata offRamVal1 _at_ 0x00; int main() { unsigned char idata index = 0; offRamVal0 = 0xAA; offRamVal1 = 0xCC; for(index=0;index<0x10;index++) { XBYTE[index+OFFRAMBASE] = 0x55; } return 0; }
0000: CC 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ 0010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ 0020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ 0090: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ 00A0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ 00B0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ 00C0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ 00D0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ 00E0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ 00F0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ 0100: 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 | UUUUUUUUUUUUUUUU 0110: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ 0120: 00 00 00 00 00 00 00 00 00 00 00 01E0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ 01F0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ 0200: AA 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
上一篇:51单片机的特殊功能寄存器SFR
下一篇:51单片机ROM与RAM
推荐阅读最新更新时间:2024-03-16 14:55
设计资源 培训 开发板 精华推荐
- 美国加紧研制爬行式遥控机器人 目标是清除海滩碎波带的地雷
- 丰田CUE类人机器人创造了新记录 连续执行篮球罚球次数最多
- 达芬奇机器人落户医院 机器人已经成为广大患者眼中的红人
- 2020世界智能网联汽车大会:未来出行的梦想与现实
- Imagination推出面向ADAS和自动驾驶应用的多核IMG Series4 NNA
- 苹果副总裁:2018年已规划iPhone13系列相机系统
- 上半年全球智能手机图像传感器市场份额:索尼位第一
- iPhone13遭殃!多家供应商因限电停产:苹果已不淡定
- 三星可望推出Galaxy Buds Z2 TWS:主动降噪,定价更低
- 苹果iPhone 13 Pro Max已经支持最高27W快充