• 大小: 201.31 KB
    文件类型: .RAR
    金币: 1
    下载: 0 次
    发布日期: 2024-11-28
  • 语言: 其他
  • 标签: SPI读写  SD卡  

资源简介

51模拟SPI读写SD卡(包括Fat和Fat32文件系统),RAR中包含整个工程,可以用Keil直接打开。采用软件模拟SPI读写SD卡,对于想用软件模拟SPI有一定的借鉴性。

资源截图

代码片段和文件信息

#include “common.h“
#include “Fat.h“
#include “Fat32.h“
#include “DEVICE.H“
#include “HAL.H“
////////////////////////////////////////
extern SYS_INFO_BLOCK xdata DeviceInfo;
extern FILE_INFO xdata ThisFile;
extern unsigned char xdata DBUF[BUFFER_LENGTH];
unsigned char xdata FATBUF[512];
////////////////////////////////////////

unsigned long FirstSectorofCluster(unsigned int clusterNum)
{
unsigned long temp;
temp=clusterNum-2;
temp=temp*DeviceInfo.BPB_SecPerClus;
temp=temp+DeviceInfo.FirstDataSector;
return temp;
}

unsigned int ThisFatSecNum(unsigned int clusterNum)
{

   unsigned int temp;
   temp=clusterNum/(DeviceInfo.BPB_BytesPerSec/2);
   temp=temp+DeviceInfo.FatStartSector;
   return temp;

}

unsigned int ThisFatEntOffset(unsigned int clusterNum)
{    
return (clusterNum%(DeviceInfo.BPB_BytesPerSec/2))*2;
}

unsigned int GetNextClusterNum(unsigned int clusterNum)
{
unsigned int FatSecNumFatEntOffset;

FatSecNum=ThisFatSecNum(clusterNum);
FatEntOffset=ThisFatEntOffset(clusterNum);
if(ThisFile.FatSectorPointer!=FatSecNum)
{

if(!SdReadSector(FatSecNum1FATBUF))
return 0xFFFF;
ThisFile.FatSectorPointer=FatSecNum;
}

///////////////////////////////////////////////////
clusterNum=FATBUF[FatEntOffset+1];
clusterNum=clusterNum<<8;
clusterNum+=FATBUF[FatEntOffset];
return clusterNum;
}

unsigned char GoToPointer(unsigned long pointer)
{

unsigned int clusterSize;

clusterSize=DeviceInfo.BPB_SecPerClus*DeviceInfo.BPB_BytesPerSec;
ThisFile.ClusterPointer=ThisFile.StartCluster;
while(pointer>clusterSize)
{
pointer-=clusterSize;
ThisFile.ClusterPointer=GetNextClusterNum(ThisFile.ClusterPointer);
if(ThisFile.ClusterPointer==0xffff)
{
return FALSE;
}
}
ThisFile.SectorofCluster=pointer/DeviceInfo.BPB_BytesPerSec;
ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer)+ThisFile.SectorofCluster;
ThisFile.OffsetofSector=pointer%DeviceInfo.BPB_BytesPerSec;
ThisFile.FatSectorPointer=0;
return TRUE;

}

unsigned char DeleteClusterlink(unsigned int clusterNum)
{
unsigned int FatSecNumFatEntOffset;
unsigned char i;
while((clusterNum>1)&&(clusterNum<0xfff0))
{
FatSecNum=ThisFatSecNum(clusterNum);
FatEntOffset=ThisFatEntOffset(clusterNum);
if(SdReadSector(FatSecNum1DBUF))
{
 if(clusterNum  clusterNum=DBUF[FatEntOffset+1];
 clusterNum=clusterNum<<8;
 clusterNum+=DBUF[FatEntOffset];  
}
else
return FALSE;
DBUF[FatEntOffset]=0x00;
DBUF[FatEntOffset+1]=0x00;
for(i=0;i {
DelayMs(5);
if(!SdWriteSector(FatSecNum+i*DeviceInfo.BPB_FATSz161DBUF))
return FALSE;
}
}
return TRUE;
}

unsigned int GetFreeCusterNum(void)
{
unsigned int clusterNumi;
unsigned long sectorNum;
unsigned char j;
clusterNum=0;
// sectorNum=DeviceInfo.FatStartSector;

    

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       6523  2003-12-13 06:09  51读写SD卡源码\AT89X52.H

     文件     191538  2007-04-29 12:48  51读写SD卡源码\C51_UsbDisk

     文件      77873  2007-04-29 12:48  51读写SD卡源码\C51_UsbDisk.hex

     文件        162  2007-04-29 12:48  51读写SD卡源码\C51_UsbDisk.lnp

     文件     136944  2007-04-29 12:48  51读写SD卡源码\C51_UsbDisk.M51

     文件       4084  2007-05-04 19:29  51读写SD卡源码\C51_UsbDisk.Opt

     文件        164  2007-05-04 19:29  51读写SD卡源码\C51_UsbDisk.plg

     文件       2336  2007-04-25 16:24  51读写SD卡源码\C51_UsbDisk.Uv2

     文件       4084  2007-04-29 12:49  51读写SD卡源码\C51_UsbDisk_Opt.Bak

     文件       2217  2006-08-19 09:52  51读写SD卡源码\C51_UsbDisk_Uv2.Bak

     文件       1078  2000-01-14 08:46  51读写SD卡源码\common.h

     文件       4046  2007-04-27 15:59  51读写SD卡源码\DEVICE.C

     文件       2055  2006-08-14 10:45  51读写SD卡源码\DEVICE.H

     文件       7343  2007-04-29 12:48  51读写SD卡源码\DEVICE.LST

     文件      17248  2007-04-29 12:48  51读写SD卡源码\DEVICE.OBJ

     文件       4625  2005-02-27 01:37  51读写SD卡源码\Fat.c

     文件        503  2004-05-08 04:04  51读写SD卡源码\Fat.h

     文件      10042  2007-04-29 12:48  51读写SD卡源码\Fat.LST

     文件      21154  2007-04-29 12:48  51读写SD卡源码\Fat.OBJ

     文件       4842  2005-02-27 01:39  51读写SD卡源码\Fat32.c

     文件        540  2003-06-01 07:54  51读写SD卡源码\Fat32.h

     文件       9941  2007-04-29 12:48  51读写SD卡源码\Fat32.LST

     文件      23232  2007-04-29 12:48  51读写SD卡源码\Fat32.OBJ

     文件       5070  2005-02-28 09:03  51读写SD卡源码\HAL.C

     文件        905  2005-02-28 21:28  51读写SD卡源码\HAL.H

     文件       9751  2007-04-29 12:48  51读写SD卡源码\HAL.LST

     文件      14789  2007-04-29 12:48  51读写SD卡源码\HAL.OBJ

     文件      30566  2007-04-26 09:56  51读写SD卡源码\HPI.C

     文件       1229  2003-06-04 09:34  51读写SD卡源码\hpi.H

     文件      77477  2007-04-29 12:48  51读写SD卡源码\HPI.LST

............此处省略16个文件信息

评论

共有 条评论