FOR/NEXT greater then 256 never completes....

Grogster

Senior Member
This is an interesting one.

Chip is 18M2 SMD.

I have a for/next loop, which has embedded routines as part of the loop.

If I set X to more then 256, the loop NEVER completes.

Can someone shine some light as to why?

Here is my code for the loop causing the problem:

Code:
'---------------------------
'MESSAGE PROCESSING ROUTINE:
'---------------------------

'===== TRANSMIT HELP MESSAGE: =====

messages:
  flag_rst=0 'Clear reset message flag for next cycle
  gosub address 'Send address specific message
  serout TXD,T2400,("H") 'Send help mode byte
  high LEDa:low LEDk 'Turn on LED(red)
  low ENA 'Disable NTX2 module
  high piezo 'Turn on piezo
  nap 6 '~1100ms low-power delay
  low piezo 'Turn off piezo
   for x=1 to 258	'Begin 2-minute delay(approximately)
    toggle LEDa:toggle LEDk 'Make dual-colour LED flash red and green

'===== RESET BUTTON PRESSED: =====

    if RST=0 then
    			if flag_rst=1 then start 'If reset message has already been sent this cycle, don't send it again
  			high LEDa:low LEDk 'Turn on LED(red)
  			nap 6 '~1100ms LED on time
  			gosub address 'Send address specific message
  			serout TXD,T2400,("R") 'Send reset mode byte
  			flag_rst=1 'Set reset message flag
  			goto start 'Back to the start
  		endif 'End of IF routine

'===== POWER FAILURE DETECTED: =====
  		
    if FAIL=0 and flag_ac=0 then
    			for x=1 to 40 'Begin 10 second delay
   			  if HLP=0 then messages 'If help button pressed, send it's message
   			  if RST=0 and flag_rst=0 then
					if flag_rst=1 then start 'If reset message has already been sent this cycle, don't send it again
			  		high LEDa:low LEDk 'Turn on LED(red)
			  		nap 6 '~1100ms LED on time
  					gosub address 'Send address specific message
  					serout TXD,T2400,("R") 'Send reset mode byte
  					flag_rst=1 'Set reset message flag
  					goto start 'Back to the start
  				endif 'End of IF routine   			  
   			  nap 4 '~288ms low-power delay
  			next 'Loop
  			if FAIL=1 then start 'If juice present again now, then do nothing
  			flag_ac=1	'Set AC plug-pack power failure flag
  			gosub address 'Send address specific message
  			serout TXD,T2400,("F") 'Send AC plug-pack power failure mode byte
  		endif 'End of IF routine

'===== POWER RESTORED DETECTED: =====
  		
    if FAIL=1 and flag_ac=1 then
    			flag_ac=0 'Set AC plug-pack power restored flag
			flag_bt=0 'Clear bad battery message flag.(once power back on, battery will recover)
  			gosub address 'Send address specific message
  			serout TXD,T2400,("K") 'Send AC mains OK mode byte
  		endif 'End of IF routine

'===== REMAINS OF LOOP: =====
  		
    nap 4 '~288ms low-power delay
   next 'Loop
  goto messages 'If reset NOT pressed after 2mins, send msg again
This code works great, UNITL I try to increase the time delay set by the X loop to more then 256.
At that point, it falls over, and NEVER completes. I have timed it out to about 90 minutes, then turned it off, as the loop never completed.
If X=255, then the loop starts over at about 1 minute and 12 seconds.

Anyone got any ideas?
 
Last edited:

DamonHD

Senior Member
Because your variable is only a byte variable (so wraps around from 255 back to 0) rather than a word variable?

Rgds

Damon
 

Svejk

Senior Member
You have unconditional jumps within that loop (goto start) it might be just a case of hiting the 255 returns stack limit.
 
Top