技术热线: 4007-888-234

用PIC12C508509单片机控制一个步进电机源程序

更新时间: 2019-01-05
阅读量:2476

; STEP_3.ASM (12C508 / 12C509)
;
; Controls a stepping motor driver on GPIO bits 4, 2, 1 and 0.
;
; The direction is controlled by a switch on GPIO bit 3. Bit 5 is not
used.
;
; (Typical drivers might include a ULN2803 Octal Driver, 2N2222 or TIP122
; transistors or power FETs. See Parallel Port Manual - Vol 1).
;
; Outputs patterns 0x01, 0x03, 0x02, 0x06, 0x04, 0x0c, 0x08, 0x09, 0x01,
; .. in sequence to advance stepping motor in one direction. This is
; achieved by causing INDEX to advance from 0 to 7, to 0, etc and
; mapping the INDEX into a pattern which is then output on bits 4, 2, 1
; and 0 of GPIO.
;
; Note that GPIO bit 3 is not used. Thus, prior to outputting, bit 3
; of PATT is copied to the bit 4 position.
;
; Motor is advanced in the other direction by causing INDEX to move in
; the opposite direction; 7 to 0 to 7, etc.
;
; Switch on GPIO, Bit 3 is read between each output to the stepping
; motor to determine whether INDEX is to be incremented (0-7, 0) or
; decremented.
;
; The delay in between steps determines the speed of the stepper. In
; this program it is 25 msecs.

LIST P=PIC12C509
__CONFIG 0EH ; MCLR - dis, CP - dis, WDT - dis, FOSC - int

include <\mplab\p12c509.inc>

CONSTANT BASE_VAR=07H

INDEX EQU BASE_VAR+0
PATT EQU BASE_VAR+1

LOOP1 EQU BASE_VAR+2 ; for timing loop
LOOP2 EQU BASE_VAR+3

ORG 000H

MOVLW 080H ; GPWU disabled, GPPU enabled, other bits not used
OPTION

MOVLW B’101000’
TRIS GPIO ; Bit 3 input, Bits 4, 2, 1 and 0 ouputs

CLRF INDEX ; start INDEX at zero
READ_SW:
BTFSC GPIO, 3 ; read direction switch
GOTO FORWARD
GOTO REVERSE

FORWARD:
INCF INDEX, F ; increment the index
MOVLW .8
SUBWF INDEX, W ; and test if equal to 8
BTFSC STATUS, Z
CLRF INDEX ; if at 8, set to 0
GOTO ONE_STEP

REVERSE:
DECF INDEX, F ; decrement the index
MOVLW 0FFH
SUBWF INDEX, W ; test if at 0xFF
BTFSC STATUS, Z
GOTO REVERSE_1 ; if at FF, then reset to 7
GOTO ONE_STEP

REVERSE_1:

MOVLW .7
MOVWF INDEX
GOTO ONE_STEP

ONE_STEP:
MOVF INDEX, W ; copy count into w
CALL GEN_PATT ; returns the correct pattern in w
MOVWF PATT ; save it
BTFSS PATT, 3 ; move bit 3 to bit 4
BCF PATT, 4
BTFSC PATT, 3
BSF PATT, 4
MOVF PATT, W
MOVWF GPIO
CALL DELAY ; 25 msecs

GOTO READ_SW
;;;;;;

GEN_PATT ; maps INDEX into appropriate stepping motor pattern
MOVF INDEX, W
ADDWF PCL, F ; add w to the program counter
RETLW 01H ; stepping motor patterns
RETLW 03H
RETLW 02H
RETLW 06H
RETLW 04H
RETLW 0CH
RETLW 08H
DELAY: ; provides nominal 25 msec delay
MOVLW .25
MOVWF LOOP1
OUTTER:
MOVLW .110
MOVWF LOOP2
INNER:
CLRWDT
NOP
NOP
NOP
NOP
NOP
DECFSZ LOOP2, F
GOTO INNER
DECFSZ LOOP1, F
GOTO OUTTER
RETURN
END