cancel
Showing results for 
Search instead for 
Did you mean: 

how to put delay without using timers?

Anand Krishnan
Associate II
Posted on April 06, 2017 at 15:58

.

9 REPLIES 9
Posted on April 06, 2017 at 17:20

Use DWT_CYCCNT ?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Imen.D
ST Employee
Posted on April 06, 2017 at 17:30

Hi

Krishnan.Anand

,

If you are HAL user, you can use HAL_Delay function.

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
S.Ma
Principal
Posted on April 06, 2017 at 19:18

Be more explicit. What is the real need behind? As some say, please elaborate. Making delays without timer can bring exotic answers such as put a cap on a gpio, charge it with the gpio, then switch it as exti input to trigger an interrupt or even a stopped clock restart...

Posted on April 06, 2017 at 17:53

That tends to use a TIM or SysTick, and a software based counter providing millisecond level granularity.

I sense the OP wants software spin loops, or sub-microsecond. The vague 'title as a question' leaves a lot to be desired. Ask better/smarter questions.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
shingadaddy
Senior
Posted on April 06, 2017 at 23:33

For...

While...

Hold the reset button longer....

Exotic yes.

antonius
Senior
Posted on April 07, 2017 at 02:18

That will make a delay :

void Delay(int nTime)

{

    unsigned int i;

    unsigned long j;

    for(i = nTime;i > 0;i--)

        for(j = 1000;j > 0;j--);    

}

or

in CubeMX

1s delay = HAL_Delay(1000);

Posted on April 07, 2017 at 03:31

The use of the volatile keyword is strongly recommended so the compiler/optimizer doesn't fold the loop. These types of loops should generally be avoided

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on April 07, 2017 at 03:35

what does 'volatile keyword' mean ? do you mean nTime ?

 

Posted on April 07, 2017 at 07:07

It's like this, to save bytes and cycles, a compiler can, unless told not to, do what you said instead of what you meant.

In your code you have:

   unsigned int i;

    unsigned long j;

    for(i = nTime;i > 0;i--)

        for(j = 1000;j > 0;j--);    

Once you get to the bottom, all you have really done is set i  and j to 0. So why not just remove the overhead of the for loops and turn those structures into

uint32_t i = 0;

uint32_t j = 0;

Or better yet, since i and j never get referenced, just delete all of the code completely. It has the same net effect. And that is what you said according to the specifications of the language. You just have 4 lines of code that have no visible side effects, so they are all optimized out. Poof.

I wouldn't expect that you believe me, so I went onto gcc.godbolt.org and put in the function:

void fu(void) {

&sharpdefine nTime 200

unsigned int i;

unsigned long j;

for(i = nTime;i > 0;i--)

for(j = 1000;j > 0;j--);

}

Set it up for ARM GCC version 5.4.1 and gave the option -O3. This code got compiled into the assembly code:

 bx      lr

more commonly known as return from subroutine. Poof.

So, if you declare i and j as volatile, you tell the optimizer to keep it's grubby hands off and don't make any assumptions about these variables because they have hidden side effects that the optimizer can't know about. If you run the code through GCC now, yeah, it does what you wanted, and what you said.

Elecia White from the embedded.fm podcast wrote an article on volatile. 

http://embedded.fm/blog/2017/2/23/explaining-the-c-keyword-volatile

 

Andrei