FAT32文件系统实现单片机MP3播放器源代码_第1页
FAT32文件系统实现单片机MP3播放器源代码_第2页
FAT32文件系统实现单片机MP3播放器源代码_第3页
FAT32文件系统实现单片机MP3播放器源代码_第4页
FAT32文件系统实现单片机MP3播放器源代码_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、#include #include /*SD 卡 MP3 播放器 源代码*/sbit XDCS =P2A2;sbit DREQ =卩3人5;sbit XRESET=P3A4;sbit XCS =P2A5;sbit CLK =P3A3;sbit DATA =P2A3;#define VOL_V ALUE 0x0000/*分区记录结构*/struct PartRecordunsigned char Active;/0x80 表示此分区有效unsigned char StartHead;/分区的开始头unsigned char StartCylSect2;/ 开始柱面与扇区 unsigned cha

2、r PartType;/分区类型unsigned char EndHead;/分区的结束头unsigned char EndCylSect2; /结束柱面与扇区 unsigned char StartLBA4; /分区的第一个扇区 unsigned char Size4;/分区的大小;/*分区扇区(绝对 0 扇区)定义如下*/struct PartSectorunsigned char PartCode446; /MBR 的引导程序 struct PartRecord Part4; /4 个分区记录 unsigned char BootSectSig0;unsigned char BootSe

3、ctSig1;struct FAT32_FAT_Itemunsigned char Item4; ;struct FAT32_FATstruct FAT32_FAT_Item Items128;/*FAT32 中对 BPB 的定义如下 一共占用 90 个字节 */struct FAT32_BPBunsigned char BS_jmpBoot3; unsigned char BS_OEMName8;unsigned char BPB_BytesPerSec2;/ 每扇区字节数/跳转指令/offset: 0 offset: 3 offset:11unsigned char BPB_SecPerC

4、lus1; / 每簇扇区数 unsigned char BPB_RsvdSecCnt2; / 保留扇区数目 unsigned char BPB_NumFATs1;/此卷中 FAT 表数unsigned char BPB_RootEntCnt2; /FAT32 为 0 unsigned char BPB_TotSec162;unsigned char BPB_Media1; unsigned char BPB_FATSz162;unsigned char BPB_SecPerTrk2; unsigned char BPB_NumHeads2;unsigned char BPB_HiddSec4;

5、 unsigned char BPB_TotSec324;/FAT32 为 0/存储介质/FAT32 为 0/磁道扇区数/磁头数/FAT 区前隐扇区数/该卷总扇区数offset:13offset:14offset:16offset:17offset:19offset:21offset:22offset:24offset:26offset:28offset:32unsigned char BPB_FATSz324; unsigned char BPB_ExtFlags2;unsigned char BPB_FSVer2;unsigned char BPB_RootClus4;unsigned c

6、har FSInfo2;unsigned char BPB_BkBootSec2; unsigned char BPB_Reserved12;unsigned char BS_DrvNum1;unsigned char BS_Reserved11;unsigned char BS_BootSig1;unsigned char BS_VolID4;unsigned char BS_FilSysType11; /一个 FAT 表扇区数/FAT32 特有/FAT32 特有/根目录簇号offset:36 offset:40 offset:42 offset:44/保留扇区 FSINFO 扇区数 off

7、set:48/ 通常为 6/扩展用/offset:50offset:52offset:64offset:65offset:66offset:67 offset:71unsigned char BS_FilSysType18; /FAT32 ;IIoffset:82/ Structure of a dos directory entry. 一个 dos 目录结构的入口struct direntryunsigned char deName8; unsigned char deExtension3; unsigned char deAttributes;/ filename, blank fille

8、d (文件名)/ extension, blank filled (扩展)/ file attributes(文件属性)unsigned char deLowerCase;/ NT VFAT lower case flags (set to zero)(系统保留)unsigned char deCHundredth; unsigned char deCTime2; unsigned char deCDate2; unsigned char deADate2;unsigned char deHighClust2; 高 16 位)unsigned char deMTime2; unsigned c

9、har deMDate2;/ hundredth of seconds in CTime (创建时间的 10 毫秒位)/ create time (文件创建时间)/ create date (文件创建日期)/ access date (文件最后访问日期)/ high unsigned chars of cluster number( 文件起始簇号的/ last update time (文件的最近修改的时间)/ last update date (文件的最近修改日期)unsigned char deLowCluster2; unsigned char deFileSize4;/ startin

10、g cluster of file (文件起始簇号的低 16 位) / size of file in unsigned chars(表示文件的长度)/ Stuctures 结构 struct FileInfoStructunsigned char unsigned long 簇号)unsigned long unsigned long unsigned long unsigned char属性)FileName12;FileStartCluster;FileCurCluster; FileNextCluster; FileSize; FileAttr;unsigned short FileC

