<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 51單片機讀寫SD卡程序

          51單片機讀寫SD卡程序

          作者: 時間:2016-11-29 來源:網(wǎng)絡 收藏

          //================================================================
          //往SD卡指定地址寫數(shù)據(jù),一次最多512字節(jié)
          //unsigned char SdWriteBlock(unsigned char *Block, unsigned long address,int len)
          unsigned char SdWriteBlock(unsigned long address,int len)
          {
          unsigned int count;
          unsigned char dataResp;
          //Block size is 512 bytes exactly
          //First Lower SS
          SD_CS=0;
          //Then send write command
          SdCommand(0x18,address,0xff);
          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;countfor(count=0;count//for(count=0;countfor(;count<512;count++) SdWrite(0);
          //data block sent - now send checksum
          SdWrite(0xff); //兩字節(jié)CRC校驗, 為0XFFFF 表示不考慮CRC
          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");
          return 0;
          }
          if(dataResp==0x05)
          return 1;
          //printf("Invalid data Response token.");
          return 0;
          }
          //printf("Command 0x18 (Write) was not received by the MMC.");
          return 0;
          }
          //=======================================================================
          //從SD卡指定地址讀取數(shù)據(jù),一次最多512字節(jié)
          unsigned char SdReadBlock(unsigned char *Block, unsigned long address,int len)
          {
          unsigned int count;
          //Block size is 512 bytes exactly
          //First Lower SS
          //printf("MMC_read_block");
          SD_CS=0;
          //Then send write command
          SdCommand(0x11,address,0xff);
          if(SdResponse()==00)
          {
          //command was a success - now send data
          //start with DATA TOKEN = 0xFE
          while(SdRead()!=0xfe);
          for(count=0;countfor(;count<512;count++) SdRead();
          //data block sent - now send checksum
          SdRead();
          SdRead();
          //Now read in the DATA RESPONSE token
          SD_CS=1;
          SdRead();
          return 1;
          }
          //printf("Command 0x11 (Read) was not received by the MMC.");
          return 0;
          }

          void initbaud(void)
          {
          TMOD=0X20;
          TH1=0XFD;
          TL1=0XFD;
          PCON=0X00;
          TR1=1;
          SCON=0X50;//8位波特可變
          //SCON=0X52;//8位波特可變 TI開中斷
          }
          //============================================================
          //主程序
          main()
          {
          unsigned int i;
          unsigned long AddTemp=331264;//SD卡地址第一個數(shù)據(jù)物理地址初始值,可以用winhex查看,這里是641物理扇區(qū),512x641=328192,根據(jù)實際SD卡內(nèi)容更改
          delayms(5);
          SdInit(); //SD卡初始化
          while(1)
          {
          SdWriteBlock(AddTemp,512);
          delayms(1000);
          AddTemp=331264;
          SdReadBlock(DATA,AddTemp,512);//每次讀出512字節(jié)放到緩沖區(qū)
          initbaud();
          for(i=0;i<512;i++)
          {
          SBUF=DATA[i];
          while(!TI);
          TI=0;
          delayms(1);
          }
          while(KEY); //等待按鍵按下繼續(xù)執(zhí)行

          }
          }
          可以在串口中看到SD卡中被寫入的數(shù)據(jù),程序在MCU-51/AVR開發(fā)板進行了實驗。具體介紹可訪問:
          http://item.taobao.com/item.htm?spm=0.0.0.50.xZjZ87&id=21441052281,http://item.taobao.com/item.htm?spm=1103*oQM.3-5SusJ.h-2Yh1mq&id=14049701171& 這是兩款功能強大的51、AVR開發(fā)板,對初學者和研發(fā)人員都是不錯的選擇,下一節(jié)給大家講下結(jié)合SD卡的TFT真彩屏的相關讀寫!




          上一頁 1 2 下一頁

          關鍵詞: 51單片機讀寫SD

          評論


          技術專區(qū)

          關閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();