色婷婷AⅤ一区二区三区|亚洲精品第一国产综合亚AV|久久精品官方网视频|日本28视频香蕉

          新聞中心

          SD卡讀寫實驗程序

          作者: 時間:2012-03-30 來源:網(wǎng)絡(luò) 收藏

          unsigned char sd_reset()
          {
          unsigned char i,tmp;
          unsigned char retry; //重復(fù)次數(shù)
          unsigned char r1=0;
          retry=0;
          delay();
          delay();
          do
          {
          for(i=0;i100;i++) SPI_WriteByte(0xff);

          r1 = _SendCommand(0,0);//發(fā)idle命令
          retry++;
          if(retry>20) return 1; //超時退出
          } while(r1 != 0x01); //等待IDLE命令返回

          本文引用地址:http://cafeforensic.com/article/149308.htm

          retry = 0;
          cs=0;
          do
          {
          for(i=0;i100;i++) SPI_WriteByte(0xff);

          r1 = _SendCommand(1, 0); //發(fā)Active命令
          retry++;
          if(retry>254) return 1; //超時退出
          } while(r1);

          for(i=0;i100;i++) SPI_WriteByte(0xff);

          r1 = _SendCommand(59, 0); //關(guān)crc
          if (r1) return 1; //返回不正確,退出初始化

          for(i=0;i100;i++) SPI_WriteByte(0xff);

          r1 = SD_SendCommand(16, 512); //設(shè)扇區(qū)大小512
          if(r1!=0) return 1; //返回不正確,退出初始化
          return 0; //正常返回
          }

          //********************寫一個扇區(qū)**************************
          unsigned char SD_WriteSingleBlock(unsigned long sector)
          {
          unsigned char r1;
          unsigned int i;
          unsigned char retry=0;
          do
          {
          for(i=0;i100;i++) SPI_WriteByte(0xff);

          r1 = SD_SendCommand(24, sector9);//寫命令
          retry++;
          if(retry>10) return 1; //超時退出
          } while(r1 != 0x00);

          cs=0;

          SPI_WriteByte(0xff);
          SPI_WriteByte(0xff);
          SPI_WriteByte(0xff);
          SPI_WriteByte(0xff);
          SPI_WriteByte(0xff);
          SPI_WriteByte(0xff);

          SPI_WriteByte(0xfe); //發(fā)開始符

          for(i=0; i512; i++) //送512字節(jié)數(shù)據(jù)
          {
          if(i255) SPI_WriteByte(i); //發(fā)送0--255
          else SPI_WriteByte(512-i); //發(fā)送255--0

          }

          SPI_WriteByte(0x95);
          SPI_WriteByte(0x95); //16-bits CRC

          r1 = SPI_WriteByte(0xff); //讀響應(yīng)位
          if(retry++ >10) return 1; //超時退出
          while(!((r10x0f)==5)); //等待數(shù)據(jù)成功接受返回信息
          while(!(SPI_WriteByte(0xff))); //等待SD卡內(nèi)部編程完成

          return 0;
          }

          //******************讀SD卡一個扇區(qū)************************
          unsigned char SD_ReadSingleBlock(unsigned long sector)
          {
          unsigned char r1,temp;
          unsigned int i,j;
          unsigned char retry=0;

          do
          {
          r1 = SD_SendCommand(17, sector9);//讀命令
          retry++;
          if(retry>10) return 1; //超時退出
          } while(r1 != 0x00);
          cs=0;
          while(SPI_WriteByte(0xff)!= 0xfe) //等待接收到開始字節(jié)
          {
          if(retry++ >100) return 1; //超時退出
          }
          for(i=0; i512; i++) //讀512個數(shù)據(jù)
          {
          temp = SPI_WriteByte(0xff); //讀取接收到的數(shù)據(jù)
          lcd[0]=(temp/100)+48;
          lcd[1]=((temp%100)/10)+48;
          lcd[2]=((temp%100)%10)+48;
          lcd_display(); //讀取數(shù)據(jù)送顯示
          for(j=0;j500;j++) {delay();}
          }

          SPI_WriteByte(0xff); //偽16-bits crc
          SPI_WriteByte(0xff);

          cs=1;

          return 0;
          }

          //***********************延時*************************
          void delay() //延時
          {
          int i; //定義整形變量
          for(i=0x100;i--;); //延時
          }

          //************************主函數(shù)**************************
          int main(void)
          {
          unsigned char loop,res;
          delay();
          delay();
          delay();
          loop=1;

          cs=1;
          while(loop)
          {
          spi_init(); //調(diào)用系統(tǒng)初始化函數(shù)
          res= sd_reset(); //調(diào)用SD卡初始化函數(shù)
          if(res) break; //SD卡初始化是否正常,不正常,退出循環(huán),不執(zhí)行下面的操作
          SD_WriteSingleBlock(1); //調(diào)用寫SD卡單BLOCK函數(shù),其中扇區(qū)號為1
          if(res) break;
          SD_ReadSingleBlock(1); //調(diào)用讀SD卡單BLOCK函數(shù),其中扇區(qū)號為1
          if(res) break;
          loop=0;
          while(1);
          }
          while(1);
          }


          上一頁 1 2 下一頁

          關(guān)鍵詞: 程序 實驗 讀寫 SD

          評論


          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