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

📄 hpi.lst

📁 完整的单片机读写SD卡程序
💻 LST
📖 第 1 页 / 共 5 页
字号:
 638   1              unsigned char bSuccess,bStop;
 639   1              
 640   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
 641   1                      return FALSE;           
 642   1              if(!ThisFile.bFileOpen)
 643   1                      return FALSE;
 644   1              ThisFile.bFileOpen=0;
 645   1              bSuccess=1;
 646   1              bStop=0;
 647   1              tlen=0;
 648   1      
 649   1              while(writeLength>0)
 650   1              {
 651   2                      if(writeLength+ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec)
 652   2                              len=DeviceInfo.BPB_BytesPerSec;
 653   2                      else
 654   2                              len=writeLength+ThisFile.OffsetofSector;
 655   2                         
 656   2                       //////////////////////////////////////////////////////
 657   2                       if(ThisFile.OffsetofSector>0)
 658   2                              {
 659   3                              if(SdReadSector(ThisFile.SectorPointer,1,DBUF))
 660   3                                      {
 661   4                                      
 662   4                                      len=len-ThisFile.OffsetofSector;
 663   4                                      for(i=0;i<len;i++)
 664   4                                              
 665   4                                              DBUF[ThisFile.OffsetofSector+i]=*(pBuffer+i);
 666   4                                      if(!SdWriteSector(ThisFile.SectorPointer,1,DBUF))
 667   4                                              return FALSE;                                           
 668   4                                      ThisFile.OffsetofSector=ThisFile.OffsetofSector+len;
 669   4                                      }
 670   3                              else
 671   3                                      return FALSE;                           
 672   3                              }
 673   2                       else
 674   2                              {
 675   3                              if(!SdWriteSector(ThisFile.SectorPointer,1,pBuffer+tlen))
C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 12  

 676   3                                      return FALSE;                           
 677   3                              ThisFile.OffsetofSector=len;
 678   3                              }
 679   2                       /////////////////////////////////////////////////////
 680   2                         writeLength-=len;
 681   2                         tlen+=len;
 682   2                       /////////////更新文件指针 //////////////////////////////
 683   2                        if(ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec-1)
 684   2                         {    
 685   3                              ThisFile.OffsetofSector-=DeviceInfo.BPB_BytesPerSec;
 686   3                              ThisFile.SectorofCluster+=1;
 687   3                              if(ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus-1)
 688   3                              {
 689   4                                      ThisFile.SectorofCluster=0;
 690   4                                       ThisFile.ClusterPointer=CreateClusterLink(ThisFile.ClusterPointer);
 691   4                                       if(ThisFile.ClusterPointer==0x00)
 692   4                                              return FALSE;                           
 693   4                                       ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer);  
 694   4                              }
 695   3                              else
 696   3                                      ThisFile.SectorPointer=ThisFile.SectorPointer+1;
 697   3                          }
 698   2                      
 699   2              
 700   2              }//end while
 701   1              ThisFile.pointer+=tlen;
 702   1          DelayMs(200);
 703   1              ///////////更新文件目录信息/////////////////////////////
 704   1              if(bSuccess==1)
 705   1              {
 706   2                      if(DirStartCluster==0)  
 707   2                      {
 708   3                      for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
 709   3                      {   
 710   4                      //////////////////////////////////////////////////
 711   4                      if(!SdReadSector(DeviceInfo.RootStartSector+sector,1,DBUF))
 712   4                              return FALSE;                           
 713   4                      ///////////////////////////////////////////////////
 714   4                      for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
 715   4                              {
 716   5                              cnum=LSwapINT16(DBUF[i+26],DBUF[i+27]);
 717   5                              if((cnum==ThisFile.StartCluster)&&(DBUF[i]!=0xe5))
 718   5                                      {
 719   6                                      if(ThisFile.pointer>ThisFile.LengthInByte)
 720   6                                              ThisFile.LengthInByte=ThisFile.pointer;                         
 721   6                                      DBUF[i+28]=(unsigned char)(ThisFile.LengthInByte&0xff);
 722   6                                      DBUF[i+29]=(unsigned char)((ThisFile.LengthInByte>>8)&0xff);
 723   6                                      DBUF[i+30]=(unsigned char)((ThisFile.LengthInByte>>16)&0xff);
 724   6                                      DBUF[i+31]=(unsigned char)((ThisFile.LengthInByte>>24)&0xff);
 725   6                                      if(!SdWriteSector(DeviceInfo.RootStartSector+sector,1,DBUF))
 726   6                                              return FALSE;                   
 727   6                                       bStop=1;
 728   6                                       break;
 729   6                                      }
 730   5                              }
 731   4                      if(bStop==1)
 732   4                              break;          
 733   4                     }
 734   3                      }
 735   2      //////////////////////////////////////////////////////////////////////////////
 736   2                      else
 737   2                      {
C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 13  

 738   3                              NowCluster=DirStartCluster;             
 739   3                              do
 740   3                              {
 741   4                                      NowSector=FirstSectorofCluster(NowCluster);
 742   4                                      for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
 743   4                                      {   
 744   5                                              if(!SdReadSector(NowSector+sector,1,DBUF))
 745   5                                                      return FALSE;                           
 746   5                                              for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
 747   5                                              {
 748   6                                                      cnum=LSwapINT16(DBUF[i+26],DBUF[i+27]);
 749   6                                                      if((cnum==ThisFile.StartCluster)&&(DBUF[i]!=0xe5))
 750   6                                                              {
 751   7                                                              if(ThisFile.pointer>ThisFile.LengthInByte)
 752   7                                                                      ThisFile.LengthInByte=ThisFile.pointer;                         
 753   7                                                              DBUF[i+28]=(unsigned char)(ThisFile.LengthInByte&0xff);
 754   7                                                              DBUF[i+29]=(unsigned char)((ThisFile.LengthInByte>>8)&0xff);
 755   7                                                              DBUF[i+30]=(unsigned char)((ThisFile.LengthInByte>>16)&0xff);
 756   7                                                              DBUF[i+31]=(unsigned char)((ThisFile.LengthInByte>>24)&0xff);
 757   7                                                              if(!SdWriteSector(NowSector+sector,1,DBUF))
 758   7                                                                      return FALSE;                   
 759   7                                                               bStop=1;
 760   7                                                               break;
 761   7                                                              }                                                       
 762   6                                      }
 763   5                                              if(bStop==1)break;
 764   5                                      }
 765   4                                      if(bStop==1)break;
 766   4                                      NowCluster=GetNextClusterNum(NowCluster);                       
 767   4                              }while(NowCluster<=0xffef);
 768   3                      
 769   3                      if(NowCluster>0xffef)
 770   3                      return FALSE;
 771   3                      }
 772   2              }
 773   1              
 774   1              ThisFile.bFileOpen=1;
 775   1              //////////////////////////////////////////////
 776   1              return TRUE;
 777   1      }
 778          
 779          unsigned char RemoveFile(unsigned char *pBuffer)
 780          {
 781   1              unsigned int sector,i;
 782   1              unsigned char bStop,j;
 783   1              int k;
 784   1                      
 785   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
 786   1                      return FALSE;           
 787   1              if(DirStartCluster==0)  
 788   1              {
 789   2              ////////////// 清除目录/////////////////////////////////////
 790   2              for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
 791   2                      {   
 792   3                      //////////////////////////////////////////////////
 793   3                      if(!SdReadSector(DeviceInfo.RootStartSector+sector,1,DBUF))
 794   3                              return FALSE;                   
 795   3                      ///////////////////////////////////////////////////
 796   3                      for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
 797   3                              {
 798   4                              if(DBUF[i]==0x00)
 799   4                                      return FALSE;                   
C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 14  

 800   4                              ///////////////////////////////////////////
 801   4                              j=0;
 802   4                              while(DBUF[i+j]==*(pBuffer+j))
 803   4                                      {
 804   5                                       j=j+1;
 805   5                                       if(j>10) break;
 806   5                                       }
 807   4                              
 808   4                              if(j>10)
 809   4                                      {       
 810   5                                      DBUF[i]=0xE5;
 811   5                                      ThisFile.StartCluster=LSwapINT16(DBUF[i+26],DBUF[i+27]);
 812   5                              
 813   5                                      for(k=(i-32);k>=0;k=k-32)
 814   5                                              {
 815   6                                              if(DBUF[k+11]==0x0F)
 816   6                                                      DBUF[k]=0xE5;
 817   6                                              else
 818   6                                                      break;
 819   6                                              }
 820   5                                      DelayMs(15);
 821   5                                      if(!SdWriteSector(DeviceInfo.RootStartSector+sector,1,DBUF))
 822   5                                              return FALSE;
 823   5      /*
 824   5                                      k=sector-1;
 825   5                                      if(k>=0)
 826   5                                              {
 827   5                                              if(!SdReadSector(DeviceInfo.RootStartSector+k,1,DBUF))
 828   5                                                      return FALSE;   
 829   5                                              for(k=DeviceInfo.BPB_BytesPerSec;k>=0;k=k-32)
 830   5                                                      {
 831   5                                                      if(DBUF[k+11]==0x0F)
 832   5                                                              DBUF[k]=0xE5;
 833   5                                                      else
 834   5                                                              break;
 835   5                                                      }
 836   5                                      //      DelayMs(15);
 837   5                                              if(!SdWriteSector(DeviceInfo.RootStartSector+sector-1,1,DBUF))
 838   5                                                      return FALSE;
 839   5                                              }
 840   5      */                      
 841   5                                      //////////////////// 清除FAT中的纪录////////////////////////
 842   5                                      DelayMs(10);
 843   5                                      if(!DeleteClusterLink(ThisFile.StartCluster))
 844   5                                              return FALSE;                                   
 845   5                                      bStop=1;
 846   5                                      break;
 847   5                                      }
 848   4                              
 849   4                              }//end for
 850   3                      if(bStop==1)
 851   3                              break;
 852   3                      
 853   3                     }//end search

⌨️ 快捷键说明

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