Skip to content

Commit

Permalink
Add Multi Channel ADC selection and example
Browse files Browse the repository at this point in the history
  • Loading branch information
TMRh20 committed Nov 12, 2016
1 parent 48ad12e commit c0e4c5b
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 2 deletions.
99 changes: 99 additions & 0 deletions examples/MultiChannelAdcStream/MultiChannelAdcStream.ino
@@ -0,0 +1,99 @@
/*
AutoAnalogAudio streaming via DAC & ADC by TMRh20
Copyright (C) 2016 TMRh20 - tmrh20@gmail.com, github.com/TMRh20
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Auto Analog Audio (Automatic DAC, ADC & Timer) library
Features:
1. Very simple user interface to Arduino DUE DAC and ADC
2. PCM/WAV Audio/Analog Data playback using Arduino Due DAC
3. PCM/WAV Audio/Analog Data recording using Arduino Due ADC
4. Onboard timers drive the DAC & ADC automatically
5. Automatic sample rate/timer adjustment based on rate of user-driven data requests/input
6. Uses DMA (Direct Memory Access) to buffer DAC & ADC data
Auto Analog Audio Library Information:
http://github.com/TMRh20
http://tmrh20.blogspot.com
**************************************************************************
Dual Pin ADC Sampling
Sample the ADC in chunks of 32-bytes at a defined sample rate
The first sample will be from pin A1, second from A0, 3rd from A1, etc...
See AnalogAudio_config.h to change the MAX_BUFFER_SIZE allowing larger chunks
**************************************************************************
*/

#include <AutoAnalogAudio.h>
AutoAnalog aaAudio;

/*********************************************************/

void setup() {

Serial.begin(115200);
Serial.println("Analog Audio Begin");

aaAudio.begin(1, 0); //Setup aaAudio using ADC only
aaAudio.autoAdjust = 0; //Disable auto adjust of timers
aaAudio.adcBitsPerSample = 12; //Sample at 12-bits
aaAudio.setSampleRate(32); //Get 32 samples every second

//AAAudio samples on analog pin A0 by default
//Enable sampling on two pins at once
aaAudio.enableAdcChannel(1); //Channels correspond to pin numbers (A1 == channel 1)
//aaAudio.enableAdcChannel(2); //Optionally enable a third channel on pin A2
//aaAudio.disableAdcChannel(0); //Optionally disable pin A0 and only sample 1 pin

//Start loading ADC buffers
aaAudio.getADC();
}

/*********************************************************/

void loop() {

// Get 32 samples from the ADC at the sample rate defined above
// Note: This function only blocks if the ADC is currently sampling and autoAdjust is set to 0
// As long as any additional code completes before the ADC is finished sampling, a continuous stream of ADC data
// at the defined sample rate will be available
aaAudio.getADC();

// Sum all the samples into a float
float pinA0Samples = 0.0;
float pinA1Samples = 0.0;
for (int i = 0; i < 32; i += 2) {
pinA1Samples += aaAudio.adcBuffer16[i]; //Samples from highest pin number (A1)
pinA0Samples += aaAudio.adcBuffer16[i + 1]; //Samples from next lowest pin number (A0)
}

// Divide the total by the number of samples
pinA1Samples /= 16.0;
pinA0Samples /= 16.0;

// This will print every second at a sample rate of 32 samples/second
Serial.print("Pin A1 Samples Total / Number of Samples == ");
Serial.println(pinA1Samples);
Serial.print("Pin A0 Samples Total / Number of Samples == ");
Serial.println(pinA0Samples);

}

/*********************************************************/

24 changes: 22 additions & 2 deletions src/AutoAnalogAudio.cpp
Expand Up @@ -77,6 +77,26 @@ void AutoAnalog::triggerADC(){



}

/****************************************************************************/

void AutoAnalog::enableAdcChannel(uint8_t pinAx){

if(pinAx > 6){ return; }
pinAx = 7 - pinAx;
ADC->ADC_CHER |= 1<< pinAx;

}

/****************************************************************************/

void AutoAnalog::disableAdcChannel(uint8_t pinAx){

if(pinAx > 6){ return; }
pinAx = 7 - pinAx;
ADC->ADC_CHDR |= 1<< pinAx;

}

/****************************************************************************/
Expand Down Expand Up @@ -208,15 +228,15 @@ void AutoAnalog::adcSetup(void){
ADC->ADC_CGR = 0x15555555 ; // All gains set to x1
ADC->ADC_COR = 0x00000000 ; // All offsets off

ADC->ADC_MR = (ADC->ADC_MR & 0xFF00FF00) | 1 << 2 | ADC_MR_TRGEN;//& ~ADC_MR_SLEEP & ~ADC_MR_FWUP // 1 = trig source TIO from TC0
ADC->ADC_MR = (ADC->ADC_MR & 0x0F00FF00) | 1 << 2 | ADC_MR_TRGEN;//& ~ADC_MR_SLEEP & ~ADC_MR_FWUP // 1 = trig source TIO from TC0
//ADC->ADC_MR = (ADC->ADC_MR & 0xFF00FF00);
//ADC->ADC_MR |= ADC_MR_LOWRES;
//MR Prescalar = 255 ADCClock == 84mhz / ( (256) * 2) == ?? MIN is 1Mhz
//ADC->ADC_MR |= 5 << 8; //Prescalar ? sets ADC Clock to 1,615,384.6 hz, 5 is 7mhz
//ADC->ADC_MR |= 3 << 20; //Settling time, full is 17ADC Clocks, 411,764.7hz, 9 is 179487.2, 5
//ADC->ADC_MR |= 1; //51470.6hz

ADC->ADC_PTCR=1;
ADC->ADC_PTCR=ADC_PTCR_RXTEN;
}

/****************************************************************************/
Expand Down
28 changes: 28 additions & 0 deletions src/AutoAnalogAudio.h
Expand Up @@ -132,6 +132,21 @@ class AutoAnalog
**/
uint8_t dacBitsPerSample;

/**
* Enable reads from the specified channel (pins A0-A6)
*
* Active ADC Channels will be read in numeric order, high to low
*
* @note Specify pins numerically: 0=A0, 1=A1, etc...
*/
void enableAdcChannel(uint8_t pinAx);

/**
* Disable reads from the specified channel (pins A0-A6)
* @note Specify pins numerically: 0=A0, 1=A1, etc...
*/
void disableAdcChannel(uint8_t pinAx);

/**@}*/

private:
Expand Down Expand Up @@ -262,6 +277,18 @@ class AutoAnalog
* This example demonstrates how to capture a steady stream of ADC data
*
* See AnalogAudio_config.h to change the MAX_BUFFER_SIZE allowing larger chunks
*/

/**
* @example MultiChannelAdcStream.ino
* <b>For Arduino Due</b><br>
*
* * Multi Channel ADC Sampling Example:
*
* This example demonstrates how to capture a steady stream of ADC data on
* multiple channels. Currently pins A0-A6 are supported.
*
* See AnalogAudio_config.h to change the MAX_BUFFER_SIZE allowing larger chunks of data
*/

/**
Expand All @@ -284,6 +311,7 @@ class AutoAnalog
* - Uses DMA (Direct Memory Access) to buffer DAC & ADC data
* - ADC & DAC: 8, 10 or 12-bit sampling
* - Single channel or stereo output
* - Multi-channel ADC sampling
*
*
Expand Down

0 comments on commit c0e4c5b

Please sign in to comment.