; Unipolar stepper motor control with a 12C508 ; C.Tavernier for Elektor - July/august 2007 ; Internet web site: www.tavernier-c.com ; LIST p=12C508 #include "p12c508.inc" __config _WDT_OFF & _IntRC_OSC & _MCLRE_OFF & _CP_OFF ; Constant definition RAM equ 0x07 ; Start of RAM Left equ 2 Right equ 3 ; Variable definition cblock RAM Step endc ; Start of code ; Initialization and I/O configuration org 0x00 movwf OSCCAL ; Internal RC oscillator calibration clrf GPIO movlw B'00001100' ; I/O direction definition TRIS GPIO movlw B'10010101' OPTION ; pull-up activated and tmr0/64 ; modify tmr0/X for other step size (see text) clrf Step clrf TMR0 goto main ; Table with values for execution of each step Table addwf PCL,f retlw b'00100001' retlw b'00100010' retlw b'00010010' retlw b'00010001' ; Main loop main btfss TMR0,7 ; 8 ms per step goto $-1 clrf TMR0 ; Load with a different value for a fine adjustement ; of the step size ; Clockwise rotation RightLabel btfsc GPIO,Right ; If D = 0 , right step goto LeftLabel ; Else, test G incf Step,f movlw .4 ; If step greater than 3 then step = 0 subwf Step,w btfsc STATUS,C clrf Step movf Step,W call Table movwf GPIO goto main ; Counter clockwise rotation LeftLabel btfsc GPIO,Left ; If G = 0 , left step goto main ; Else goto main loop decf Step,f movlw .3 btfsc Step,7 ; If step lower than 0 then step = 3 movwf Step movf Step,W call Table movwf GPIO goto main end