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

📄 hpi.lst

📁 c51读写SD卡原代码,c51编写,串口接收
💻 LST
📖 第 1 页 / 共 5 页
字号:
 638   1              
 639   1              return TRUE;
 640   1      }
 641          
 642          unsigned char WriteFile(unsigned long writeLength,unsigned char *pBuffer)
 643          {
 644   1              unsigned int  len,sector,i,cnum,tlen;
 645   1              unsigned char bSuccess,bStop;
 646   1              
 647   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
 648   1                      return FALSE;           
 649   1              if(!ThisFile.bFileOpen)
 650   1                      return FALSE;
 651   1              ThisFile.bFileOpen=0;
 652   1              bSuccess=1;
 653   1              bStop=0;
 654   1              tlen=0;
 655   1      
 656   1              while(writeLength>0)
 657   1              {
 658   2              if(ThisFile.SectorofCluster>=DeviceInfo.BPB_SecPerClus)
 659   2                              {
 660   3                                      ThisFile.SectorofCluster=0;
 661   3                                       ThisFile.ClusterPointer=CreateClusterLink(ThisFile.ClusterPointer);
 662   3                                       if(ThisFile.ClusterPointer==0x00)
 663   3                                              return FALSE;                           
 664   3                                       ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer);  
 665   3                              }
 666   2      
 667   2                      if(writeLength+ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec)
 668   2                              len=DeviceInfo.BPB_BytesPerSec;
 669   2                      else
 670   2                              len=writeLength+ThisFile.OffsetofSector;
 671   2                         
 672   2                       //////////////////////////////////////////////////////
 673   2                       if(ThisFile.OffsetofSector>0)
 674   2                              {
 675   3                              if(SdReadSector(ThisFile.SectorPointer,1,DBUF))
C51 COMPILER V7.00  HPI                                                                    02/15/2008 12:14:58 PAGE 12  

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

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

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

⌨️ 快捷键说明

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