Using an Encoder for Controlling Movement of a Stepper Motor
Lucy
Posts: 1
I am new to the SX and SX/B, and I need help.· I want to use a 64 PPR quadrature encoder to tell a bipolar stepper motor when to move and in what direction, either clockwise or counterclockwise.· When the encoder stops moving, I want the stepper motor to stop moving.· I know the SX is supposed to be able to keep up, but for some reason, it’s not seeing all of the encoder pulses.
·
I am using a stepper motor driver, so I only send a high or low pulse to its Enable, Step, and Direction pins using the SX.
·
This is my set-up:
Encoder – Phase A:····· PIN RA.1 and pull-up resistor (1k) to VDD
Encoder – Phase B:······ PIN RA.0 and pull-up resistor (1k) to VDD
Driver – Enable:··········· PIN RB.5
Driver – Step:·············· PIN RB.6
Driver – Direction:······· PIN RB.7
·
Does anyone see any problems with the program?· I used the example in the SX/B help file for quadrature input as a starting point.
·
Here is copy of my program.
·
DEVICE········· SX28, OSCHS3, TURBO, STACKX, OPTIONX
FREQ··········· 50_000_000
'
' IO Pins
'
EncPort··································· PIN······ RA
TRIS_Enc······· ··········· ··········· VAR···· TRIS_A
MotorEnable················ PIN······ RB.5··
MotorStep······· ··········· PIN······ RB.6··
MotorDirection····· ······· PIN ······RB.7··
'
' Constants
'
Yes················· CON··· 1
No··················· CON··· 0
'
' Variables
'
encOld········· ··· VAR···· Byte
encNew················· VAR···· Byte
tmpB4························· VAR··· Byte
'
· INTERRUPT
'
ISR_Start:
encNew = EncPort & %00000011········
tmpB4 = encOld XOR encNew
IF tmpB4 > 0 THEN················
··· encOld = encOld << 1
··· encOld = encOld XOR encNew
··· IF encOld.1 = 1 THEN·········
········ INC encVal
··· ELSE········ ·············
········ DEC encVal
··· ENDIF
ENDIF
encOld = encNew
ENDIF
·
ISR_Exit:
· RETURNINT
'=====================================================================
· PROGRAM Start '=====================================================================
·
'
' Program Code
'
Start:
LOW MotorEnable
encNew = EncPort & %00000011
encOld = encNew
Option = $88
·
Main:
IF encOld.1 = 1 THEN
········ HIGH MotorDirection··············· 'clockwise
········ PULSOUT MotorStep, 200
ELSE
········ LOW MotorDirection··················'counterclockwise
········ PULSOUT MotorStep, 200
ENDIF
GOTO Main
END
·
I am using a stepper motor driver, so I only send a high or low pulse to its Enable, Step, and Direction pins using the SX.
·
This is my set-up:
Encoder – Phase A:····· PIN RA.1 and pull-up resistor (1k) to VDD
Encoder – Phase B:······ PIN RA.0 and pull-up resistor (1k) to VDD
Driver – Enable:··········· PIN RB.5
Driver – Step:·············· PIN RB.6
Driver – Direction:······· PIN RB.7
·
Does anyone see any problems with the program?· I used the example in the SX/B help file for quadrature input as a starting point.
·
Here is copy of my program.
·
DEVICE········· SX28, OSCHS3, TURBO, STACKX, OPTIONX
FREQ··········· 50_000_000
'
' IO Pins
'
EncPort··································· PIN······ RA
TRIS_Enc······· ··········· ··········· VAR···· TRIS_A
MotorEnable················ PIN······ RB.5··
MotorStep······· ··········· PIN······ RB.6··
MotorDirection····· ······· PIN ······RB.7··
'
' Constants
'
Yes················· CON··· 1
No··················· CON··· 0
'
' Variables
'
encOld········· ··· VAR···· Byte
encNew················· VAR···· Byte
tmpB4························· VAR··· Byte
'
· INTERRUPT
'
ISR_Start:
encNew = EncPort & %00000011········
tmpB4 = encOld XOR encNew
IF tmpB4 > 0 THEN················
··· encOld = encOld << 1
··· encOld = encOld XOR encNew
··· IF encOld.1 = 1 THEN·········
········ INC encVal
··· ELSE········ ·············
········ DEC encVal
··· ENDIF
ENDIF
encOld = encNew
ENDIF
·
ISR_Exit:
· RETURNINT
'=====================================================================
· PROGRAM Start '=====================================================================
·
'
' Program Code
'
Start:
LOW MotorEnable
encNew = EncPort & %00000011
encOld = encNew
Option = $88
·
Main:
IF encOld.1 = 1 THEN
········ HIGH MotorDirection··············· 'clockwise
········ PULSOUT MotorStep, 200
ELSE
········ LOW MotorDirection··················'counterclockwise
········ PULSOUT MotorStep, 200
ENDIF
GOTO Main
END
Comments
Bean
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"The welfare of the people in particular has always been the alibi of tyrants." ~ Camus
www.iElectronicDesigns.com
·
I've also used an SX48 to watch and encoder and drive a DC motor using the PWM for another project with excellent results.
The only issue I see that may cause problems with your project is the resolution of your stepper motor in relation to your encoder. If the resolution of them is different then you'll need to account for that in your program. An example would be if the encoder was higher resolution than the stepper and not divisible by the same amount you may get a variation in the number of encoder counts per stepper motor pulse. As long as you are aware of it that may not be a problem.
Robert
I have little experience with the SX, although I have several.
However, I use two BS2s to keep track of the encoders on the gear motors which turn the tracks of Ugly Buster. I learned early on that in my configuration when one track falls behind the other it is best to speed the slow one up until it catches or is greater than the other. Thus, it is neat to hear and watch the encoders' BS2s telling the master where they are and having the master send speed instructions to two more BS2s that control two HB-25s which, in turn, drive the gear motors.
My machine currently has five microcontrollers. I imagine a single SX could easily replace all five.
--Bill
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
You are what you write.