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

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

Open
shirriff opened this issue Apr 5, 2022 · 0 comments
Open

Comments

@shirriff
Copy link

shirriff commented Apr 5, 2022

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.

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