Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
shirriff opened this issue Apr 17, 2022 · 1 comment
Open

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

shirriff opened this issue Apr 17, 2022 · 1 comment

Comments

@shirriff
Copy link

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

@shirriff
Copy link
Author

I've found a workaround. Start the timer as follows:

  noInterrupts();
  Timer1.initialize(100);
  Timer1.attachInterrupt(timerInterrupt);
  Timer1.start();
  TIFR1 |= 1;
  interrupts();

Setting TIFR1 clears the pending timer interrupt, which removes one glitch. Calling Timer1.start() ensures that the timer starts from 0, which removes the other glitch. Both are necessary to get the timer interrupt to fire after the specified delay.

Adding the TIFR1 line to the start() method in TimerOne.h might be a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant