正文

sd卡  Secure Digital Card2005-07-16 15:43:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/niao0311/2894.html

分享到:

   这是我自己搜改的   希望大家多提建议    #include "HAL.H" //============================================================ //往SD卡定一字节 void SdWrite(unsigned char  n) {        unsigned char i;   for(i=8;i;i--)    {     SD_CLK=0;     SD_DI=(n&0x80);     n<<=1;     SD_CLK=1;    }   SD_DI=1;       }    //================================================================ //从SD卡读一字节 unsigned char SdRead() { unsigned char n,i; for(i=8;i;i--)   {    SD_CLK=0;    SD_CLK=1;    n<<=1;    if(SD_DO) n|=1;      } return n; } //================================================================ //读SD卡响应 unsigned char SdResponse() {   unsigned char i=0,response;      while(i<=8)   {     response=SdRead();     if(response==0x00)       break;     if(response==0x01)       break;     i++;   }   return response; }   //================================================================ void SdCommand(unsigned char command, unsigned long argument, unsigned char CRC) {      SdWrite(command|0x40);   SdWrite(((unsigned char *)&argument)[0]);   SdWrite(((unsigned char *)&argument)[1]);   SdWrite(((unsigned char *)&argument)[2]);   SdWrite(((unsigned char *)&argument)[3]);   SdWrite(CRC); } //================================================================ unsigned char SdInit(void) {   int delay=0, trials=0;   unsigned char i;   unsigned char response=0x01;    //你这里定义response=0x01是不是多余啊   SD_CS=1;   for(i=0;i<=9;i++)     SdWrite(0xff);   SD_CS=0;      //Send Command 0 to put MMC in SPI mode   SdCommand(0x00,0,0x95);              //send CMD0      response=SdResponse();      if(response!=0x01)    {     return 0;    }      while(response==0x01)   {     SD_CS=1;     SdWrite(0xff);     SD_CS=0;     SdCommand(0x01,0x00ffc000,0xff);    // send CMD1 这里一定要写0x00ffc000吗?     response=SdResponse();   }        SD_CS=1;   SdWrite(0xff);   return 1;   } //================================================================ unsigned char SdWriteBlock(unsigned char *Block, unsigned long address) {   unsigned char  count;   unsigned char dataResp;   //Block size is 512 bytes exactly   //First Lower SS      SD_CS=0;   //Then send write command   SdCommand(0x18,address,0xff);  //CMD24 WRITE_BLOCK      if(SdResponse()==00)   {     SdWrite(0xff);     SdWrite(0xff);     SdWrite(0xff);     //command was a success - now send data     //start with DATA TOKEN = 0xFE     SdWrite(0xfe);     //now send data     for(count=0;count<128;count++)     {       SdWrite(*Block++);       SdWrite(*Block++);       SdWrite(*Block++);       SdWrite(*Block++);     }     //data block sent - now send checksum     SdWrite(0xff);     SdWrite(0xff);     //Now read in the DATA RESPONSE token     dataResp=SdRead();     //Following the DATA RESPONSE token     //are a number of BUSY bytes     //a zero byte indicates the MMC is busy     while(SdRead()==0);     dataResp=dataResp&0x0f;    //mask the high byte of the DATA RESPONSE token     SD_CS=1;     SdWrite(0xff);     if(dataResp==0x0b)       {       //printf("DATA WAS NOT ACCEPTED BY CARD -- CRC ERROR\n");       return 0;       }     if(dataResp==0x05)       return 1;            //printf("Invalid data Response token.\n");     return 0; }   //printf("Command 0x18 (Write) was not received by the MMC.\n");   return 0; } //======================================================================= unsigned char SdReadBlock(unsigned char *Block, unsigned long address) {    unsigned char count;   //Block size is 512 bytes exactly   //First Lower SS    //  printf("MMC_read_block\n");      SD_CS=0;   //Then send read command   SdCommand(0x11,address,0xff);       //send CMD17 READ_SINGLE_BLOCK      if(SdResponse()==00)   {     //command was a success - now send data     //start with DATA TOKEN = 0xFE     while(SdRead()!=0xfe);     for(count=0;count<128;count++)     {       *Block++=SdRead();       *Block++=SdRead();       *Block++=SdRead();       *Block++=SdRead();     }     //data block sent - now send checksum     SdRead();     SdRead();          //send two CRC bytes to host     //Now read in the DATA RESPONSE token     SD_CS=1;     SdRead();     return 1; } //  printf("Command 0x11 (Read) was not received by the MMC.\n");   return 0; }

阅读(27966) | 评论(1)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册