Busy wait loop with interrupts disabled

Hello.

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
asm("nop\n\t");
asm("nop\n\t");
// + 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

:slight_smile:
Thanks

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.

gfvalvo:
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.

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.

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

Best regards
Lissandro