Skip to content

unprecisse timing on SAMD21 (Arduino Zero) #18

@daniel-mohr

Description

@daniel-mohr

On an Arduino Zero (SAMD21) using this simple sketch shows 12 us or 13 us instead of 10 us:

#include "uTimerLib.h"

/* serial interface */
#define SERIAL_BAUD_RATE 115200 // baud = bits per second
#define SERIAL_TIMEOUT 1000 // timeout in 1e-6 seconds

#define LED_PIN 0

unsigned long microstime_old = 0;
unsigned long microstime_delta = 0;

void handler (void) {
  digitalWrite(LED_PIN, !digitalRead(LED_PIN));
  const unsigned long mymicros = micros();
  microstime_delta = mymicros - microstime_old;
  microstime_old = mymicros;
}

void setup() {
  // set serial communication:
  Serial.begin(SERIAL_BAUD_RATE);
  Serial.setTimeout(SERIAL_TIMEOUT);
  delay(1000);
  pinMode(LED_PIN, OUTPUT);
  TimerLib.setInterval_us(handler, 10);
}

void loop() {
  const unsigned long mtime = millis();
  Serial.print("mtime: ");
  Serial.print(mtime);
  Serial.print(" SystemCoreClock: ");
  Serial.print(SystemCoreClock);
  Serial.print(" microstime_delta: ");
  Serial.println(microstime_delta);
  delay(1000);
}

A more precise measurement with an oscilloscope shows there is an additional time of about 2.5 us (= 2.5e-6 s). So the handler function is called every 12.5 us instead of every 10 us.

The same is true for longer periods. Also for TimerLib.setInterval_us(handler, 100); I get about 102.5 us instead of 100 us.

Unfortunately I have to say that this is not a hardware problem. With my own trigger implementation I get the setting as measurement.

And further it is not clear for my why uTimerLib shows this behavior. Maybe you have an idea?

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