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

📄 hpi32.lst

📁 完整的单片机读写SD卡程序
💻 LST
📖 第 1 页 / 共 4 页
字号:
 450          
 451          
 452          unsigned char WriteFile32(unsigned long writeLength,unsigned char *pBuffer)
 453          {
 454   1          unsigned long cnum;
 455   1              unsigned int  len,sector,i,tlen;
 456   1              unsigned char bSuccess,bStop;
 457   1              
 458   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
 459   1                      return FALSE;           
 460   1              if(!ThisFile.bFileOpen)
 461   1                      return FALSE;
 462   1              ThisFile.bFileOpen=0;
 463   1              bSuccess=1;
 464   1              bStop=0;
 465   1              tlen=0;
 466   1      
 467   1              while(writeLength>0)
 468   1              {
 469   2                      if(writeLength+ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec)
 470   2                              len=DeviceInfo.BPB_BytesPerSec;
 471   2                      else
 472   2                              len=writeLength+ThisFile.OffsetofSector;
 473   2                         
 474   2                       //////////////////////////////////////////////////////
 475   2                       if(ThisFile.OffsetofSector>0)
 476   2                              {
 477   3                              if(SdReadSector(ThisFile.SectorPointer,1,DBUF))
 478   3                                      {
 479   4                                      
 480   4                                      len=len-ThisFile.OffsetofSector;
 481   4                                      for(i=0;i<len;i++)                                      
 482   4                                              DBUF[ThisFile.OffsetofSector+i]=*(pBuffer+i);
 483   4                                      if(!SdWriteSector(ThisFile.SectorPointer,1,DBUF))
 484   4                                              return FALSE;                                           
 485   4                                      ThisFile.OffsetofSector=ThisFile.OffsetofSector+len;
 486   4                                      }
 487   3                              else
 488   3                                      return FALSE;                           
 489   3                              }
C51 COMPILER V8.02   HPI32                                                                 04/29/2007 12:48:53 PAGE 9   

 490   2                       else
 491   2                              {
 492   3                              if(!SdWriteSector(ThisFile.SectorPointer,1,pBuffer+tlen))
 493   3                                      return FALSE;                           
 494   3                              ThisFile.OffsetofSector=len;
 495   3                              }
 496   2                       /////////////////////////////////////////////////////
 497   2                         writeLength-=len;
 498   2                         tlen+=len;
 499   2                       /////////////更新文件指针 //////////////////////////////
 500   2                        if(ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec-1)
 501   2                         {    
 502   3                              ThisFile.OffsetofSector-=DeviceInfo.BPB_BytesPerSec;
 503   3                              ThisFile.SectorofCluster+=1;
 504   3                              if(ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus-1)
 505   3                              {
 506   4                                      ThisFile.SectorofCluster=0;
 507   4                                       ThisFile.ClusterPointer=CreateClusterLink32(ThisFile.ClusterPointer);
 508   4                                       if(ThisFile.ClusterPointer==0x00)
 509   4                                              return FALSE;                           
 510   4                                       ThisFile.SectorPointer=FirstSectorofCluster32(ThisFile.ClusterPointer);        
 511   4                              }
 512   3                              else
 513   3                                      ThisFile.SectorPointer=ThisFile.SectorPointer+1;
 514   3                          }
 515   2                      
 516   2              
 517   2              }//end while
 518   1              ThisFile.pointer+=tlen;
 519   1              ///////////更新文件目录信息/////////////////////////////
 520   1              if(bSuccess==1)
 521   1              {
 522   2                      NowCluster32=DirStartCluster32;         
 523   2                              do
 524   2                              {
 525   3                                      NowSector=FirstSectorofCluster32(NowCluster32);
 526   3                                      for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
 527   3                                      {   
 528   4                                              if(!SdReadSector(NowSector+sector,1,DBUF))
 529   4                                                      return FALSE;                           
 530   4                                              for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
 531   4                                              {
 532   5                                                      cnum=LSwapINT32(DBUF[i+26],DBUF[i+27],DBUF[i+20],DBUF[i+21]);
 533   5                                                      if((cnum==ThisFile.StartCluster)&&(DBUF[i]!=0xe5))
 534   5                                                              {
 535   6                                                              if(ThisFile.pointer>ThisFile.LengthInByte)
 536   6                                                                      ThisFile.LengthInByte=ThisFile.pointer;                         
 537   6                                                              DBUF[i+28]=(unsigned char)(ThisFile.LengthInByte&0xff);
 538   6                                                              DBUF[i+29]=(unsigned char)((ThisFile.LengthInByte>>8)&0xff);
 539   6                                                              DBUF[i+30]=(unsigned char)((ThisFile.LengthInByte>>16)&0xff);
 540   6                                                              DBUF[i+31]=(unsigned char)((ThisFile.LengthInByte>>24)&0xff);
 541   6                                                              if(!SdWriteSector(NowSector+sector,1,DBUF))
 542   6                                                                      return FALSE;                   
 543   6                                                               bStop=1;
 544   6                                                               break;
 545   6                                                              }                                                       
 546   5                                      }
 547   4                                              if(bStop==1)break;
 548   4                                      }
 549   3                                      if(bStop==1)break;
 550   3                                      NowCluster32=GetNextClusterNum32(NowCluster32);                 
 551   3                              }while(NowCluster32<=DeviceInfo.TotCluster);
C51 COMPILER V8.02   HPI32                                                                 04/29/2007 12:48:53 PAGE 10  

 552   2                      
 553   2                      if(NowCluster32>DeviceInfo.TotCluster)
 554   2                      return FALSE;           
 555   2              }
 556   1              
 557   1              ThisFile.bFileOpen=1;
 558   1              //////////////////////////////////////////////
 559   1              return TRUE;
 560   1      }
 561          
 562          
 563          unsigned char RemoveFile32(unsigned char *pBuffer)
 564          {
 565   1              unsigned int sector,i;
 566   1              unsigned char bStop,j;
 567   1              int k;
 568   1                      
 569   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
 570   1                      return FALSE;
 571   1      ////////////// 清除目录/////////////////////////////////////            
 572   1              NowCluster32=DirStartCluster32;         
 573   1                      do
 574   1                      {
 575   2                              NowSector=FirstSectorofCluster32(NowCluster32);
 576   2                              for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
 577   2                      {   
 578   3                                      if(!SdReadSector(NowSector+sector,1,DBUF))
 579   3                                              return FALSE;                           
 580   3                                      for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
 581   3                                      {
 582   4                                              if(DBUF[i]==0x00)
 583   4                                                      return FALSE;
 584   4                                              j=0;
 585   4                                              while(DBUF[i+j]==*(pBuffer+j))
 586   4                                              {
 587   5                                                       j=j+1;
 588   5                                                       if(j>10)
 589   5                                                              break;
 590   5                                              }
 591   4                                              if(j>10)
 592   4                                              { 
 593   5                                                      DBUF[i]=0xE5;
 594   5                                                      ThisFile.StartCluster=LSwapINT32(DBUF[i+26],DBUF[i+27],DBUF[i+20],DBUF[i+21]);
 595   5                                                      for(k=(i-32);k>=0;k=k-32)
 596   5                                                      {
 597   6                                                      if(DBUF[k+11]==0x0F)
 598   6                                                              DBUF[k]=0xE5;
 599   6                                                      else
 600   6                                                              break;
 601   6                                                      }
 602   5                                                      DelayMs(15);
 603   5                                                      if(!SdWriteSector(NowSector+sector,1,DBUF))
 604   5                                                              return FALSE;                                   
 605   5                                      //////////////////// 清除FAT中的纪录////////////////////////
 606   5                                                      DelayMs(10);
 607   5                                                      if(!DeleteClusterLink32(ThisFile.StartCluster))
 608   5                                                              return FALSE;                                   
 609   5                                                      bStop=1;
 610   5                                                      break;
 611   5                                                      }
 612   4                                      }
 613   3                                      if(bStop==1)break;              
C51 COMPILER V8.02   HPI32                                                                 04/29/2007 12:48:53 PAGE 11  

 614   3                      }
 615   2                              if(bStop==1)break;      
 616   2                              NowCluster32=GetNextClusterNum32(NowCluster32);                 
 617   2                      }while(NowCluster32<=DeviceInfo.TotCluster);    
 618   1              
 619   1              if(NowCluster32>DeviceInfo.TotCluster)
 620   1                  return FALSE;
 621   1              
 622   1              return TRUE;
 623   1      }
 624          
 625          
 626          unsigned char GetCapacity32(void)
 627          {
 628   1              unsigned int sectorNum,i;       
 629   1              unsigned long FreeSize,Freesectorcnt;
 630   1      
 631   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
 632   1                      return FALSE;
 633   1                      
 634   1              ////////////////////////////////////////////////////////////////////////
 635   1              sectorNum=DeviceInfo.FatStartSector;
 636   1              Freesectorcnt=0;
 637   1              while(sectorNum<DeviceInfo.BPB_FATSz32+DeviceInfo.FatStartSector)
 638   1              {
 639   2                      
 640   2                      if(SdReadSector(sectorNum,1,DBUF))
 641   2                      {
 642   3                        for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+4)
 643   3                              {                        
 644   4                               if((DBUF[i]||DBUF[i+1]||DBUF[i+2]||DBUF[i+3])==0x00)
 645   4                                      {       
 646   5                                      Freesectorcnt++;
 647   5                                      }         
 648   4                              }       
 649   3                      }
 650   2                      else                    
 651   2                              return FALSE;                   
 652   2                      sectorNum++;
 653   2              }
 654   1              
 655   1              ////////////////////////////////////////////////////////////////////////
 656   1              FreeSize=DeviceInfo.BPB_BytesPerSec*DeviceInfo.BPB_SecPerClus;
 657   1              FreeSize=Freesectorcnt*FreeSize;
 658   1              
 659   1              UARTBUF[0]=(unsigned char)((FreeSize>>24)&0xff);
 660   1              UARTBUF[1]=(unsigned char)((FreeSize>>16)&0xff);
 661   1              UARTBUF[2]=(unsigned char)((FreeSize>>8)&0xff);
 662   1              UARTBUF[3]=(unsigned char)(FreeSize&0xff);
 663   1              Response.len=4;
 664   1              return TRUE;
 665   1      }
 666          
 667          
 668          unsigned char CreateDir32(unsigned long len,unsigned char *pBuffer,unsigned char *pName)
 669          {
 670   1              unsigned int sector,i,j,DirCount;
 671   1              unsigned long cnum;
 672   1              unsigned char xdata bstop,InByte,bwrite;
 673   1              unsigned long ClusterPointer;
 674   1              
 675   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
C51 COMPILER V8.02   HPI32                                                                 04/29/2007 12:48:53 PAGE 12  

 676   1                      return FALSE;
 677   1              if((len%32)!=0)

⌨️ 快捷键说明

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