
;*******************************
;Stepper Motor.asm
;drives a Stepper motor forward and reverse
;13-11-2010 
;*******************************


	list	p=12F629
	radix	dec
	include	"p12f629.inc"
	
		errorlevel	-302	; Dont complain about BANK 1 registers

	__CONFIG	_MCLRE_OFF & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT  ;Internal osc.

;_MCLRE_OFF  - master clear must be off for gp3 to work as input pin 

;******************************
; variables - names and files
;*****************************

temp1		equ 20h	;
temp2		equ 21h	;

Sw_Flag		equ	25h	;switch flag for "inching"
loops		equ	26h ;loops for full stepping
count		equ	27h	;loops of discharge time for 100n
PotValue	equ 28h ;value of pot
look		equ	29h ;look for pot value every 100 loops


;***************************
;Equates
;***************************
status		equ	0x03
rp1			equ	0x06
rp0			equ	0x05
GPIO 		equ 0x05
			

status		equ	03h
option_reg	equ 81h


		; bits on GPIO
				
pin7		equ	0	;GP0  100k speed pot
pin6	 	equ	1	;GP1  output1 to stepper motor
pin5		equ	2	;GP2  output2 to stepper motor 
pin4		equ	3	;GP3  Input from buttons
pin3		equ	4	;GP4  output3 to stepper motor
pin2		equ	5	;GP5  output4 to stepper motor   
 

		;bits
				
rp0			equ	5			;bit 5 of the status register

;**********************
;Beginning of program
;**********************
		org		0x00
		nop
		nop
		nop
		nop
		nop			
SetUp	bsf		status, rp0 	;Bank 1			
       	movlw	b'11001000'		;Set TRIS  GP1,2,4,5 out GP3 input 
		movwf	TRISIO	   	    ;		
		bcf		status, rp0		;bank 0
		movlw   07h         	;turn off Comparator ports
        movwf   CMCON       	;must be placed in bank 0 
        clrf 	GPIO       		;Clear GPIO of junk			
        clrf	Sw_Flag
        incf	Sw_Flag,1		;put a bit onto Sw_Flag
		goto 	Main	
			


;****************
;delays			*
;****************

_uS		movlw	08Ch
		movwf	temp1
		decfsz 	temp1,f
		goto 	$-1
		retlw 	00
		
		
		;delay for pulsing servo anticlockwise
		
acw
		movlw	60h
		movwf	temp1
		decfsz 	temp1,f
		goto 	$-1
		retlw 	00
		
	
_1mS	nop
		decfsz 	temp1,f
		goto 	_1mS
		retlw 	00
		
_5mS	movlw	.5
		movwf	temp2
		nop
		decfsz 	temp1,f
		goto 	$-2
		decfsz 	temp2,f
		goto 	$-4	
		retlw 	00	
		
		
				

_10mS	movlw	0Ah
		movwf	temp2
		nop
		decfsz 	temp1,f
		goto 	$-2
		decfsz 	temp2,f
		goto 	$-4	
		retlw 	00
		
_15mS	movlw	.15
		movwf	temp2
		nop
		decfsz 	temp1,f
		goto 	$-2
		decfsz 	temp2,f
		goto 	$-4	
		retlw 	00	
		
		
_18mS	movlw	.18
		movwf	temp2
		nop
		decfsz 	temp1,f
		goto 	$-2
		decfsz 	temp2,f
		goto 	$-4	
		retlw 	00		
		
_50mS	movlw	.50
		movwf	temp2
		nop
		decfsz 	temp1,f
		goto 	$-2
		decfsz 	temp2,f
		goto 	$-4	
		retlw 	00		
		
		
		
			
_100mS	movlw	.100
		movwf	temp2
		nop
		decfsz 	temp1,f
		goto 	$-2
		decfsz 	temp2,f
		goto 	$-4	
		retlw 	00			
		
		
_200mS	movlw	.200
		movwf	temp2
		nop
		decfsz 	temp1,f
		goto 	$-2
		decfsz 	temp2,f
		goto 	$-4	
		retlw 	00	
					
			
		
		;delay to create value for pot for speed
		
PotDel	movlw	040h		;40h produces 12 loops
		movwf	temp1
		decfsz 	temp1,f
		goto 	$-1
		retlw 	00		
	
			
;***************************
; Sub Routines 		       *
;***************************
	
	
		;position of pot creates a value in PotValue
		
Pot		bsf		status,rp0			
		bcf		trisio,0		;Make GP0 output
		bcf		status,rp0
		bcf		gpio,0			;make GP0 LOW	
		call	_1mS			;create delay to discharge 100n
		bsf		status,rp0			
		bsf		trisio,0		;Make GP0 input
		bcf		status,rp0	
		clrf	PotValue
		call	PotDel				
		incf	PotValue,f		
		btfss	gpio,0			;is input HIGH?				
		goto	$-3	
		retlw	00				;returns with a value in PotValue
				
			
	
		
											
;****************************************************************
;* Main 							*
;****************************************************************

