;Stepping Motor Speed Control Program

	INCLUDE"p16F88.inc"
	list p=16F88

	__CONFIG    _CONFIG1,  _HS_OSC & _WDT_OFF & _PWRTE_OFF & _CP_OFF & _LVP_OFF
	__CONFIG    _CONFIG2,  _FCMEN_OFF & _IESO_OFF

OutMode EQU	0x20			;OutMode

	ORG	0
	GOTO	START			;Main Program starts from START

	ORG	4
	CALL    Timer0_interrupt
	RETFIE

START

;Setting of Port A and B
	BSF	STATUS,RP0
	BCF	STATUS,RP1		;Selection of Bank 1
	MOVLW	B'00000000'
	MOVWF	TRISB			;RB0-7 -> Output Port
	MOVLW	B'00000010'
	MOVWf	TRISA			;RA1 -> Input Port

;Setting of Timer0
	BCF	OPTION_REG, T0CS	;Internal instruction cycle clock (CLKO)
	BCF	OPTION_REG, PSA		;Internal instruction cycle clock (CLKO)
	BSF	OPTION_REG, PS2		;
	BSF	OPTION_REG, PS1		;
	BSF	OPTION_REG, PS0		;Timer0 Rate 1:256

;Setting of Interrupt
	BSF	INTCON, GIE		;Enables all unmasked interrupts
	BSF	INTCON, PEIE		;Enables all unmasked peripheral interrupts
	BSF	INTCON, TMR0IE		;Enables the TMR0 interrupt
	BCF	INTCON, TMR0IF		;TMR0 register did not overflow


;Setting of AD Converter
	MOVLW	B'00000010'
	MOVWF	ANSEL			;AN1:Analog I/O
	BCF	ADCON1, ADFM		;Left Justified
	BCF	ADCON1, ADCS2		;ADCS2 = 0, ADCS = 01 -> A/D Conversion Clock = FOSC/8
	BCF	ADCON1, VCFG1		;Voltage Reference = VSS 
	BCF	ADCON1, VCFG0		;Voltage Reference = VDD

	BCF	STATUS,RP0
	BCF	STATUS,RP1		;Selection of Bank 0

	BCF	ADCON0, ADCS1		;
	BSF	ADCON0, ADCS0		;ADCS2 = 0, ADCS = 01 -> A/D Conversion Clock = FOSC/8
	BCF	ADCON0, CHS2
	BCF	ADCON0, CHS1
	BSF	ADCON0, CHS0		;CHS = 001 -> AN1
	BSF	ADCON0, ADON		;A/D converter module is operating

;Setting of Initial Value of PORTB & the Register ‘OutMode’
	MOVLW	B'00000101'		;'00000101' -> (W)
	MOVWF	PORTB			;(W) -> (PORTB)

	MOVLW	B'00000000'
	MOVWF	OutMode

; Main Program

STEP1	GOTO	STEP1

;End of Main Program

;Sub Routine1
Timer0_interrupt

	MOVLW	B'00000100'		;'00000100' -> (W)
	MOVWF	PORTA			;(W) -> (PORTA)

	BSF	ADCON0, GO		;AD Conversion Start
AD_Wait	
	BTFSC	ADCON0, 2			
	GOTO	AD_Wait			;Wait until AD conv is done

	MOVF	ADRESH, 0		;ADRESH -> (W)
	MOVWF	TMR0			;(W) -> TMR0

	CALL	Rotate

	MOVLW	B'00000000'		;'00000000' -> (W)
	MOVWF	PORTA			;(W) -> (PORTA)

	BCF	INTCON, TMR0IF		;TMR0 register did not overflow

	RETURN


;Rotating Step Motor routine
Rotate	BTFSS	OutMode,0	;If the 0-th bit = 1, then skip the next command line
		GOTO 	CaseX0
		GOTO	CaseX1

CaseX0	BTFSS	OutMode,1	;If the 1-th bit = 1, then skip the next command line
		GOTO	Case00
		GOTO	Case10

CaseX1	BTFSS	OutMode,1	;If the 1-th bit = 1, then skip the next command line
		GOTO	Case01
		GOTO	Case11

Case00	MOVLW	B'00000110'	; '00000101' -> '00000110'
		GOTO	Fin

Case01	MOVLW	B'00001010'	; '00000110' -> '00001010'
		GOTO	Fin

Case10	MOVLW	B'00001001'	; '00001010' -> '00001001'
		GOTO	Fin

Case11	MOVLW	B'00000101'	;'00001001' -> '00000101'

Fin	MOVWF	PORTB		;(W) -> (PORTB)

	INCF	OutMode,1	;(OutMode) + 1 -> (OutMode)

	RETURN

	END
