问题:这是测试AT45DB041的程序,供电状态下读写都没有问题,一旦断电在通上电,读操作保存的数据都正确,就是别写,一写之前保存的数据全错了,但后写入的数据还是对的。实在想不出怎么回事了?
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<<i)&0x80) {SPI_SI=1;} else {SPI_SI=0;} SPI_SCK=0;
SPI_SCK=1;
} }
unsigned char AT45DB041B_StatusRegisterRead(void) { unsigned char i; SPI_CS=0; SPI_HostWriteByte(0xd7); i=SPI_HostReadByte(); SPI_CS=1;
return i; }
void AT45DB041B_ContinuousArrayRead(unsigned int PA,unsigned int BFA,unsigned char *pHeader,unsigned int len) { unsigned int i;
while(!(AT45DB041B_StatusRegisterRead()&0x80)); 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<len;i++) { pHeader=SPI_HostReadByte(); } SPI_CS=1;
}
void AT45DB041B_BufferWrite(unsigned char buffer,unsigned int BFA,unsigned char *pHeader,unsigned int len) { unsigned int i;
while(!(AT45DB041B_StatusRegisterRead()&0x80)); 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<len;i++) { SPI_HostWriteByte(pHeader); } SPI_CS=1; }
void AT45DB041B_BufferToMainMemoryPageProgramWithBuilt_inErase(unsigned char buffer,unsigned int PA,unsigned int BFA,unsigned char *pHeader,unsigned int len) {
AT45DB041B_BufferWrite(buffer,BFA,pHeader,len); while(!(AT45DB041B_StatusRegisterRead()&0x80)); 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;
}
void AT45DB041B_BufferToMainMemoryPageProgramWithoutBuilt_inErase(unsigned int buffer,unsigned int PA,unsigned int BFA,unsigned char *pHeader,unsigned int len){ unsigned int i=0;
AT45DB041B_BufferWrite(buffer,BFA,pHeader,len);
while(!(AT45DB041B_StatusRegisterRead()&0x80)); SPI_CS=0; SPI_HostWriteByte(0x87+buffer); SPI_HostWriteByte((unsigned char)(PA>>7)); SPI_HostWriteByte((unsigned char)(PA<<1)); SPI_HostWriteByte(0x00); for(i=0;i<len;i++) { SPI_HostWriteByte(pHeader); } SPI_CS=1;
}
/********************************** 主函数 **********************************/ void main(void) { unsigned int i;
UartInit();
SPI_RESET = 0; Delay(200); SPI_RESET = 1;
while(1) { if(flag_rxd) { if(uartrxbuff[0]==0xaa) { AT45DB041B_BufferToMainMemoryPageProgramWithBuilt_inErase(1,uartrxbuff[1],uartrxbuff[2],uartrxbuff,6); SBUF = 0xaa; while(!TI); TI = 0; } else { AT45DB041B_ContinuousArrayRead(uartrxbuff[1],uartrxbuff[2],uarttxbuff,6); for(i=0;i<6;i++) { SBUF = uarttxbuff; while(!TI); TI = 0; } }
for(i=0;i<2000;i++); flag_rxd = 0; } } }
|