SX Microcontroller Input / Ouput Methods

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
	CBLOCK  0X20
               CounterA,CounterB,store
        ENDC

f	equ	1
w	equ	0
OUTPUT	equ	06

	org	00h


	clr	RA
	clr	RB
	clr	RC
	clr	store
	setb	STATUS.RP0
	mov	W, #%00000001
;*** WARNING: TRIS registers are accessed by MOV !Rx, W (M = $0F or $1F). 
;        MOVWF   TRISA           ;all outputs except RA0
	mov	TRISA, W	;all outputs except RA0
;*** WARNING: TRIS registers are accessed by MOV !Rx, W (M = $0F or $1F). 
;        CLRF    TRISB
	clr	TRISB
;*** WARNING: TRIS registers are accessed by MOV !Rx, W (M = $0F or $1F). 
;        CLRF    TRISC
	clr	TRISC
	mov	W, #%00010110	;left justified, RA0 = input
	mov	ADCON1, W
	mov	W, #%11010111	;prescaler 1:256 tmr0, internal clock
;*** WARNING: OPTION register is accessed only by MOV !OPTION, W. 
;        MOVWF   OPTION_REG
	mov	OPTION_REG, W
	clrb	STATUS.RP0

	mov	W, #%01000001	;a2d = on, ch0, fosc/8
	mov	ADCON0, W

mnloop  ;	sb	INTCON.T0IF	;50us loop delay @ 4Mhz
;	jmp	mnloop	;This part makes motor too slow!

	clrb	INTCON.T0IF
	setb	ADCON0.GO_DONE	;start a2d conversion
WAITA2D	nop	;wait 4 finish
	snb	ADCON0.GO_DONE
	jmp	WAITA2D

	mov	W, ADRESH	;upper 8 bits-ignor lower 3,semi colon 4 debug
;	mov	W, #$03	;for debug only
	mov	store, W	;put a/d value in reg for time delay
	mov	RC, W	;display binary on portC

	mov	W, #$03	;step sequence
	mov	OUTPUT, W	;to motor driver circuitry
	call	DELAY	;variable delay
	mov	W, #$06	;To reverse motor direction...
	mov	OUTPUT, W	;9
	call	DELAY	;C
	mov	W, #$0C	;6
	mov	OUTPUT, W	;3
	call	DELAY
	mov	W, #$09
	mov	OUTPUT, W
	call	DELAY
	jmp	mnloop


DELAY	mov	W, store
	mov	CounterB, W
	mov	CounterA, W
loop	nop
	decsz	CounterA
	jmp	loop
	decsz	CounterB
	jmp	loop
	ret

	END

Interested: