2018-01-06 05:42 AM
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 ); }2018-01-06 05:52 AM
Maybe compiler optimization will help - set O0 (no optimization)?
2018-01-06 06:53 AM
I do not use IAR so the O0 (no optimization) comes from Keil IDE
2018-01-06 07:21 AM
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.
2018-01-06 10:20 AM
Personally I would use timers for counting delays - this frees MCU resources for other tasks.
2018-01-07 01:30 AM
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.2018-01-07 02:40 AM
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
2018-01-07 02:46 AM
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.
2018-01-08 04:33 AM
Hi,
you could also use HAL_Delay() function which relies on SysTick for the timing.
2018-01-08 04:45 AM
I have not suggested this solutions as the original code mentioned here does not refer to HAL