The micros() documentation notes that the return value will always be a multiple of 4.
Is there any way to get a higher resolution microsecond click, preferably down to the 1 microsecond level?
Dropping down to the AVR level is acceptable.
arduinotiming
The micros() documentation notes that the return value will always be a multiple of 4.
Is there any way to get a higher resolution microsecond click, preferably down to the 1 microsecond level?
Dropping down to the AVR level is acceptable.
Best Answer
Yes, depending your Arduino's basic clock rate. For example here are the counter-timer input frequencies and periods after pre-scaling, for an ATMega2560's counter-timer 2, and a basic clock rate of 16MHz. The timer has built in "prescaler" value options which determine frequency/period, shown in this table:
For better timing resolution, you use a value called TCNT2. There is build in counter that goes from 0 to 255 because the timer is 8 bit. When the counter reaches the value assigned by TCNT2 it triggers an interrupt. This interrupt is called TIMER2_OVF_vect.
given this information, the resulting interrupt rate would be: 16MHz / (prescaler * (255 - TCNT2))
You could get the timer to run at the full 16MHz (62.5nSec) though that's way faster than you need; 2MHz with an initial count of (255-2) would give you 1MHz interrupt rate. Divide that by 2 in your ISR:
The data sheet for your MCU is the basic resource; this article will give you (and gave me!) a good head-start.