51單片機(jī)資源擴(kuò)展:擴(kuò)展片外RAM
以前擴(kuò)展RAM是用匯編語言訪問存儲(chǔ)器,好處是定位精準(zhǔn),指哪打哪,壞處就是:程序規(guī)模一大就有點(diǎn)難維護(hù)了,所以還得改用C實(shí)現(xiàn)。
本文引用地址:http://m.butianyuan.cn/article/201611/318425.htm對(duì)應(yīng)于匯編語言用R1,R0/DPTR訪問外部RAM,keil C擴(kuò)展了存儲(chǔ)類型,增加了如pdata(等同用movx @Rn訪問方式)/xdata(等同于movx @DPTR訪問方式)存儲(chǔ)類型用于訪問片外ram。同時(shí),還提供了絕對(duì)地址訪問的宏,如PBYTE/XBYTE,查看定義:
#define PBYTE((unsigned char volatile pdata*)0);#define XBYTE((unsigned char volatile xdata*)0);
其實(shí),也還是定義相應(yīng)存儲(chǔ)類型的指針~。
上仿真圖和代碼前,整理一下keil c提供訪問絕對(duì)地址的方法:
1._at_定義變量:
變量類型 [存儲(chǔ)類型] 變量名 _at_ 常數(shù),指定變量存放在常數(shù)所指定的ram位置,注意bit型變量不能使用_at_指定位置;
2.絕對(duì)地址訪問:
頭文件absacc.h提供了絕對(duì)地址訪問宏,用于字節(jié)/字尋址,如
val=XBYTE[0x0000];讀片外ram 0x00處內(nèi)存值
XBYTE[0x0200]=val;寫片外ram 0x0200處內(nèi)存值
擴(kuò)展ram仿真圖:
上段去年用匯編寫的訪問ram的代碼:
ORG 0SJMP STARTORG 10HSTART:;1456是要訪問的內(nèi)存地址MOV DPTR,#1456H;MOV A, #0FFH;;0xFF移入[1456]MOVX @DPTR,A;CLR A;MOVX A, @DPTR; ;對(duì)外部RAM 1456H讀出數(shù)據(jù)到ANOPMOV A, #0A0H;INC DPLMOVX @DPTR,A; ;將0xA0寫入外部RAM 1457HNOP;從外部RAM [1457]H讀出數(shù)據(jù)到AMOVX A, @DPTR; NOPSJMP STARTENDc語言版本:
#include最后看下程序運(yùn)行后6264內(nèi)部存儲(chǔ)的數(shù)據(jù):#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 | ................
評(píng)論