Latency function error: volatile and volatile

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.