I2S microphone demo for LPC1768

Dependencies:   I2S mbed

Fork of flash_audio_playerI2S by jim hamblen

See https://os.mbed.com/users/4180_1/notebook/using-an-i2s-microphone---sph0645lm4h/ for details.

Revision:
2:04ffcc436ac0
Parent:
1:8fda76e11be8
--- a/main.cpp	Tue Dec 05 01:55:44 2017 +0000
+++ b/main.cpp	Fri Jan 26 00:55:11 2018 +0000
@@ -1,47 +1,43 @@
 #include "mbed.h"
+//I2S Demo to display values from I2S microphone
+//
+//Only works on mbed LPC1768 - I2S is not an mbed API!
+//Microphone used is SPH0645LM4H
+//see https://www.adafruit.com/product/3421
+
 #include "I2S.h"
-Ticker sampletick;
-DigitalOut myled(LED1);
-//Plays Audio Clip using Array in Flash
-//using I2S input Class D audio amp
-//setup const array in flash with audio values
-//from free wav file conversion tool at
-//http://ccgi.cjseymour.plus.com/wavtocode/wavtocode.htm
-//see https://os.mbed.com/users/4180_1/notebook/using-flash-to-play-audio-clips/
-//tested on MAX98357A
-#include "cylonbyc.h"
-#define sample_freq 11025.0
+//uses patched mbed I2S library from 
+//https://os.mbed.com/users/p07gbar/code/I2S/
+//Needed a typo patch - clock was swapped p30/p15 in pin function setup code
+//"if (_clk == p15)" changed to "if (_clk != p15)" in I2S.cpp line 494
+BusOut mylevel(LED4,LED3,LED2,LED1);
+//4 built in mbed LEDs display audio level
+
+#define sample_freq 32000.0
 
-I2S i2s(I2S_TRANSMIT, p5, p6, p7);
-//p5(PWM value sent as serial data bit) <-> Din
-//p6(WS) <-> LRC left/right channel clock
-//p7(bit clock) <-> BCLK
+//Uses I2S hardware for input
+I2S i2srx(I2S_RECEIVE, p17, p16, p15);
+//p17(PWM value sent as serial data bit) <-> Dout
+//p16(WS) <-> LRC left/right channel clock
+//p15(bit clock) <-> BCLK 
+
+volatile int i=0;
 
-int i = 0;
-int bufflen = 1;
-int buffer[1];
-void isr()
+void i2s_isr() //interrupt routine to read microphone
+{
+    i = i2srx.read();//read value using I2S input
+    mylevel = (i>>7)& 0xF; //Display Audio Level on 4 LEDs
+}
+int main()
 {
-    buffer[0] = sound_data[i]>>1;//scale down volume a bit on amp
-    i2s.write(buffer, bufflen);//Send next PWM value to amp
-    i++;
-    if (i>= NUM_ELEMENTS) {
-        i = 0;
-        sampletick.detach();
-        i2s.stop();
-        myled = 0;
+    i2srx.stereomono(I2S_MONO);//mono not stereo mode
+    i2srx.masterslave(I2S_MASTER);//master drives clocks
+    i2srx.frequency(sample_freq);//set sample freq
+    i2srx.set_interrupt_fifo_level(1);//interrupt on one sample
+    i2srx.attach(&i2s_isr);//set I2S ISR
+    i2srx.start();//start I2S and interrupts
+    while(1) {
+        printf("%X\n\r",(i>>16)&(0x0000FFFF));
+        wait(0.5);
     }
 }
-
-int main()
-{
-    i2s.stereomono(I2S_MONO);
-    i2s.masterslave(I2S_MASTER);
-    i2s.frequency(sample_freq);
-    while(1) {
-        i2s.start();
-        myled = 1;
-        sampletick.attach(&isr, 1.0/sample_freq);//turn on timer interrupt
-        wait(10);
-    }
-}