Programació en mpasm del PIC 16F690

Referència Trucs Perifèrics   Recursos CITCEA
Tutorial Exemples Projectes   Inici

Modes de funcionament del motor pas a pas

Programa del grup 3

Aquest grup va codificar el sentit de gir amb l'interruptor connectat a RA5 i el mode manual amb l'interruptor connectat a RB4. En mode manual, el polsador connectat a RA2 fa avançar els passos. Els diferents modes es controlen segons la següent taula:

RB7 RB5 Mode
0 0 Aturat
0 1 Half step
1 0 Wave drive
1 1 Full step

El seu programa es basa en tenir una funció per a cada mode en la que hi ha camins diferents segons el sentit de gir i segons si és automàtic o manual. El programa final és:

#include <p16F690.inc>
	__config (_INTRC_OSC_NOCLKOUT&_WDT_OFF&_PWRTE_OFF&_MCLRE_OFF&_CP_OFF&_BOR_OFF&_IESO_OFF&_FCMEN_OFF)
	cblock	0x20
Delay					; Definim una variable de retard
Delay1					; Definim variables de comptatge
Delay2
Temp1
Temp2
Temp3
Display					
LastState				; Una variable per controlar el polsador
Counter					; I un comptador
	endc
	org 0
Inici
	bsf		STATUS,RP0	; Tria el banc 1
	bcf		OPTION_REG,7
	movlw		0xFF		; Posa l'acumulador a FFh (tot uns)
	movwf		TRISA		; Posa tots els bits del port A com a entrada
	movwf		WPUA
	movwf		TRISB
	clrf		TRISC		; Posa tots els bits del port C com a sortida
	movlw		b'00010000'	; Posa el valor 10h (0001 0000) al registre W
	movwf		ADCON1		; Copia W a la configuració del conversor A/D
					; Posa el conversor a 1/8 de la freqüència
	bcf		STATUS,RP0
	bsf		STATUS,RP1	; Tria el banc 2
	movlw		0xFF		; Posa l'acumulador a F7h (1111 0111)
	movwf		WPUB
	movlw		0x01
	movwf		ANSEL		; Posa AN0-AN2 i AN4-AN7 com entrades analògiques
	movlw		0x00
	movwf		ANSELH
	bcf		STATUS,RP0
	bcf		STATUS,RP1	; Tria el banc 0
	movlw		b'00000001'	; Posa el valor 01h (0000 0001) al registre W
	movwf		ADCON0		; activa el conversor A/D connectat a AN0
					; amb el resultat justificat per l'esquerra
	clrf		Display		; Posa la variable a valor zero
	movf		Display,w	; Copia Display a l'acumulador
	movwf		PORTC		; I ho posa als LED
	movlw		1		; Carrega l'acumulador amb 1 (01h)
	movwf		LastState	; Posa el valor a la variable LastState
					; Un 1 indica que el polsador no està premut
	clrf		Counter		; Posa la variable Counter a zero
					;
programa
	movf		PORTB,w
	andlw		b'10100000'
	movwf		Temp1
	xorlw		b'10000000'
	btfsc		STATUS,Z
	goto 		wavedrive
	movf		Temp1,w
	xorlw		b'00100000'
	btfsc		STATUS,Z
	goto 		halfstep
	movf		Temp1,w
	xorlw		b'10100000'
	btfsc		STATUS,Z
	goto 		fullstep
	goto		programa		
					;
wavedrive
	movf		PORTA,w
	andlw		b'00100000'	
	movf		Temp2
	xorlw		b'00100000'
	btfsc		STATUS,Z
	goto 		wave2	
wave1
	movf		PORTB,w
	andlw		b'00010000'	
	movf		Temp3
	xorlw		b'00010000'
	btfsc		STATUS,Z
	goto 		wave12
wave11
	call		Variaciovelocitat
	movlw		b'01000100'
	movwf		PORTC
	call		retard
	call		Variaciovelocitat
	movlw		b'00010001'
	movwf		PORTC
	call		retard
	call		Variaciovelocitat
	movlw		b'10001000'
	movwf		PORTC
	call		retard
	call		Variaciovelocitat
	movlw		b'00100010'
	movwf		PORTC
	call		retard
	goto		programa
wave12
	call		Avançpolsador	
wave120
	movlw		b'01000100'
	movwf		PORTC
	call		Avançpolsador
wave121	
	movlw		b'00010001'
	movwf		PORTC
	call 		Avançpolsador
wave122
	movlw		b'10001000'
	movwf		PORTC
	call 		Avançpolsador
wave123
	movlw		b'00100010'
	movwf		PORTC
	goto 		programa		
wave2
	movf		PORTB,w
	andlw		b'00010000'	
	movf		Temp3
	xorlw		b'00010000'
	btfsc		STATUS,Z
	goto 		wave22
