Can ADC running PDB be polled?

Status
Not open for further replies.

Tending

New member
I'm using a Teensy 3.2. I've successfully run an ADC with PDB using interrupts but I'm hoping to poll the ADC directly instead of the adc0_isr. Here's what I've been trying but after the first iteration the value returned is always 1, not something like expected: 1 1 4095 4095. Is it possible to trigger the ADC at precise intervals and poll for the results?

#include <ADC.h>

const uint8_t adc_pin = A9;
const uint8_t out_pin = 23; // put PWM on analog input for testing adc

ADC adc;

void setup() {
pinMode(adc_pin, INPUT);
Serial.begin(9600);
delay(2000);

adc.setAveraging(4);
adc.setResolution(13);
adc.setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED);
adc.setSamplingSpeed(ADC_SAMPLING_SPEED::LOW_SPEED);
adc.adc0->analogRead(adc_pin); // performs various ADC setup stuff

adc.adc0->stopPDB();
adc.adc0->startPDB(1000); // set PDB frequency Hz

analogWriteFrequency(out_pin, 72e6 / pow( (double) 2, 18)); // 274.658203125 ideal frquency per PJRC similar to signal
analogWrite(out_pin, 128); // Full dutty at 255
}

void loop() {
Serial.print("Converting: < ");
Serial.print(adc.adc0->isConverting());
Serial.print(" > ");
Serial.print("Complete: < ");
Serial.print(adc.adc0->isComplete());
Serial.print(" > ");
Serial.print("Error: <");
adc.printError();
Serial.println("> ");

Serial.println(adc.adc0->readSingle());

delay(10);
}

Results:
Converting: < 0 > Complete: < 0 > Error: <>
929
Converting: < 0 > Complete: < 1 > Error: <>
1
Converting: < 0 > Complete: < 0 > Error: <>
1
Converting: < 0 > Complete: < 0 > Error: <>
1
Converting: < 0 > Complete: < 0 > Error: <>
1
Converting: < 0 > Complete: < 0 > Error: <>
1
Converting: < 0 > Complete: < 0 > Error: <>
 
The simplest way is to keep the interrupt approach and inside the interrupt function, write to a volatile variable as your signal to the main program than the ADC has completed another sample. The in the main program, just poll that variable for changes. The main trick needed is "volatile" in the variable declaration, so the compiler doesn't apply optimizations that "know" variables remain the same once written to memory. In this case, from the main program's point of view, the variable does spontaneously change.

Of course there are many other much more advanced ways, like using DMA to transfer many samples into a buffer and then an interrupt to notify you. But best to start simple...
 
Status
Not open for further replies.
Back
Top