Skip to content

First cycle after start() is half the specified interval on Teensy 3.6 #55

@shirriff

Description

@shirriff

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.

DS1Z_QuickPrint5

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.

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