Demo for new class to play a note on a speaker using a PWM output See http://mbed.org/users/4180_1/notebook/using-a-speaker-for-audio-output/

Dependencies:   mbed

Committer:
4180_1
Date:
Tue Oct 21 03:00:15 2014 +0000
Revision:
1:2e6ea42675c7
Parent:
0:b2fdf3770282
ver 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:b2fdf3770282 1 #include "mbed.h"
4180_1 1:2e6ea42675c7 2 #include "SongPlayer.h"
4180_1 0:b2fdf3770282 3
4180_1 1:2e6ea42675c7 4 // Song test program - plays a song using PWM and timer interrupts
4180_1 0:b2fdf3770282 5 // for documentation see http://mbed.org/users/4180_1/notebook/using-a-speaker-for-audio-output/
4180_1 1:2e6ea42675c7 6 // can be used to play a song, if you have the notes and durations
4180_1 0:b2fdf3770282 7 // for musical note frequencies see http://en.wikipedia.org/wiki/Piano_key_frequencies
4180_1 0:b2fdf3770282 8
4180_1 1:2e6ea42675c7 9 //Set up notes and durations for sample song to play
4180_1 1:2e6ea42675c7 10 // A 0.0 duration note at end terminates song play
4180_1 1:2e6ea42675c7 11 float note[18]= {1568.0,1396.9,1244.5,1244.5,1396.9,1568.0,1568.0,1568.0,1396.9,
4180_1 1:2e6ea42675c7 12 1244.5,1396.9,1568.0,1396.9,1244.5,1174.7,1244.5,1244.5, 0.0
4180_1 1:2e6ea42675c7 13 };
4180_1 1:2e6ea42675c7 14 float duration[18]= {0.48,0.24,0.72,0.48,0.24,0.48,0.24,0.24,0.24,
4180_1 1:2e6ea42675c7 15 0.24,0.24,0.24,0.24,0.48,0.24,0.48,0.48, 0.0
4180_1 1:2e6ea42675c7 16 };
4180_1 1:2e6ea42675c7 17
4180_1 1:2e6ea42675c7 18 DigitalOut led1(LED1);
4180_1 0:b2fdf3770282 19 int main()
4180_1 0:b2fdf3770282 20 {
4180_1 1:2e6ea42675c7 21 // setup instance of new SongPlayer class, mySpeaker using pin 26
4180_1 0:b2fdf3770282 22 // the pin must be a PWM output pin
4180_1 1:2e6ea42675c7 23 SongPlayer mySpeaker(p26);
4180_1 1:2e6ea42675c7 24 // Start song and return once playing starts
4180_1 1:2e6ea42675c7 25 mySpeaker.PlaySong(note,duration);
4180_1 1:2e6ea42675c7 26 // loops forever while song continues to play to end using interrupts
4180_1 0:b2fdf3770282 27 while(1) {
4180_1 1:2e6ea42675c7 28 led1 = !led1;
4180_1 1:2e6ea42675c7 29 wait(.1);
4180_1 0:b2fdf3770282 30 }
4180_1 0:b2fdf3770282 31 }
4180_1 1:2e6ea42675c7 32