📄 file_sys.c
字号:
#include <string.h>
#include "AT89C51SND1_REG.H"
#include "FILE_SYS.H"
#include "MCU_UART.H"
unsigned char PageBuf[512];
unsigned char FAT_TYPE;
unsigned char SecPerClus;
unsigned int RsdSecCnt,BytesPerSec,RootEntCnt,FatSize16;
unsigned long TotalSector,FatStartSec,RootStartSec,FirstDataSec;
unsigned long CurrentClus,CurrentSec,DataRemain = 0;
#define BootSector 0
void Init_FAT_Info(void)
{
ReadPage(BootSector,PageBuf);
RsdSecCnt = PageBuf[14] + PageBuf[15] * 256;
printuf16x("Reserved Sector Count : 0x%x\n",RsdSecCnt);
SecPerClus = PageBuf[13];
printuf16x("Sector per Clus : 0x%x\n",SecPerClus);
BytesPerSec = PageBuf[12] * 256 + PageBuf[11];
printuf16x("Bytes per Sector: 0x%x\n",BytesPerSec);
RootEntCnt = PageBuf[18] * 256 + PageBuf[17];
printuf16x("Root Entry Count : 0x%x\n",RootEntCnt);
TotalSector = PageBuf[20] * 256 + PageBuf[19];
printuf16x("Total Sector Count : 0x%x\n",TotalSector);
FatSize16 = PageBuf[23] * 256 + PageBuf[22];
if(FatSize16==0)
{ FAT_TYPE = FAT32; return; }
FatStartSec = BootSector + RsdSecCnt;
RootStartSec = FatStartSec + 2 * FatSize16;
FirstDataSec = RootStartSec + (RootEntCnt * 32 + (BytesPerSec - 1)) / BytesPerSec;
printuf16x("Fat Start Sector : 0x%x\n",FatStartSec);
printuf16x("Root Start Sector : 0x%x\n",RootStartSec);
printuf16x("First Data Sector : 0x%x\n",FirstDataSec);
FAT_TYPE = FAT16;
}
unsigned char OpenFile(unsigned char *FileName)
{
unsigned char data i, k;
unsigned long data page;
page = RootStartSec;
while (1)
{
ReadPage(page, PageBuf);
for (k=0; k<16; k++)
{
if (PageBuf[k * 32] == 0x00) return 0;
if (PageBuf[k * 32] == 0xE5) continue;
if (!memcmp(FileName, &PageBuf[k * 32], 11)) //file find
{
CurrentClus = PageBuf[32 * k + 27] * 256 + PageBuf[32 * k + 26];
CurrentSec = (CurrentClus - 2) * SecPerClus + FirstDataSec;
for (i=31; i>=28; i--)
DataRemain = (DataRemain << 8) | PageBuf[k * 32 + i];
printu("File Name :");
for(i=0;i<11;i++)
printuf("%c",PageBuf[k * 32+i]);
printu("\n");
printuf16x("File Size : 0x%x",DataRemain/0x10000);
printuf16x("%x\n",DataRemain%0x10000);
printuf16x("First Clus : 0x%x\n",CurrentClus);
printuf16x("First Sector : 0x%x\n",CurrentSec);
return 1;
}
}
page++;
}
}
unsigned int ReadFile()
{
ReadPage(CurrentSec++, PageBuf);
if (DataRemain < 512)
return (DataRemain);
else
{ DataRemain -= 512;
return (512);
}
}
void Flash_Reset()
{
K9F_FUN = COMMAND;
K9F5608 = 0xff;
K9F_FUN = INACTIVE;
}
void ReadPage(unsigned int BlockPage,unsigned char *PageBuf)
{
unsigned int data i;
K9F_FUN = COMMAND;
K9F5608 = 0x00;
K9F_FUN = ADDRESS;
K9F5608 = 0; //A0-A7
K9F5608 = BlockPage; //A9-A16
K9F5608 = BlockPage >> 8; //A17-A24
K9F_FUN = D_DATA;
i = 512;
while(!(K9F_FUN & RB));
while(i--)
*PageBuf++ = K9F5608;
K9F_FUN = INACTIVE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -