我正在使用Atmega32设计数字音频播放器。我正在使用microSD卡存储(.wav)文件。该卡使用的文件系统是FAT32。到目前为止,我已成功初始化SD卡并且能够读取主引导记录(MBR)。现在我不知道如何从SD卡读取和播放文件。我使用了以下链接中的代码。 http://www.dharmanitech.com/2009/01/sd-card-interfacing-with-atmega8-fat32.html 以下是从引导扇区读取读取数据的函数的代码片段。
unsigned char getBootSectorData (void)
{
struct BS_Structure *bpb; //mapping the buffer onto the structure
struct MBRinfo_Structure *mbr;
struct partitionInfo_Structure *partition;
unsigned long dataSectors;
unusedSectors = 0;
SD_readSingleBlock(0);
bpb = (struct BS_Structure *)buffer;
if(bpb->jumpBoot[0]!=0xE9 && bpb->jumpBoot[0]!=0xEB) //check if it is boot sector
{
mbr = (struct MBRinfo_Structure *) buffer; //if it is not boot sector, it must be MBR
if(mbr->signature != 0xaa55) return 1; //if it is not even MBR then it's not FAT32
partition = (struct partitionInfo_Structure *)(mbr->partitionData);//first partition
unusedSectors = partition->firstSector; //the unused sectors, hidden to the FAT
SD_readSingleBlock(partition->firstSector);//read the bpb sector
bpb = (struct BS_Structure *)buffer;
if(bpb->jumpBoot[0]!=0xE9 && bpb->jumpBoot[0]!=0xEB) return 1;
}
//I am sure the following parameters are useful but I am not sure how to use
//them
bytesPerSector = bpb->bytesPerSector;
sectorPerCluster = bpb->sectorPerCluster;
reservedSectorCount = bpb->reservedSectorCount;
rootCluster = bpb->rootCluster;// + (sector / sectorPerCluster) +1;
firstDataSector = bpb->hiddenSectors + reservedSectorCount + (bpb->numberofFATs * bpb->FATsize_F32);
dataSectors = bpb->totalSectors_F32
- bpb->reservedSectorCount
- ( bpb->numberofFATs * bpb->FATsize_F32);
totalClusters = dataSectors / sectorPerCluster;
if((getSetFreeCluster (TOTAL_FREE, GET, 0)) > totalClusters) //check if FSinfo free clusters count is valid
freeClusterCountUpdated = 0;
else
freeClusterCountUpdated = 1;
return 0;
}
在他的代码中还有另一个函数来读取文件,但问题是,该函数将一个指向filename的指针作为其参数之一,我需要的是将一个标志传递给READ文件和一个用于保存的数组文件名。我还希望函数返回正在读取的文件的第一个簇.Below是一个读取文件的函数。
//***************************************************************************
//Function: if flag=READ then to read file from SD card and send contents to UART
//if flag=VERIFY then functions will verify whether a specified file is already existing
//Arguments: flag (READ or VERIFY) and pointer to the file name
//return: 0, if normal operation or flag is READ
// 1, if file is already existing and flag = VERIFY
// 2, if file name is incompatible
//***************************************************************************
unsigned char readFile (unsigned char flag, unsigned char *fileName)
{
struct dir_Structure *dir;
unsigned long cluster, byteCounter = 0, fileSize, firstSector;
unsigned int k;
unsigned char j, error;
error = convertFileName (fileName); //convert fileName into FAT format
if(error) return 2;
dir = findFiles (GET_FILE, fileName); //get the file location
if(dir == 0)
return (0);
if(flag == VERIFY) return (1); //specified file name is already existing
cluster = (((unsigned long) dir->firstClusterHI) << 16) | dir->firstClusterLO;
fileSize = dir->fileSize;
TX_NEWLINE;
TX_NEWLINE;
while(1)
{
firstSector = getFirstSector (cluster);
for(j=0; j<sectorPerCluster; j++)
{
SD_readSingleBlock(firstSector + j);
for(k=0; k<512; k++)
{
transmitByte(buffer[k]);
if ((byteCounter++) >= fileSize ) return 0;
}
}
cluster = getSetNextCluster (cluster, GET, 0);
if(cluster == 0) {transmitString_F(PSTR("Error in getting cluster")); return 0;}
}
return 0;
}
任何可以帮助我的人都会非常感激 我也看了看这段代码 http://blog.vinu.co.in/2012/02/tv-remote-controller-high-quality.html 提前谢谢。