grim_reaper
Senior Member
I ran across a weird issue last night, and re-coded to avoid it at the time. Now I've got more time on my hands, I managed to re-produce the issue using the code below. What I think is the problem is that the code fails to exit a loop-within-a-loop, when another loop is also present. It's difficult to explain, so try the code below in the simulator (I was using 6.0.7.1 before realising I needed to upgrade to 6.0.7.3, so I've tested in both). If you run it as is, the code loops once, then on the second pass it gets stuck on the 'Exit' command once the condition is satisfied. If you then comment out the 'For...Next' loop, it runs fine. Weird, eh?!
I've tested with all sorts of numbers/passes through the loop. I couldn't reproduce it for a while, but then when I copied my 'real' code back into the middle loop it started doing it again and I realised it was the presence of the third loop that did it. I guess the stack gets a bit confused with all the Loop/Next commands?
Just to throw some more info in here, the first time this happened I assumed it was the simulator (the condition only occurs rarely in the real application), so I programmed a 20X2 with it and after a few minutes it hung - I have an LED that flashes when data is transmitted, about once per second, so I could test this quite easily. Loading a particular value into EEPROM and letting it run a few passes before that value was used to exit the loop confirmed this happened within the chip as well.
I've tried simulating with various chip types, so it seems to be a generic problem rather than anything specific. I hope that's everything, of course let me know if you need anything else.
Thanks.
Code:
#PICAXE 20X2
; Variables
Symbol vTest = b0
Symbol vLoop = b1
Do
; This can be set to any value
vTest = 4
Do
; 'Exit' hangs when the condition is met - IF another loop (For/Next in this case) is present
If vTest = 5 Then Exit
For vLoop = 1 To 5
' Do something else in a loop
Next
Inc vTest
Loop
Pause 500
Loop
Just to throw some more info in here, the first time this happened I assumed it was the simulator (the condition only occurs rarely in the real application), so I programmed a 20X2 with it and after a few minutes it hung - I have an LED that flashes when data is transmitted, about once per second, so I could test this quite easily. Loading a particular value into EEPROM and letting it run a few passes before that value was used to exit the loop confirmed this happened within the chip as well.
I've tried simulating with various chip types, so it seems to be a generic problem rather than anything specific. I hope that's everything, of course let me know if you need anything else.
Thanks.