给你一个调试成功的代码
Uint8 SD_Init(void)
{
Uint8 time=0;
temp = 0;
Uint8 Rev_Data[4]={0};
Uint8 pcmd8[] = {0x48,0x00,0x00,0x01,0xAA,0x87}; //CMD8
Uint8 pcmd55[] = {0x77,0x00,0x00,0x00,0x00,0x00}; //CMD55
Uint8 pcmd41[] = {0x69,0x40,0x00,0x00,0x00,0x00}; //CMD41
Uint8 pcmd58[] = {0x7A,0x00,0x00,0x00,0x00,0x00}; //CMD58
P9OUT &= ~BIT0; //打开片选
temp = SD_WriteCmd(pcmd8); //读取卡片版本
if(temp==0x05)
{
SD_Type =SDV1;
//设置卡类型为SD V1.0,如果是V1.0卡,CM8指令后没有后续数据
P9OUT |= BIT0;
SD_ReadWriteByte(0xff); //多发8时钟
P9OUT &= ~BIT0;
do
{
temp = SD_WriteCmd(pcmd55);
if(temp!=0x01) return temp;
temp = SD_WriteCmd(pcmd41);
time++;
}while((temp!= 0x00)&& (time<200));
P9OUT |= BIT0;
}// if(temp==0x05)
else if(temp == 0x01)
{
//V2.0卡,CM8命令后会传回4字节的数据,要跳过再结束本命令
Rev_Data[0] = SD_ReadWriteByte(0xff); //返回0x00
Rev_Data[1] = SD_ReadWriteByte(0xff); //返回0x00
Rev_Data[2] = SD_ReadWriteByte(0xff); //返回0x01
Rev_Data[3] = SD_ReadWriteByte(0xff); //返回0xAA
P9OUT |= BIT0;
SD_ReadWriteByte(0xff); //多发8时钟
P9OUT &= ~BIT0;
do
{
temp = SD_WriteCmd(pcmd55);
if(temp!=0x01) return temp;
temp = SD_WriteCmd(pcmd41);
time++;
if(time>250) return temp;
}while(temp!= 0x00);
//初始化指令发送完成,接下来获取OCR信息
/*------鉴别SD2.0卡版本--------------*/
temp=SD_WriteCmd(pcmd58);
if(temp != 0x00)return temp;
//读OCR指令发出后,紧接着是4字节的OCR信息
Rev_Data[0] = SD_ReadWriteByte(0xff);
Rev_Data[1] = SD_ReadWriteByte(0xff);
Rev_Data[2] = SD_ReadWriteByte(0xff);
Rev_Data[3] = SD_ReadWriteByte(0xff);
P9OUT |= BIT0;
SD_ReadWriteByte(0xff);
if(Rev_Data[0] & 0x40) SD_Type = SDHC;
else SD_Type =SDV2;
}// else if(temp == 0x01)
UCB2BR0 = 0x01; //SD为高速
UCB2BR1 = 0x00;
return 1;
|