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

📄 hpi32.lst

📁 c51读写SD卡原代码,c51编写,串口接收
💻 LST
📖 第 1 页 / 共 4 页
字号:
 452          
 453          
 454          unsigned char WriteFile32(unsigned long writeLength,unsigned char *pBuffer)
 455          {
 456   1          unsigned long cnum;
 457   1              unsigned int  len,sector,i,tlen;
 458   1              unsigned char bSuccess,bStop;
 459   1              
 460   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
 461   1                      return FALSE;           
 462   1              if(!ThisFile.bFileOpen)
 463   1                      return FALSE;
 464   1              ThisFile.bFileOpen=0;
 465   1              bSuccess=1;
 466   1              bStop=0;
 467   1              tlen=0;
 468   1      
 469   1              while(writeLength>0)
 470   1              {
 471   2                      if(ThisFile.SectorofCluster>=DeviceInfo.BPB_SecPerClus)
 472   2                              {
 473   3                                      ThisFile.SectorofCluster=0;
 474   3                                       ThisFile.ClusterPointer=CreateClusterLink32(ThisFile.ClusterPointer);
 475   3                                       if(ThisFile.ClusterPointer==0x00)
 476   3                                              return FALSE;                           
 477   3                                       ThisFile.SectorPointer=FirstSectorofCluster32(ThisFile.ClusterPointer);        
 478   3                              }
 479   2      
 480   2                      if(writeLength+ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec)
 481   2                              len=DeviceInfo.BPB_BytesPerSec;
 482   2                      else
 483   2                              len=writeLength+ThisFile.OffsetofSector;
 484   2                         
 485   2                       //////////////////////////////////////////////////////
 486   2                       if(ThisFile.OffsetofSector>0)
 487   2                              {
 488   3                              if(SdReadSector(ThisFile.SectorPointer,1,DBUF))
 489   3                                      {
C51 COMPILER V7.00  HPI32                                                                  02/15/2008 12:14:59 PAGE 9   

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

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

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

 676   1              unsigned char xdata bstop,bwrite;
 677   1              unsigned long ClusterPointer;
 678   1              
 679   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
 680   1                      return FALSE;

⌨️ 快捷键说明

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