-
Notifications
You must be signed in to change notification settings - Fork 214
Description
Description
On the Teensy 3.6, the first timer interrupt happens after half the specified interval. Subsequent interrupts happen at the right time.
Steps To Reproduce Problem
To reproduce, start a timer and measure the time until the first interrupt. The code below reproduces the problem, generating pulses on pin 6.
Hardware & Software
Board: Teensy 3.6
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
Any other software or hardware? Rigol DS1054Z scope
Arduino Sketch
#include <TimerOne.h>
#define PIN_DEBUG 6
void timerInterrupt() {
digitalWrite(PIN_DEBUG, HIGH);
delayMicroseconds(50); // Small delay so the pulse shows up on the oscilloscope
digitalWrite(PIN_DEBUG, LOW);
}
void setup() {
pinMode(PIN_DEBUG, OUTPUT);
Timer1.initialize(1000); // Timer should fire every 1000 microseconds
Timer1.attachInterrupt(timerInterrupt);
digitalWrite(PIN_DEBUG, HIGH); // Pulse to indicate the timer start time.
delayMicroseconds(50);
digitalWrite(PIN_DEBUG, LOW);
Timer1.start();
}
void loop() {}
Errors or Incorrect Output
In this oscilloscope trace, the first interval is 500 microseconds, while the following intervals are 1 millisecond.
Expected behavior: all intervals are 1 millisecond.
I think the underlying cause of the problem is that the timer uses CPWMS mode, which runs the timer up and down. The first interrupt fires after the timer goes up, while subsequent interrupts fire after the timer goes down and then up, which takes twice as long.
