请教一下SD卡的问题,第一个问题是:以下函数中的r1 = SD_ReadWriteByte(0xFF);是什么意思?为什么要发送0xff,且if(retry==0xfffe) 中的0xff又是意思?
u8 SD_WaitReady(void) {
u8 r1;
u16 retry;
retry = 0;
do
{
r1 = SD_ReadWriteByte(0xFF);
if(retry==0xfffe) {
return 1;
}
}while(r1!=0xFF);
return 0;
}
我在不完全手册上看到的是 r1=SPIx_ReadWriteByte(0xFF)&0X1F,为什么要取低5位呢?
第二个问题如下:是不是每次写命令都要在写命令之前和写完命令之后添加8个时钟周期,也就是添加SD_ReadWriteByte(0xff)函数,其中的while((r1 = SD_ReadWriteByte(0xFF))==0xFF)该怎么理解呢?在线急等答复,再次谢谢!
u8 SD_SendCommand(u8 cmd, u32 arg, u8 crc)
{
unsigned char r1;
unsigned char Retry = 0;
SD_ReadWriteByte(0xff);
//片选端置低,选中SD卡
SD_CS_ENABLE(); //片选置低
//发送
SD_ReadWriteByte(cmd | 0x40);
SD_ReadWriteByte(arg >> 24);
SD_ReadWriteByte(arg >> 16);
SD_ReadWriteByte(arg >> 8);
SD_ReadWriteByte(arg);
SD_ReadWriteByte(crc); //写CRC效验值
//等待响应,或超时退出
while((r1 = SD_ReadWriteByte(0xFF))==0xFF)
{
Retry++;
if(Retry > 200)
{
break;
}
}
//关闭片选
SD_CS_DISABLE();
//在总线上额外增加8个时钟,让SD卡完成剩下的工作
SD_ReadWriteByte(0xFF);
//返回状态值
return r1;
}