11、reateTime; 立时间)unsigned short FileCreateDate;立日期)unsigned short FileMTime; unsigned short FileMDate; unsigned long FileSector; unsigned long FileOffset;/文件名/ file starting cluster for last file accessed (文件首/文件当前簇号/下一簇号/ file size for last file accessed (文件大小)/ file attr (属性)for last file accessed (

12、文件 / file creation time for last file accessed(文件建/ file creation date for last file accessed(文件建/文件修改时间/文件修改日期/file record place (文件当前扇区)/file record offset (文件偏移量);/*FAT32 初始化时初始参数装入如下结构体中*/struct FAT32_Init_Arg/BPB 所在扇区号 /磁盘的总容量 /根目录的开始簇 /文件数据开始扇区号 /每个扇区的字节数/FAT 表所占扇区数 /每簇的扇区数 / 第一个 FAT 表所在扇区 /第一

13、个目录所在扇区 /根目录所占扇区数 /根目录下的目录与文件数unsigned char BPB_Sector_No; unsigned long Total_Size; unsigned long FirstDirClust; unsigned long FirstDataSector; unsigned int BytesPerSector; unsigned int FATsectors; unsigned int SectorsPerClust; unsigned long FirstFATSector; unsigned long FirstDirSector; unsigned lo

14、ng RootDirSectors; unsigned long RootDirCount;/#define FIND_BPB_UP_RANGE 2000 /BPB 不 一 定 在 0 扇 区 , 对 0 FINE_BPB_UP_RANGE 扇区进行扫描unsigned char xdata FAT32_Buffer512; / 扇区数据读写缓冲区struct FAT32_Init_Arg Init_Arg;/ 初始化参数结构体实体struct FileInfoStruct FileInfo;/ 文件信息结构体实体unsigned char * FAT32_ReadSector(unsigne

15、d long LBA,unsigned char *buf) /FAT32 中读取扇区 的函数MMC_get_data_LBA(LBA,512,buf);return buf;unsigned char FAT32_WriteSector(unsigned long LBA,unsigned char *buf)/FAT32 中写扇区的 函数return MMC_write_sector(LBA,buf);unsigned long lb2bb(unsigned char *dat,unsigned char len) / 小端转为大端unsigned long temp=0;unsigned

16、 long fact=1;unsigned char i=0;for(i=0;iPart0).StartLBA),4); else return 0;unsigned long FAT32_Get_Total_Size() / 存储器的总容量,单位为 MMMC_Init(); /SD 卡初始化FAT32_ReadSector(Init_Arg.BPB_Sector_No,FAT32_Buffer);return (float)(lb2bb(struct FAT32_BPB *)(FAT32_Buffer)-BPB_TotSec32),4)*0.0004883; void FAT32_Init(

17、struct FAT32_Init_Arg *arg) struct FAT32_BPB *bpb;/ 将数据缓冲区指针转为 struct/FAT32_FindBPB()bpb=(struct FAT32_BPB *)(FAT32_Buffer);FAT32_BPB 型指针arg-BPB_Sector_No =FAT32_FindBPB(); 可以返回 BPB 所在的扇区号arg-Total_Size/FAT32_Get_Total_Size() 可以返回磁盘的总容量,单位是兆arg-FATsectors 区数到 FATsectors 中=lb2bb(bpb-BPB_FATSz32),4);a

18、rg-FirstDirClustFirstDirClust 中=lb2bb(bpb-BPB_RootClus) ,4);arg-BytesPerSectorBytesPerSector 中arg-SectorsPerClustSectorsPerClust 中=lb2bb(bpb-BPB_BytesPerSec),2);=lb2bb(bpb-BPB_SecPerClus) ,1);=FAT32_Get_Total_Size();/ 装入 FAT 表占用的扇/装入根目录簇号到/装 入每 扇区 字节 数到/ 装 入 每 簇 扇 区 数 到arg-FirstFATSector=lb2bb(bpb-B

19、PB_RsvdSecCnt),2)+arg-BPB_Sector_No;/ 装入第个 FAT 表扇区号到 FirstFATSector 中/装入根目录项数arg-RootDirCount =lb2bb(bpb-BPB_RootEntCnt) ,2); 到 RootDirCount 中arg-RootDirSectors =(arg-RootDirCount)*329;的扇区数到 RootDirSectors 中/装入根目录占用arg-FirstDirSector =(arg-FirstFATSector)+(bpb-BPB_NumFATs0)*(arg-FATsectors); / 装入第一个

