Go Down

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

mad_b

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

:-)
Thanks

gfvalvo

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.

Delta_G

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.

MorganS

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."

mad_b

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

Best regards
Lissandro

Go Up