Im working with the sph0645 I2S microphone and an Arduino MKR 1010. I can read the data from the microphone but the 5 first bits from the sph0645 are always high.
This is the code I'm using:
/*
This example reads audio data from an Invensense's ICS43432 I2S microphone
breakout board, and prints out the samples to the Serial console. The
Serial Plotter built into the Arduino IDE can be used to plot the audio
data (Tools -> Serial Plotter)
Circuit:
* Arduino/Genuino Zero, MKR family and Nano 33 IoT
* ICS43432:
* GND connected GND
* 3.3V connected to 3.3V (Zero, Nano) or VCC (MKR)
* WS connected to pin 0 (Zero) or 3 (MKR) or A2 (Nano)
* CLK connected to pin 1 (Zero) or 2 (MKR) or A3 (Nano)
* SD connected to pin 9 (Zero) or A6 (MKR) or 4 (Nano)
created 17 November 2016
by Sandeep Mistry
*/
#include <I2S.h>
void setup() {
// Open serial communications and wait for port to open:
// A baud rate of 115200 is used instead of 9600 for a faster data rate
// on non-native USB ports
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start I2S at 8 kHz with 32-bits per sample
if (!I2S.begin(I2S_PHILIPS_MODE, 48000, 32)) {
Serial.println("Failed to initialize I2S!");
while (1); // do nothing
}
}
void loop() {
// read a sample
int sample = I2S.read();
Serial.print("Sample 32 bits: ");
Serial.println(String(sample, BIN));
int32_t signed_sample_18bits = (sample>>14) & 0x0003FFFF; //convert 32bits into 18bits signed
//int32_t signed_sample_18bits = 0b011111111111111111; // Debug conversion: 0b100000000000000000 0b111111111111111111 0b011111111111111111
Serial.print("Sample 18 bits: ");
Serial.println(String(signed_sample_18bits, BIN));
int32_t sign_bit = signed_sample_18bits >> 17; // only keep the sign bit
int32_t absolute_sample = signed_sample_18bits & 0x0001FFFF; //taking off the sign bit
delay(100);
if (sign_bit == 1) {
absolute_sample = ~absolute_sample & 0x0001FFFF; // Invert + 1 to make 2's compliment
absolute_sample += 1;
//Serial.print("-");
absolute_sample *= -1;
}
Serial.println(absolute_sample);
delay(200);
}
The Data Format is I2S, 24 bit, 2’s compliment, MSB first. The Data Precision is 18 bits, unused bits are zeros. So I made a conversion of the data but I still get only negative samples. Do you have a solution?