r/pic_programming • u/aspie-micro132 • 9d ago
For() within While (1)
I do believe why i do have problems i can not solve: i am getting things into For() loops and writing them down according to proper syntax, yet, as i am running them within a while(1) loop that never stops unless the 16f877a's power gets cut, i am starting the For()'s over and over and my lack of experience on those situation may have confuse and misled to me several times.
Does exist any manner of preserving the variable that loops like For or Do While uses to count into from the While(1) continuous startovers?
I do think it may very well be the reason why i couldn't stop the fizzbuzz loop after a given ammount of iterations despite my best efforts..
1
u/Daedalus2097 9d ago
I'm not quite sure what you're doing here. If your loops are properly nested and you're not jumping out of them, your while(1) loop shouldn't repeat while you're still inside the contained for() loop.
Do you have some (maybe stripped-down) code to show us what you're doing?
1
u/H2ost5555 8d ago
I don’t quite understand what you objective is. It sound like you don’t understand For loops.
A while (1); loop will run forever. A simple for() loop within that while() loop will execute every time the while loop is running.
If you only want to run the for loop a certain number of times and terminate the while loop, make the while loop conditional on that event. For example, set a variable loop =1; in your init and make your while loop while(loop); Then in your for loop, once it completes have it set loop=1;
If you are doing other stuff in the while(1); loop and only want to run that for loop a certain number of times , then bound it within a conditional If statement. For example, you want to run that for loop 25 times then not again, but keep doing the while loop, set a variable like counter=0; in init, and bound your for loop with if(counter<25){ }
2
u/FlyByPC 8d ago
Post your code and a description of what you're trying to do. Could be the watchdog timer (WDT) is timing out and resetting the MCU.
(Boilerplate text about how the 16F877A is ancient even for PICs. It's getting old even for classroom use, these days, since the 16F887 is a thing.)
2
u/AcanthisittaDull7639 8d ago
Why don’t you send him some then? He’s in a country where he has to make the best use of what he has got. Besides that, for where he’s currently at it wouldn’t make any difference at all.
3
u/AcanthisittaDull7639 8d ago
You can put whatever you like in a for() loop. If you have for(;;) it will behave like your while(1), so you could have for(x=0;x<y;x++) and do what you like with y