Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; File STEP508.ASM
- ; ... for PIC12f508 microcontroller
- ; Program to use PIC as a step and direction controller for a unipolar
- ; step motor. Step and direction pins are GPIO-5, GPIO-3; GPIO_0, GPIO_1, GPIO_2, GPIO_4, are ; the windings; in order (driven by NPN small sig transistors or MOSFETS)
- ; Steps on negative going edge of step pulse.
- ; CPU configuration
- ; (It's a 12f508, Internal RC oscillator,
- ; watchdog timer off, power-up timer on)
- LIST P=12f508
- processor 12f508
- include <p12f508.inc>
- __CONFIG _OSC_IntRC & _WDT_OFF & _CP_OFF & _MCLRE_OFF
- ; 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)
- inport equ H'11' ;Value of port A when read (stored for later access)
- temp equ H'12'
- #DEFINE STEP inport,5 ; Step pulse input
- #DEFINE DIR inport,3 ; Direction Input
- OPMASK EQU B'11000000'
- IOMASK EQU B'00101000' ; all bits output (except GP3 and GP5)
- ; GP0 , GP1 , GP2 , GP4 controls The stepper Coils
- ; GP3 controls direction
- ; GP5 Controls Step Pulses
- ORG 0
- ; start of main code
- ;***************************************************
- ;
- ; START OF PIC 12f508 CODE FOR STEP;
- ;
- ;***************************************************
- ;
- ;------------------------------------------
- ;****Power on reset startpoint
- ;------------------------------------------
- ;***Initialization of program
- MOVWF OSCCAL
- MOVLW OPMASK
- OPTION
- ; Set GPIO for input & output
- MOVLW IOMASK
- TRIS GPIO
- ;Clear port and zero motors
- movlw B'00000001'
- movwf GPIO
- clrf lastA
- clrf pattA
- ;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 GPIO, w
- movwf inport
- CLRWDT
- A10: btfsc lastA, 0
- goto A01
- btfss STEP
- goto A01
- bsf lastA, 0
- A01: btfss lastA, 0
- goto B10
- btfsc STEP
- goto B10
- bcf lastA, 0
- goto stepA
- B10: goto main
- ;------------------------------------------
- ;***stepA - sub to cycle axis A one Full step
- ; Dir of 1 is increase, else decrease
- stepA: btfss DIR
- decf pattA, f
- btfsc DIR
- incf pattA, f
- ;Check for pattern overflow and fix
- movf pattA, w
- XORLW D'255'
- movlw D'07'
- btfsc STATUS, Z
- movwf pattA
- movf pattA, w
- XORLW D'08'
- btfsc STATUS, Z
- clrf pattA
- ;Get step pattern and send to GPIO on bits 0-1-2-4
- movf pattA, w
- call dcode
- movwf GPIO
- goto main
- ;------------------------------------------
- ;***stepcode - sub to generate bit pattern for number in w (!!MUST BE 0-7!!)
- ; pattern is stored in w register
- dcode: addwf PCL, f ; Use below column for the half Step
- retlw B'00000001' ;0 retlw B'00000001' ;0
- retlw B'00000010' ;1 retlw B'00000011' ;1
- retlw B'00000100' ;2 retlw B'00000010' ;2
- retlw B'00010000' ;3 retlw B'00000110' ;3
- retlw B'00000001' ;4 retlw B'00000100' ;4
- retlw B'00000010' ;5 retlw B'00010100' ;5
- retlw B'00000100' ;6 retlw B'00010000' ;6
- retlw B'00010000' ;7 retlw B'00010001' ;7
- ;Mandatory end of program command
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement