Latency function error: volatile and volatile
An inexplicable error. Latency caused by the use of javasick.
The initialization is like this:
// SysTick Configuration
SysTick_CLKSourceConfig (SysTick_CLKSource_HCLK_Div8 );
If (SysTick_Config (SystemCoreClock/1000) // enable clock interruption and configure Clock.
{
While (1 );
}
Then:
// Latency function. 1 nTime is equivalent to 1 ms.
Static uint32_t TimingDelay;
Void Delay (_ IO uint32_t nTime)
{
TimingDelay = nTime;
While (TimingDelay! = 0 );
}
Void TimingDelay_Decrement (void)
{
If (TimingDelay! = 0x00)
{
TimingDelay --;
}
}
Everything is normal, and there is no problem with the code.
But something is wrong today.
While (TimingDelay! = 0 );
When TimingDelay is reduced to 0, it still does not exit. Delay failed. Something inexplicable.
Then I discussed the problem in the group and found the problem.
This is optimized by the compiler.
Define static uint32_t TimingDelay;
Change to static _ IO uint32_t TimingDelay = 0;
That is, static volatile uint32_t TimingDelay = 0;
That's all.
Volatile is used as a command keyword to ensure that this command is not omitted due to compiler optimization and requires that the value be read directly each time.