C type volatile behaviour

Status
Not open for further replies.

shaiko

Advanced Member level 5
Joined
Aug 20, 2011
Messages
2,644
Helped
303
Reputation
608
Reaction score
297
Trophy points
1,363
Visit site
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.

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?
 

if your compiler consider "int" as 2 byte int then their might be problem with your counter value with 500000.
 
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
I am giving it a try
Volatile keyword is an indivation to the compiler that this variable is going to change values unexpectedly. I think your LED toggle code is in an ISR and hence having this counter declared as volatile integer is the right way to use. Otherwise the counter value gets lost. Refer to the link - https://www.barrgroup.com/Embedded-Systems/How-To/C-Volatile-Keyword
 

But in my case, "counter" is incremented as long as the PB is pushed and remain unchanged when PB isn't pushed
Not according to your program, in which when the button is pushed the led is toggled and then the variable counter ends up set to 50,000 in a convoluted way which has no other side effects as the C compiler understands them.

Is counter read anywhere apart from in that loop condition?

The C compiler in the non volatile case is optimising to minimise execution time (Usually what you want), and seeing as counter is never read inside the loop, and there is no loop body, it is free to simply optimise the loop away replacing the whole thing with "counter = 50000".

It may even be optimising the variable counter away entirely if it is not used elsewhere.

This sort of case is exactly where volatile is useful.

Regards, Dan.
 
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
I think your LED toggle code is in an ISR
I don't think it's the case...The pin isn't configured as an interrupt source.

Dan Mills,
This sort of case is exactly where volatile is useful.
Can you suggest a general "rule of thumb" as to when declare your variables as "volatiles"
 

You'll declare a variable volatile if all writes to it are required as coded or it's assumed to be changed from other side, either because it's used in an interrupt function or connected to external hardware.
 
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…