搜索
bottom↓
回复: 3

读SD卡的一个问题!--请更改成能说明帖子大意的标题

[复制链接]

出0入0汤圆

发表于 2005-12-11 19:59:01 | 显示全部楼层 |阅读模式
我在读SD卡的时候。SD卡的初始化都完成了,但是在接收开始信号0XFE的时候一直接受不到!

//Send the start byte

while(Read_Byte_SD()!=0xfe); 一直运行它!

找不出原因来,请大家帮帮!

程序如下:

#include <iom16v.h>

#include <macros.h>

#include <SD.h>

#include <main.h>



void Write_Byte_SD(char Byte)

{

SPDR=Byte;

while(!(SPSR&(1<<SPIF)));

}



char Read_Byte_SD(void)

{

char Byte;

SPDR=0xff;

while(!(SPSR&(1<<SPIF)));

Byte=SPDR;

return(Byte);

}





char Write_Command_SD(char*CMD)

{

char a;

char tmp=0xff;

char Timeout=0;



// Raise chip select

SD_Disable();



// Send an 8 bitpulse

Write_Byte_SD(0xFF);

  



// Lower chip select

SD_Enable();



//Send the 6 byte command

for(a=0;a<0x06;a++)

    {

     Write_Byte_SD(*CMD++);

    }



//Wait for the response

while(tmp==0xff)

      {

       tmp=Read_Byte_SD();

       if(Timeout++>100)

         {

          break;

         }

      }

//for some reason we need to delay here

Delay_ms(1);



return(tmp);

}





char SDInit(void)

{

char a,b,retry;

char CMD[]={0x40,0x00,0x00,0x00,0x00,0x95};



// Set certain pins to inputs and others to outputs

// Only SPI_DI (data in) is an input

SD_Direction_REG&=~(1<<SPI_DI);

SD_Direction_REG|=(1<<SPI_Clock);

SD_Direction_REG|=(1<<SPI_DO);

SD_Direction_REG|=(1<<SD_Chip_Select);

SD_Direction_REG|=(1<<SPI_SS);

SD_Write|=(1<<SD_Chip_Select);



//We need to wait for the SD_Direction_REG to be ready

for(a=0;a<200;a++)

    {  

     nop();

    };

//Enable SPI in Master Mode with IDLE low and clock at 1/128

SPCR=(1<<SPE)|(1<<MSTR)|(1<<SPR0)|(1<<SPR1);

SPSR=(0<<SPI2X);

  

// We need to give the card about a Hundred cycles to load

for (b=0;b<0x0f;++b)

     {

      Write_Byte_SD(0xff);

     }



//Send the initialization commands to the card

retry=0;

  

//Note,many examples check for(Write_Command_SD(CMD)==R1_IN_IDLE_STATE)

//However,with our setup the card was in IDLE state and returning another command





while((Write_Command_SD(CMD)&0X01)==0)

{

//fail and return

if(retry++>50)

{  

return 1;

}

}



//Send the 2nd command

retry=0;

CMD[0]=0x41;

CMD[5]=0xFF;



while(Write_Command_SD(CMD)!=0)

      {

       if (retry++>50)

          {

          return 2;

          }

       }



   



/*//Set the SPI bus to full speed

SPCR=SPCR|(0<<SPR0)|(0<<SPR1);

SPSR=SPSR|(1<<SPI2X);

*/



//Raise Chip Select



SD_Disable();



return 0;

}





/*char SD_set_length(unsigned long length)

{char retry;

//Command to set the block length;

char CMD[]={0x50,0x00,0x00,0x00,0x00,0xFF};



CMD[1]=((length&0xFF000000)>>24);

CMD[2]=((length&0x00FF0000)>>16);

CMD[3]=((length&0x0000FF00)>>8);

CMD[4]= (length&0x000000FF);



while(Write_Command_SD(CMD)!=0)

      {

       if (retry++>50)

          {

          return 1;

          }

       }

SD_Disable();

return 0;

}

*/



char SD_Read_Block(unsigned long addr,unsigned char*Buffer,unsigned int length)

{

int a;

char retry;

//Command to read a block;

char CMD[]={0x51,0x00,0x00,0x00,0x00,0xFF};



CMD[1]=((addr&0xFF000000)>>24);

CMD[2]=((addr&0x00FF0000)>>16);

CMD[3]=((addr&0x0000FF00)>>8);



// Send the read command

  

while(Write_Command_SD(CMD)!=0)

      {

       if (retry++>50)

          {

          return 1;

          }

       }



//Send the start byte

while(Read_Byte_SD()!=0xfe);                                                                           

//Read off all the bytes in the block

for(a=0;a<length;++a)

{

*Buffer++=Read_Byte_SD();

}  



//Read CRC byte

Read_Byte_SD();

Read_Byte_SD();

  

// Set SD_Chip_Select to high

SD_Disable();

  

return 0;

}



<font color=red>以下红色文字由版主:judy58 于:2005-12-26,13:45:34 加入。
请更改成能说明帖子大意的标题

本论坛规定:一定要起一个能说明帖子大意的标题。不允许“关于 AVR 的 ADC 使用!”这种笼统标题。作为标题,必须说明清楚:

①你是在请教问题,还是在介绍有关的知识?

②ADC的问题有许多,你是想说那方面的内容?

注意以上两点,标题应该改成如:“请教:ADC 可以不使用中断吗?”。

起一个能说明帖子大意的标题,除了减轻我们的帖子分类整理工作,还方便其它人阅读,节省大家的时间。

谢谢你的支持。

注:你可以使用编辑功能修改标题,并将这段红色文字删除。谢谢你的支持!

阿莫论坛20周年了!感谢大家的支持与爱护!!

曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……

出0入0汤圆

 楼主| 发表于 2005-12-12 09:27:48 | 显示全部楼层
顶上去哦

出0入0汤圆

 楼主| 发表于 2005-12-13 09:20:19 | 显示全部楼层
顶上去哦

出0入0汤圆

发表于 2016-4-26 15:50:30 | 显示全部楼层
这么多年了,楼主白顶了。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-6-1 08:46

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表