與51單片機(jī)擴(kuò)展有關(guān)的知識(shí)點(diǎn)
1.單片機(jī)中的關(guān)鍵字_at_:定義變量的存儲(chǔ)地址。
本文引用地址:http://m.butianyuan.cn/article/201611/318422.htm2.這段程序是什么意思?
- #define XBYTE((char*)0x20000L)
- XBYTE[0x8000]=0x41;
define XBYTE ((char*)0x20000L) 定義了XBYTE是個(gè)指向地址 0x0000L 的字符型指針(或者數(shù)組)變量XBYTE[0x8000]=0x41; 對(duì)數(shù)組XBYTE的第 0x8000個(gè)變量進(jìn)行賦值,賦值為0x41。即對(duì)地址0x8000L的內(nèi)存單元賦值為0x41。這說明你用的是帶外部數(shù)據(jù)總線的51芯片,外部的存儲(chǔ)器多半是RAM,RAM的容量至少有 0x8000(32k)字節(jié)-RAM的容量可能是64k字節(jié)?L代表 long,這說明 0x20000L 是一個(gè)長(zhǎng)整數(shù)。一般來說,8位單片機(jī)如51,整數(shù)用16位二進(jìn)制表示,長(zhǎng)整數(shù)用32位二進(jìn)制表示.0x20000L當(dāng)中的2是存儲(chǔ)器類型xdata,而不是地址的一部分.
3.51單片機(jī)的存儲(chǔ)類型:
data--->可尋址片內(nèi)ram
bdata--->可位尋址的片內(nèi)ram
idata--->可尋址片內(nèi)ram,允許訪問全部?jī)?nèi)部ram
pdata--->分頁(yè)尋址片外ram (MOVX @R0) (256 BYTE/頁(yè))
xdata--->可尋址片外ram (64k 地址范圍)
code--->程序存儲(chǔ)區(qū) (64k 地址范圍),對(duì)應(yīng)MOVC @DPTR
4.absacc.h
- /*--------------------------------------------------------------------------
- ABSACC.H
- Direct access to 8051, extended 8051 and Philips 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
例如:
rval=CBYTE[0x0002];指向程序存貯器的0002h地址
rval=XWORD [0x0002];指向外RAM的0004h地址 //WORD是一個(gè)字,兩個(gè)字節(jié),故為0004h
#define COM8255 XBYTE[0X060FF] //后面若出現(xiàn)COM8255,則單片機(jī)端口P0和P2聯(lián)合輸出0X060FF絕對(duì)物理地址(地址指向82C55指令寄存器)
#define PA8255 XBYTE[0X000FF] //后面若出現(xiàn)PA8255,則單片機(jī)端口P0和P2聯(lián)合輸出0X000FF絕對(duì)物理地址(地址指向82C55的A組端口寄存器)
#define PB8255 XBYTE[0X020FF] //后面若出現(xiàn)PB8255,則單片機(jī)端口P0和P2聯(lián)合輸出0X020FF絕對(duì)物理地址(地址指向82C55的B組端口寄存器)
#define PC8255 XBYTE[0X040FF] //后面若出現(xiàn)PC8255,則單片機(jī)端口P0和P2聯(lián)合輸出0X040FF絕對(duì)物理地址(地址指向82C55的C組端口寄存器)
4.驅(qū)動(dòng)外部ram的例程
- /**************************************************************************
- *程序名稱: 51單片機(jī)片外擴(kuò)展RAM測(cè)試
- *程序功能: 把數(shù)據(jù)寫入片外RAM,再?gòu)钠釸AM讀取數(shù)據(jù),點(diǎn)亮P1口的8個(gè)LED
- *開發(fā)工具:WSF-51DB開發(fā)板
- * MCU型號(hào):AT89S52-24PU
- *時(shí)鐘頻率:12MHZ
- *程序作者: 吳師傅
- *版權(quán)說明:吳師傅版權(quán)所有,轉(zhuǎn)載請(qǐng)注明來源地址和作者。
- **************************************************************************/
- #include
- #include //包含片外存儲(chǔ)空間絕對(duì)地址訪問函數(shù):XBYTE[]
- sbit oe=P2^7;//片外RAM芯片片選和地址鎖存器74HC573使能
- unsigned char code ramdata[100]= //定義100個(gè)數(shù)據(jù)
- {
- 0,1,2,3,4,5,6,7,8,9,
- 10,11,12,13,14,15,16,17,18,19,
- 20,21,22,23,24,25,26,27,28,29,
- 30,31,32,33,34,35,36,37,38,39,
- 40,41,42,43,44,45,46,47,48,49,
- 50,51,52,53,54,55,56,57,58,59,
- 60,61,62,63,64,65,66,67,68,69,
- 70,71,72,73,74,75,76,77,78,79,
- 80,81,82,83,84,85,86,87,88,89,
- 90,91,92,93,94,95,96,97,98,99
- };
- //延時(shí)ms函數(shù):
- void Delayms(unsigned int t)
- {
- unsigned int i,j;
- for(i=t;i>0;i--)
- for(j=120;j>0;j--);
- }
- //主函數(shù):
- int main(void)
- {
- unsigned char i;
- oe=0;//使能RAM芯片片選和地址鎖存器
- for(i=0;i<100;i++)
- XBYTE[300+i]=ramdata[i];
- //將100個(gè)數(shù)據(jù)寫入片外RAM,起始地址為:300
- while(1)
- {
- for(i=0;i<100;i++)
- {
- P1=XBYTE[300+i];//從片外RAM讀取數(shù)據(jù),點(diǎn)亮P1口的8個(gè)LED
- Delayms(500);
- }
- }
- return 0;
- }
評(píng)論