首页 > 评测 > 8位机也玩音频-Arduino+SD+DAC+运放打造的播放器

8位机也玩音频-Arduino+SD+DAC+运放打造的播放器

8位机   Arduino   SD   DAC   
  • 作者:zhanzr
  • 来源:21ic
  • [导读]
  • 比如本文要介绍的Adafruit Wave Shield,就能是专门为Arduino设计的音频模块.确切的说是为AVR这一类的8位机而设计的音频模块.下文将从硬件到软件详细介绍一下子此模块的设计细节与使用方法.

FatReader root; // This holds the information for the volumes root directory

WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time

uint8_t dirLevel; // indent level for file/dir names (for prettyprinting)

dir_t dirBuf; // buffer for directory reads

/*

* Define macro to put error messages in flash memory

*/

#define error(msg) error_P(PSTR(msg))

// Function definitions (we define them here, but the code is below)

void play(FatReader &dir);

//Arduino程序的初始化函数,主要是加载文件系统

void setup() {

Serial.begin(9600); // set up Serial library at 9600 bps for debugging

putstring_nl("\nWave test!"); // say we woke up!

putstring("Free RAM: "); // This can help with debugging, running out of RAM is bad

Serial.println(FreeRam());

// if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you

if (!card.init()) { //play with 8 MHz spi (default faster!)

error("Card init. failed!"); // Something went wrong, lets print out why

}

// enable optimize read - some cards may timeout. Disable if you're having problems

card.partialBlockRead(true);

// Now we will look for a FAT partition!

uint8_t part;

for (part = 0; part < 5; part++) { // we have up to 5 slots to look in

if (vol.init(card, part))

break; // we found one, lets bail

}

if (part == 5) { // if we ended up not finding one :(

error("No valid FAT partition!"); // Something went wrong, lets print out why

}

// Lets tell the user about what we found

putstring("Using partition ");

Serial.print(part, DEC);

putstring(", type is FAT");

Serial.println(vol.fatType(), DEC); // FAT16 or FAT32?

// Try to open the root directory

if (!root.openRoot(vol)) {

error("Can't open root dir!"); // Something went wrong,

}

// Whew! We got past the tough parts.

putstring_nl("Files found (* = fragmented):");

// Print out all of the files in all the directories.

root.ls(LS_R | LS_FLAG_FRAGMENTED);

}

//主循环就是播放,播放是通过中断来进行的,所以也可以同时做其他操作

void loop() {

root.rewind();

play(root);

}

//一些出错报告函数

/*

* print error message and halt

*/

void error_P(const char *str) {

PgmPrint("Error: ");

SerialPrint_P(str);

sdErrorCheck();

while(1);

}

/*

* print error message and halt if SD I/O error, great for debugging!

*/

void sdErrorCheck(void) {

if (!card.errorCode()) return;

PgmPrint("\r\nSD I/O error: ");

Serial.print(card.errorCode(), HEX);

  • 本文系21ic原创,未经许可禁止转载!

网友评论