wave21	
	call		Variaciovelocitat
	movlw		b'10001000'
	movwf		PORTC
	call		retard
	call		Variaciovelocitat
	movlw 		b'00010001'
	movwf		PORTC
	call		retard
	call		Variaciovelocitat
	movlw		b'01000100'
	movwf		PORTC
	call		retard
	call		Variaciovelocitat
	movlw		b'00100010'
	movwf		PORTC
	call		retard
	goto		programa
wave22
	call		Avançpolsador	
wave220
	movlw		b'10001000'
	movwf		PORTC
	call		Avançpolsador
wave221	
	movlw		b'00010001'
	movwf		PORTC
	call 		Avançpolsador
wave222
	movlw		b'01000100'
	movwf		PORTC
	call 		Avançpolsador
wave223
	movlw		b'00100010'
	movwf		PORTC
	goto 		programa
					;
halfstep
	movf		PORTA,w
	andlw		b'00100000'	
	movf		Temp2
	xorlw		b'00100000'
	btfsc		STATUS,Z
	goto 		half2
half1
	movf		PORTB,w
	andlw		b'00010000'	
	movf		Temp3
	xorlw		b'00010000'
	btfsc		STATUS,Z
	goto 		half12
half11
	call		Variaciovelocitat
	movlw 		b'10011001'
	movwf 		PORTC
	call		retard
	call 		Variaciovelocitat
	movlw 		b'10001000'
	movwf 		PORTC
	call		retard
	call		Variaciovelocitat
	movlw 		b'10101010'
	movwf	 	PORTC
	call		retard
	call		Variaciovelocitat
	movlw 		b'00100010'
	movwf 		PORTC
	call		retard
	call		Variaciovelocitat
	movlw 		b'01100110'
	movwf 		PORTC
	call		retard
	call		Variaciovelocitat	
	movlw 		b'01000100'
	movwf 		PORTC
	call		retard
	call		Variaciovelocitat
	movlw		b'01010101'
	movwf 		PORTC
	call		retard
	call		Variaciovelocitat	
	movlw 		b'00010001'
	movwf 		PORTC
	call		retard	
	goto		programa
half12
	call		Avançpolsador
half120
	movlw		b'10011001'
	movwf		PORTC
	call		Avançpolsador
half121	
	movlw		b'10001000'
	movwf		PORTC
	call 		Avançpolsador
half122
	movlw		b'10101010'
	movwf		PORTC
	call 		Avançpolsador
half123
	movlw		b'00100010'
	movwf		PORTC
	call 		Avançpolsador
half124
	movlw		b'01100110'
	movwf		PORTC
	call		Avançpolsador
half125	
	movlw		b'01000100'
	movwf		PORTC
	call 		Avançpolsador
half126
	movlw		b'01010101'
	movwf		PORTC
	call 		Avançpolsador
half127
	movlw		b'00010001'
	movwf		PORTC
	goto 		programa
half2
	movf		PORTB,w
	andlw		b'00010000'	
	movf		Temp3
	xorlw		b'00010000'
	btfsc		STATUS,Z
	goto 		half22
half21
	call		Variaciovelocitat
	movlw 		b'00010001'
	movwf	 	PORTC
	call		retard
	call		Variaciovelocitat
	movlw 		b'01010101'
	movwf 		PORTC
	call		retard
	call		Variaciovelocitat
	movlw 		b'01000100'
	movwf 		PORTC
	call		retard
	call		Variaciovelocitat
	movlw 		b'01100110'
	movwf 		PORTC
	call		retard
	call		Variaciovelocitat
	movlw 		b'00100010'
	movwf 		PORTC
	call		retard	
	call		Variaciovelocitat
	movlw 		b'10101010'
	movwf 		PORTC
	call		retard
	call		Variaciovelocitat
	movlw 		b'10001000'
	movwf 		PORTC
	call		retard
	call		Variaciovelocitat
	movlw 		b'10011001'
	movwf 		PORTC
	call		retard
	goto		programa
half22			
	call		Avançpolsador
half220
	movlw		b'00010001'
	movwf		PORTC
	call		Avançpolsador
half221	
	movlw		b'01010101'
	movwf		PORTC
	call 		Avançpolsador
half222
	movlw		b'01000100'
	movwf		PORTC
	call 		Avançpolsador
half223
	movlw		b'01100110'
	movwf		PORTC
	call 		Avançpolsador
half224
	movlw		b'00100010'
	movwf		PORTC
	call		Avançpolsador
half225	
	movlw		b'10101010'
	movwf		PORTC
	call 		Avançpolsador
half226
	movlw		b'10001000'
	movwf		PORTC
	call 		Avançpolsador
half227
	movlw		b'10011001'
	movwf		PORTC
	goto 		programa
					;
fullstep
	movf		PORTA,w
	andlw		b'00100000'	
	movf		Temp2
	xorlw		b'00100000'
	btfsc		STATUS,Z
	goto 		full2
full1
	movf		PORTB,w
	andlw		b'00010000'	
	movf		Temp3
	xorlw		b'00010000'
	btfsc		STATUS,Z
	goto 		full12
