Go Down

Topic: Function Micros and precision (Read 779 times) previous topic - next topic

Loutchiano

Hello everyone,

I read on arduino.cc/en/reference/micros that I can know the number of microseconds since the Arduino  began running the program.
On my 16 MHz Arduino Uno board, the function has a resolution of four microseconds and less with a 8 MHz board. I found the arduino Due (arduino.cc/en/Main/ArduinoBoardDue)  and it has a 84 MHz clock speed.

Could I have a better resolution than 4 ┬Ás ? I would like to have a precision with nanos seconds.


MarkT

The code for micros() is quite complex on the Due, I suspect it won't give much better
accuracy.  However the underlying tick count is available fast, its just the accounting
for its overflow that is tricky.
[ I won't respond to messages, use the forum please ]

panther3001

#2
Feb 26, 2014, 06:27 am Last Edit: Feb 26, 2014, 06:29 am by panther3001 Reason: 1

Hello everyone,

I read on arduino.cc/en/reference/micros that I can know the number of microseconds since the Arduino  began running the program.
On my 16 MHz Arduino Uno board, the function has a resolution of four microseconds and less with a 8 MHz board. I found the arduino Due (arduino.cc/en/Main/ArduinoBoardDue)  and it has a 84 MHz clock speed.

Could I have a better resolution than 4 µs ? I would like to have a precision with nanos seconds.




I have written a code to get resolution on a standard Arduino board (Uno, Nano, Ethernet, etc) down to 0.5us.  It is super easy to implement.  The code and an example can be found here: http://electricrcaircraftguy.blogspot.com/2014/02/Timer2Counter-more-precise-Arduino-micros-function.html

I chose to use the Atmega328 Timer2 (note: it has 3 timers) in order to preserve the functionality of the Servos library, which uses Timer1.  However, if you follow my example of what I am doing to Timer2, and apply it to Timer1, you can get a good timer with resolution down to 62.5 nanoseconds.  As a matter of fact, Timer2 can also have this resolution, but the counter will quickly overflow since it is only 8-bit.  In either case, check out what I've done, and see what you think.

PS. Here is my description of my code:

"I wrote a "libary" to get 0.5us precision on a "micros()" replacement function, so that I can get repeatable results reading a PWM or PPM signal, to within 1us.  I searched all around the internet and could not find something comparable (or that was easy to use, and maintained the Arduino's ability to write PWM signals via the Servo Libary), so I think this is my first real contribution to the world of Arduino and Radio Control." 
http://electricrcaircraftguy.blogspot.com/2014/02/Timer2Counter-more-precise-Arduino-micros-function.html
Visit my RC aircraft blog at http://electricrcaircraftguy.blogspot.com/

panther3001


The code for micros() is quite complex on the Due, I suspect it won't give much better
accuracy.  However the underlying tick count is available fast, its just the accounting
for its overflow that is tricky.


I am also wondering what the precision is of the micros() function when used on the Due.....
Visit my RC aircraft blog at http://electricrcaircraftguy.blogspot.com/

Go Up