Loops and going round in them!!
Posted: Fri Jan 03, 2025 10:12 am
Hi All,
I wonder if someone could explain the do's and don'ts around loops, in particular counting, please, what works and what doesn't. I have been caught up in this a number of times and it always seems to be a trial and error job, it would be nice to know some definitive rules to save the experimentation.
I will start with what I think I know and what I have tried. All described below works fine in the simulate but the issues arise when it's running in a PIC. This project was to generate a tone of varying length and frequency. The idea being to change the time duration for the frequency and the loop count to change the time the tone is on. All variables as integers and no number created greater than 2000.
If I set the loop to "count" I can make it work as long as I only put a number in the count box, if I put a variable in there and change the value of variable, it doesn't work. The time the tone was on did not change, changing the value of the variable. It counted a number of loops, but I can't say how many or where it got the number from. Why? It would be so useful for this to work.
So I added a count function, to count manually and exit the loop when the count conditions are met.
This works with the expression While "Count < Count_Target", however if I swap the the expression around and make it While "Count_Target > Count" it doesn't work, it exits the loop after 1 pass regardless. Why?
So out of curiosity I change it to Until "Count_Target < Count", that didn't work either. Again Why?
It seems to me that you should be able to use 4 different connotations of the While / Until expression, and achieve exactly the same goal, and in simulate this is confirmed, but in reality in the PIC it's not the case. In this case I was using a PIC 16F1503, but in other cases I have been using other 8 bit PICs such as the 16F1937 etc. I may be missing the obvious, quite possible, but I can't seem to find anything on this from searches etc. I know my variables were changing correctly, and doubly proved this by only changing the loop setup between each flash of the PIC.
Many Thanks.
Simon.
I wonder if someone could explain the do's and don'ts around loops, in particular counting, please, what works and what doesn't. I have been caught up in this a number of times and it always seems to be a trial and error job, it would be nice to know some definitive rules to save the experimentation.
I will start with what I think I know and what I have tried. All described below works fine in the simulate but the issues arise when it's running in a PIC. This project was to generate a tone of varying length and frequency. The idea being to change the time duration for the frequency and the loop count to change the time the tone is on. All variables as integers and no number created greater than 2000.
If I set the loop to "count" I can make it work as long as I only put a number in the count box, if I put a variable in there and change the value of variable, it doesn't work. The time the tone was on did not change, changing the value of the variable. It counted a number of loops, but I can't say how many or where it got the number from. Why? It would be so useful for this to work.
So I added a count function, to count manually and exit the loop when the count conditions are met.
This works with the expression While "Count < Count_Target", however if I swap the the expression around and make it While "Count_Target > Count" it doesn't work, it exits the loop after 1 pass regardless. Why?
So out of curiosity I change it to Until "Count_Target < Count", that didn't work either. Again Why?
It seems to me that you should be able to use 4 different connotations of the While / Until expression, and achieve exactly the same goal, and in simulate this is confirmed, but in reality in the PIC it's not the case. In this case I was using a PIC 16F1503, but in other cases I have been using other 8 bit PICs such as the 16F1937 etc. I may be missing the obvious, quite possible, but I can't seem to find anything on this from searches etc. I know my variables were changing correctly, and doubly proved this by only changing the loop setup between each flash of the PIC.
Many Thanks.
Simon.