📄 file.c
字号:
*函数名称:read_BPB()
*功能描述:读取BPB,并将数据从小端模式转换为大端模式
*形式参数:void
*返 回 值:void
*---------------------------------------------------------------*/
void read_BPB()
{
unsigned char i;
unsigned char xdata *p;
mmc_readblock(BootStartSector,buffer);
p=(unsigned char xdata *)BPB.BS_jmpBoot;
for(i=0;i<54;i++)
*(p++)=buffer[i];
//喂狗
WDTCN=0xa5;
//FAT文件系统为little endian,要将数据转换为大端模式
//转换BPB_BytsPerSec,unsigned int
p=(unsigned char xdata *)(&BPB.BPB_BytsPerSec);
{
unsigned char temp;
temp=*p;
*p=*(p+1);
*(p+1)=temp;
}
//转换BPB_RsvdSecCnt,unsigned int
p=(unsigned char xdata *)(&BPB.BPB_RsvdSecCnt);
{
unsigned char temp;
temp=*p;
*p=*(p+1);
*(p+1)=temp;
}
//转换BPB_RootEntCnt,unsigned int
p=(unsigned char xdata *)(&BPB.BPB_RootEntCnt);
{
unsigned char temp;
temp=*p;
*p=*(p+1);
*(p+1)=temp;
}
//转换BPB_TotSec16,unsigned int
//喂狗
WDTCN=0xa5;
p=(unsigned char xdata *)(&BPB.BPB_TotSec16);
{
unsigned char temp;
temp=*p;
*p=*(p+1);
*(p+1)=temp;
}
//转换BPB_FATSz16,unsigned int
p=(unsigned char xdata *)(&BPB.BPB_FATSz16);
{
unsigned char temp;
temp=*p;
*p=*(p+1);
*(p+1)=temp;
}
//转换BPB_FATSz16,unsigned int
p=(unsigned char xdata *)(&BPB.BPB_SecPerTrk);
{
unsigned char temp;
temp=*p;
*p=*(p+1);
*(p+1)=temp;
}
//转换BPB_NumHeads,unsigned int
p=(unsigned char xdata *)(&BPB.BPB_NumHeads);
{
unsigned char temp;
temp=*p;
*p=*(p+1);
*(p+1)=temp;
}
//转换BPB_HiddSec,unsigned long
p=(unsigned char xdata *)(&BPB.BPB_HiddSec);
for(i=0;i<2;i++)
{
unsigned char temp;
temp=*(p+(3-i));
*(p+(3-i))=*(p+i);
*(p+i)=temp;
}
//转换BPB_TotSec32,unsigned long
p=(unsigned char xdata *)(&BPB.BPB_TotSec32);
for(i=0;i<2;i++)
{
unsigned char temp;
temp=*(p+(3-i));
*(p+(3-i))=*(p+i);
*(p+i)=temp;
}
}
/*------------------------------------------------------------
*函数名称:compute_BPB()
*功能描述:
*形式参数:void
*返 回 值:void
*---------------------------------------------------------------*/
void compute_BPB()
{
unsigned long TotSec; //磁盘总的扇区数
//计算2个FAT区的起始地址
FAT1_FirstSector = BootStartSector+BPB.BPB_RsvdSecCnt;
FAT2_FirstSector =FAT1_FirstSector+ BPB.BPB_FATSz16;
//文件根目录开始的扇区号
FDT_FirstSector = FAT2_FirstSector+BPB.BPB_FATSz16;
//文件根目录所占的扇区数
RootDirSectors = ((BPB.BPB_RootEntCnt*32)+(BPB.BPB_BytsPerSec-1))/BPB.BPB_BytsPerSec;
//数据区开始的扇区号
FirstDataSector = FDT_FirstSector+RootDirSectors;
if(BPB.BPB_TotSec16 != 0)
TotSec = BPB.BPB_TotSec16;
else
TotSec = BPB.BPB_TotSec32;
DataSec = TotSec - FirstDataSector;
CountofClusters = DataSec / BPB.BPB_SecPerClus;
}
/*------------------------------------------------------------
*函数名称:read_partition()
*功能描述:读取分区表中的信息和每个分区的入口地址和基本信息
//将信息存放到partitionTable1中
*形式参数:void
*返 回 值:void
*---------------------------------------------------------------*/
void read_partition()
{
unsigned int xdata i;
unsigned char xdata *p;
mmc_readblock(0,buffer); //读取Partition Table 中的数据
p=(unsigned char xdata *)(&partitionTable1.PartionState); //指向分区信息结构体的首地址
//读取第一个分区的信息
for(i=0;i<16;i++)
*(p+i) = buffer[PARTITON1_START_OFFSET+i];
//将短整型和长整型的数据转换为大端模式
//转换StartSector,unsigned int
p=(unsigned char xdata *)(&partitionTable1.StartSector);
{
unsigned char temp;
temp=*p;
*p=*(p+1);
*(p+1)=temp;
}
//EndSector,unsigned int
p=(unsigned char xdata *)(&partitionTable1.EndSector);
{
unsigned char temp;
temp=*p;
*p=*(p+1);
*(p+1)=temp;
}
//SectorsPrecedingPartition,unsigned long
p=(unsigned char xdata *)(&partitionTable1.SectorsPrecedingPartition);
for(i=0;i<2;i++)
{
unsigned char temp;
temp=*(p+(3-i));
*(p+(3-i))=*(p+i);
*(p+i)=temp;
}
//SectorsInPartion,unsigned long
p=(unsigned char xdata *)(&partitionTable1.SectorsInPartion);
for(i=0;i<2;i++)
{
unsigned char temp;
temp=*(p+(3-i));
*(p+(3-i))=*(p+i);
*(p+i)=temp;
}
}
/*------------------------------------------------------------
*函数名称:file_name()
*功能描述:由系统时间,创建文件的文件名和扩展名,扩展名默认为TXT
*形式参数:void
*返 回 值:void
*---------------------------------------------------------------*/
void file_name()
{
unsigned char xdata month;
unsigned char xdata day;
unsigned char xdata hour;
unsigned char xdata minute;
unsigned char xdata second;
unsigned char xdata i;
unsigned char xdata temp;
//先将从DS1302中获得的BCD码格式的数据转化为16进制的数据
read_time(); //获取当前的时间
//将BCD码表示的时间转化为十进制的数
for(i=0;i<7;i++)
{
temp=time[i] &0xf0; //取高半字节
temp>>=4; //放到低位参与运算
time[i] &=0x0f;
time[i] = temp *10 + time[i];
}
month = time[2]; //月
day = time[3]; //日
hour = time[4];
minute =time[5];
second = time[6];
//填充文件名,都为ASCII码值
//填充月
if(month<10)
{
fileName.NAME[0]= 0x30; //ASCII 0
fileName.NAME[1]= 0x30+month;
}
else
{
fileName.NAME[0]= 0x30+month/10;
fileName.NAME[1]= 0x30+month%10;
}
//填充日
if(day<10)
{
fileName.NAME[2]=0x30; //ASCII 0
fileName.NAME[3]=0x30 +day;
}
else
{
fileName.NAME[2]=0x30 + day/10;
fileName.NAME[3]=0x30 +day%10;
}
//填充时
if(hour<10)
{
fileName.NAME[4]=0x30; //ASCII 0
fileName.NAME[5]=0x30 +hour;
}
else
{
fileName.NAME[4]=0x30 + hour/10;
fileName.NAME[5]=0x30 +hour%10;
}
//填充分
if(minute<10)
{
fileName.NAME[6]=0x30; //ASCII 0
fileName.NAME[7]=0x30 +minute;
}
else
{
fileName.NAME[6]=0x30 + minute/10;
fileName.NAME[7]=0x30 +minute%10;
}
//文件类型
fileName.TYPE[0]='T';
fileName.TYPE[1]='X';
fileName.TYPE[2]='T';
}
/*------------------------------------------------------------
*函数名称:file_time(unsigned char type)
*功能描述:有形式参数,确定是填充文件创建时间还是文件更新时间
*形式参数: 0:文件创建时间
1:文件更新时间
*返 回 值:void
*---------------------------------------------------------------*/
void file_time(unsigned char type)
{
unsigned char i;
unsigned char temp;
unsigned int updata=0x0000;
//创建时间的填充
if(type ==0)
{
//先将从DS1302中获得的BCD码格式的数据转化为16进制的数据
read_time(); //获取当前的时间
//将BCD码表示的时间转化为十进制的数
for(i=0;i<7;i++)
{
temp=time[i] &0xf0; //取高半字节
temp>>=4; //放到低位参与运算
time[i] &=0x0f;
time[i] = temp *10 + time[i];
}
//计算年
temp=time[0]+20; //时间基准为1980年1月1号
updata =temp;
updata<<=4; //年占高7位
//计算月
temp=time[2];
updata |=temp; //月为高四位
updata <<=5;
//计算data
temp = time[3];
updata |=temp; //date 占低五位
FileIndex.FileCreateDate[1]=(unsigned char)((updata &0xff00)>>8);
FileIndex.FileCreateDate[0]=(unsigned char)(updata &0x00ff);
FileIndex.FileCreateDate2[1]=(unsigned char)((updata &0xff00)>>8);
FileIndex.FileCreateDate2[0]=(unsigned char)(updata &0x00ff);
//更新的时间
FileIndex.FileUpdateDate[1]=(unsigned char)((updata &0xff00)>>8);
FileIndex.FileUpdateDate[0]=(unsigned char)(updata &0x00ff);
//计算hour
temp=time[4];
updata = temp;
updata <<=6;
//计算minute
temp=time[5];
updata |=temp;
updata<<=5;
//计算second
temp=time[6]/2;
updata |=temp;
FileIndex.FileCreateTime[1]=(unsigned char)((updata &0xff00)>>8);
FileIndex.FileCreateTime[0]=(unsigned char)(updata &0x00ff);
//更新的时间
FileIndex.FileUpdateTime[1]=(unsigned char)((updata &0xff00)>>8);
FileIndex.FileUpdateTime[0]=(unsigned char)(updata &0x00ff);
FileIndex.reserved = 0x10; //填充保留值
FileIndex.cr_time=0x06;
FileIndex.UnUsed[0]=0;
FileIndex.UnUsed[1]=0;
}//endof if()
else if(type ==1)
{
read_time(); //获取当前的时间
//将BCD码表示的时间转化为十进制的数
for(i=0;i<7;i++)
{
temp=time[i] &0xf0; //取高半字节
temp>>=4; //放到低位参与运算
time[i] &=0x0f;
time[i] = temp *10 + time[i];
}
//计算年
temp=time[0]+20; //时间基准为1980年1月1号
updata =temp;
updata<<=4; //年占高7位
//计算月
temp=time[2];
updata |=temp; //月为高四位
updata <<=5;
//计算data
temp = time[3];
updata |=temp; //date 占低五位
FileIndex.FileUpdateDate[1]=(unsigned char)((updata &0xff00)>>8);
FileIndex.FileUpdateDate[0]=(unsigned char)(updata &0x00ff);
//计算hour
temp=time[4];
updata = temp;
updata <<=6;
//计算minute
temp=time[5];
updata |=temp;
updata<<=5;
//计算second
temp=time[6]/2;
updata |=temp;
FileIndex.FileUpdateTime[1]=(unsigned char)((updata &0xff00)>>8);
FileIndex.FileUpdateTime[0]=(unsigned char)(updata &0x00ff);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -