Dennis-van-Gils / SAMD51_InterruptTimer

SAMD51 Interrupt Timer library for e.g. Adafruit M4 Metro/Feather/ItsyBitsy Express
MIT License
25 stars 7 forks source link

I don't think your example is showing you deltas correctly #8

Closed squeedee closed 3 years ago

squeedee commented 3 years ago

https://github.com/Dennis-van-Gils/SAMD51_InterruptTimer/blob/a1a2bee95ee18d94ca3c417671385c8d717b6613/examples/TC3_timer/TC3_timer.ino#L11

Do you really mean = now

I ran it without it, and I get even more stable timing.

Dennis-van-Gils commented 3 years ago

Yes, I really mean = now. Notice the static identifier. This line is only executed once at start-up and primes prev-tick with a value for the first evaluation of dT. After the first evaluation that line 11 is not executed anymore (or so it should be by the C-compiler), so I find it peculiar if you would notice any change in stability of the timing by removing line 11. What kind of improvement in stability are we talking about? Can you quantify it for me? I don't have an Arduino right now to test it myself.

squeedee commented 3 years ago

Wow ok that's on me, I thought the static var's had to be initialized outside a function for that behavior. I don't do much C these days, my apologies.

squeedee commented 3 years ago

As for the stability it was literally the difference between always '1000' (without the init) and a small 1 or 2 uSec drift with the init. I only observed it with a sample size of one run, so it could easily have been some other factor. I think you're safe to ignore this, and the next time I have the device in front of me I'll recheck my assumptions.

Dennis-van-Gils commented 3 years ago

Glad it helped you. And a jitter of around a microsecond seems to be in spec for many boards. Notice also that we're printing to the serial port in this demo (From inside of the main loop! Don't print from inside of the ISR, unless you like undefined behavior). If that port has support for hardware serial communication it will internally rely on interrupt routines, too. These, in turn, could also potentially affect your ISR. For more info on the topic, see https://arduino.stackexchange.com/questions/30968/how-do-interrupts-work-on-the-arduino-uno-and-similar-boards