英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流

 找回密码
 立即注册
搜索
电子烟方案单片机单片机开发深圳单片机开发
单片机方案国产单片机8位单片机电子烟方案开发
查看: 2752|回复: 0
打印 上一主题 下一主题

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

[复制链接]
跳转到指定楼层
1#
发表于 2009-5-5 17:25:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
; STEP_3.ASM (12C508 / 12C509) ;PIC单片机 ;
; 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:\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
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|公司首页|Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流 ( 粤ICP备09008620号 )

GMT+8, 2024-11-24 04:18 , Processed in 0.051991 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表