Go Down

Topic: Busy wait loop with interrupts disabled (Read 150 times) previous topic - next topic



I've been searching and found many ways to do a timeout.

*** Obs. I Don't want to use millis, micros, interrupts or timers of any kind or external circuit. So don't bother suggesting it ***

I would like to implement something like:


noInterrupts(); // Something else needed?

unsigned long counter = n_cycles_value;

while (counter-- > 0) // or counter = 0 + while (counter++ < nnn)
   __asm__("nop\n\t"); // 62.5ns at 16MHz
   // + some nops else, upto the correct amount


In order to have a "programmable" predefined precise timeout loop.

The question: Do someone know how to, or can point out how can I obtain the "overhead" cycles time value of the while loop with the unsigned long variable incremented, in order to, adding the 'nop' instructions in a precise quantity, so I can have the timeout loop calculation to the most precise way (what I can get with the arduino ex. a nano) ?

Of course I could experiment with reasonable precision making tests and a chronometer.
That's my plan B



Look at the assembly language code produced by the compiler and count cycles. Of course, it might change if compiler options change or the compiler itself ever updated.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.


Of course, it might change if compiler options change or the compiler itself ever updated.
And that's the problem with the particular method that the OP picked out.  But he doesn't want to hear about anything better, so he's stuck with this. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.


Toggle a pin and measure the frequency with a frequency counter or oscilloscope. The pin toggle can be done in 1 instruction so it can be replaced with a NOP (or left in place) after tuning.

Deciding not to use the hardware timers is like buying a sailboat and deciding not to use the sails. That is what they are there for.
"The problem is in the code you didn't post."


Thanks for the suggestions!
I'll continue from that.

Best regards

Go Up