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

📄 fat.c~

📁 以AVRMEGA32播放SD卡中的WAV音乐
💻 C~
字号:
#include <mega32.h>
#include "type.h"
#include "fat.h"
#include "sd.c"
u8     bf[512];
u8      secpclus; //每籁包函扇区数
u16     fr_fatsec;   //FAT表首扇区
u16        rodir;       // 根目录首扇区
u8      firstrodirclus; //根目录的首籁号
u32     filefirstclus;   //文件首籁号 
u32     filesize;        //
 BYTE (* FAT_ReadSector)(DWORD sector, BYTE * buffer) = MMC_SD_ReadSingleBlock;//device read
   
///FAT初始化
unsigned char FAT_Init()
{       u32     x;
	
	DWORD   *p; 
	u16     *rscnt;//保留扇区数
	u32     *fatsz;//FAT大小
	if(FAT_ReadSector(0,bf))return 1; //读出启动扇区
	p=&bf[454];         //取出分区启动扇区地址  
	x=*p;
	FAT_ReadSector(x,bf); //读分区的启动扇区
	secpclus=bf[13];    //每籁的扇区数
	rscnt=&bf[14];       //保留扇区
	fatsz=&bf[36];       //FAT大小
	firstrodirclus=bf[44];//根目录的首籁号0
	fr_fatsec=x+(*rscnt);    //FAT首扇区
	rodir=fr_fatsec+(*fatsz)*2; //算出根目录  
	
	
	return 0;
}
     

//读一个簇中的一个扇区
unsigned char FAT_LoadPartCluster(unsigned long cluster,unsigned part,BYTE * buffer)
{
	DWORD sector;
	sector=rodir+(DWORD)(cluster-2)*(DWORD)secpclus;//算出扇区
	if(MMC_SD_ReadSingleBlock(sector+part,buffer))return 1;
	else return 0;
}




//读下一簇簇号
unsigned long FAT_NextCluster(unsigned long cluster)
{       u32 *x;
	DWORD sector;
	DWORD offset; 
	offset = cluster/128;
	if(cluster<2){return 0x0ffffff8;}
	sector=fr_fatsec+offset;
	if(FAT_ReadSector(sector,bf))return 0x0ffffff8;//读FAT表
		offset=cluster%128;//因为籁号大于128时就会跨扇区
		x=&bf[offset*4];
	return *x;
}





//----------------------------------------
//读下一簇簇号
unsigned long FAT_NextCluster1(unsigned long cluster)
{       u32 *x;
        u8 r1,i;
        u16 retry=0;
        u8  buf[4];
	DWORD sector;
	DWORD offset; 
	offset = cluster/128;
	//if(cluster<2){return 0x0ffffff8;}
	sector=(fr_fatsec+offset)*512;
	offset=cluster%128;//因为籁号大于128时就会跨扇区 
	sector=sector+offset*4;
	
	MMC_SD_SendCommand(16, 4);
	r1 = MMC_SD_SendCommand(17, sector);
	if(r1 != 0x00)
		return r1;  
		SPI_CS_Assert();  //cs=0
	//等数据的开始	
	while(SPI_WriteByte(0xff) != 0xfe)if(retry++ > 1000){SPI_CS_Deassert();return 1;}
         for(i=0; i<4; i++)//读4个数据	
	{
		buf[i] = SPI_WriteByte(0xff);
	}
        x=buf;
	SPI_WriteByte(0xff);//伪crc
	SPI_WriteByte(0xff);
	MMC_SD_SendCommand(16,512);
	SPI_CS_Deassert(); //cs=1
        
        
	return *x;
}




//-------------------------


///////////////////////////////////////

//search the file , when *count = 0 it will bring the number whole songs, when *cout != 0 the *MusicInfo will bring the infomation of the file
BYTE Search(WORD *Count)//当COUNT为零时,有它带回这个目录下总共有多少首音乐
{      u32      tempclust,sector; 
        u8      cnt;
        u16     offset;
        u16     i=0;    
        u16    *p; 
                                       
	tempclust=firstrodirclus;
		while(1)
		{       sector=rodir+(DWORD)(tempclust-2)*(DWORD)secpclus;//算出扇区
			for(cnt=0;cnt<secpclus;cnt++)//读一个籁
			{
				if(FAT_ReadSector(sector+cnt,bf)){return 1;}
				
				for(offset=0;offset<512;offset+=32)//扇区内遍历
				{      
				     if((bf[offset] !=0)&(bf[offset] !='.')&(bf[offset] !=0xe5)&(bf[offset+11]!=0x0f))
					{    
						if((bf[offset+8] == 'W')&&(bf[offset+9] == 'A')&&(bf[offset+10] == 'V'))
						{
	 
	                                        p=&bf[offset+20]; 
	                                        filefirstclus=*p;
	                                        //TransmitByte(filefirstclus);
	                                        filefirstclus=filefirstclus<<8;
	                                        p=&bf[offset+26]; 
	                                        filefirstclus=(filefirstclus+*p);
						p=&bf[offset+28];
						filesize=*p;
						i++;
						if(i==*Count){return 0;}	
						}
			
					}
				}
			}
			
			tempclust=FAT_NextCluster(tempclust);//next cluster
			if(tempclust == 0x0fffffff || tempclust == 0x0ffffff8 )break;
		}
	
	if(*Count==0)*Count=i;
	return 0;
}

⌨️ 快捷键说明

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