Skip to main content
orn
Associate III
October 25, 2012
Question

delay function and execution time

  • October 25, 2012
  • 3 replies
  • 1125 views
Posted on October 25, 2012 at 09:18

Hi, I wanted to create a delay function, and I thought of something classic like this:

whlile(n_count){
n_count--;
}

but how do you know how long it loses precisely my function? I believe that there should be a link between the value of n_count and sysclock but do not know which one. I would also like to understand how can I calculate how long it takes each instruction to be executed by STM32F4 and how long it takes my development board to run the entire program! anyone know how to do these things? #hll-timing-loop---just-say-no
    This topic has been closed for replies.

    3 replies

    Tesla DeLorean
    Guru
    October 25, 2012
    Posted on October 25, 2012 at 13:04

    Software delay loops are generally to be avoided. You could look at the code/instructions generated, there should be a y = mx + c type relationship with the core clock, where m is the number of cycles consumed per loop iteration, and c is the call/setup/return overhead.

    You could use the cycle counter in the trace unit to a) calibrate/benchmark, or b) act as a delta comparison for time.

    https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/DispForm.aspx?ID=11943&RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Duration of FLOAT operations

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Andrew Neil
    Super User
    October 27, 2012
    Posted on October 27, 2012 at 21:45

    ''but how do you know how long it loses precisely my function?''

     

     

    You cannot predict the timing of a High-Level Language. The best you can do is to compile it, and then inspect the generated code.

    http://www.8052.com/forum/read/162556

    Note that compiler optimisation can greatly affect the generated code - to the point of nothing being generated at all!

    If you really want to write a timing loop of known duration, you will have to write it in assembler.

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    orn
    ornAuthor
    Associate III
    October 28, 2012