Room echo calculator

Dependencies:   Adafruit-GFX-MbedOS6 Adafruit_SSD1331_MbedOS6

Committer:
timo_k2
Date:
Wed Oct 07 13:04:40 2020 +0000
Revision:
0:33edf726559b
Child:
1:2b95f921e7f2
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
timo_k2 0:33edf726559b 1 /*
timo_k2 0:33edf726559b 2 *
timo_k2 0:33edf726559b 3 * Connections
timo_k2 0:33edf726559b 4 * L432KC D3 --- SS PmodMIC3
timo_k2 0:33edf726559b 5 * L432KC D12 --- MISO PmodMIC3
timo_k2 0:33edf726559b 6 * L432KC D13 --- SCK PmodMIC3
timo_k2 0:33edf726559b 7 * GND --- GND PmodMIC3
timo_k2 0:33edf726559b 8 * Vcc --- VCC PmodMIC3
timo_k2 0:33edf726559b 9 *
timo_k2 0:33edf726559b 10 * Pmod MIC3
timo_k2 0:33edf726559b 11 * https://reference.digilentinc.com/reference/pmod/pmodmic3/start
timo_k2 0:33edf726559b 12 * ADC7476
timo_k2 0:33edf726559b 13 * https://www.ti.com/lit/ds/symlink/adcs7476.pdf
timo_k2 0:33edf726559b 14 *
timo_k2 0:33edf726559b 15 * Timo Karppinen 7.10.2020
timo_k2 0:33edf726559b 16 **************************************************/
timo_k2 0:33edf726559b 17 #include "mbed.h"
timo_k2 0:33edf726559b 18
timo_k2 0:33edf726559b 19 SPI spi(D11, D12, D13); // mosi, miso, sclk
timo_k2 0:33edf726559b 20 DigitalOut mic3cs(D3);
timo_k2 0:33edf726559b 21
timo_k2 0:33edf726559b 22
timo_k2 0:33edf726559b 23 int raw = 0; // 16 bits from MIC3
timo_k2 0:33edf726559b 24 int sound32bit = 0;
timo_k2 0:33edf726559b 25 int sound = 0;
timo_k2 0:33edf726559b 26 DigitalOut LED(D9);
timo_k2 0:33edf726559b 27
timo_k2 0:33edf726559b 28
timo_k2 0:33edf726559b 29 int main()
timo_k2 0:33edf726559b 30 {
timo_k2 0:33edf726559b 31 // Chip must be deselected
timo_k2 0:33edf726559b 32 mic3cs.write(1);
timo_k2 0:33edf726559b 33
timo_k2 0:33edf726559b 34 // Setup the spi for 16 bit data, low steady state clock,
timo_k2 0:33edf726559b 35 // rising edge capture, with a 1MHz clock rate
timo_k2 0:33edf726559b 36 spi.format(16, 0);
timo_k2 0:33edf726559b 37 spi.frequency(1000000);
timo_k2 0:33edf726559b 38 ThisThread::sleep_for(100ms);
timo_k2 0:33edf726559b 39
timo_k2 0:33edf726559b 40 while(1){
timo_k2 0:33edf726559b 41 // Select the device by seting chip select low
timo_k2 0:33edf726559b 42 mic3cs.write(0);
timo_k2 0:33edf726559b 43 ThisThread::sleep_for(1ms); // > 100 ns for the MAX31855
timo_k2 0:33edf726559b 44
timo_k2 0:33edf726559b 45 // Send 0x0000 to nowhere to read the 16 bits
timo_k2 0:33edf726559b 46 raw = spi.write(0x0000);
timo_k2 0:33edf726559b 47
timo_k2 0:33edf726559b 48 ThisThread::sleep_for(1ms);
timo_k2 0:33edf726559b 49
timo_k2 0:33edf726559b 50 // Deselect the device
timo_k2 0:33edf726559b 51 mic3cs.write(1);
timo_k2 0:33edf726559b 52 printf("16 bits MIC3 = 0x%X", raw);
timo_k2 0:33edf726559b 53
timo_k2 0:33edf726559b 54 // It is a two's complement for negative numbers. The sign is now on the 12th bit.
timo_k2 0:33edf726559b 55
timo_k2 0:33edf726559b 56 sound32bit = raw << 22; // 22 bits to the left to create 32 bit two's complement
timo_k2 0:33edf726559b 57 sound = sound32bit / 16777216; // 2 exp24 = 16 7777 216 means shifting 24 bits left without shifting the sign!
timo_k2 0:33edf726559b 58 printf(" sound 12 bit = %d\n", sound);
timo_k2 0:33edf726559b 59
timo_k2 0:33edf726559b 60 if(sound > 128){
timo_k2 0:33edf726559b 61 LED.write(1);
timo_k2 0:33edf726559b 62 }
timo_k2 0:33edf726559b 63 else{
timo_k2 0:33edf726559b 64 LED.write(0);
timo_k2 0:33edf726559b 65 }
timo_k2 0:33edf726559b 66 ThisThread::sleep_for(200ms);
timo_k2 0:33edf726559b 67 }
timo_k2 0:33edf726559b 68 }
timo_k2 0:33edf726559b 69