;******************************************************************************
;Filename:	smotor.asm
;Author:	Eric Torrence
;Date:		21/05/06  	
;Version:	1.00
;Description:
; Implement full-step drive sequence.  2 inputs UP/DOWN and STEP on GP2 and
; GP3 (pins 5 and 4) and 4 outputs for winding A on GP0/GP1 (pins 7,6) and
; winding B on GP4/5 (pins 3,2)
;
;        ------------
;  +5 V -|          |- GND
;   B1  -| GP5  GP0 |- A1
;   B2  -| GP4  GP1 |- A2
;  STEP -| GP3  GP2 |- U/D
;        ------------
;
;******************************************************************************
;Revision History:
;  none
;******************************************************************************
	list      p=12f675            ; list directive to define processor
	#include <p12f675.inc>        ; processor specific variable definitions

	errorlevel  -302              ; suppress message 302 from list file

	__CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT  

; '__CONFIG' directive is used to embed configuration word within .asm file.
; The labels following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.

;************************** VARIABLE DEFINITIONS ******************************

STATE equ 0x20

;*************************** DEFINE STATEMENTS ********************************

; Motor lines    BB  AA 
#define OUT0 B'00100001'
#define OUT1 B'00010001'
#define OUT2 B'00010010'
#define OUT3 B'00100010'

;****************************** Start of Program ******************************
	org     0x000			; processor reset vector

	banksel	OPTION_REG
	clrf	OPTION_REG
	bsf	OPTION_REG, NOT_GPPU	; Weak pullups: disabled

	banksel ANSEL
	clrf	ANSEL			; Set all inputs as digital
	
	banksel TRISIO
	clrf	TRISIO			; Set all I/O pins as outputs
	bsf	TRISIO, 2		; Except GP2 (pin 5) for UP
	bsf	TRISIO, 3		; And GP3 (pin 4) for STEP

	banksel	GPIO
	clrf	GPIO			; clear all outputs

	clrf	STATE			; Set initial state
	
;; Loop waiting for STEP to go lo
LoWait:	
	btfsc	GPIO, 3		; GP3 pulled lo
	goto	LoWait

;; GP3 is lo, no wait for it to go hi
HiWait:
	btfss	GPIO, 3
	goto	HiWait
	
;; Increment or decriment STATE based upon UP input
	btfss	GPIO, 2
	goto	Down
	goto	Up
	
Up:	
	incf	STATE, f
	goto	StateMachine

Down:
	decf	STATE, f
	goto	StateMachine

StateMachine:
	movf	STATE, w	; Mask off all but low 2 bits
	andlw	0x03
	addwf	PCL, f		; Increment program clock (PCL) by
				; the value of STATE
	goto	State0		; PCL + 0
	goto	State1		; PCL + 1
	goto	State2		; PCL + 2
	goto	State3		; PCL + 3
	
State0:	
	movlw	OUT0
	movwf	GPIO
	goto	LoWait

State1:	
	movlw	OUT1
	movwf	GPIO
	goto	LoWait

State2:	
	movlw	OUT2
	movwf	GPIO
	goto	LoWait

State3:	
	movlw	OUT3
	movwf	GPIO
	goto	LoWait

end			; directive 'end of program'