51單片機資源擴展:擴展片外RAM
以前擴展RAM是用匯編語言訪問存儲器,好處是定位精準,指哪打哪,壞處就是:程序規(guī)模一大就有點難維護了,所以還得改用C實現。
本文引用地址:http://m.butianyuan.cn/article/201611/318425.htm對應于匯編語言用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提供了絕對地址訪問宏,用于字節(jié)/字尋址,如
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 STARTENDc語言版本:
#include最后看下程序運行后6264內部存儲的數據:#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;}
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 | ................
評論