Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of flash_audio_playerI2S by
main.cpp@1:8fda76e11be8, 2017-12-05 (annotated)
- Committer:
- 4180_1
- Date:
- Tue Dec 05 01:55:44 2017 +0000
- Revision:
- 1:8fda76e11be8
- Parent:
- 0:e4f991474a45
- Child:
- 2:04ffcc436ac0
ver 1.0
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| 4180_1 | 0:e4f991474a45 | 1 | #include "mbed.h" |
| 4180_1 | 1:8fda76e11be8 | 2 | #include "I2S.h" |
| 4180_1 | 0:e4f991474a45 | 3 | Ticker sampletick; |
| 4180_1 | 0:e4f991474a45 | 4 | DigitalOut myled(LED1); |
| 4180_1 | 0:e4f991474a45 | 5 | //Plays Audio Clip using Array in Flash |
| 4180_1 | 1:8fda76e11be8 | 6 | //using I2S input Class D audio amp |
| 4180_1 | 1:8fda76e11be8 | 7 | //setup const array in flash with audio values |
| 4180_1 | 0:e4f991474a45 | 8 | //from free wav file conversion tool at |
| 4180_1 | 0:e4f991474a45 | 9 | //http://ccgi.cjseymour.plus.com/wavtocode/wavtocode.htm |
| 4180_1 | 0:e4f991474a45 | 10 | //see https://os.mbed.com/users/4180_1/notebook/using-flash-to-play-audio-clips/ |
| 4180_1 | 1:8fda76e11be8 | 11 | //tested on MAX98357A |
| 4180_1 | 0:e4f991474a45 | 12 | #include "cylonbyc.h" |
| 4180_1 | 0:e4f991474a45 | 13 | #define sample_freq 11025.0 |
| 4180_1 | 1:8fda76e11be8 | 14 | |
| 4180_1 | 1:8fda76e11be8 | 15 | I2S i2s(I2S_TRANSMIT, p5, p6, p7); |
| 4180_1 | 1:8fda76e11be8 | 16 | //p5(PWM value sent as serial data bit) <-> Din |
| 4180_1 | 1:8fda76e11be8 | 17 | //p6(WS) <-> LRC left/right channel clock |
| 4180_1 | 1:8fda76e11be8 | 18 | //p7(bit clock) <-> BCLK |
| 4180_1 | 0:e4f991474a45 | 19 | |
| 4180_1 | 1:8fda76e11be8 | 20 | int i = 0; |
| 4180_1 | 1:8fda76e11be8 | 21 | int bufflen = 1; |
| 4180_1 | 1:8fda76e11be8 | 22 | int buffer[1]; |
| 4180_1 | 1:8fda76e11be8 | 23 | void isr() |
| 4180_1 | 0:e4f991474a45 | 24 | { |
| 4180_1 | 1:8fda76e11be8 | 25 | buffer[0] = sound_data[i]>>1;//scale down volume a bit on amp |
| 4180_1 | 1:8fda76e11be8 | 26 | i2s.write(buffer, bufflen);//Send next PWM value to amp |
| 4180_1 | 0:e4f991474a45 | 27 | i++; |
| 4180_1 | 0:e4f991474a45 | 28 | if (i>= NUM_ELEMENTS) { |
| 4180_1 | 0:e4f991474a45 | 29 | i = 0; |
| 4180_1 | 0:e4f991474a45 | 30 | sampletick.detach(); |
| 4180_1 | 1:8fda76e11be8 | 31 | i2s.stop(); |
| 4180_1 | 0:e4f991474a45 | 32 | myled = 0; |
| 4180_1 | 0:e4f991474a45 | 33 | } |
| 4180_1 | 0:e4f991474a45 | 34 | } |
| 4180_1 | 1:8fda76e11be8 | 35 | |
| 4180_1 | 0:e4f991474a45 | 36 | int main() |
| 4180_1 | 0:e4f991474a45 | 37 | { |
| 4180_1 | 1:8fda76e11be8 | 38 | i2s.stereomono(I2S_MONO); |
| 4180_1 | 1:8fda76e11be8 | 39 | i2s.masterslave(I2S_MASTER); |
| 4180_1 | 1:8fda76e11be8 | 40 | i2s.frequency(sample_freq); |
| 4180_1 | 0:e4f991474a45 | 41 | while(1) { |
| 4180_1 | 1:8fda76e11be8 | 42 | i2s.start(); |
| 4180_1 | 0:e4f991474a45 | 43 | myled = 1; |
| 4180_1 | 1:8fda76e11be8 | 44 | sampletick.attach(&isr, 1.0/sample_freq);//turn on timer interrupt |
| 4180_1 | 1:8fda76e11be8 | 45 | wait(10); |
| 4180_1 | 0:e4f991474a45 | 46 | } |
| 4180_1 | 0:e4f991474a45 | 47 | } |
