



;**********************************************************************
; Filename: rotary_encoder_stepper_motor.asm
; PIC16F628A with 16 mHz crystal 
; Date: Nov. 15, 2013        
; Author: Lewis Loflin  
; http://www.bristolwatch.com  
; Rotary encoder connected to RA0 and RA1  
; This sends a series of pulses to Easy Driver to
; control direction of a bipolar stepper motor. 
; RB4 - step, RB5 - Dir, RB3 - Enable   
; Connect an LED to ground on Rb6 
;
; Table 1.   Microstep Resolution Truth Table
; MS1   MS2   Resolution
; L    L     Full step (2 phase)
; H    L     Half step
; L    H     Quarter step
; H    H     Eighth step
 
; DIR = RB5
; Step = RB4  // LOW to HIGH
; SLEEP = +5 // active LOW
; MS1 = GRD
; MS2 = GND
; Enable = RB3 active LOW 
;**********************************************************************

	list      p=16f628A           ; list directive to define processor
	#include <p16F628A.inc>       ; processor specific variable definitions

errorlevel  -302              ; suppress message 302 from list file

__CONFIG   _CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _HS_OSC 
	
; Use  _INTOSC_OSC_NOCLKOUT  for
; internal 4 mHz osc and no ext reset, use pin RA5 as an input
; Use _HS_OSC for a 16 mHz ext crystal. 
; Use _XT_OSC for 4 mHz ext crystal. Page 95 in spec sheet. 


;------------------------------------------------------------
	cblock 0x20	; Begin General Purpose-Register
;-------------------------- counters	
	count1
	count2
	count3
	count4
	count5
	val
	i
	endc


;*********************************************************************
	ORG     0x000             ; processor reset vector
	goto    setup              ; go to beginning of program
;**********************************************************************
	
setup ; init PIC16F628A

	movlw	0x07		
	; Turn comparators off and enable pins for I/O 
	movwf	CMCON
	banksel TRISA 
	movlw   b'00000011' ; RA0, RA1 input, others output
	movwf   TRISA
	clrf    TRISB
	banksel PORTA ; back to bank 0
	clrf	PORTA
	clrf	PORTB
	goto main

main
; remaining code goes here
rr  clrf PORTB 
	bsf PORTA, RA3 ; disable stepper control
	btfss PORTA, RA0 ; wait for low
	goto $+2
	goto $-2 ; wait for low
	btfss PORTA, RA1
	goto $+2
	goto qq
	
	bsf PORTB, RB5
	bcf PORTB, RB3 ; enable stepper
	movlw d'199'
	movwf i
	call toggle ; positive slope increments stepper
	call delay_1ms
	call toggle
	call delay_1ms
	decfsz i, F
	goto $-5
	goto rr

qq	bsf PORTB, RB6 ; just an indicator
	bcf PORTB, RB3 ; enable stepper
	movlw d'199'
	movwf i
	call toggle ; positive slope increments stepper
	call delay_1ms
	call toggle
	call delay_1ms
	decfsz i, F
	goto $-5
	goto rr
	goto	main 


;********************************************
;     Delay					                                             
;********************************************


delay_1ms
	; 16 mHz crystal
	movlw 0x07
	movwf count1
aa	movlw 0xBC
	movwf count2
	decfsz count2, F
	goto $-1
	decfsz count1, F
	goto aa 
	return


delay_10ms
	; 16  mHz crystal
	movlw d'9'
	movwf count3
bb	call delay_1ms
	decfsz count3, F
	goto bb
	return


delay_100ms
	; 16  mHz crystal
	movlw d'99'
	movwf count4
cc	call delay_1ms
	decfsz count4, F
	goto cc
	return

toggle ; invert state of RB4
	btfss PORTB, RB4
	goto $+3
	bcf PORTB, RB4
	return	
	bsf PORTB, RB4
	return

	END  ; directive 'end of program'
