4238|0

76

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

求救!!发送CMD17后无法返回0XFE! [复制链接]


正常初始化后在发送CMD17准备读取SD卡中的数据时一直得不到0XFE的响应。各位帮帮忙~~谢谢
串口发送出来的结果:
THIS IS A TEST!
CMD0'S RESPONSE IS 1
CMD55'S RESPONSE IS 1
CMD55'S RESPONSE IS 1
THIS IS A TEST!
CMD0'S RESPONSE IS 1
CMD55'S RESPONSE IS 1
CMD55'S RESPONSE IS 1
CMD55'S RESPONSE IS 1
CMD55'S RESPONSE IS 1
ACMD41'S RESPONSE IS 0
SD CARD INITIALIZE IS DONE!
CMD16'S RESPONSE IS 0
CMD17'S RESPONSE IS ff
CMD17'S RESPONSE IS 0
THE END CMD17'S RESPONSE IS 0
WAIT FOR THE 0XFE RESPONSE IS ff
WAIT FOR THE 0XFE RESPONSE IS 0
WAIT FOR THE 0XFE RESPONSE IS ff
WAIT FOR THE 0XFE RESPONSE IS 0
WAIT FOR THE 0XFE RESPONSE IS ff
WAIT FOR THE 0XFE RESPONSE IS 5
WAIT FOR THE 0XFE RESPONSE IS ff
----------------------------------------------------------------代码如下-----------------------------------------------------------------------------------------------------------BYTE SDRDDATA[SD_DATA_SIZE];
BYTE SDWRDATA[SD_DATA_SIZE];
BYTE SDCMD[SD_CMD_SIZE];
BYTE CODE[1]={0XFF};
void SPI_INIT(void)            SPI初始化{
PINSEL1=0X00A8;
IODIR0=SPI_SEL;
IOSET0=SPI_SEL;
SSPCR0=0X0707;
SSPCR1=0X02;
SSPCPSR=0XFE;  
}
void SPI_SEND(BYTE *buf,DWORD LENGTH)
{

if(LENGTH==0)
return ;
while(LENGTH!=0)
{
  while(!(SSPSR&0X02));
  SSPDR=*buf;
  while((SSPSR&0X10));
  LENGTH--;
  *buf++;
}
return;
}
BYTE SPI_ReceiveByte(void)
{
BYTE data;
SSPDR=0XFF;
while(SSPSR&0X10);
data=SSPDR;
return(data);
}
BYTE SPI_Receive(BYTE *buf,DWORD LENGTH)
{
DWORD i;
for(i=0;i {
  *buf=SPI_ReceiveByte();
  *buf++;
  }
return 0 ;
}
int SD_INIT(void)
{
DWORD i,retry,response;
for(i=0;i {
  SDWRDATA=i;
  }
IOSET0=SPI_SEL;
for(i=0;i<0x0f;i++)
{
  SDRDDATA=0xff;
  }
SPI_SEND(SDRDDATA,0x0f);
IOCLR0=SPI_SEL;
SDCMD[0]=0x40;
SDCMD[1]=0x00;
SDCMD[2]=0x00;
SDCMD[3]=0x00;
SDCMD[4]=0x00;
SDCMD[5]=0x95;
retry=0;
do
{
  SPI_SEND(SDCMD,SD_CMD_SIZE);
  retry++;
  response=SPI_ReceiveByte();
  if(retry>=200)
  printf("CMD0 ERROR!TIME OUT!\n ");
  
  }while(response!=1) ;
  printf("CMD0'S RESPONSE IS %x\n ",response);
  IOSET0=SPI_SEL;
  SPI_SEND(SDRDDATA,1);
  IOCLR0=SPI_SEL;
retry=0 ;
do
{
  SDCMD[0]=0x77;
  SDCMD[5]=0xff;
  SPI_SEND(SDCMD,SD_CMD_SIZE);
  response=SPI_ReceiveByte();
  printf("CMD55'S RESPONSE IS %x\n",response);
  IOSET0=SPI_SEL;
  SPI_SEND(SDRDDATA,1);
  IOCLR0=SPI_SEL;
  SDCMD[0]=0x69;
  response=SPI_ReceiveByte();
  retry++;
  }while((response!=0)&&(retry<100) )  ;
  printf("ACMD41'S RESPONSE IS %x\n",response);
  printf("SD CARD INITIALIZE IS DONE!\n");
  IOSET0=SPI_SEL;
  SPI_SEND(SDRDDATA,1);
  IOCLR0=SPI_SEL;
  SDCMD[0]=0X50;
  SDCMD[1]=0X00;
  SDCMD[2]=0X00;
  SDCMD[3]=0X02;
  SDCMD[4]=0X00;
  SDCMD[5]=0XFF;
  retry=0;
  do
  {
  SPI_SEND(SDCMD,SD_CMD_SIZE);
  response=SPI_ReceiveByte();
  retry++;
  }while((response!=0)&&(retry<100));
  printf("CMD16'S RESPONSE IS %x\n",response);
  return ;
}

WORD SD_read_block(WORD block_num)             就是这部分出问题!{
WORD Checksum;
WORD varh,varl;
WORD response,retry,i;
DWORD TEMP;
IOCLR0=SPI_SEL;
varh=((block_num&0x003f)<<9);
varl=((block_num&0xffc0)<<7);
SDCMD[0]=0x51;
SDCMD[1]=0x00;
SDCMD[2]=0x00;
SDCMD[3]=0x00;
SDCMD[4]=0x00;
SDCMD[5]=0xff;
SPI_SEND(CODE,1);
retry=0;
do
{
SPI_SEND(SDCMD,SD_CMD_SIZE);
SPI_SEND(CODE,1);
response=SPI_ReceiveByte();
printf("CMD17'S RESPONSE IS %x\n",response);
retry++;
if(retry>=100)
printf("TIME OUT!\n");
}while(response!=0);
printf("THE END CMD17'S RESPONSE IS %x\n",response);
SPI_SEND(CODE,1);
retry=0;
for(i=0;i<1000;i++)
{
  response=SPI_ReceiveByte();
  retry++;
  if(response==0xfe)
  i=1000;
  if(retry==1000)
  printf("ERROR!CAN'T READ 0XFE!\n");
  delay_nms(1);
  printf("WAIT FOR THE 0XFE RESPONSE IS %x\n",response);
  }
  
  TEMP=SPI_Receive(SDRDDATA,SD_DATA_SIZE);
  Checksum=SPI_ReceiveByte();
  Checksum=Checksum<<0x08|SPI_ReceiveByte();
  printf("READ SD:%x",TEMP);
  IOSET0=SPI_SEL;
  SPI_SEND(SDCMD,1);
  return (TEMP) ;
}
点赞 关注

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表