3 ms·
When I was an intern at ON Semi, I worked on a testbed that was designed to stress power mosfets with extremely time-accurate pulses of current. The prototype w
by kayson 4y ago
When I was an intern at ON Semi, I worked on a testbed that was designed to stress power mosfets with extremely time-accurate pulses of current. The prototype was using an Arduino microcontroller, and I noticed that when I passed small values to delayMicroseconds(), it was just not accurate at all. After a lot of digging, it turned out that they used a small inline assembly loop to count clock cycles, but they had miscounted the number of clock cycles required by the setup. When you're talking about 2-3us with an 8MHz clock, even half a dozen clock cycles will make a big difference!
- kevin_thibedeau 4y agoArduino is notorious for promoting this sort of synchronous, main-loop only code. It is a horrible model for embedded devices unless they are severely resource constrained.
- gsliepen 4y agoIt's not uncommon to be resource constrained on embedded devices. I actually worked on something quite similar as kayson, helping create a device to synchronize a camera with a LED that was pulsed with a constant current in order to generate photon transfer curves. The pulses had to be as accurate as possible, and indeed doing the inline assembly loop with NOP instructions was the way to go: simple, deterministic, and the possibility to have pulses down to a single clock cycle. I believe PWM wasn't possible because it couldn't have pulses that short and still support the required minimum interval between pulses. But I do agree that it would be much more helpful if tutorials for Arduino would promote using timers and interrupts.
- scoutt 4y agoIf the alternative you propose is RTOS/scheduler then the synchronous main-loop approach for embedded is, by far, more common than the alternative.