Go Down

Topic: Measuring Execution Time for a Piece of Code (Read 1 time) previous topic - next topic


Aug 20, 2015, 02:51 am Last Edit: Aug 20, 2015, 02:52 am by bobloblaw651
Hi all,

I am working on developing some code for the Arduino Due to sample a digital pin every 250ns (21 cycles), but I am not sure how to verify how long the code is currently taking to execute.

The code that I am interested in is written in inline assembly and I have been using nops to adjust the length of time taken by the code (it is somewhere under 250ns currently and I would like to make it take exactly 250ns +/- clock jitter (21 cycles)).

I have tried using the micros() method to time the code, but the resolution I need is beyond the scope of that function.

I have also tried setting a digital pin high while the code is executing and measuring the time on an oscilloscope, however, I believe the compiler is rearranging some of the code resulting in an unreliable measurement of the timing.

Are there any other methods that you are aware of that would enable me to time this code down to a 1cycle (11.9...ns) resolution. Or some way to prevent the compiler from messing with the assembly code.

Your help and advice is greatly appreciated. Thanks so much


Instead of using a loop why not use an interrupt on one of the DUE's timers? That way it won't matter  how long the code takes to execute.



Aug 20, 2015, 06:43 pm Last Edit: Aug 20, 2015, 07:29 pm by bobloblaw651
Hi Jeff,

Thanks for the suggestion. I have actually already looked into using this library. As far as I can tell the maximum possible frequency (set a timer to execute a single instruction) is limited somewhere below 4 MHz which is what I need to get for this as I need to sample a pin to collect data from a sensor and I don't want to miss any bits.

If you know of any way to get the cycling frequency >4 MHz that would be awesome.

Thanks again for the quick response.

UPDATE: I did some quick testing and it seems the library (in its current state) can only do frequencies up to 1 MHz (Period of 1 us)
Also the code of interest is a single piece of code in a much larger loop and so as a result using the timer method would not be a good solution.

Ideally I would like to get this done by writing the appropriate number of NOP's to occupy the time until the next piece of code has to run, but I don't know how to figure out how many NOP's I need to do that.



could you do 1000 reads and measure that time?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up