Main	clrf	gpio
		bcf		gpio,5			;gpio,5 high for a short time 
		call	_1mS
		bsf		gpio,5			
		nop	
		btfss	gpio,3
		goto	$+2
		goto	$+6			;go to half step Forward
		clrf	gpio
		bsf		gpio,1	
		btfss	gpio,3		
		goto	Reverse		;go to half step Reverse
		goto	_FS      	;go to Full Step - for forward or reverse
		
		btfsc	Sw_Flag,0
		goto	_1HSF_
		btfsc	Sw_Flag,1
		goto	_2HSF_
		btfsc	Sw_Flag,2
		goto	_3HSF_
		btfsc	Sw_Flag,3
		goto	_4HSF_
		btfsc	Sw_Flag,4
		goto	_5HSF_
		btfsc	Sw_Flag,5
		goto	_6HSF_
		btfsc	Sw_Flag,6
		goto	_7HSF_
		goto	_8HSF_
		
_1HSF_	movlw	b'00100000'		;1
		goto	_C			
_2HSF_	movlw	b'00110000'		;2
		goto	_C		
_3HSF_	movlw	b'00010000'		;3
		goto	_C		
_4HSF_	movlw	b'00010100'		;4
		goto	_C		
_5HSF_	movlw	b'00000100'		;5
		goto	_C		
_6HSF_	movlw	b'00000110'		;6
		goto	_C		
_7HSF_	movlw	b'00000010'		;7
		goto	_C		
_8HSF_	movlw	b'00100010'		;8
		movwf	gpio
		call	_200mS
		clrf	gpio
		call	_200mS
		call	_200mS
		clrf	Sw_Flag
		incf	Sw_Flag,1
		bsf		gpio,5			
		nop	
		btfsc	gpio,3		;see if sw is still pressed for full speed
		goto	HS_Fwd	
		bcf		gpio,5		
		goto	Main		
		
			
_C		movwf	gpio
		call	_200mS
		clrf	gpio
		call	_200mS
		call	_200mS
		bcf		status,0	;clear the carry		
		rlf		Sw_Flag,1		
		bsf		gpio,5			
		nop	
		btfsc	gpio,3		;see if sw is still pressed for full speed
		goto	HS_Fwd	
		bcf		gpio,5
		goto	Main	
		
		
		
		
		;reverse - half step
		
		
Reverse
		clrf	gpio
		bcf		gpio,4			;gpio,4 high for a short time 
		call	_1mS
		bsf		gpio,4			
		nop	
		btfss	gpio,3
		goto	Main	
		
		btfsc	Sw_Flag,0
		goto	_1HSR_
		btfsc	Sw_Flag,1
		goto	_2HSR_
		btfsc	Sw_Flag,2
		goto	_3HSR_
		btfsc	Sw_Flag,3
		goto	_4HSR_
		btfsc	Sw_Flag,4
		goto	_5HSR_
		btfsc	Sw_Flag,5
		goto	_6HSR_
		btfsc	Sw_Flag,6
		goto	_7HSR_
		goto	_8HSR_
		
_1HSR_		movlw	b'00100000'		;1
		movwf	gpio		
		call	_200mS
		clrf	gpio
		call	_200mS
		call	_200mS
		Movlw	80h
		movwf	Sw_Flag
		bsf		gpio,4			
		nop	
		btfsc	gpio,3		;see if sw is still pressed for full speed
		goto	HS_Rev	
		bcf		gpio,4		
		goto	Main	
		
_2HSR_	movlw	b'00110000'		;2
		goto	_D		
_3HSR_	movlw	b'00010000'		;3
		goto	_D		
_4HSR_	movlw	b'00010100'		;4
		goto	_D		
_5HSR_	movlw	b'00000100'		;5
		goto	_D		
_6HSR_	movlw	b'00000110'		;6
		goto	_D		
_7HSR_	movlw	b'00000010'		;7
		goto	_D		
_8HSR_	movlw	b'00100010'		;8
		goto	_D			
		
							
_D		movwf	gpio
		call	_200mS
		clrf	gpio
		call	_200mS
		call	_200mS
		bcf		status,0	;clear the carry		
		rrf		Sw_Flag,1		
		bsf		gpio,4			
		nop	
		btfsc	gpio,3		;see if sw is still pressed for full speed
		goto	HS_Rev	
		bcf		gpio,4					
		goto	Main
		
		

			;half step forward  


HS_Fwd

		clrf	gpio
		bcf		gpio,5			;gpio,5 high for a short time 
		call	_1mS
		bsf		gpio,5			
		nop	
		btfss	gpio,3
		goto	Main	
							;goto	_a
		btfsc	Sw_Flag,0
		goto	_1HSF
		btfsc	Sw_Flag,1
		goto	_2HSF
		btfsc	Sw_Flag,2
		goto	_3HSF
		btfsc	Sw_Flag,3
		goto	_4HSF
		btfsc	Sw_Flag,4
		goto	_5HSF
		btfsc	Sw_Flag,5
		goto	_6HSF
		btfsc	Sw_Flag,6
		goto	_7HSF
		goto	_8HSF
		
