Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 14th February 2005, 06:04 PM   (permalink)
Experienced Member
 
raitl is on a distinguished road
Default Stepper code for PIC

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?
__________________
Here...let me improve it for you!

*Puff of smoke* ....oops...
raitl is offline   Reply With Quote
Old 14th February 2005, 11:34 PM   (permalink)
Experienced Member
 
motion is on a distinguished road
Default

Quote:
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
__________________
"Having to do with Motion Control"
motion is offline   Reply With Quote
Old 15th February 2005, 05:44 AM   (permalink)
Experienced Member
 
raitl is on a distinguished road
Default

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...
__________________
Here...let me improve it for you!

*Puff of smoke* ....oops...
raitl is offline   Reply With Quote
Old 15th February 2005, 05:55 AM   (permalink)
Super Moderator
 
Nigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to behold
Default

Quote:
Originally Posted by raitl
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.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is online now   Reply With Quote
Old 15th February 2005, 07:09 AM   (permalink)
Experienced Member
 
raitl is on a distinguished road
Default

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.
__________________
Here...let me improve it for you!

*Puff of smoke* ....oops...
raitl is offline   Reply With Quote
Old 15th February 2005, 08:06 AM   (permalink)
Super Moderator
 
Nigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to behold
Default

Quote:
Originally Posted by raitl
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.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is online now   Reply With Quote
Old 15th February 2005, 01:44 PM   (permalink)
Experienced Member
 
motion is on a distinguished road
Default

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
__________________
"Having to do with Motion Control"
motion is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes




All times are GMT. The time now is 10:40 PM.


Electronic Circuits  |  Radio Controlled
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.