⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sd.lst

📁 51单片机读写SD列子程序 可以学习51单片机如何读写SD卡
💻 LST
📖 第 1 页 / 共 2 页
字号:
 208   2                      Delay5us();
 209   2              }
 210   1              
 211   1              for (temp=0;temp<8;temp++) ReadBuffer[temp]=SD_2Byte_Read();//Get the CSD data
 212   1              
 213   1              for (temp=0;temp<16;temp++)//Provide 16 clock to remove the last 2 bytes of data response (CRC)
 214   1              {
 215   2                      sd_clk=0;//CLK Low
 216   2                      Delay5us();
 217   2                      sd_clk=1;//CLK High
 218   2                      Delay5us();
 219   2              }
 220   1              
 221   1              sd_cse=1;//CS_High()
 222   1              
 223   1              for (temp=0;temp<8;temp++)//Provide 8 extra clock after data response
 224   1              {
 225   2                      sd_clk=0;//CLK Low
 226   2                      Delay5us();
 227   2                      sd_clk=1;//CLK High
 228   2                      Delay5us();
 229   2              }
 230   1              
 231   1              BlockNR=((ReadBuffer[3]<<2)&0x0fff)+((ReadBuffer[4]>>14)&0x0003)+1;//Calcuate MULT
 232   1              BlockNR=BlockNR*(0x0002<<(((ReadBuffer[4]<<1)&0x0007)+((ReadBuffer[5]>>15)&0x0001)+1));//Calcuate Block_N
             -umber
 233   1              return Response;
 234   1      }
 235          //********************************************
 236          unsigned int SD_Overall_Initiation()
 237          {
 238   1              unsigned int Response,Response_2;
C51 COMPILER V7.50   SD                                                                    07/20/2008 10:05:07 PAGE 5   

 239   1              Response=0x0000;
 240   1              Response_2=0xff00;
 241   1              
 242   1              sd_dao=1;//[1] Do High
 243   1                                                      //[1] Do must be High when there is no transmition
 244   1              do
 245   1                      Response=SD_Reset_Card();//[2] Send CMD0
 246   1              while (Response!=0xff01);
 247   1              
 248   1              if (Response!=0xff01) Response_2+=8;
 249   1              
 250   1              //Response=SD_CMD_Write(8,0x00000000,1,0);//Send CMD8
 251   1              
 252   1              Response=SD_Initiate_Card();//[3] Send CMD55+ACMD41 
 253   1              if (Response==0xff00)
 254   1                      ;
 255   1              else
 256   1                      {
 257   2                      Response_2+=4;
 258   2                      ;
 259   2                      }
 260   1              
 261   1              do
 262   1                      Response=SD_Get_CardInfo();//[4] Read CSD
 263   1              while (Response!=0xff00);
 264   1              if (Response==0xff01) Response_2+=2;
 265   1              
 266   1              return Response_2;
 267   1      //      0000|0000||0000|0000 Response_2
 268   1      //                  |||_CSD Fail
 269   1      //                  ||__CMD55+ACMD41 Fail
 270   1      //                  |___CMD0 Fail
 271   1      }
 272          //********************************************
 273          unsigned int SD_Get_CardID()//Read CID register
 274          {
 275   1              unsigned int temp,Response,MaximumTimes;
 276   1              MaximumTimes=10;
 277   1              
 278   1              for(temp=0;temp<MaximumTimes;temp++)
 279   1              {
 280   2                      Response=SD_CMD_Write(10,0x00000000,1,1);//Send CMD9
 281   2                      if (Response==0xff00)
 282   2                              temp=MaximumTimes;
 283   2              }
 284   1              
 285   1               for (temp=0;temp<8;temp++)//Provide 8 clock to romove the first byte of data response (0x00fe)
 286   1              {
 287   2                      sd_clk=0;//CLK Low
 288   2                      Delay5us();
 289   2                      sd_clk=1;//CLK High
 290   2                      Delay5us();
 291   2              }
 292   1              
 293   1              for (temp=0;temp<8;temp++) ReadBuffer[temp]=SD_2Byte_Read();//Get the CID data
 294   1              
 295   1              for (temp=0;temp<16;temp++)//Provide 16 clock to remove the last 2 bytes of data response (CRC)
 296   1              {
 297   2                      sd_clk=0;//CLK Low
 298   2                      Delay5us();
 299   2                      sd_clk=1;//CLK High
 300   2                      Delay5us();
C51 COMPILER V7.50   SD                                                                    07/20/2008 10:05:07 PAGE 6   

 301   2              }
 302   1              
 303   1              sd_cse=1;//CS_High()
 304   1              
 305   1              for (temp=0;temp<8;temp++)//Provide 8 extra clock after data response
 306   1              {
 307   2                      sd_clk=0;//CLK Low
 308   2                      Delay5us();
 309   2                      sd_clk=1;//CLK High
 310   2                      Delay5us();
 311   2              }
 312   1              
 313   1              return Response;
 314   1      }
 315          //********************************************
 316          unsigned int Read_Single_Block(unsigned long int BlockAddress)
 317          {
 318   1              unsigned int temp,Response,MaximumTimes;
 319   1              MaximumTimes=10;
 320   1              
 321   1              if (BlockAddress>BlockNR) return 0xff20;//whether BlockAddress out of range?
 322   1              
 323   1              for(temp=0;temp<MaximumTimes;temp++)
 324   1              {
 325   2                      Response=SD_CMD_Write(17,BlockAddress,1,1);//Send CMD17
 326   2                      if (Response==0xff00)
 327   2                              temp=MaximumTimes;
 328   2              }
 329   1              
 330   1              while (SD_Read()!=0xfffe) {;}
 331   1              
 332   1              for (temp=0;temp<256;temp++) ReadBuffer[temp]=SD_2Byte_Read();//Get the readed data
 333   1              
 334   1              for (temp=0;temp<16;temp++)//Provide 16 clock to remove the last 2 bytes of data response (CRC)
 335   1              {
 336   2                      sd_clk=0;//CLK Low
 337   2                      Delay5us();
 338   2                      sd_clk=1;//CLK High
 339   2                      Delay5us();
 340   2              }
 341   1              
 342   1              sd_cse=1;//CS_High()
 343   1              
 344   1              for (temp=0;temp<8;temp++)//Provide 8 extra clock after data response
 345   1              {
 346   2                      sd_clk=0;//CLK Low
 347   2                      Delay5us();
 348   2                      sd_clk=1;//CLK High
 349   2                      Delay5us();
 350   2              }
 351   1              
 352   1              return Response;
 353   1      }
 354          //********************************************
 355          unsigned int Write_Single_Block(unsigned long int BlockAddress)
 356          {
 357   1              unsigned int temp,Response,MaximumTimes;
 358   1              MaximumTimes=10;
 359   1              
 360   1              if (BlockAddress>BlockNR) return 0xff20;//whether BlockAddress out of range?
 361   1              
 362   1              for(temp=0;temp<MaximumTimes;temp++)
C51 COMPILER V7.50   SD                                                                    07/20/2008 10:05:07 PAGE 7   

 363   1              {
 364   2                      Response=SD_CMD_Write(24,BlockAddress,1,1);//Send CMD24
 365   2                      if (Response==0xff00)
 366   2                              temp=MaximumTimes;
 367   2              }
 368   1              
 369   1              for (temp=0;temp<8;temp++)//Provide 8 extra clock after CMD response
 370   1              {
 371   2                      sd_clk=0;//CLK Low
 372   2                      Delay5us();
 373   2                      sd_clk=1;//CLK High
 374   2                      Delay5us();
 375   2              }
 376   1              
 377   1              SD_Write(0x00fe);//Send Start Block Token
 378   1              for (temp=0;temp<256;temp++) SD_2Byte_Write(WriteBuffer[temp]);//Data Block
 379   1              SD_2Byte_Write(0xffff);//Send 2 Bytes CRC
 380   1              
 381   1              Response=SD_Read();
 382   1              while (SD_Read()!=0xffff) {;}
 383   1              
 384   1              sd_cse=1;//CS_High()
 385   1              
 386   1              for (temp=0;temp<8;temp++)//Provide 8 extra clock after data response
 387   1              {
 388   2                      sd_clk=0;//CLK Low
 389   2                      Delay5us();
 390   2                      sd_clk=1;//CLK High
 391   2                      Delay5us();
 392   2              }
 393   1              
 394   1              return Response;
 395   1      }
 396          main()
 397          {
 398   1              unsigned int Data,M_Response;
 399   1              M_Response=0x0000;
 400   1              M_Response=SD_Overall_Initiation();
 401   1      //      M_Response=SD_CMD_Write(16,512,1,0);
 402   1              Data=SD_Get_CardID();
 403   1              Write_Single_Block(0x0000);
 404   1          Read_Single_Block(0x0000);
 405   1          while(1);
 406   1      }
 407                  
 408                  


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   2458    ----
   CONSTANT SIZE    =     31    ----
   XDATA SIZE       =   1036      68
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----    ----
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -