AT45DB041讀寫程序
/******************************************************************************/
/*正常操作電壓為2.7~3.6V,實(shí)驗(yàn)中發(fā)現(xiàn)當(dāng)電壓超過4.25V后讀出的狀態(tài)字節(jié)為9A(正常 */
/*的狀態(tài)字節(jié)值為9D),并且讀寫數(shù)據(jù)均不準(zhǔn)確,所以應(yīng)當(dāng)保證卡片的供電電壓不超過 */
/*4.25V。 */
/*SPI規(guī)范:Data is always clocked into the device on the rising edge of SCK a-*/
/* nd clocked out of the device on the falling edge of SCK.All instruction-*/
/* s,addresses and data are transferred with the most significant bit(MSB) */
/* first. */
/* 2005-06-02*/
/******************************************************************************/
#pragma optimize(5)
#i nclude
#i nclude
#define UCHAR unsigned char
#define UINT unsigned int
sbit SPI_CS = P2^0;
sbit SPI_SCK = P2^2;
sbit SPI_SO = P2^3;
sbit SPI_SI = P2^4;
unsigned char SPI_HostReadByte(void){
unsigned char i,rByte=0;
for(i=0;i<8;i++){
SPI_SCK=0;
SPI_SCK=1;
rByte<<=1;
rByte|=SPI_SO;
}
return rByte;
}
void SPI_HostWriteByte(unsigned char wByte){
unsigned char i;
for(i=0;i<8;i++){
if((wByte< else{SPI_SI=0;}
SPI_SCK=0;
SPI_SCK=1;
}
}
/******************************************************************************/
/*Status Register Format: */
/* ----------------------------------------------------------------------- */
/* | bit7 | bit6 | bit5 | bit4 | bit3 | bit2 | bit1 | bit0 | */
/* |--------|--------|--------|--------|--------|--------|--------|--------| */
/* |RDY/BUSY| COMP | 0 | 1 | 1 | 1 | X | X | */
/* ----------------------------------------------------------------------- */
/* bit7 - 忙標(biāo)記,0為忙1為不忙。 */
/* 當(dāng)Status Register的位0移出之后,接下來的時(shí)鐘脈沖序列將使SPI器件繼續(xù)*/
/* 將最新的狀態(tài)字節(jié)送出。 */
/* bit6 - 標(biāo)記最近一次Main Memory Page和Buffer的比較結(jié)果,0相同,1不同。 */
/* bit5 */
/* bit4 */
/* bit3 */
/* bit2 - 這4位用來標(biāo)記器件密度,對(duì)于AT45DB041B,這4位應(yīng)該是0111,一共能標(biāo)記 */
/* 16種不同密度的器件。 */
/* bit1 */
/* bit0 - 這2位暫時(shí)無效 */
/******************************************************************************/
unsigned char AT45DB041B_StatusRegisterRead(void){
unsigned char i;
SPI_CS=0;
SPI_HostWriteByte(0xd7);
i=SPI_HostReadByte();
SPI_CS=1;
return i;
}
/******************************************************************************/
/*描述: */
/* When the last bit in the main memory array has been read,the device will*/
/* continue reading back at the beginning of the first page of memory.As w-*/
/* ith crossing over page boundaries,no delays will be incurred when wrapp-*/
/* ing around from the end of the array to the beginning of the array. */
/*參數(shù): */
/* PA - 頁地址,0~2047 */
/* BFA - 指定BUFFER中的起始寫入地址 */
/* pHeader - 指定數(shù)據(jù)的首地址 */
/* len - 指定數(shù)據(jù)的長(zhǎng)度 */
/******************************************************************************/
void AT45DB041B_ContinuousArrayRead(UINT PA,UINT BFA,unsigned char *pHeader,UINT len){
unsigned int i;
while(i++<255){if(AT45DB041B_StatusRegisterRead()&0x80){break;}}
SPI_CS=0;
SPI_HostWriteByte(0xe8);
SPI_HostWriteByte((unsigned char)(PA>>7));
SPI_HostWriteByte((unsigned char)((PA<<1)|(BFA>>8)));
SPI_HostWriteByte((unsigned char)BFA);
for(i=0;i<4;i++){SPI_HostWriteByte(0x00);}
for(i=0;i
}
/******************************************************************************/
/*描述: */
/* 將指定數(shù)據(jù)寫入從某個(gè)地址(0~263)開始的BUFFER中。 */
/*參數(shù): */
/* buffer - 選擇BUFFER,01H選擇BUFFER 1,02H選擇BUFFER 2 */
/* 在該指令序列中,操作碼84H選擇BUFFER 1,87H選擇BUFFER 2 */
/* BFA - BUFFER中的起始地址,0~263 */
/* pHeader - 待存數(shù)據(jù)的頭指針 */
/* len - 待存數(shù)據(jù)的長(zhǎng)度1~264 */
/******************************************************************************/
void AT45DB041B_BufferWrite(UCHAR buffer,UINT BFA,UCHAR *pHeader,UINT len){
unsigned int i;
while(i++<255){if(AT45DB041B_StatusRegisterRead()&0x80){break;}}
SPI_CS=0;
switch(buffer){
case 1:SPI_HostWriteByte(0x84);break;
case 2:SPI_HostWriteByte(0x87);break;
}
SPI_HostWriteByte(0x00);
SPI_HostWriteByte((unsigned char)(BFA>>8));
SPI_HostWriteByte((unsigned char)BFA);
for(i=0;i
}
/******************************************************************************/
/*描述: */
/* 將指定數(shù)據(jù)寫入從某個(gè)地址(0~263)開始的頁中:包含2個(gè)動(dòng)作,首先將指定數(shù)據(jù)*/
/* 寫入到BUFFER 1或者BUFFER 2中,其中可以指定BUFFER中的起始寫入地址,此寫入*/
/* 動(dòng)作不影響B(tài)UFFER中其它地址中的數(shù)據(jù),然后再將BUFFER中的整個(gè)數(shù)據(jù)寫入到某指*/
/* 定頁中(帶預(yù)擦除)。 */
/*參數(shù): */
/* buffer - 選擇BUFFER,01H選擇BUFFER 1,02H選擇BUFFER 2 */
/* PA - 頁地址,0~2047 */
/* BFA - 指定BUFFER中的起始寫入地址 */
/* pHeader - 指定數(shù)據(jù)的首地址 */
/* len - 指定數(shù)據(jù)的長(zhǎng)度 */
/******************************************************************************/
void AT45DB041B_BufferToMainMemoryPageProgramWithBuilt_inErase(UCHAR buffer,UINT PA,UINT BFA,UCHAR *pHeader,UINT len){
unsigned int i;
AT45DB041B_BufferWrite(buffer,BFA,pHeader,len);
while(i++<1000){if(AT45DB041B_StatusRegisterRead()&0x80){break;}}
SPI_CS=0;
switch(buffer){
case 1:SPI_HostWriteByte(0x83);break;
case 2:SPI_HostWriteByte(0x86);break;
}
SPI_HostWriteByte((unsigned char)(PA>>7));
SPI_HostWriteByte((unsigned char)(PA<<1));
SPI_HostWriteByte(0x00);
SPI_CS=1;
}
/******************************************************************************/
/*描述: */
/* 與上一個(gè)函數(shù)的唯一區(qū)別是不帶預(yù)擦除。 */
/******************************************************************************/
void AT45DB041B_BufferToMainMemoryPageProgramWithoutBuilt_inErase(UCHAR buffer,UINT PA,UINT BFA,UCHAR *pHeader,UINT len){
unsigned int i=0;
AT45DB041B_BufferWrite(buffer,BFA,pHeader,len);
while(i++<1000){if(AT45DB041B_StatusRegisterRead()&0x80){break;}}
SPI_CS=0;
SPI_HostWriteByte(0x88+buffer);
SPI_HostWriteByte((unsigned char)(PA>>7));
SPI_HostWriteByte((unsigned char)(PA<<1));
SPI_HostWriteByte(0x00);
for(i=0;i
}
void main(void){
unsigned char i;
UCHAR data test[32];
i=AT45DB041B_StatusRegisterRead();
memset(test,0x77,32);
AT45DB041B_BufferToMainMemoryPageProgramWithBuilt_inErase(1,2047,0,test,32);
memset(test,0x26,32);
AT45DB041B_ContinuousArrayRead(2047,0,test,32);
memset(test,0x88,32);
AT45DB041B_BufferToMainMemoryPageProgramWithBuilt_inErase(2,2047,0,test,32);
memset(test,0x55,32);
AT45DB041B_ContinuousArrayRead(2047,0,test,32);
while(1){}
}