full11
	call		Variaciovelocitat
	movlw 		b'10011001' 
	movwf 		PORTC
	call		retard
	call		Variaciovelocitat	
	movlw 		b'10101010'
	movwf 		PORTC
	call		retard
	call		Variaciovelocitat
	movlw 		b'01100110'
	movwf 		PORTC
	call		retard
	call		Variaciovelocitat
	movlw 		b'01010101'
	movwf 		PORTC
	call 		retard
	goto		programa
full12
	call		Avançpolsador	
full120
	movlw		b'10011001'
	movwf		PORTC
	call		Avançpolsador
full121	
	movlw		b'10101010'
	movwf		PORTC
	call 		Avançpolsador
full122
	movlw		b'01100110'
	movwf		PORTC
	call 		Avançpolsador
full123
	movlw		b'01010101'
	movwf		PORTC
	goto 		programa		
full2
	movf		PORTB,w
	andlw		b'00010000'	
	movf		Temp3
	xorlw		b'00010000'
	btfsc		STATUS,Z
	goto 		full22
full21
	call		Variaciovelocitat
	movlw	 	b'10011001' 
	movwf 		PORTC
	call		retard
	call		Variaciovelocitat
	movlw		b'01010101'
	movwf		PORTC
	call		retard
	call		Variaciovelocitat
	movlw 		b'01100110'
	movwf 		PORTC
	call		retard
	call		Variaciovelocitat
	movlw 		b'10101010'
	movwf 		PORTC
	call		retard	
	goto 		programa
full22
	call		Avançpolsador	
full220
	movlw		b'10011001'
	movwf		PORTC
	call		Avançpolsador
full221	
	movlw		b'01010101'
	movwf		PORTC
	call 		Avançpolsador
full222
	movlw		b'01100110'
	movwf		PORTC
	call 		Avançpolsador
full223
	movlw		b'10101010'
	movwf		PORTC
	goto 		programa
Variaciovelocitat
	bsf		ADCON0,GO	; Inicia la conversió
	btfsc		ADCON0,GO	; Quan el bit sigui 0 la conversió haurà acabat
					; repetim la línia fins que deixi de ser 0
	goto		$-1		; repetim la línia fins que deixi de ser 0
	movf		ADRESH,w	; Copia els bits superiors a l'acumulador
	addlw		1		; Li suma 1 i tenim la durada variable del cicle
	movwf		Delay2		; Ho copia a la variable Delay2
	return
Avançpolsador
	btfsc		LastState,0	; Mira si està activat el bit 0 de LastState
					; Si no està activat, no fa la instrucció següent
	goto		ForUp		; Si està activat, salta
ForDown					; funció per quan el polsador està premut
	clrw				; Posa l'acumulador a zero
	btfss		PORTA,2		; Mira si està activada l'entrada A2
					; Si està activada, el polsador no està premut
					; Si està activat, no fa la instrucció següent
					; Fa la instrucció si l'entrada està desactivada
	incf		Counter,w	; Incrementa Counter i ho guarda a l'acumulador
	movwf		Counter		; Copia l'acumulador a Counter
					; Hi posa un 0 si el polsador està premut
					; i el valor incrementat si no està premut
	goto		Debounce	; Fi de la funció
ForUp					; funció per quan el polsador no està premut
	clrw				; Posa l'acumulador a zero
	btfsc		PORTA,2		; Mira si està desactivada l'entrada A2
					; Si està desactivat, no fa la instrucció següent
					; Fa la instrucció si l'entrada està activada
	incf		Counter,w	; Incrementa Counter i ho guarda a l'acumulador
	movwf		Counter		; Copia l'acumulador a Counter
					; Hi posa un 0 si el polsador no està premut
					; i el valor incrementat si està premut
Debounce
	movf		Counter,w	; Copia Counter a l'acumulador
	xorlw		5		; Si val 5 posa 0 a w
	btfss		STATUS,Z	; Si w es 0 no fa la instrucció següent
	goto		Delay1mS	; Si es 1, ja estem
	comf		LastState,f	; Complementa (intercanvia uns i zeros) LastState
	clrf		Counter		; Posa Counter a zero
	btfss		LastState,0	; Mira si LastState era 1 (premut)
	goto		Delay1mS	; Si no ho era, ja estem
	return
Delay1mS				; Bucle de retard
	movlw		.71		; Carrega l'acumulador amb 71
	movwf		Delay		; I ho copia a Delay
	decfsz		Delay,f		; Bucle de retard de 0,215 ms
	goto		$-1		
	decfsz		Delay,f		; Bucle de retard de 0,786 ms
	goto		$-1		; En total 1 ms
	goto		Avançpolsador
retard
	nop
	nop
	nop
	nop
	nop
	nop
	decfsz 		Delay1,f
	goto 		retard
	decfsz 		Delay2,f
	goto 		retard	
	return
	end

 

 

Llicència de Creative Commons
Aquesta obra d'Oriol Boix està llicenciada sota una llicència no importada Reconeixement-NoComercial-SenseObraDerivada 3.0.