SD卡驱动程序(51单片机)初始化SD卡到SPI模式
unsigned char SD_Init()
{
unsigned char retry,temp;
unsigned char i;
unsigned char CMD[] = {0x40,0x00,0x00,0x00,0x00,0x95};
SD_Port_Init(); //初始化驱动端口
Init_Flag=1; //将初始化标志置1
for (i=0;i<0x0f;i++)
{
Write_Byte_SD(0xff); //发送至少74个时钟信号
}
//向SD卡发送CMD0
retry=0;
do
{ //为了能够成功写入CMD0,在这里写200次
temp=Write_Command_SD(CMD);
retry++;
if(retry==200)
{ //超过200次
return(INIT_CMD0_ERROR);//CMD0 Error!
}
}
while(temp!=1); //回应01h,停止写入
//发送CMD1到SD卡
CMD[0] = 0x41; //CMD1
CMD[5] = 0xFF;
retry=0;
do
{ //为了能成功写入CMD1,写100次
temp=Write_Command_SD(CMD);
retry++;
if(retry==100)
{ //超过100次
return(INIT_CMD1_ERROR);//CMD1 Error!
}
}
while(temp!=0);//回应00h停止写入
Init_Flag=0; //初始化完毕,初始化标志清零
SPI_CS=1; //片选无效
return(0); //初始化成功
}
1