单片机读写SD卡教程Word格式文档下载.doc
《单片机读写SD卡教程Word格式文档下载.doc》由会员分享,可在线阅读,更多相关《单片机读写SD卡教程Word格式文档下载.doc(8页珍藏版)》请在冰点文库上搜索。
//用来调程序标志
//===========================================================
//定义512字节缓冲区,,89C52直接定义成unsignedcharDATA[80];
,太大了RAM不够
unsignedcharxdataDATA[512];
voiddelay(unsignedintz)
{
unsignedintx,y;
for(x=z;
x>
0;
x--);
for(y=110;
y>
y--);
}
//写一字节到SD卡,模拟SPI总线方式
voidSdWrite(unsignedcharn)
unsignedchari;
for(i=8;
i;
i--)
SD_CLK=0;
SD_DI=(n&
0x80);
n<
<
=1;
SD_CLK=1;
SD_DI=1;
}
//从SD卡读一字节,模拟SPI总线方式
unsignedcharSdRead()
unsignedcharn,i;
SD_CLK=1;
SD_CLK=0;
n<
if(SD_DO)n|=1;
returnn;
//============================================================
//检测SD卡的响应
unsignedcharSdResponse()
uchari=0,response=0;
while(i<
=8)
response=SdRead();
if(response==0x00)
break;
if(response==0x01)
i++;
returnresponse;
//================================================================
//发命令到SD卡
voidSdCommand(unsignedcharcommand,unsignedlongargument,unsignedcharCRC)
SdWrite(command|0x40);
/*SdWrite(((unsignedchar*)&
argument)[0]);
SdWrite(((unsignedchar*)&
argument)[1]);
argument)[2]);
argument)[3]);
*/
SdWrite(argument>
>
24);
16);
8);
SdWrite(argument);
SdWrite(CRC);
//初始化SD卡
unsignedcharSdInit(void)
unsignedchari;
unsignedcharresponse=0xFF;
P3=0xff;
SD_CS=1;
for(i=0;
i<
=0xfe;
i++)
SdWrite(0xff);
SD_CS=0;
SdCommand(0x00,0,0x95);
SD_DI=1;
response=SdResponse();
if(response!
=0x01)
{
return0;
}
{ //不管什么SD卡都能进入这一步,同时也说明硬件没问题
SD_CS=1;
//Beep=0;
//while
(1);
//用来查看程序能否运行到这一步,去掉//即可
SdWrite(0xff);
SD_CS=0;
while
(1)
{
SdCommand(0x01,0,0xff);
//SdCommand(0x01,0x00000000,0xff);
//进SPI
response=0xff;
SD_DI=1;
for(i=0;
250;
i++)//response!
=0x00//等待回复
{
response=SdResponse();
//Beep=0;
/
if(response==0)break;
}
if(response==0){break;
}//回复0则通过SPI,只要通过SPI后面的指令才能继//续
// Beep=0;
}
// Beep=0;
//看程序能否跳出来,挑不出来则进不了SPI
return1;
}
//往SD卡指定地址写数据,一次最多512字节最好不要乱写否则fat系统被改掉SD卡打不开
unsignedcharSdWriteBlock(unsignedchar*Block,unsignedlongaddress,intlen)
unsignedintcount;
unsignedchardataResp;
SD_CS=0;
SdCommand(0x18,address,0xff);
if(SdResponse()==00)
SdWrite(0xff);
//commandwasasuccess-nowsenddata
//startwithDATATOKEN=0xFE
SdWrite(0xfe);
//nowsenddata
for(count=0;
count<
len;
count++)SdWrite(*Block++);
for(;
512;
count++)SdWrite(0);
//datablocksent-nowsendchecksum
//两字节CRC校验,为0XFFFF表示不考虑CRC
//NowreadintheDATARESPONSEtoken
dataResp=SdRead();
while(SdRead()==0);
dataResp=dataResp&
0x0f;
//maskthehighbyteoftheDATARESPONSEtoken
SD_CS=1;
if(dataResp==0x0b)
return0;
if(dataResp==0x05)return1;
//printf("
Command0x18(Write)wasnotreceivedbytheSD.\n"
);
//=======================================================================
//从SD卡指定地址读取数据,一次最多512字节
unsignedcharSdReadBlock(unsignedchar*Block,unsignedlongaddress,intlen)
unsignedintcount,i;
ucharresponse;
// while
(1)
// {
SdCommand(0x11,address,0xff);
// SD_DO=1;
// P0=SdResponse();
// P0=0xff;
// while
(1);
while(response!
=0xfe&
&
i<
200){response=SdRead();
P0=response;
i++;
// if(i>
=200)Beep=0;
// P0=0xf0;
// }
// Beep=0;
for(count=0;
count++)*Block++=SdRead();
SdRead();
//NowreadintheDATARESPONSEtoken
return1;
voidmain()
unsignedintmm;
unsignedlongAddTemp=0x11200;
//70144;
//SD卡地址第一个数据物理地址初始值70144是通过winhex查看,对于你的SD卡肯定要改
//可以用winhex查看,一定要是512整数倍
mm=SdInit();
//
SdReadBlock(DATA,AddTemp,1);
delay(10);
if(DATA[0]==0xeb)Beep=0;
//看读的对不对
while
(1);