Teensy 3.6 Dual ADC multiple channel reads

Status
Not open for further replies.

mranky

New member
Hi all,
I am working on a project which required me to read around 10 sensors as fast as possible. For this purpose I am thinking of using the two ADCs of teensy and using 5 channels of each and sequentially reading each channel. After going through pedivide's ADC library examples and many forum posts on the ADC configuration, I have written an example sketch which reads 1 channel of each ADC simultaneously and calculates the time taken for the reads in contrast to direct analog read.

Code:
#include<ADC.h>
#include<ADC_util.h>

const int readPin1 = A9; //ADC0
const int readPin2 = A16; //ADC1

ADC *adc = new ADC();

void setup() {
  // put your setup code here, to run once:
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(readPin1, INPUT);
  pinMode(readPin2, INPUT);

  Serial.begin(9600);

  adc->adc0->setAveraging(16);
  adc->adc0->setResolution(16);
  adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED_16BITS);
  adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::HIGH_SPEED);

  adc->adc1->setAveraging(16);
  adc->adc1->setResolution(16);
  adc->adc1->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED_16BITS);
  adc->adc1->setSamplingSpeed(ADC_SAMPLING_SPEED::HIGH_SPEED);

  adc->adc0->startSingleRead(readPin1);
  adc->adc1->startSingleRead(readPin2);
  

}

int val1,val2;

void loop() {
  // put your main code here, to run repeatedly:

  uint32_t start = millis();
  uint32_t ctr = 0;

//  for(int i = 0; i < 10000; i++)
//  {
//    
//    val1 = adc->adc0->analogRead(readPin1);
//    val2 = adc->adc1->analogRead(readPin2);
//  }
//Gives around 1268 us for 10 reads

  while(ctr < 10000) 
  {
    if(adc->adc0->isComplete())
    {
      val1 = adc->adc0->readSingle();
      adc->adc0->startSingleRead(readPin1);
      ctr++;
    }
    else if(adc->adc1->isComplete())
    {
      val2 = adc->adc1->readSingle();
      adc->adc1->startSingleRead(readPin2);
      ctr++;
    }
    else
    {
    }
  }
//Gives around 315 us for 10 reads

  uint32_t end = millis();

  Serial.print(end - start);
  Serial.println(" MicroSeconds for 10 readings");

  digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));

  delay(50);
}

I was wondering if the approach I have used here is correct for simultaneously using both ADCs. To scale this code for multiple channels I will need to have a complex nested if .. else which determines the pin on which to start the next read. Is there a better approach to accomplish this, I tried looking into interrupts for telling when a conversion was completed but I couldnt understand how I could scale it for multiple channels. All suggestions are welcome as this is my first time working with ADCs.
Thank you!
 
Status
Not open for further replies.
Back
Top