shaiko
Advanced Member level 5
- Joined
- Aug 20, 2011
- Messages
- 2,644
- Helped
- 303
- Reputation
- 608
- Reaction score
- 297
- Trophy points
- 1,363
- Activity points
- 18,302
I use this code to toggle an LED on IAR using STM32F4.
GPIO_Pin_13 is the LED
GPIO_Pin_0 is a Push Button.
When "counter" is defined as "volatile int" and the PB is pushed, the LED blinks around 10 times per second as intended.
When "counter" is defined simply as "int" and the PB is pushed, the LED appears to stay always on but emits less light as if it's switching on/off at a high frequency (PWM effect).
As far as I understand - the keyword volatile prevents the compiler from doing optimization on "counter".
But in my case, "counter" is incremented as long as the PB is pushed and remain unchanged when PB isn't pushed - It's very clear and there's no ambiguity about it...What is there to optimize? What am I missing here?
GPIO_Pin_13 is the LED
GPIO_Pin_0 is a Push Button.
Code:
while (1)
{
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0))
{
GPIO_ToggleBits(GPIOG, GPIO_Pin_13);
for ( counter = 0 ; counter < 500000 ; counter++ ) ;
}
}
When "counter" is defined as "volatile int" and the PB is pushed, the LED blinks around 10 times per second as intended.
When "counter" is defined simply as "int" and the PB is pushed, the LED appears to stay always on but emits less light as if it's switching on/off at a high frequency (PWM effect).
As far as I understand - the keyword volatile prevents the compiler from doing optimization on "counter".
But in my case, "counter" is incremented as long as the PB is pushed and remain unchanged when PB isn't pushed - It's very clear and there's no ambiguity about it...What is there to optimize? What am I missing here?