Room echo calculator

Dependencies:   Adafruit-GFX-MbedOS6 Adafruit_SSD1331_MbedOS6

main.cpp

Committer:
timo_k2
Date:
2020-10-07
Revision:
0:33edf726559b
Child:
1:2b95f921e7f2

File content as of revision 0:33edf726559b:

/*
*
* Connections
* L432KC D3   --- SS PmodMIC3
* L432KC D12  --- MISO PmodMIC3
* L432KC D13  --- SCK PmodMIC3
* GND         --- GND PmodMIC3
* Vcc         --- VCC PmodMIC3
*
* Pmod MIC3
* https://reference.digilentinc.com/reference/pmod/pmodmic3/start
* ADC7476
* https://www.ti.com/lit/ds/symlink/adcs7476.pdf
*
* Timo Karppinen 7.10.2020
**************************************************/
#include  "mbed.h"

SPI spi(D11, D12, D13); // mosi, miso, sclk
DigitalOut mic3cs(D3);


int raw = 0;        // 16 bits from MIC3
int sound32bit = 0;
int sound = 0;
DigitalOut LED(D9);


int main()
{
    // Chip must be deselected
    mic3cs.write(1);

    // Setup the spi for 16 bit data, low steady state clock,
    // rising edge capture, with a 1MHz clock rate
    spi.format(16, 0);
    spi.frequency(1000000);     
    ThisThread::sleep_for(100ms);
    
    while(1){
    // Select the device by seting chip select low
    mic3cs.write(0);
    ThisThread::sleep_for(1ms);  // > 100 ns for the MAX31855
    
    // Send 0x0000 to nowhere to read the 16 bits
    raw = spi.write(0x0000);
    
    ThisThread::sleep_for(1ms);

    // Deselect the device
    mic3cs.write(1);
    printf("16 bits MIC3 = 0x%X", raw);
    
    // It is a two's complement for negative numbers. The sign is now on the 12th bit.
    
    sound32bit = raw << 22; // 22 bits to the left to create 32 bit two's complement
    sound = sound32bit / 16777216; // 2 exp24 = 16 7777 216  means shifting 24 bits left without shifting the sign!
    printf("   sound 12 bit = %d\n", sound);
   
    if(sound > 128){
        LED.write(1);
        }
        else{
            LED.write(0);
            }
    ThisThread::sleep_for(200ms);
    }
}