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

📄 motors.asm

📁 代码保护功能处于持续发展中。Microchip 承诺将不断改进产品的代码保护功能。任何试图破坏Microchip 代码保护功能的行为均可视 为违反了《数字器件千年版权法案(Digital Mille
💻 ASM
字号:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;	Project:		Interfacing PICs 
;	Source File Name:	MOTORS.ASM		
;	Devised by:		MPB		
;	Date:			19-8-05
;	Status:			Working
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; 	Demonstrates DC, SERVO & STEPPER MOTOR control
;	Select motor and direction using push button inputs
;	DC Motor PWM speed control - working
;	DC Servo position control - rollover not fixed
;	Stepper direction control - working
;	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	PROCESSOR 16F877A
;	Clock = XT 4MHz, standard fuse settings
	__CONFIG 0x3731

;	LABEL EQUATES	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	INCLUDE "P16F877A.INC" 	
	; standard register labels 	

;----------------------------------------------------------
; User register labels
;----------------------------------------------------------

Count1	EQU	20		; delay counter
Count2	EQU	21		; delay counter
Target	EQU	22		; servo target position

;----------------------------------------------------------
; PROGRAM BEGINS
;----------------------------------------------------------

	ORG	0		; Default start address 
	NOP			; required for ICD mode

;----------------------------------------------------------	
; Port & PWM setup

init	NOP
	BANKSEL	TRISB		; Select control registers
	CLRF	TRISC		; Output for dc motors
	CLRF	TRISD		; Output for stepper
	MOVLW	B'00000010'	; Analogue input setup code
				; PortA = analogue inputs
				; Vref = Vdd
	MOVWF	ADCON1		; Port E = digital inputs
	MOVLW	D'249'		; PWM = 4kHz
	MOVWF	PR2		; TMR2 preload value	

	BANKSEL PORTB		; Select output registers
	CLRF	PORTC		; Outputs off
	CLRF	PORTD		; Outputs off
	MOVLW	B'01000001'	; Analogue input setup code
	MOVWF	ADCON0		; f/8, RA0, done, enable  
	MOVLW	D'128'		; intial servo position
	MOVWF	Target		

;----------------------------------------------------------
; MAIN LOOP
;----------------------------------------------------------

but0	BTFSC	PORTE,0		; wait for select button
	GOTO	but0

	MOVLW	B'00001100'	; Select PWM mode
	MOVWF	CCP1CON		; 
	MOVLW	D'128'		; PWM = 50%
	MOVWF	CCPR1L		;

but1	BTFSS	PORTE,0		; wait for button release
	GOTO	but1
	CALL	motor		; check for speed change
	BTFSC	PORTE,0		; wait for select button
	GOTO	but1
	MOVLW	B'00000000'	; deselect PWM mode
	MOVWF	CCP1CON		; 
	CLRF	PORTC		; switch off outputs

but2	BTFSS	PORTE,0		; wait for button release
	GOTO	but2
	CALL	servo		; move servo cw or ccw
	BTFSC	PORTE,0		; wait for select button
	GOTO	but2
	CLRF	PORTC		; switch off servo

but3	BTFSS	PORTE,0		; wait for button release
	GOTO	but3
	CALL	step		; output one step cycle 	
	BTFSC	PORTE,0		; wait for select button
	GOTO	but3		
	CLRF	PORTD		; disable stepper outputs
	GOTO	but0		; start again

;-----------------------------------------------------------
; SUBROUTINES
;-----------------------------------------------------------

; Change dc motor speed by one step and wait 1ms
; to debounce and control rate of change....................

motor	BSF	PORTC,1		; switch on motor LED

	BTFSS	PORTE,1		; inc speed?
	INCF	CCPR1L		; yes
	MOVLW	D'248'		; max speed?
	SUBWF	CCPR1L,W	
	BTFSS	STATUS,Z	
	GOTO	lower		; no
	DECF	CCPR1L		; yes - dec speed


lower	BTFSS	PORTE,2		; dec speed?
	DECFSZ	CCPR1L		; yes - min speed?
	GOTO	done		; no 
	INCF	CCPR1L		; yes - inc speed

done	CALL	onems		; 1ms debounce
	RETURN

; Move servo 10 bits cw or ccw................................

servo	BSF	PORTC,4		; switch on servo LED
	BSF	PORTC,7		; enable drive chip
	BTFSC	PORTE,1		; move forward?
	GOTO	rev		; no

wait1	BTFSS	PORTE,1		; yes- wait for button..
	GOTO	wait1		; ..release
	MOVLW	D'10'		; add 10... 
	ADDWF	Target		; ..to servo target position
	BSF	PORTC,5		; move..
	BCF	PORTC,6		; .. forward

getfor	CALL	getADC		; get position
	BSF	STATUS,C	; set carry flag
	MOVF	Target,W	; load position
	SUBWF	ADRESH		; compare with target 
	BTFSS	STATUS,C	; far enough?
	GOTO	getfor		; no - repeat
	
	BCF	PORTC,5		; yes - stop
	MOVLW	D'250'		; wait 250ms ..
	CALL	xms		; .. before next step
rev	BTFSC	PORTE,2		; move reverse?
	RETURN			; no

wait2	BTFSS	PORTE,2		; yes- wait for button..
	GOTO	wait2		; ..release
	MOVLW	D'10'		; yes - sub 10 from... 
	SUBWF	Target		; .. servo target position
	BCF	PORTC,5		; move ..
	BSF	PORTC,6		; ..reverse

getrev	CALL	getADC		; get position
	BSF	STATUS,C	; set carry flag
	MOVF	Target,W	; load position
	SUBWF	ADRESH		; compare with target 
	BTFSC	STATUS,C	; far enough?
	GOTO	getrev		; no - repeat
	
	BCF	PORTC,6		; yes - stop
	MOVLW	D'250'		; wait 250ms ..
	CALL	xms		; .. before next step
	RETURN

; Output one cycle of stepper clock.........................

step	BSF	PORTD,0		; switch on stepper LED
	BSF	PORTD,1		; enable stepper drive
	
	BTFSS	PORTE,1		; test cw button
	BSF	PORTD,2		; select clockwise
	BTFSS	PORTE,2		; test ccw button
	BCF	PORTD,2		; select counter-clockwise

	BSF	PORTD,3		; clock high
	MOVLW	D'25'		; load delay time
	CALL	xms		
	BCF	PORTD,3		; clock low
	MOVLW	D'25'		; load delay time
	CALL	xms		

	RETURN

; Stepper software delay ...................................

xms	MOVWF	Count2		; receive x ms in W
down2	CALL	onems
	DECFSZ	Count2
	GOTO	down2
	RETURN

onems	MOVLW	D'249'		; delay one millisec
	MOVWF	Count1
down1	NOP
	DECFSZ	Count1
	GOTO	down1
	RETURN

; Read ADC input and store .................................

getADC	BSF	ADCON0,GO	; start ADC..
wait	BTFSC	ADCON0,GO	; ..and wait for finish
	GOTO	wait
	
	MOVF	ADRESH,W	; store result, high 8 bits
	RETURN		  		
	
;----------------------------------------------------------
	END			; of source code
;----------------------------------------------------------

⌨️ 快捷键说明

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