Как читать (.WAV) файлы с помощью Atmega32

Я проектирую цифровой аудиоплеер с использованием Atmega32. Я использую карту microSD для хранения файлов (.wav). Файловая система, используемая картой, - FAT32. До сих пор я успешно инициализировал SD-карту и смог прочитать Мастер Boot Record(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;
}

В его коде есть еще одна функция для чтения файлов, но проблема в том, что функция принимает указатель на имя файла в качестве одного из аргументов, мне нужно передать флаг в файл READ и массив для сохранения имени файла. Я также хочу, чтобы функция возвращала первый кластер файла для чтения. Ниже приведена функция для чтения файла.

//***************************************************************************
//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 Заранее спасибо.

0 ответов

Другие вопросы по тегам