我是用的新华龙单片机C8051F060,采用模拟SPI总线,初始化SD卡成功;初始化程序如下所示:
uchar MMC_SD_init(void)
{
uchar i;
uchar retry;
uchar r1=0;
uchar type=0;
SFRPAGE = CONFIG_PAGE;
SPI_SD = 0;
retry = 0;
do
{
for(i=0;i<20;i++) SPI_Write(0xFF);
r1 = MMC_SD_SendCmd(0, 0); //发送COM0,需要返回0x01
WDTCN = 0xA5;
retry++;
if(retry > 200) return (type=0x01);
}while(r1!=0x01);
retry = 0;
do
{
r1 = MMC_SD_SendCmd(8, 0); //发送COM8,查询SD卡版本号;若返回0x01说明此卡版本2.0;若返回0x05则为版本1.0
WDTCN = 0xA5; //发送COM58获取SD卡电压参数
retry++;
if(retry > 100) return (type=0x01);
}while((r1!=0x01)&&(r1!=0x05)); //||r1!=0x05 &&(r1!=0x09)
retry = 0;
do
{
r1 = MMC_SD_SendCmd(55, 0); //发送COM55,需要返回0x01
if(r1 == 0x01)
{
r1 = MMC_SD_SendCmd(41,0); //发送COM41,需要返回0x00
if(r1 == 0x00) type = 0x10;
}
else
{
r1 = MMC_SD_SendCmd(1,0);
if(r1 == 0x00) type = 0x20;
}
WDTCN = 0xA5;
retry++;
if(retry > 200) return (type=0x01);
}while(r1!=0x00);
SPI_Read(0xFF);
r1 = MMC_SD_SendCmd(59, 0); //发送COM59是否进行CRC校验,发送COM38擦除所选择的块;发送COM24单块写命令,返回0x00,
r1 = MMC_SD_SendCmd(16, 512); //COM16设置块长度默认正式传输的数据长度是512字节,COM17单块读取命令,返回0x00,
SPI_SD = 1;
return type;
}
函数返回值为0X10;且读取的SD卡版本号r1=0x01即为2.0卡
然后读取SD卡第一块数据,函数如下所示:
//---------------------------------------------------
//读取单个扇区数据;512字节
//---------------------------------------------------
uchar MMC_SD_ReadSingleBlock(ulong sector,uchar *buffer)
{
uchar r1;
uint i;
SFRPAGE = CONFIG_PAGE;
SPI_SD = 0;
r1 = MMC_SD_SendCmd(17, sector<<9); //COM16设置块长度,COM17单块读取命令,返回0x00,
if(r1 != 0x00) return r1;
while(SPI_Read(0xFF) != 0xFE) //这里一直读取不到0xfe???
{
WDTCN = 0xA5;
}
for(i=0; i<512; i++)
{
*buffer++ = SPI_Read(0xFF);
}
SPI_Write(0xFF);
SPI_Write(0xFF);
SPI_SD = 1;
return 0;
}
主程序上电初始化SD卡能返回0x10说明成功了,
然后2秒后读取第0块数据,一直死在循环while(SPI_Read(0xFF) != 0xFE) 这里了,读出来的一直是0xFF.想请教各位兄弟姐妹原因在那里?
|