Timing

The delay() and delayMicroseconds() functions allow us to pause program execution for times greater than one microseconds. But seeing as Arduinos run @ 16MHz, is there a way I can delay for less than one microsecond? In theory, 1/16Mhz = 62.5 nanosecond period, so I should be able to accurately use delays of ~65ns.

If I can do this, I am thinking of making a more accurate tone() frequency that accepts floating point frequencies.

And:

Before you yell at me for this, I realize that 1us= 1Mhz, which we can't hear. But a function such as the one I hope to use would give more precision, i.e If I need a delay of 1.5us, I can just do 1500ns.

Thanks!

baum

If I need a delay of 1.5us, I can just do 1500ns.

24 NOPs

So a NOP (No Op Performed) essentially is 1 instruction, and the arduino can do 16MIPS. How do implement this?

You put 24 NOPs into your program at the point you need a 1599ns pause.

But how do I do a nop?

You don't, the processor does - use "asm"

Found it on the playground.

  __asm__("nop\n\t");

I should be able to accurately use delays of ~65ns.

If you mean granularity, ie time increments smaller than ms you are right. If you need accuracy, ie time delays of a precisely defined duration just be aware that the accuracy of the 16-ish MHz oscillator is not defined anywhere. Do not count on anything far better than 2%. See below for a recent discussion of the oscillator accuracy. http://arduino.cc/forum/index.php/topic,53946.0.html

2% of 62.5ns +/-1.25 ns.... I can live with that.

24 nops will not be always as precise as you want to unless you disable interrupts.

Using noInterrupts()? or clj() and sei()?