Return-Path: robot-board@oberon.com
Received: by media.mit.edu (5.57/DA1.0.4.amt)
	id AA21075; Fri, 23 Jul 93 12:23:29 -0400
Received: from  ([127.0.0.1]) by oberon.com (4.1/SMI-4.1_Armado.MX)
	id AA21963; Fri, 23 Jul 93 12:22:55 EDT
Date: Fri, 23 Jul 93 12:22:54 EDT
Message-Id: <9307231541.AA11769@bullfrog.nscee.edu.nscnet>
Comment: Discussions of Robot controller boards
Originator: robot-board@oberon.com
Errors-To: gkulosa@oberon.com
Reply-To: "David A. Ence" <ence@bullfrog.nscee.edu>
Sender: robot-board@oberon.com
Version: 5.5 -- Copyright (c) 1991/92, Anastasios Kotsikonas
From: "David A. Ence" <ence@bullfrog.nscee.edu>
To: Multiple recipients of list <robot-board@oberon.com>
Subject: STEPPER CODE


	Ok. Here it is. I don't clame to be the best assembly coder
so this code could be looked over and mabey optimized a bit.  I hope
it will help you guys and what you are doing.  
	
	I still am seeking someone to talk to about the SPI
subsystem.

	I would like any and all feed back on this. Also any
changes that improved the code. Thank you.

THE HARDWARE:
	
	The stepper I have been using is a 15 degree 12 Volt
bipolor.  One may hook up one stepper to port1 and port2, and
another to port3 and port4. Thus only 2 steppers may be hooked up
to one miniboard at one time.  If one hooks up a stepper to port1
and port2, connect one coil of the stepper to each port.  Do the
same to connect a stepper to port3 and port4. 

THE SOFTWARE:
	
	As one may notice, the routine is implemented with interupts.
each time a timer interrupt fires one step is made.  One may change
the time between steps by changing the lengh varible. This is done
in the "stepper" subroutine after the pointers and direction mask are
cleared.

	clr	ptr1		
	clr	ptr2		
	clr	stpctrl		
	ldd	#$0000	<-------HERE Change This Value	
	std	lengh

	The routine "stepper" is called after 2 values have been
pushed onto the stack.  For an example I left the routine "main" in
the code so you could see what I'm talking about.  The first value
that is pushed onto the stack is a -24. Since my stepper is a 15
degree stepper, It will make one full revolution.  Since the value
is negitive it will go CounterClockWise. This value is for the
stepper attached to port1 and port2.  The second value is 24. It is
for the stepper connected to port3 and port4. The stepper will turn
ClockWise one full revolution.

	If you need any help drop me a line. Have fun stepping.

	Later,
	David A. Ence
	ence@nye.nscee.edu



-------------------------------cut here-----------------------------


*-----------------------------------------------------
*	Title: step.asm
*	Author:	David A. Ence
*		ence@nye.nscee.edu
*	Date:7/3/93
*------------------------------------------------------
RESETINIT	equ	$fe
OC2I		equ	$e6
BASE		equ	$1000
TCTL1		equ	$1020
TCNT		equ	$100e
TOC2		equ	$1018
TFLG1		equ	$1023
TMSK1		equ	$1022
TMSK2		equ	$1024
PORTB		equ	$1004

	org	$00
ptr1	rmb	1	;pointer into steptab1
ptr2    rmb	1	;pointer into steptab2
stpctrl	rmb	1	;direction mask
cnt1    rmb     1	;holds number of steps for stepper1
cnt2    rmb     1       ;holds number of steps for stepper2
lengh	fdb	2       ;Time between steps
 
	org	$ff00+RESETINIT
	fdb	main	;set interrupt vector for restart
	org	$ff00+OC2I
	fdb	stepi	;set interrupt vector for OC2I

	org	$f800	;start of eeprom
*----------------------------------------------------------
*  Table for step sequence
*----------------------------------------------------------
steptab1 	fcb	%00110011  ;red red
		fcb	%00110010  ;red grn
		fcb	%00110000  ;grn grn
		fcb	%00110001  ;grn red

steptab2 	fcb	%11001100  ;red red
		fcb	%11001000  ;red grn
		fcb	%11000000  ;grn grn
		fcb	%11000100  ;grn red
*-----------------------------------------------
*	Here we push 2 values onto the stack
*	a neg value for ccw
*	a pos value for cw
*-----------------------------------------------
main	equ	*
	lds	#$ff
	lda	#-24	;change this val for stepper1
	psha	
	lda	#24	;change this val for stepper2
	psha
	bsr	stepper ;call the routine
	pula
	pula
done	bra 	done
*-------------------------------------------------
stepper equ	*	

	bra	start
start	psha
	pshb
	pshy


	clr	ptr1		;clear the pointers 
	clr	ptr2		
	clr	stpctrl		;clear direction mask
	ldd	#$0000		;change this value to change speed of steps
	std	lengh
	tsy

	ldaa 	7,y		;get values off stack
	staa	cnt1

	ldaa	6,y
	staa	cnt2

	ldaa	cnt1		
	tsta
	bmi	ovr1
	bset    stpctrl $01	;set direction bits in stctrl mask

ovr1	ldaa	cnt2
	tsta
	bmi	ovr2
	bset    stpctrl $02

ovr2	ldx	#BASE

	bset	TCTL1,x $C0	;when interr occur drive OC2 high
	ldd	TCNT,x		;avoid premature cmp
	addd	lengh
	std	TOC2,x		;store to TOC2
	ldaa	#$40
	staa	TFLG1,x		;Set up flag
	staa	TMSK1,x		;Enable Timer2 interrupt
	cli	

ck	lda	cnt1		;loop til cnt1 & cnt2 == 0
	ora	cnt2
	tsta
	bne	ck
	bclr 	TMSK1,x $bf

	puly
	pulb
	pula
	rts
*----------------------------------------	
stepi	equ	*

	ldx	#BASE
	ldd	lengh		;put back to lengh  Add lengh to TOC2
	addd	TOC2,x		;to set up for
	std	TOC2,x		;next step.
	bclr	TFLG1,x,$bf	;Clear interrupt flag.
	clra

	ldab	cnt1		;if cnt1 ==0 
	tstb	
	beq	stpr2		;then ck stepper2 else step stepper1
	ldy	#steptab1	;get bit pattern 
	ldab	ptr1		;for stepper1 to be stored in PORTB
	aby
	ora	0,y
	brclr	stpctrl $01 cw1 ;update pointers and
	dec	cnt1		;counter for stepper1 depending on 
	inc	ptr1		;direction.
	cmpb	#3		
	bne	stpr2		
	clr	ptr1		
	bra	stpr2
cw1	dec	ptr1
	inc	cnt1
	tstb
	bne	stpr2
	ldab	#3
	stab	ptr1
stpr2	ldab	cnt2		;if cnt2 == 0
	tstb	
	beq	dostep		;then perform the step
	ldy	#steptab2	;get step pattern for stepper2
	ldab	ptr2		
	aby
	ora	0,y
	brclr	stpctrl $02 cw2	;update pointers
	dec	cnt2		;and counters
	inc	ptr2		;for stepper2 depending on direction.
	cmpb	#3		
	bne	dostep		
	clr	ptr2		
	bra	dostep
cw2	dec	ptr2
	inc	cnt2
	tstb	
	bne	dostep
	ldab	#3
	stab	ptr2

dostep	staa	PORTB,x		;do the step

	rti


