Embedded system Fun Blog
























































Find out all the best information, libraries and circuit about the latest Embedded systems.
Showing posts with label AnalogOut. Show all posts
Showing posts with label AnalogOut. Show all posts

Saturday, 7 January 2012

MBED Example: How to play WAV sound file from SDCard using SPI and AnalogOut

.from: https://github.com/leroilion/mbed/blob/master/PlayMusic/Examples/PlayMusic/main.cpp

#include "mbed.h"
#include "SDFileSystem.h"
SDFileSystem sd(11, 12, 13, 14, "sd");
AnalogOut aout(18);
int main() {
int ch; //first read byte
int ch2; //second read byte
int dat; //combined 16 bit word for DtoA
FILE *fp = fopen("/sd/QI2.wav", "r"); //open File to read
if(fp == NULL) {error("Could not open file for write\n");}
while(!feof(fp)) { //Play the music until End Of File
dat = 0;
ch = fgetc(fp); //get first byte
ch2 =fgetc(fp); //get second byte
ch = ch << 8; //shift bits 8 places to left
dat = ch2 | ch; //combine bits to single 16 bit int
aout.write_u16((unsigned short)(dat)); //DtoA conversion
wait_us(88); //set for 11k sample rate
}
fclose(fp); //close the file
}

MBED Example: http://mbed.org/projects/libraries/svn/mbed/trunk/AnalogOut.h

.from: http://mbed.org/projects/libraries/svn/mbed/trunk/AnalogOut.h

// Make a sawtooth output

#include "mbed.h"

AnalogOut tri(p18);

int main() {
 while(1) {
     tri = tri + 0.01;
     wait_us(1);
     if(tri == 1) {
         tri = 0;
     }
 }
}