Effect of empty while() loop

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello, after i checked that empty while() loops have a undesired effect to my program, i decided to write the simple below program:

 

//just before main():

 

    stop = 0;

    while(stop ==0){

    }

 

//external interrupt: (when i activate it, the var stop changes)

 

ISR(INT0_vect){    
        stop =1;
}

 

The above program, will never leave while(stop == 0), no matter how many times i activate the interrupt. The solution was to add a _delay_ms() inside while():

 

//just before main():

 

    stop = 0;

    while(stop ==0){

     _delay_ms(10);
    }

 

//external interrupt: 

 

ISR(INT0_vect){    
        stop =1;
}

 

Now, it worked fine, and passed the while() loop. I have come across this 'issue' many times before, and i always solved it using delay(). But not i think its the moment of truth. What is going on? Why do i need the delay(), or a -kind of- big code inside while() to make it work?

My guess is, the compiler optimizes the code... If that is the case, how can i avoid code optimization, or, what is a way around?

 

thanks

This topic has a solution.
Last Edited: Mon. Sep 9, 2019 - 11:33 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Basilisvirus wrote:
no matter how many times i activate the interrupt.
That's the most important part of the code and you haven't shown that??

 

Anyway is "stop" set to be volatile? - it MUST be for all the usual reasons.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That's the most important part of the code and you haven't shown that??

I did not bother to show that part, since i know it works (because the whole code works using _delay_ms() )

 

And thanks! it worked. I set the stop to volatile and it works. I now can see... https://stackoverflow.com/questions/4437527/why-do-we-use-volatile-keyword

 

Now i need to know when should i make a variable volatile, since i dont know which of my variables the optimizer might mess with, which seems to answered here: https://stackoverflow.com/questions/17708835/is-it-bad-if-all-variables-are-defined-as-volatile-on-avr-programming

Last Edited: Mon. Sep 9, 2019 - 07:55 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

It's also the top entry in the FAQ in the user manual:

 

https://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_volatile

 

Variables should be "volatile" when they are accessed in more than one thread of execution. In an AVR program that usually means a variable that is accessed in both main() and an ISR().

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Del

Last Edited: Mon. Sep 9, 2019 - 02:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


AndreyT wrote:
Del
...Taco.  Like Marco...Polo?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Del Shannon?

Letting the smoke out since 1978

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lana Del Rey, obviously

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

digitalDan wrote:
Del Shannon?

I'm going to Runaway from that one.

AndreyT wrote:
Lana Del Rey, obviously

And her little daughter Marina.

(I suppose her son is a Playa)

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Wed. Sep 11, 2019 - 12:46 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No es bueno!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dude.  You're getting a Del.  S.