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

Thread: How can I make interrupts (input and output) faster? Teensy 3.2

  1. #1
    Junior Member
    Join Date
    Oct 2021
    Posts
    1

    How can I make interrupts (input and output) faster? Teensy 3.2

    Hello,

    I'm working on a project which includes a Teensy 3.2 and another device. The other device sends 400 times per second high voltage ('1') to the Teensy 3.2 and for each interrupt of a high voltage(rising) this happens:
    i++;
    serial.println(i);

    The thing is, if the rate is 200times per second, it sends one number each time (video number 1). If the rate is 400times per second, it sends the whole 400 numbers each time (which is not good for me) (video number 2).

    1.Anyone knows how can I fix it?
    2.Is this the fastest method I can use for interrupts?

    The code:
    define input 1
    int i = 0;

    void setup()
    {
    pinMode(input, INPUT);
    attachInterrupt(input, srService, RISING);
    }

    void loop()
    {
    }

    void isrService()
    {
    cli();
    Serial.println(i);
    i++;
    sei();
    }

    Video 1: https://vimeo.com/638341671
    Video 2: https://vimeo.com/638341686?from=outro-local


    Thanks!

  2. #2
    From what's posted on the JJRC website, the Teensy 3.2 can run about 1.25 million interrupts per second.

    I think you have a few issues
    1) not sure why you are disabling the interrupt inside the ISR--never seen this approach, but feels wrong
    2) adding println inside your ISR is super slow and is what's probably causing misreads.

    I would have only i++ in the ISR and move the println stuff into loop with some code to print every second or so.
    Also using int for i will reset counting after 80 seconds or so maybe use unsigned long i; ?

  3. #3
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,766
    If a variable is used both within an ISR and outside it, it must be declared volatile.
    Try this code, which incorporates @KrisKasprzak suggestions and a couple of my own.
    Code:
    volatile uint32_t i = 0;
    volatile uint8_t i_flag = 0;
    
    void setup()
    {
      pinMode(input, INPUT);
      attachInterrupt(input, isrService, RISING);
    }
    
    void loop()
    {
      if(i_flag) {
        Serial.println(i);
        i_flag = 0;
      }
    }
    
    void isrService()
    {
      i++;
      i_flag = 1;
    }
    Pete

  4. #4
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,766
    I'm just trying my version of the code with a timer interrupting at 200 or 400/sec and I see the same behaviour that you do.
    I suspect that it is something to do with USB buffering between the Teensy and the PC.

    Pete
    Code:
    #include <TimerOne.h>
    
    volatile uint32_t i = 0;
    volatile uint8_t i_flag = 0;
    
    void setup()
    {
      Timer1.initialize(1000000/400);
      Timer1.attachInterrupt(isrService);
    }
    
    void loop()
    {
      if(i_flag) {
        Serial.println(i);
        i_flag = 0;
      }
    }
    
    void isrService()
    {
      i++;
      i_flag = 1;
    }

Posting Permissions

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