Skip to content

Two glitches at start of timer on Teensy 2.0++ #56

@shirriff

Description

@shirriff

Description

On the Teensy 2.0++, the timer library calls the interrupt handler twice immediately after starting, before settling down to the specified rate.

Steps To Reproduce Problem

Start the timer and look at when the timer interrupt is called. See the sketch below.

Expected behavior: the timer should wait the specified interval and then call the timer interrupt periodically.
Observed behavior: the timer interrupt is immediately called twice and then it starts working as expected.

Hardware & Software

Board: Teensy 2.0++
Shields / modules used: none
Arduino IDE version: 1.8.19
Teensyduino version (if using Teensy): 1.56
Version info & package name (from Tools > Boards > Board Manager)
Operating system & version: macOS Monterey 12.3..1 on MacBook Air

Note: I opened issue #55 for a timer problem on the Teensy 3.6. This is a different issue on the Teensy 2.0++.

Arduino Sketch

#include <TimerOne.h>
#define PIN_DEBUG2 7
#define PIN_DEBUG 6

void timerInterrupt() {
  // Generate a debugging pulse
  digitalWriteFast(PIN_DEBUG, HIGH); delayMicroseconds(5); digitalWriteFast(PIN_DEBUG, LOW);
}

void setup() {
  pinMode(PIN_DEBUG, OUTPUT);
  pinMode(PIN_DEBUG2, OUTPUT);
  digitalWriteFast(PIN_DEBUG2, HIGH); delayMicroseconds(5); digitalWriteFast(PIN_DEBUG2, LOW);
  Timer1.initialize(100);
  Timer1.attachInterrupt(timerInterrupt);
}

void loop() {}

Errors or Incorrect Output

In the oscilloscope trace below, the yellow pulse is when setup() is called. The green pulses are when timerInterrupt() is called.

Expected behavior: a green pulse 100 microseconds after the yellow pulse, followed by green pulses every 100 microseconds.
Observed behavior: two green pulses immediately after the yellow pulse, followed by the correct pulses every 100 microseconds.

Oscilloscope trace

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions