PIC Microcontoller IO Routine

A2D and Stepper Motor Driver

From: Mark Willcox

;  stepr.asm, To learn stepper motor and a2d converter for a 16f873
;last edited 5/25/2000. Originated by Tony Nixon and modified by
;Mark Willcox. Step code, 3-6-C-9,is output on portB, Hex code is 
;output on portC by an 8 LED bargraph display. A 50k pot is used to vary
;time delay eg, step speed.
;  The wiper of the pot goes to RA0 (analog input) the high end of the pot
;goes to +Vdd and the other end of the pot goes to -Vss/ground.
;  PortB is connected to a darlington pair consisting of a 2N3904 driving
;a MJE3055T. Each coil(collector) is clamped by a 1N4934 diode to common 
;ground. The stepper motor is a SLO-SYN 200oz/in, 3V@4A. 
;  This is a fun code to play with, you can reverse direction by 
;reversing the step sequence, ie, 9-C-6-3. 
;  Have fun and please send me any modifications that make this code
;better. Thanks again to Tony Nixon for all his help. Mark Willcox.

        #include <p16F873.inc>
        LIST p=16F873, ;R=HEX
        __config _XT_OSC & _PWRTE_ON & _WDT_OFF & _LVE_OFF
	errorlevel 1,-302

f       EQU     1
w       EQU     0
OUTPUT	EQU	06        	

	org 00h

	clrf    PORTA
        clrf    PORTB
        clrf    PORTC
	clrf	store
        bsf     STATUS,RP0
        MOVLW   B'00000001'
        MOVWF   TRISA           ;all outputs except RA0
        CLRF    TRISB
        CLRF    TRISC
        MOVLW   B'00010110'     ;left justified, RA0 = input
        MOVWF   ADCON1
        MOVLW   B'11010111'     ;prescaler 1:256 tmr0, internal clock
        BCF     STATUS,RP0

        MOVLW   B'01000001'     ;a2d = on, ch0, fosc/8
        MOVWF   ADCON0

mnloop  ;btfss   INTCON,T0IF     ;50us loop delay @ 4Mhz
        ;goto    mnloop		;This part makes motor too slow!

        BCF     INTCON,T0IF
        BSF     ADCON0,GO_DONE  ;start a2d conversion
WAITA2D NOP                     ;wait 4 finish
        GOTO    WAITA2D
        MOVF    ADRESH,w 	;upper 8 bits-ignor lower 3,semi colon 4 debug
	;MOVLW	H'03'		;for debug only
	MOVWF	store		;put a/d value in reg for time delay
	MOVWF   PORTC    	;display binary on portC

      	MOVLW   H'03'		;step sequence
        MOVWF   OUTPUT		;to motor driver circuitry
        CALL    DELAY		;variable delay
        MOVLW   H'06'		;To reverse motor direction...
        MOVWF   OUTPUT		;9
        CALL    DELAY		;C
        MOVLW   H'0C'		;6
        MOVWF   OUTPUT		;3
        CALL    DELAY
        MOVLW   H'09'
        MOVWF   OUTPUT
        CALL    DELAY
	GOTO    mnloop

DELAY	movf	store,w
	movwf	CounterB
	movwf	CounterA
loop	nop
	decfsz	CounterA,f
	goto	loop
	decfsz	CounterB,f
	goto	loop