Debugging this has been troubling me a great deal now at this point. I've written a very basic script which polls in `loop()` to create a 200Hz square wave with 10us high time.
My logic analyzer (saleae logic pro 8) reports f_mean 200.000503 Hz with 3.27ns standard deviation for the first 40 seconds
after which this happens
zooming in on the spurious measurements reveals
This has really been gnawing at me, I simply cannot fathom how this could be happening with such a basic script. Cables are short and far from external, powered devices. So I figure it's not an interference issue. Pins are soldered to the teensy so there should be any possibility for movement. Note also, logic analyzer sampling at 500 MS/s
Here is the code I ran, I didn't clean it for sake of not accidentally removing something which could somehow affect the performance in a surprising way.
My logic analyzer (saleae logic pro 8) reports f_mean 200.000503 Hz with 3.27ns standard deviation for the first 40 seconds
after which this happens
zooming in on the spurious measurements reveals
This has really been gnawing at me, I simply cannot fathom how this could be happening with such a basic script. Cables are short and far from external, powered devices. So I figure it's not an interference issue. Pins are soldered to the teensy so there should be any possibility for movement. Note also, logic analyzer sampling at 500 MS/s
Here is the code I ran, I didn't clean it for sake of not accidentally removing something which could somehow affect the performance in a surprising way.
Code:
#include <Arduino.h>
const uint8_t _pin_out_ = 2;
const uint8_t _pin_gnd_ = 1;
elapsedMicros g_rising_edge_timer;
elapsedMicros g_falling_edge_timer;
uint32_t g_rising_edge_cycles;
uint32_t g_falling_edge_cycles;
uint32_t threshold;
bool g_is_high{false};
uint32_t g_interval = 5000 * (F_CPU / 1000000); // 5ms
uint32_t g_width = 10 * (F_CPU / 1000000); // 10us
void setup()
{
pinMode(_pin_gnd_, OUTPUT);
digitalWriteFast(_pin_gnd_, LOW);
pinMode(_pin_out_, OUTPUT);
digitalWriteFast(_pin_out_, LOW);
if (ARM_DWT_CYCCNT == ARM_DWT_CYCCNT) { // Enable CPU Cycle Counter
ARM_DEMCR |= ARM_DEMCR_TRCENA; // enable debug/trace
ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA; // enable cycle counter
}
Serial.begin(9600);
while (!Serial) {
}
g_rising_edge_timer = 0;
g_falling_edge_timer = 0;
g_rising_edge_cycles = ARM_DWT_CYCCNT;
}
void loop()
{
// if (g_rising_edge_timer >= g_interval) {
// digitalWriteFast(_pin_out_, HIGH);
// g_rising_edge_timer -= g_interval;
// g_falling_edge_timer = 0;
// // threshold = g_falling_edge_timer + g_width;
// g_is_high = true;
// }
// if (g_is_high && (g_falling_edge_timer >= g_width)) {
// // if (g_is_high && (g_falling_edge_timer >= threshold)) {
// digitalWriteFast(_pin_out_, LOW);
// g_is_high = false;
// }
if (ARM_DWT_CYCCNT - g_rising_edge_cycles >= g_interval) {
digitalWriteFast(_pin_out_, HIGH);
g_is_high = true;
g_rising_edge_cycles = ARM_DWT_CYCCNT;
g_falling_edge_cycles = g_rising_edge_cycles;
}
if (g_is_high) {
if (ARM_DWT_CYCCNT - g_falling_edge_cycles >= g_width) {
digitalWriteFast(_pin_out_, LOW);
g_is_high = false;
}
}
}
Last edited: