Using teensy 3.6 to sample at 1-us intervals within a for loop

Status
Not open for further replies.

Kenneth

New member
Greetings, I am attempting to use the teensy 3.6 and the ADC library to sample an analog signal (from a photodiode circuit) at roughly 1us intervals and 100 samples per measurment (exact timing would be great). Because my sampling routine utilizes a 'for loop', I think I need to use interrupts. I have attempted to mimic PEDVIDE's great examples for his ADC, but am still having issues. I have attached a truncated version of the codeand am hoping someone might be able to steer me in the correct direction. Thanks in advance for your help!

Regards,

Kenneth D. Hoadley



Code:
#include <ADC.h>
#include <ADC_Module.h>
#include <RingBuffer.h>
#include <RingBufferDMA.h>
#include <SD.h>
#include <SPI.h>
#include <TimeLib.h>
#include <string.h>


int AUTO = 1; //0 sets single aquisitions, 1 sets auto aquistion.
const int BEEFY = 2000;  //Size of arrays to be used to collect samples. 
int REP = 10;  //Number of repetitions of the full protocol
int del = 300;  //Delay between flashes in protocol
const int SIG = A0;   //Analog channel used for reading fluorescence signal
ADC *adc = new ADC(); // adc object
int sample = 500;  //Size of array dictates number of samples taken/ length of flash. Each sample is 0.66 useconds
int initial = 100; //Number of samples to be recorded prior to flash protocol. this is used to normalize F0.
int time[BEEFY]; //Time constant. Adjusts automatically to size of array/number of samples.
float TIMEX[BEEFY];  //Time constant. Adjusts automatically to size of array/number of samples.
float timepersample; //Time constant. Adjusts automatically to size of array/number of samples.
float sampletimey;

//FOR BLUE LED EXCITATION READINGS

float BLUERef[BEEFY]; // variable to store the value coming from Reference Channel
float BLUESig[BEEFY]; // variable to store the value coming from Fluoresence Channel
float BLUEAveRef[BEEFY]; // Average Reference readings based on each REP
float BLUEAveSig[BEEFY]; // Average Fluorecence readings based in each REP
float BLUENorm[BEEFY]; // Normalize Fluorescence readings by Reference.
float BLUEPreS; //Average Fluorsence reading prior to excitation
float BLUEPreR; //Average Reference reading prior to excitation
float BLINREG[5]={0,0,0,0,0}; //Array to store initial Lin Regression values and readings from average and normalized data.
float BLUEfit[BEEFY]; //variable to store fit curve

float result[BEEFY]; //SET up for simulatneous ADC reading

int i=0;
int t=0;
int g=0;

const int blue = 4; //digitalWrite pin for UV LED excitation -blue-label-in-code-

void setup() 
{
     Serial.begin(9600);
     pinMode(blue, OUTPUT);
     pinMode(SIG, INPUT);
     analogWriteResolution(12);
   
     adc->enableInterrupts(ADC_0);
     adc->setAveraging(1);
     adc->setResolution(12);
     adc->setConversionSpeed(ADC_CONVERSION_SPEED::MED_SPEED); // change the conversion speed
     // it can be any of the ADC_MED_SPEED enum: VERY_LOW_SPEED, LOW_SPEED, MED_SPEED, HIGH_SPEED or VERY_HIGH_SPEED
     adc->setSamplingSpeed(ADC_SAMPLING_SPEED::MED_SPEED); // change the sampling speed

     adc->startSingleRead(SIG, ADC_0); 

     //timeSave();
}


void loop() 
{   
   if (AUTO == 0)
   {
       //serialInput(); //search for serial input of new variables
       //timeSave();
   }
   if (AUTO == 1)
   {   
       //serialInput();     //search for serial input of new variables
       //timeSave();        //syncs sd and time
       data();            //data aqcuisition
       //curvefitter();     //runs curve fit protocol
       //CURVEdataPrint();  //print normalized induction curves to serial
       //PHOTOdataPrint();  //print calculated photochemical values to serial
       //dataSav();         //write to SD card 
       //feedback();        // converts fvfm into analog output
   }  
}


void data()
{   //Serial.println(dataFile);
    for (int R = 0; R < REP; R++)
    {         
              //BLUE
              for (i=0;i<initial;i++)
              { 
                result[i]=adc->readSingle(ADC_0); //test
                BLUEPreS += (uint16_t)result[i];
                BLUEPreR += 1;
              }
              digitalWriteFast(blue, HIGH);
              for (i=0;i<sample;i++)
              {
                time[i]=micros();
                result[i]=adc->readSingle(ADC_0);
                BLUESig[i] += result[i];
                BLUERef[i] += 4095;
                //delayMicroseconds(1);
              } 
              digitalWriteFast(blue, LOW);
              timepersample += (time[sample-1]-time[0]);  
              delay(del);
    }
}

void adc0_isr() 
{
        adc->readSingle(ADC_0);
}
 
Status
Not open for further replies.
Back
Top