Nikolay Sergeev / Mbed 2 deprecated Nucleo_ADC_microphone

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002  
00003 #define MAX_QUEUE 512          // Indexed from 0 to 511
00004  
00005 AnalogOut speaker(PA_4);        // DAC for the speaker/output
00006 AnalogIn  mic(A2);            // ADC for the microphone/input
00007 //AnalogIn  pot(p20);            // ADC for the pot
00008 
00009 int main()
00010 {
00011     int queue[MAX_QUEUE];      // Will be a circular buffer
00012 
00013     int ii = 0;                // Queue write position
00014     int jj = 0;                // Q read position
00015     int offset = 0;            // Delay (number of samples)
00016     int oo;                    // The analogue data read or written
00017     
00018     while(1)                   // For ever
00019     {
00020         oo = mic.read_u16() - 32767;    // Read ADC and remove DC offset
00021       
00022         queue[ii] = oo;                 // Add sample to queue
00023         
00024         jj = ii - offset;               // Calculate playback position
00025         
00026         if (jj < 0) jj = jj + MAX_QUEUE;// If queue pointer is negative, wrap it around.
00027         
00028         ii = (ii + 1) % MAX_QUEUE;      // Next position in the circular queue
00029         
00030         if (ii == 0)                    // Read the potentiometer
00031         {
00032             offset = MAX_QUEUE * 32767 / 65535;  // Scale the reading to fit the queue
00033             
00034             if (offset >= MAX_QUEUE) offset = MAX_QUEUE - 1;  // Limit the upper value
00035         }
00036 
00037         oo = oo + queue[jj];            // Add zero referenced signals: input + delayed_input
00038 
00039         oo = oo + 32767;                // Restore the DC Offset.
00040         
00041         speaker.write_u16(oo);          // DAC output
00042         //printf();
00043         
00044         if (!(ii % 16)) printf("oo = %5d | ii = %5d | jj = %5d | offset = %5d \n\r", oo, ii, jj, offset);
00045         // FOR DEBUGGING if (!(ii % 16)) printf("oo = %5d | ii = %5d | jj = %5d | offset = %5d \n\r", oo, ii, jj, offset);
00046     }
00047 }