新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 用PIC12C508509單片機控制一個步進電機源程序

        用PIC12C508509單片機控制一個步進電機源程序

        作者: 時間:2011-02-24 來源:網絡 收藏
        ; 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 c:mplabP12C509.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

        51單片機相關文章:51單片機教程


        單片機相關文章:單片機教程


        單片機相關文章:單片機視頻教程


        單片機相關文章:單片機工作原理




        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 龙里县| 南部县| 松阳县| 安多县| 石渠县| 昌乐县| 体育| 安远县| 和政县| 霍邱县| 石渠县| 镇原县| 涿州市| 卓资县| 满洲里市| 开封县| 德庆县| 阜阳市| 铁力市| 静海县| 阿勒泰市| 岳池县| 遂溪县| 方正县| 塘沽区| 黄梅县| 扎赉特旗| 米泉市| 巴东县| 方正县| 贺兰县| 老河口市| 河曲县| 罗田县| 英超| 栖霞市| 咸宁市| 广汉市| 同德县| 永年县| 陵川县|