用ATmega128A里面的硬件SPI控制SD卡,初始化和读取数据都成功,在向SD卡扇区里面写数据的时候,时序也是对的,但是该扇区里面的数据全部变成0xFF,而不是我想写进去的数据。
BYTE disk_writep (
const BYTE *buff, /* Pointer to the bytes to be written (NULL:Initiate/Finalize sector write) */
DWORD sa /* Number of bytes to send, Sector number (LBA) or zero */
)
{
BYTE res =1;
BYTE r;
unsigned int ty = 0;
SELECT();
if(!(CardType&CT_BLOCK)){
sa*= 512;
}
if(send_cmd(CMD24,sa) == 0){
SPI_MasterReviceByte();
SPI_MasterSendByte(0xFE);
for(ty = 0; ty <512; ty++){
SPDR = buff[ty];
while(!(SPSR&(1<<SPIF)));
}
SPI_MasterReviceByte();
SPI_MasterReviceByte();
for(ty =0; ty <1000; ty++){
res = SPI_MasterReviceByte();
if(res!=0xFF){
break;
}
}
if((res&0x1F)==0x05){
res = 0;
}
do{
r= SPI_MasterReviceByte();
}while(r!=0xff);
}
DESELECT();
SPI_MasterReviceByte();
return res;
}
问题解决,在ICCAVR里面把const放在flash里面,这时候buff指向的地址就是flash里面的地址,而不是内存里面的地址,把const去掉或者在函数里面定义一个unsigned char *buff1 = (unsigned char *)buff;发送数据时候用buff1.
|