|
我先写了72个时钟,然后cs拉低,发送CM0的命令。得到响应为0x01.然后我发送0x08,确认是否是HCSD卡时,结果返回值为0x09.但是资料上说返回值一般为0x01和0x00两种情况而已。
于是我就跳过这一步以后直接发送0x55,结果又返回0x05.和资料上的又不一样了。如果发送CMD01的话,返回值为0x01.也不能达到效果。求大神看看是怎么回事呢???这个问题纠结了好几天了。。。。。。
附上代码
//===========================================================
//写一字节到SD卡,模拟SPI总线方式
void SdWrite(unsigned char n)
{
unsigned char i;
for(i=8;i;i--)
{
SD_CLK_CLR;
if(n&0x80)
{
SD_DI_SET;
}
else
{
SD_DI_CLR;
}
n<<=1;
SD_CLK_SET;
}
SD_DI_SET;
}
//===========================================================
//从SD卡读一字节,模拟SPI总线方式
unsigned char SdRead()
{
unsigned char n,i;
for(i=8;i;i--)
{
SD_CLK_CLR;
SD_CLK_SET;
n<<=1;
if(SD_DO) n|=1;
}
return n;
}
//================================================================
//发命令到SD卡
unsigned char SdCommand(unsigned char command, unsigned long argument, unsigned char CRC)
{
unsigned char r1,time=0;
SD_CS_SET;
SdWrite(0xff);
SD_CS_CLR;
SdWrite(command|0x40);
SdWrite(((unsigned char *)&argument)[0]);
SdWrite(((unsigned char *)&argument)[1]);
SdWrite(((unsigned char *)&argument)[2]);
SdWrite(((unsigned char *)&argument)[3]);
SdWrite(CRC);
do
{
r1=SdRead();
time++;
}while(r1==0xff&&time<TRY_TIME);
return r1;
}
//================================================================
//初始化SD卡
unsigned char SdInit(void)
{
unsigned char time,i;
unsigned char r1;
SD_CS_SET;
for(i=0;i<=9;i++)
SdWrite(0xff);
SD_CS_CLR;
time=0;
do
{
r1=SdCommand(0x00,0,0x95);
time++;
if(time==TRY_TIME) return 1;
}while(r1!=0x01);
r1=SdCommand(0x01,0,0xff); //此处为调试部分
time=0;
do
{
r1=SdCommand(0x01,01000000,0xff);
time++;
if(time==TRY_TIME) return 1;
}while(r1!=0);
SD_CS_SET;
SdWrite(0xff);
return 0;
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|