Problem with code for a stepper motor

The code shown below is a modification of the sample shown in Manual #3. It runs fine for a full turn forward and a full turn back. But then I get the following message and don't know how to correct it.
"Stack error - more nested gosubs than the stack allows!"
Code:
;    Filename: Stepper motor		
;    Date: 			
;    File Version: 	
;    Written by: 		
;    Function:
			'Run a Bipolar Stepper motor.
			'96 steps for 360 Deg
			'continuous forward and reverse motion  		
;    Last Revision:Oct 6, 2008
;    Target PICAXE: 28A	
; *******************************
'Directives
#picaxe 28A 
#com 4 
'======================================
Main:
for W5 = 0 to 95 		'start a for...next loop to left
gosub lstep 			'call LEFT step sub-procedure00 
next W5 				'next loop

for W6 = 0 to 95 		'start a for...next loop to right
gosub rstep 			'call RIGHT step sub-procedure
next W6 				'next loop

lstep:
let b1 = b1 + 1 		'add 1 to variable b1
goto step1 			'goto the lookup table

rstep:
let b1 = b1 - 1 		'subtract 1 from variable b1
goto step1

step1:
let b1 = b1 & %00000011 	'mask lower two bits of b1
lookup b1,(%10100000,%10010000,%01010000,%01100000),b2 'lookup code into b2
B2=B2/16
let pins = b2 			'output b2 onto control lines
If W6=96 then goto Main
Return
 

westaust55

Moderator
Code:
Main:
for W5 = 0 to 95 		'start a for...next loop to left
gosub lstep

 			'call LEFT step sub-procedure00 
next W5 				'next loop

for W6 = 0 to 95 		'start a for...next loop to right
gosub rstep 

			'call RIGHT step sub-procedure
next W6 				'next loop
[B]goto main[/B]

lstep:
let b1 = b1 + 1 		'add 1 to variable b1
goto step1 			'goto the lookup table

rstep:
let b1 = b1 - 1 		'subtract 1 from variable b1
goto step1

step1:
let b1 = b1 & %00000011 	'mask lower two bits of b1
lookup b1,(%10100000,%10010000,%01010000,%01100000),b2 'lookup code into b2
B2=B2/16
let pins = b2 			'output b2 onto control lines


Return
EDIT: improved code
 
Last edited:

westaust55

Moderator
Looking further at your code,

is there any reason why you have the lookup values 16 time the desired value?
changing the lookup values would save code/space having to divide by 16 after the lookup command.

A more optimised version might be:

Code:
Main:
for w5 = 0 to 95 		'start a for...next loop to left
  let b1 = b1 + 1       'add 1 to variable b1
  gosub step1           'goto the lookup table
			
next w5 			' next loop

for w6 = 0 to 95 		'start a for...next loop to right
  let b1 = b1 - 1       'subtract 1 from variable b1
  gosub step1           'goto the lookup table
next w6 			'next loop
goto main

step1:
let b1 = b1 & %00000011 	'mask lower two bits of b1
lookup b1,(%00001010,%00001001,%00000101,%00000110),b2 'lookup code into b2
let pins = b2 			'output b2 onto control lines

Return
 
You are right; I do not need to do that. The reason it was like that is that I was using pins 5, 6, 7, and 8. Then I decided to change to pins 0 to 4 and dividing by 16 was a fast patch. I had changed already to:
Lookup b1,(%00001010,%00001001,%00000101,%00000110),b2
Thank you again for your help
Andres
 
Top