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

Thread: T3.6 Analog controlled samplin.

  1. #1
    Junior Member
    Join Date
    Jan 2020

    T3.6 Analog controlled samplin.

    Hello, Im trying to control the sampling rate of the Teensy with an analog signal so that Im always sampling at 1000 x the frequency of my analog control signal. To do that I wanted to use interrupts but cant get them to work for this application. Using an interrupt timer works with my ISR with no issues at 60 kHz (16 microsecond interval timer) but I encounter 2 issues :
    1. Feeding a changing wave into the interrupt pin does not trigger the interrupts.
    2. When i force an interrupts by shorting the pin to ground (which somehow works) the values stored in my buffer are all identical. This does not happen with an interval timer.

    Any help is greatly appreciated.

    #include "ADC.h"
    #include "RingBuffer.h"
    #include "array"

    const uint8_t adc_pin = A0;
    const uint8_t out_pin = A21;
    const uint8_t out_pin_av = A22;
    #define PinInt1 13

    ADC *adc = new ADC();
    std::array<volatile uint16_t, 1000> buffer;
    volatile size_t write_pos = 0;

    volatile uint16_t loop_count = 0;

    volatile uint16_t adc_val = 0;
    volatile uint16_t test =0;

    void setup(){


    pinMode(adc_pin, INPUT);

    adc->adc0->setAveraging(1); // set number of averages
    adc->adc0->setResolution(12); // set bits of resolution
    adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::MED_SPEE D); // change the conversion speed
    adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::MED_SPEED) ; // change the sampling speed
    adc->adc0->analogRead(adc_pin); // performs various ADC setup stuff

    pinMode(PinInt1, INPUT);

    attachInterrupt(PinInt1, isrService, CHANGE);

    void loop()


    void isrService()
    size_t write_pos_ = write_pos;
    size_t loop_count_ = loop_count;
    buffer[write_pos_] = adc->adc0->readSingle();
    //test = adc->adc0->readSingle();
    if(write_pos_ >= buffer.size())
    write_pos_ = 0;
    loop_count = loop_count_;
    write_pos = write_pos_;



  2. #2
    Junior Member
    Join Date
    Feb 2020
    I'm assuming you are using the ADC library that Pedvide shared in this thread:

    I notice that you are using readSingle(), which is not listed in the documentation for that ADC library as being interrupt-safe:

    That could be interfering with anything else that uses interrupts or relies on a shared resource. Does changing it to use "analogRead()" from the same ADC library help? That should be interrupt-safe for capturing a read of the analog pin, although I can't say for sure that that is causing either of your issues, just something to check.

    You'll likely need to check for any other potential interrupt conflicts for the Ringbuffer.h library. I couldn't find the source code (I'm at work), but make sure that (1) the other library is interrupt safe and/or that you are using interrupt-safe calls for that library, and (2) that there aren't conflicts in using any associated interrupts (there may be interrupt priority/masking or timing issues). It is hard to say without looking at the source code for RingBuffer.h.

    Can you give some more information about the waveform being input to the analog pin and the hardware you are connecting? I'm wondering if there is a timing or an impedance issue with your input signal that is preventing the interrupts from triggering.

    Are the grounds of the two systems able to be tied together? If they are separate that might explain why the interrupt triggers when shorted to the Teensy ground pin but not when you use your control signal.

    I hope something here helps.

Posting Permissions

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