2017-04-06 06:58 AM
.
2017-04-06 08:20 AM
Use DWT_CYCCNT ?
2017-04-06 08:30 AM
Hi
Krishnan.Anand
,If you are HAL user, you can use HAL_Delay function.
Imen
2017-04-06 10:18 AM
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...
2017-04-06 10:53 AM
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.
2017-04-06 02:33 PM
For...
While...
Hold the reset button longer....
Exotic yes.
2017-04-06 05:18 PM
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);
2017-04-06 08:31 PM
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
2017-04-06 08:35 PM
what does 'volatile keyword' mean ? do you mean nTime ?
2017-04-07 12:07 AM
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 200unsigned 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