Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: Can ADC running PDB be polled?

  1. #1
    Junior Member
    Join Date
    Dec 2018
    Posts
    1

    Can ADC running PDB be polled?

    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: <>

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,007
    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...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •