您的位置 首页 ADAS

51单片机资源扩展:扩展片外RAM

51单片机资源扩展:从片内ROM跳转到片外ROM一文中扩展了单片机的程序存储器,4KB存储空间提升到64KB。其实,4K的代码空间还凑合,但是…

51单片机资源扩展:从片内ROM跳转到片外ROM 一文中扩展了单片机程序存储器,4KB存储空间提升到64KB。其实,4K的代码空间还将就,可是51自带的256B数据存储空间运用起来还真严重,其间留给用户的连128B都不到,所以不得不扩展片外RAM。扩展RAM办法和扩展ROM差不多,都是占用P0/P2口做地址线,一起P0用锁存器74373分时复用地址和数据信号。

曾经扩展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  0SJMP  STARTORG  10HSTART:;1456是要拜访的内存地址MOV  DPTR,#1456H;MOV  A,  #0FFH;;0xFF移入[1456]MOVX @DPTR,A;CLR  A;MOVX  A, @DPTR; ;对外部RAM 1456H读出数据到ANOPMOV  A, #0A0H;INC  DPLMOVX @DPTR,A; ;将0xA0写入外部RAM 1457HNOP;从外部RAM [1457]H读出数据到AMOVX  A, @DPTR; NOPSJMP STARTEND

c言语版别:

#include #include #define OFFRAMBASE 0x100unsigned 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;}

最终看下程序运转后6264内部存储的数据:

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 | UUUUUUUUUUUUUUUU0110: 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 0001E0: 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 | ................

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/adas/265626.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部