;Stepping Motor Control Program (Speed fixed)

	INCLUDE"p16F88.inc"
	list p=16F88

	__CONFIG    _CONFIG1,  _HS_OSC & _WDT_OFF & _PWRTE_OFF & _CP_OFF & _LVP_OFF
	__CONFIG    _CONFIG2,  _FCMEN_OFF & _IESO_OFF


Register	EQU	0x20
MEM1		EQU	Register+0	;MEM1  at 20		
TIME1		EQU	Register+1	;TIME1 at 21
TIME2		EQU	Register+2	;TIME2 at 22
TIME3		EQU	Register+3	;TIME3 at 23

	ORG	0
	GOTO	START		;Main Program starts at START

	ORG	4

START

;Setting of Port B
	BSF	STATUS,RP0
	BCF	STATUS,RP1	;Selection of Bank 1
	MOVLW	B'11000000'
	MOVWF	TRISB		;RB0-5 -> Output Port, RB6,7 -> Input Port
	BCF	ANSEL, 6	;RB7/AN6 Digital Input
	BCF	ANSEL, 5	;RB6?AN5 Digital Input

	BCF	STATUS,RP0
	BCF	STATUS,RP1	;Selection of Bank 0
	
	MOVLW	B'00000101'	;'00001010' -> (W)
	MOVWF	PORTB		;(W) -> (PORTB)


; Main Program

STEP1	MOVF	PORTB,0		;(RB)->(W)
	ANDLW	B'11000000'	;(W) and 11000000 -> (W)
	MOVWF	MEM1		;(W) -> (MEM1)

	BTFSS	MEM1,7		;If the 7th bit = 1, Then skip
	CALL	RotateR
	BTFSS	MEM1,6		;If the 6th bit = 1, Then skip
	CALL	RotateL
	GOTO	STEP1


;End of Main Program



;Sub Routine1
RotateR	
	MOVLW	B'00000101'	;'00000101' -> (W)
	MOVWF	PORTB		;(W) -> (PORTB)
	CALL	COUNT1
	MOVLW	B'00000110'
	MOVWF	PORTB
	CALL	COUNT1
	MOVLW	B'00001010'	
	MOVWF	PORTB
	CALL	COUNT1
	MOVLW	B'00001001'
	MOVWF	PORTB
	CALL	COUNT1
	RETURN

;Sub Program2
RotateL
	MOVLW	B'00000101'
	MOVWF	PORTB
	CALL	COUNT1
	MOVLW	B'00001001'
	MOVWF	PORTB
	CALL	COUNT1
	MOVLW	B'00001010'	
	MOVWF	PORTB
	CALL	COUNT1
	MOVLW	B'00000110'
	MOVWF	PORTB
	CALL	COUNT1
	RETURN


;Idling timer
COUNT1	MOVLW	0x10
		MOVWF	TIME1
STEPM	MOVWF	TIME2
STEPM1	MOVWF	TIME3
STEPM2	DECFSZ	TIME3,1
		GOTO	STEPM2
		DECFSZ	TIME2,1
		GOTO	STEPM1
		DECFSZ	TIME1,1
		GOTO	STEPM
		RETURN

		END