_1HSF	movlw	b'00100000'		;1
		goto	_CC			
_2HSF	movlw	b'00110000'		;2
		goto	_CC		
_3HSF	movlw	b'00010000'		;3
		goto	_CC		
_4HSF	movlw	b'00010100'		;4
		goto	_CC		
_5HSF	movlw	b'00000100'		;5
		goto	_CC		
_6HSF	movlw	b'00000110'		;6
		goto	_CC		
_7HSF	movlw	b'00000010'		;7
		goto	_CC		
_8HSF	movlw	b'00100010'		;8
		movwf	gpio
		call	_5mS
		clrf	gpio		
		clrf	Sw_Flag
		incf	Sw_Flag,1
		goto	HS_Fwd		
		
		
_CC		movwf	gpio
		call	_5mS
		clrf	gpio		
		bcf		status,0	;clear the carry		
		rlf		Sw_Flag,1
		goto	HS_Fwd
		
				
		;half step reverse 
		
HS_Rev		
		clrf	gpio
		bcf		gpio,4			;gpio,4 high for a short time 
		call	_1mS
		bsf		gpio,4			
		nop	
		btfss	gpio,3
		goto	Main	
		
		btfsc	Sw_Flag,0
		goto	_1HSR
		btfsc	Sw_Flag,1
		goto	_2HSR
		btfsc	Sw_Flag,2
		goto	_3HSR
		btfsc	Sw_Flag,3
		goto	_4HSR
		btfsc	Sw_Flag,4
		goto	_5HSR
		btfsc	Sw_Flag,5
		goto	_6HSR
		btfsc	Sw_Flag,6
		goto	_7HSR
		goto	_8HSR
		
_1HSR	movlw	b'00100000'		;1
		movwf	gpio
		call	_10mS
		clrf	gpio		
		Movlw	80h
		movwf	Sw_Flag
		goto	HS_Rev
_2HSR	movlw	b'00110000'		;2
		goto	_G		
_3HSR	movlw	b'00010000'		;3
		goto	_G		
_4HSR	movlw	b'00010100'		;4
		goto	_G		
_5HSR	movlw	b'00000100'		;5
		goto	_G		
_6HSR	movlw	b'00000110'		;6
		goto	_G		
_7HSR	movlw	b'00000010'		;7
		goto	_G		
_8HSR	movlw	b'00100010'		;8
		goto	_G			
		
				
_G		movwf	gpio
		call	_10mS
		clrf	gpio		
		bcf		status,0	;clear the carry		
		rrf		Sw_Flag,1
		goto	HS_Rev	
		
		
				
		;decide on full step forward or reverse
		
		
_FS		clrf	gpio
		bsf		gpio,5
		btfss	gpio,3
		goto	$+2
		goto	FS_Fwd
		bsf		gpio,4
		btfss	gpio,3
		goto	$+2
		goto	FS_Rev
		clrf	gpio
		call	_1mS		
		goto	$-10

		
		;Full Step Forward
		
FS_Fwd	call	pot		;returns with value in PotValue
		movlw	.50
		movwf	look	;look at pot every 50 loops
		movlw	b'00000010'		;full step forward
		movwf	gpio		
		movf	PotValue,w
		movwf	loops
		call	_1mS
		decfsz	loops,1
		goto	$-2					
		movlw	b'00000100'
		movwf	gpio
		movf	PotValue,w
		movwf	loops
		call	_1mS
		decfsz	loops,1
		goto	$-2		
		movlw	b'00010000'
		movwf	gpio
		movf	PotValue,w
		movwf	loops
		call	_1mS
		decfsz	loops,1
		goto	$-2	
		movlw	b'00100000'		
		movwf	gpio
		movf	PotValue,w
		movwf	loops
		call	_1mS
		decfsz	loops,1
		goto	$-2
		decfsz	look,1
		goto	FS_Fwd+3  ;don't look at pot
		goto	FS_Fwd	;look at pot
		
		
		
		
				;Full Step Reverse
		
FS_Rev	call	pot		;returns with value in PotValue
		movlw	.50
		movwf	look	;look at pot every 50 loops
		movlw	b'00100000'		;full step reverse
		movwf	gpio		
		movf	PotValue,w
		movwf	loops
		call	_1mS
		decfsz	loops,1
		goto	$-2					
		movlw	b'00010000'
		movwf	gpio
		movf	PotValue,w
		movwf	loops
		call	_1mS
		decfsz	loops,1
		goto	$-2		
		movlw	b'00000100'
		movwf	gpio
		movf	PotValue,w
		movwf	loops
		call	_1mS
		decfsz	loops,1
		goto	$-2	
		movlw	b'00000010'		
		movwf	gpio
		movf	PotValue,w
		movwf	loops
		call	_1mS
		decfsz	loops,1
		goto	$-2
		decfsz	look,1
		goto	FS_Rev+3  ;don't look at pot
		goto	FS_Rev	;look at pot
		
			
		
		END


	

								
	
		
		
		