//实现功能:从SD卡指定地址读取数据,一次最多512字节(参考SD卡读时序)
//================================================================================================
unsigned char SdReadBlock(unsigned char *Block, unsigned long address,unsigned int len)
{
unsigned int count;
SD_CS=0; //使能SD卡
SdCommand(17,address,0xff);//send command 17读扇区命令
if(SdResponse()==0x00)//command was a success - now send data
{
while(SdRead()!=0xfe); //等待数据的起始令牌号
for(count=0;count<len;count++)
*Block++=SdRead(); //读512个数据
SD_CS=1;
return 0;
}
return 1;
}
//*************************************************************************
//向指定地址写入512个字节
//************************************************************************
unsigned char SdWriteBlock(unsigned char *Block,unsigned long address,unsigned int len)
{
unsigned int count;
SD_CS=0; //使能SD卡
SdCommand(24,address,0xff);//send command 24 写扇区命令
if(SdResponse()==0x00) //command was a success - now send data
{
SdWrite(0xfe) ;//发送数据起始令牌
for(count=0;count<len;count++)
SdWrite(*Block++); //写入512个字节
if((SdResponse()&0x0F)==0x05) //sd卡接受数据
{
SD_CS=1;
return 0;
}
else
return 1;
}
else
return 1;
}
当我用SdWriteBlock某一个扇区写入512字节后,接着用SdReadBlock读出这512个字节,但是发现执行第一次SdReadBlock函数时SdResponse函数返回的一直是0xff,只有再一次执行SdReadBlock函数的时候,才可以把512个字节读出来,请问大家这是什么情况了,谢谢 |
|