⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 halfstep.asm

📁 pic得电机控制程序
💻 ASM
字号:
; File HALFSTEP.ASM
; ... for PIC16F84 microcontroller
; Program to use F84 as a step and direction controller for a unipolar
; step motor.  Step and direction pins are RA0, RA1; RA2, RA3; RB0-3 and RB4-7 are the windings
; in order (driven by NPN small sig transistors or MOSFETS). RA4 selects full or half-step.
; Steps on negative going edge of step pulse.

; CPU configuration
; 	(It's a 16F84, RC oscillator,
; 	watchdog timer off, power-up timer on)

	processor 16f84
	include	  <p16f84.inc>
	__config  _XT_OSC & _WDT_OFF & _PWRTE_ON

; Declare variables

pattA	equ	H'0D'	;Current step pattern number (0-7) for axis A
lastA	equ	H'0E'   ;Last state of step pin on axis A (1 is high, 0 is low)
pattB	equ	H'0F'	;Current step pattern number (0-7) for axis B
lastB	equ	H'10'   ;Last state of step pin on axis B (1 is high, 0 is low)
inport	equ	H'11'	;Value of port A when read (stored for later access)
temp	equ	H'12'

; Program

	org	0	  ; start at address 0

;***************************************************
;
;	START OF PIC 16F84 CODE FOR STEP;
;
;***************************************************
;

;------------------------------------------
;****Power on reset startpoint
;------------------------------------------

;***Initialization of program	

; Set port B as output and port A as input (except bit 4)

	movlw	B'00011111'
	tris    PORTA		
	movlw	B'00000000'
	tris	PORTB		

;Clear ports and zero motors

	clrf	PORTA
	movlw	B'00010001'
	movwf	PORTB
	clrf	lastA
	clrf	lastB
	clrf	pattA
	clrf	pattB

;Loop around for a while to let everything stabilize

	movlw	d'255'
	movwf	inport
loop:	decfsz	inport, f
;	goto loop

;***Basic program loop

;Main routine - check pin states and step on negative edge
;Get port data and store, then check axis A
;A10 checks if old is 0, new is 1 (update register)
;A01 checks if old is 1, new is 0 (step and update register)
;Similarly for axis B

main:	movf	PORTA, w
	movwf	inport
A10:	btfsc	lastA, 0
	goto A01
	btfss	inport, 2
	goto A01
	bsf	lastA, 0
A01:	btfss	lastA, 0
	goto B10
	btfsc	inport, 2
	goto B10
	bcf	lastA, 0
	call stepA

B10:	btfsc	lastB, 0
	goto B01
	btfss	inport, 0
	goto B01
	bsf	lastB, 0
B01:	btfss	lastB, 0
	goto main
	btfsc	inport, 0
	goto main
	bcf	lastB, 0
	call stepB
	goto main

;------------------------------------------
;***stepA - sub to cycle axis A one half step
;  Dir of 1 is increase, else decrease

stepA:	btfss	inport, 3
	decf	pattA, f
	btfsc	inport, 3
	incf	pattA, f

;Check for pattern overflow and fix

	movf	pattA, w
	sublw	D'255'
	movlw	D'07'
	btfsc	STATUS, 2
	movwf	pattA

	movf	pattA, w
	sublw	D'08'
	btfsc	STATUS, 2
	clrf	pattA	

;Get step pattern and send to port B on bits 0-3

	movf	PORTB, w
	andlw	B'11110000'
	movwf	temp
	movf	pattA, w
	btfsc   PORTA , 4
	call dcode
	btfss	PORTA , 4
	call dfcode
	iorwf	temp, w
	movwf	PORTB

	return

;------------------------------------------
;***stepB - sub to cycle axis B one half step
;  Dir of 1 is increase, else decrease

stepB:	btfss	inport, 1
	decf	pattB, f
	btfsc	inport, 1
	incf	pattB, f

;Check for pattern overflow and fix

	movf	pattB, w
	sublw	D'255'
	movlw	D'07'
	btfsc	STATUS, 2
	movwf	pattB

	movf	pattB, w
	sublw	D'08'
	btfsc	STATUS, 2
	clrf	pattB	

;Get step pattern and send to port B on bits 4-7

	movf	PORTB, w
	andlw	B'00001111'
	movwf	temp
	swapf	temp, f
	movf	pattB, w
	btfsc   PORTA , 4
	call dcode
	btfss	PORTA , 4
	call dfcode
	iorwf	temp, f
	swapf	temp, w
	movwf	PORTB

	return

;------------------------------------------
;***stepcode - sub to generate bit pattern for number in w (!!MUST BE 0-7!!)
;  pattern is stored in w register (lower four bits) for half step pattern

dcode:	addwf	PCL, f
	retlw	B'00000001'	;0
	retlw	B'00000011'	;1
	retlw	B'00000010'	;2
	retlw	B'00000110'	;3
	retlw	B'00000100'	;4
	retlw	B'00001100'	;5
	retlw	B'00001000'	;6
	retlw	B'00001001'	;7
dfcode:	addwf	PCL, f
	retlw	B'00000001'	;0
	retlw	B'00000010'	;1
	retlw	B'00000100'	;2
	retlw	B'00001000'	;3
	retlw	B'00000001'	;4
	retlw	B'00000010'	;5
	retlw	B'00000100'	;6
	retlw	B'00001000'	;7


;Mandatory end of program command

	end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -