cancel
Showing results for 
Search instead for 
Did you mean: 

why does Led blink modify in this code?

hossein hosseini
Associate II
Posted on January 06, 2018 at 14:42

hi.

i wrote a code in IAR for stm32f103ret, the code is for blink led and  problem is the time is different when the led is on and the led is off .

But the wait is same both of set led and reset led.

19 seconds led is on but 22 seconds led is off. and it repeat again and again.

who know why?what do we do?

so thanks.

#include 'stm32f10x.h'

#include 'stm32f10x_gpio.h'  // for Enable LCD

#include 'stm32f10x_rcc.h'   //for Enable LCD

#include 'LCDLib.h'

#include 'delay.h'

void GPIOC_init(void);

int main()

{

  int32_t dly=0;

  GPIOC_init();

 

  while(1)

  {

    GPIO_WriteBit(GPIOB,GPIO_Pin_0, Bit_SET);

    while(dly<100000000)dly+=1;

    dly=0;

    GPIO_WriteBit(GPIOB,GPIO_Pin_0, Bit_RESET);

    while(dly<100000000)dly+=1;

    dly=0;

  }

 

}

void GPIOC_init(void)

{

    RCC_APB2PeriphClockCmd  ( RCC_APB2Periph_GPIOB, ENABLE );

    

    GPIO_InitTypeDef gpioC;

    gpioC.GPIO_Mode = GPIO_Mode_Out_PP ;

    gpioC.GPIO_Pin = GPIO_Pin_All;

    gpioC.GPIO_Speed = GPIO_Speed_10MHz;  

    GPIO_Init  (  GPIOB,  &gpioC );

 

}
9 REPLIES 9
Posted on January 06, 2018 at 14:52

Maybe compiler optimization will help - set O0 (no optimization)?

Posted on January 06, 2018 at 14:53

I do not use IAR so the O0 (no optimization) comes from Keil IDE

Posted on January 06, 2018 at 16:21

Put the delay code in a function, and make the variable volatile. This way the execution time will be consistent and less subject to optimization.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 06, 2018 at 19:20

Personally I would use timers for counting delays - this frees MCU resources for other tasks.

hossein hosseini
Associate II
Posted on January 07, 2018 at 10:30

thanks guys.

i fix the problem with your help.

any ways , why this happening occurred !!!??

what is the difference between the while in MAIN and the while in function?both of them is same. But the activity in main and function is difference ?

how can we trust another code work correctly and it does not have problem like this problem ??

so thanks.
Posted on January 07, 2018 at 10:40

Compilers are over intelligent nowdays  - you can optimize the generated code for size, speed, etc.

'Volatile' keyword for example prevents from such optimization especially if the variable is shared between two thread (or main routine and IT handler) - it says do not do 'shortcuts' (do not assume that the variable does not change out of your control), etc.

The design matters - you have decided to count delays using clock cycles. More reliable and preferred way is to use hardware built-in timers.

See this documents for reference:

http://www.keil.com/support/man/docs/armcc/armcc_chr1359124222426.htm

 

http://www.keil.com/support/man/docs/armcc/armcc_chr1359124222941.htm

 
Posted on January 07, 2018 at 10:46

Please note that the compiler 'hidden activity' did not break the logic of your program, only the timing was affected. So selecting right design for solving this particular problem of blinking the led is crucial. SysTick counter would be right for the job (you can increase the counter in the systick IT handler for example) and decide when to toggle the led in your main program loop.

Posted on January 08, 2018 at 12:33

Hi,

you could also use HAL_Delay() function which relies on SysTick for the timing.

Posted on January 08, 2018 at 12:45

I have not suggested this solutions as the original code mentioned here does not refer to HAL