This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to create a steady delay?

Hi,

My application runs on a STM32F103ZE uC. The application needs to light up some indicator Leds for a fixed amount of time just to indicate a correct startup. This amount of time should be system clock (internal SYSCLK signal) independent and also optimization level independent.

As it is a startup indicator it should be a simple mechanism so my first idea is using a simple 'for' loop.

The SYSCLK and internal prescalers AHB/APB1/APB2 are all initialized using '#defines' so I can calculate the internal code execution speed and define a corresponding relative for-loop value from it to keep the loop execution at a fixed speed.

But the optimization level may be of influence.

My question is: how can I disable the optimization (set it to level 0) only for a small piece of code? I mean, after execution of the startup Led pattern the optimization should be automatically set back to the value set by the Project.

Thanks

Henk

Parents
  • "As it is a startup indicator it should be a simple mechanism so my first idea is using a simple 'for' loop."

    "But the optimization level may be of influence."

    Why do you think a for loop is a simple way to get a stable delay that isn't affected by compiler settings, compiler version, ...?

    What do you think the timers in the chip are intended for? Nice beautiful hardware that may dutifully count time while your program is busy doing other things...

Reply
  • "As it is a startup indicator it should be a simple mechanism so my first idea is using a simple 'for' loop."

    "But the optimization level may be of influence."

    Why do you think a for loop is a simple way to get a stable delay that isn't affected by compiler settings, compiler version, ...?

    What do you think the timers in the chip are intended for? Nice beautiful hardware that may dutifully count time while your program is busy doing other things...

Children