Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Stepper code for PIC

Status
Not open for further replies.

raitl

New Member
Hi.

Writing a code for my bot and I ran into this problem...
The following is a piece of my code for moving the stepper motors.

Code:
STEPS  macro  number
 movlw  number
 movwf  STEPCOUNT
 endm

STEPPER  macro  dir
 movlw  dir
 movwf  PORTB
 WAIT  .100
 DECF  STEPCOUNT,1 
 btfsc  STATUS,Z
 goto  loop
 endm


and to move 10 steps forward:


Code:
STEPS .10

forward
 STEPPER  0x88
 STEPPER  0x9A
 STEPPER  0x12
 STEPPER  0x56
 STEPPER  0x44
 STEPPER  0x65
 STEPPER  0x21
 STEPPER  0xA9
goto  forward

The problem is, that how can I save the motors position so I can pick up where I left off after doing something else?
 
The problem is, that how can I save the motors position so I can pick up where I left off after doing something else?

Use a counter to store the last motor state. Use this counter to index to a look-up table containing the above values.

Code:
STEPPER:
	call	GET_MOTOR_STATE
	movwf	PORTB
	wait	.100
	decfsz	STEPCOUNT,f
	goto	STEPPER
	return
GET_MOTOR_STATE:
	incf	COUNTER,f
	movf	COUNTER,w
	andlw	b'00000111'
	addwf	PCL,F
	retlw  	0x88 
	retlw  	0x9A 
	retlw  	0x12 
	retlw  	0x56 
	retlw  	0x44 
	retlw  	0x65 
	retlw  	0x21 
	retlw  	0xA9
 
but what happens when I need to turn after moving forward? Let's say the mottors stopped at 0x21. There won't be such position in left-turn table...
 
raitl said:
but what happens when I need to turn after moving forward? Let's say the mottors stopped at 0x21. There won't be such position in left-turn table...

You use two tables, one for forwards, one for reverse. The two tables have the same values in reverse order - you simply start the reversing from the same table value.

So if your forward table is stopped at 0x21, you need to start at 0x21 in the reverse table.
 
yes, but for turning the values are 22,44,88,AA,99,11

I was thinking of having separate tables for each stepper. Like 0x8,0x9,0xA for one stepper ans 0x08, 0x09, 0x0A for the other and adding up the two values to get the actual portb value.
 
raitl said:
yes, but for turning the values are 22,44,88,AA,99,11

I was thinking of having separate tables for each stepper. Like 0x8,0x9,0xA for one stepper ans 0x08, 0x09, 0x0A for the other and adding up the two values to get the actual portb value.

I never thought you were doing it any other way?, you have forward and reverse tables, and that's it - if you want to control two stepper motors on a single port you simply combine the two outputs to give a single 8 bit value - or, read the port, and combine the new (single motor) value with the other motors value (see ANDWF etc.), and write back to the port.
 
You need two separate counters, one for each motor. To move forward, you increment the counter. To move in reverse, you decrement. After increment/decrement, call GET_MOTOR_STATE to obtain the motor phase (once per motor/ twice for two motors). You then merge the values and output to PORTB.

Code:
STEPPER:
   btfsc   MOTOR_DIR1 
   incf    COUNTER1,f
   btfss   MOTOR_DIR1 
   decf    COUNTER1,f
;
   movf    COUNTER1,w
   call    GET_MOTOR_STATE
   movwf   TEMP_REG
   swapf   TEMP_REG,f
;
   btfsc   MOTOR_DIR2 
   incf    COUNTER2,f
   btfss   MOTOR_DIR2 
   decf    COUNTER2,f
;
   movf    COUNTER2,w
   call    GET_MOTOR_STATE
   iorwf   TEMP_REG,w
   movwf   PORTB
;
   wait    .100
   decfsz  STEP_COUNT,f
   goto    STEPPER
;
   return

GET_MOTOR_STATE: 
   andlw   b'00000111' 
   addwf   PCL,F 
   retlw   0x8 
   retlw   0xA 
   retlw   0x2 
   retlw   0x6 
   retlw   0x4 
   retlw   0x5 
   retlw   0x1 
   retlw   0x9
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top