while loop not ending

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define F_CPU 1000000UL //  MHz clock speed
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
	int y = 1;
	int x = 0;
	DDRB = 0xFF; //Makes PORTB as Output
	while(y) //loop
	{
		x += 1; //counting seconds
		PORTB = 0x00;
		_delay_ms(1000);
		PORTB = (1 << PB1); //beeping buzzer
		_delay_ms(1);
		if(x == 30){ //30 second mark
			PORTB = (1 << PB1);
			_delay_ms(500); //long beep
			PORTB = 0x00;
			y = 0;//end loop
		}
	}
}

the loop isn't ending

also, why is the x value resetting to 0 after the if statement is run

 

This topic has a solution.
Last Edited: Mon. Jun 6, 2022 - 02:48 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Where are you expecting main() to go after the while loop ends? 

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

Run your program in the AS7.0 Simulator.

 

Hint.  Either tell the Simulator to use F_CPU=1kHz or use _delay_us() instead of _delay_ms().   Then it will not take too long to execute.

 

David.

 

p.s. your code looks ok to me.  It will be interesting to hear your explanation / solution.

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

Brian Fairchild wrote:

Where are you expecting main() to go after the while loop ends? 

main() goes to MCU heaven when you leave the while() loop.   i.e. it treads water indefinitely.

 

but the OP should hear the 1 second beep before it enters the abyss.

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

I barely have an idea of how to use the simulator.

I did 'Start debug and break" and opened the I/O tab. 

Not sure what to do next.

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

Google for "Atmel Studio Simulator"

 

I am sure that there are Tutorials and videos.   It is worth spending time on learning.

It is much better for you to do your own "study".  

The lessons,  videos etc have been carefully designed.   So will be much better than our random comments

 

By all means ask specific questions.   We are fine at answering specific points.

 

Meanwhile,  I might create an AS7.0 project and see for myself.   (a bit later today)

 

I would expect to see a long beep.

If the program restarts with short beeps it means the WatchDog is resetting.  (check fuses)

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

While I am by no means on the level of David and Brian, I do see some fundamental issues in your code that would leave one to think that the loop is not exiting.  I also am of the opinion that the buzzer/sounding device does not work either.

 

While the suggestion of using the Simulator is honorable, I do not suggest it be used for this application as it does not work very well when you have "_delay_ms()" in teh code as the simulator just sits for a very long time in those loops leaving you to think that the code has gone amok.  If you are to use teh simulator, then comment out the "_delay_ms()" lines.

 

But back to the code itself.  Go get a cup of tea and some paper and pencil and write out what you want each line to do, and then write out what each line actually does. 

 

Once you do this you will find that the code:

Does indeed exit the loop.

Teh buzzer does not do what you want it to do.

 

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

david.prentice wrote:
I would expect to see a long beep.

You must have much better eyes than me then!

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

anonymous69 wrote:

the loop isn't ending

also, why is the x value resetting to 0 after the if statement is run

 

How do you know either of those?

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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


anonymous69 wrote:

the loop isn't ending

also, why is the x value resetting to 0 after the if statement is run

The loop DOES end.

In the screenshot of the simulator I am paused on the last line of main. One more step and it will exit. Also note the values and location of x and y, they are what is expected.

 

 

After one final step: I'm out in the rough. The C Runtime will now clean up after main().

 

 

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

What N.Winter... shows is what I would expect. The question remains -- what are you seeing that says otherwise? The only odd thing

that I see is the extra close curly brace ( 2 opens and 3 closes). I would think that the compiler might whine about that, but what do I know.

 

Just to be a nitpicker, it will only count 29 seconds since x is set to 1 before the delay.

 

hj

 

 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just simply rewrote the code. First project of summer vacations done and dusted.

int main(void)
{
	int c = binary();
	int x;
	DDRB = 0x00;
	DDRB = (1 << PB1); //Makes PORTB as Output
	for(x = 0; x <c; x++){
		PORTB = 0x00;
		_delay_ms(1000);
		PORTB = (1 << PB1); //beeping buzzer
		_delay_ms(1);
	}
	 if(x == c){ //mark
		PORTB = (1 << PB1);
		_delay_ms(500); //long beep
		PORTB = (0 << PB1);
			}
}

 

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

You have not understood any of the advice.

 

I ran this in the AS7.0 Simulator.   Life is easier with a very SLOW F_CPU like 0.001MHz.  Just tell the "Processor Status" window that Frequency is 0.001 MHz.

 

You can read off the exact number of microseconds from the "Stop Watch" field.

And you can see what happens when you "fall off the end" of main().

 

As a general rule you put your own "catch all" at the end of main() so that you remain in control.

 

/*
* anonymous69_beep2.c
*
* Created: 06-Jun-22 16:04:41
* Author : David Prentice
*/

#define F_CPU 1000UL //  1kHz clock speed for simulator
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
    int c = 30; //binary();   //binary() is unknown fuction
    int x;
    DDRB = 0x00;
    DDRB = (1 << PB1); //Makes PORTB as Output
    for(x = 0; x <c; x++){
        PORTB = 0x00;
        _delay_ms(1000);
        PORTB = (1 << PB1); //beeping buzzer
        _delay_ms(1);
    }
    if(x == c){ //mark
        PORTB = (1 << PB1);   //breakpoint
        _delay_ms(500); //long beep
        PORTB = (0 << PB1);   //breakpoint
    }
}                             //breakpoint

 

Last Edited: Mon. Jun 6, 2022 - 03:24 PM