20、目录扇区到 FirstDirSector 中arg-FirstDataSector =(arg-FirstDirSector)+(arg-RootDirSectors); / 装入第一个数据扇区到 FirstDataSector 中send_s(FATchushihuachengong); /FAT 初始化成 功void FAT32_EnterRootDir()unsigned long iRootDirSector;unsigned long iDir;struct direntry *pDir;for(iRootDirSector=(Init_Arg.FirstDirSector);iRo

21、otDirSector(Init_Arg.FirstDirSector)+(Init_Ar g.SectorsPerClust);iRootDirSector+)FAT32_ReadSector(iRootDirSector,FAT32_Buffer);for(iDir=0;iDirdeName)0!=0x00 /* 无效目录项 */ & (pDir-deName)0!=0xe5 /*无效目录项 */ & (pDir-deName)0!=0x0f /* 无效属性 */)Printf_File_Name(pDir-deName);void FAT32_CopyName(unsigned char

22、 *Dname,unsigned char *filename)unsigned char i=0;for(;i11;i+)Dnamei=filenamei;Dnamei=0;unsigned long FAT32_EnterDir(char *path)unsigned long iDirSector;unsigned long iCurSector=Init_Arg.FirstDirSector;unsigned long iDir;struct direntry *pDir;unsigned char DirName12;unsigned char depth=0,i=0;while(p

23、athi!=0)if(pathi=)depth+;i+;if(depth=1)return iCurSector; / 如果是根目录,直接返回当前扇区号for(iDirSector=iCurSector;iDirSector(Init_Arg.FirstDirSector)+(Init_Arg.SectorsPerClust);iDirS ector+)FAT32_ReadSector(iDirSector,FAT32_Buffer);for(iDir=0;iDirdeName)0!=0x00 /* 无效目录项 */ & (pDir-deName)0!=0xe5 /*无效目录项 */ & (p

24、Dir-deName)0!=0x0f /* 无效属性 */) Printf_File_Name(pDir-deName);unsigned char FAT32_CompareName(unsigned char *sname,unsigned char *dname)unsigned char i,j=8;unsigned char name_temp12; for(i=0;i11;i+) name_tempi=0x20;name_temp11=0;i=0;while(snamei!=.)name_tempi=snamei;i+;i+;while(snamei!=0)name_tempj+=

25、snamei;i+;/Printf(name_temp,0); for(i=0;iItems)LastCluster%128); /在算出的扇区中提取簇项return lb2bb(pFAT_Item,4); /返回下一簇号/* 我们最终要实现的是对文件的读取,须要作到给定文件名后,可以得到相应文件的首簇。 主要是思想就是对根目录区中的记录进行扫描, 对记录中的文件名进行匹配。 具体的实现如 下函数*/struct FileInfoStruct * FAT32_OpenFile(char *filepath)unsigned char depth=0;unsigned char i,index;

26、unsigned long iFileSec,iCurFileSec,iFile;struct direntry *pFile;unsigned char len=strlen(filepath);for(i=0;ilen;i+)if(filepathi=)depth+;index=i+1; iCurFileSec=FAT32_EnterDir(filepath);/Printf(iCurFileSec,iCurFileSec); for(iFileSec=iCurFileSec;iFileSeciCurFileSec+(Init_Arg.SectorsPerClust);iFileSec+)

27、 FAT32_ReadSector(iFileSec,FAT32_Buffer);for(iFile=0;iFiledeName) / 对文件名进行匹配 /PutHex(pFile-deFileSize0);/PutHex(pFile-deFileSize1);/PutHex(pFile-deFileSize2);/PutHex(pFile-deFileSize3);FileInfo.FileSize=lb2bb(pFile-deFileSize,4); strcpy(FileInfo.FileName,filepath+index); FileInfo.FileStartCluster=lb

28、2bb(pFile-deLowCluster,2)+lb2bb(pFile-deHighClust,2)*65536;FileInfo.FileCurCluster=FileInfo.FileStartCluster; FileInfo.FileNextCluster=FAT32_GetNextCluster(FileInfo.FileCurCluster); FileInfo.FileOffset=0;Printf(FileStartCluster,FileInfo.FileStartCluster);return &FileInfo; / 这个函数在找到目标文件后。会将此文件的一些参数信息

29、装入到文件结构中, 为以后的文件读取作好准备 ./ 该文件结构为 struct FileInfoStructvoid spi_write(unsigned char x) unsigned char i=0;for(i=0;i8;i+)CLK=0;DATA=(xFileSize-pstru-FileOffset;unsigned long iSectorInCluster=0;unsigned long i=0;unsigned long temp=0;Mp3Reset();Sintest(200); delay(50000);XDCS=0;Printf( 进入整簇 ,0);while(pstru-FileNextCluster!=0x0fffffff) / 如果 FAT 中的簇项为 0x0fffffff ,说明无后继簇 Printf( 进入整簇 1 ,0);for(iSectorInCluster=0;iSectorInClusterFileCurCluster)-2)*(Ini

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论