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

Thread: Problems with below 1us IntervalTimer in Teensy 4.0

  1. #1
    Junior Member
    Join Date
    Mar 2020
    Location
    Spain
    Posts
    1

    Problems with below 1us IntervalTimer in Teensy 4.0

    Hi all,

    As this is my 1st post let me introduce myself. I'm CAR, a mechanical engineer struggling to learn embedded coding and some electronics.

    After learning the basics, I proposed myself to sample a 100kHz signal. To test it, I used an internal PWM of my Teensy 4.0.

    To measure time, I use the IntervalTimer function included in Teenduino, nevertheless, whenever I try to sample at a frequency below 1.6us I'm unable to capture any data. Could anybody help me?

    In my opinion, I'd need a sample rate of 1MHz to properly measure the signal that I intend to measure and, as far as I understood the F_BUS is 150MHz in the Teensy 4.0 so It should be more than enough.

    Code:
    // Fast Measure code
    
    #include <ADC.h>
    #include <ADC_util.h>
    
    #define SAMPLES (10000)      // how many samples to combine for pp, std.dev statistics
    
    IntervalTimer myTimer;
    
    const byte readPin = A0; // ADC0
    const byte interruptPin = 18;
    const byte pwmtrig = 6;
    const float init_t = 1.0;
    
    // Volatile variables, change in the interrupt
    
    volatile float x[SAMPLES][2];
    volatile int i;
    volatile float t=init_t;
    
    ADC *adc = new ADC(); // adc object;
    
    void setup() {
    
        pinMode(interruptPin, INPUT_PULLUP);
        pinMode(readPin, INPUT);
        pinMode(pwmtrig, OUTPUT);
        analogWriteFrequency(pwmtrig, 101000);
        analogWriteResolution(8); 
        analogWrite(pwmtrig, 127);
    
        Serial.begin(115200); //Ignored if using USB.
    
        ///// ADC0_Set-up /////
        // This is where we set-up the ADC.
          adc->adc0->setAveraging(1);
          adc->adc0->setResolution(10); // set bits of resolution
          adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED); // change the conversion speed
          adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::VERY_HIGH_SPEED);
        //
        ///// Timer1 Set-up /////
        // Interrupt for signal trigger
        attachInterrupt(digitalPinToInterrupt(interruptPin), TrigMeas, RISING);
    
    }
    void loop(){
      //END OF MEASURE CONDITION
        if (i == SAMPLES) 
          {
            myTimer.end();
            PrintValues();
            i = 0;
            t = 1.0;
          }
    }
    
    void TrigMeas() {
        // Here there is usually a set of intructions. Erased for clarity
          myTimer.begin(Read, init_t); // call every init_t us "ticks"
    }
    
    void Read() {
          x[i][0] = t;
          x[i][1] = analogRead(readPin);
          i = i + 1;
          t = t + init_t;
    }
    
    void PrintValues(){
          Serial.println("Time  Voltage");
          for (int i=0;i<SAMPLES;i++) {
            Serial.print(x[i][0]);
            Serial.print("  ");
            Serial.println(x[i][1]);
          }
    }
    Thank you very much,

    CAR

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    6,306
    There have been a few threads on some of this.

    I have a Pull request: https://github.com/PaulStoffregen/cores/pull/425
    That allows the user to change to use the higher speed bus. To get higher speeds.

    But in the mean time, the Timers magic numbers were changed to allow some faster speeds
    https://github.com/PaulStoffregen/cores/pull/422 (which was merged in).

    I don't remember which build this made it into. But if you have not tried it you might try the latest release, which allows the T4 to have as at least as fast of Interval Timer as T3.2...

    But of course everything is a trade off. That is you go for a faster clock speed than you can get faster timers, but on the other side you can not have one that is as slow... Don't remember the maximum periods of each .

Posting Permissions

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