Electronic – Calculating time spent by a function in Microcontroller Applications

armcmicrocontrollerstm32timer

I would like to write a simple profiler for my STM32F100VE microcontroller. I have seen that I can include time.h and based on the c standard functions I can use it, but I was confused about _CLOCKS_PER_SEC_ 1000. I'm not sure if that approach will work or not.

How would one try measure the time a function took in micro-controller application?

Best Answer

The most basic maneuver for complicated functions is to utilize the onboard timers.

Depending on how you set them up, you can get them to count exactly the number of clock cycles it takes for a certain function to execute. Simply read the timer before the function call and then again after the function call.

You will have to look through the assembly code to know how much time to take off the timer for the read calls but if it is a lengthy function or if you don't mind being by a couple of clock cycles then it is ok.

The only error in the conversion from clock cycles back to time is the possible error in the system clock's oscillating frequency. Again, this will be negligible for functions which take significant time to execute.

It sounds like in your case you want something around 30Hz (30 FPS) which is a very long time compared time cycle of the processor clock.