Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#INCLUDE<plib.h>
#define MS_1 BIT_1 // Microstepping bit
#define MS_2 BIT_2
#define stepperAquater() (LATBSET = MS_1 , LATBCLR = MS_2 )
#define stepperAeighth() (LATBSET = MS_1 , LATBSET = MS_2 )
#define stepperAhalf() (LATBCLR = MS_1 , LATBSET = MS_2 )
#define mstepperAdis() (LATECLR = BIT_4)
#define mstepperAena() (LATESET = BIT_4)
#define mstepperArev() (LATECLR = BIT_5)
#define mstepperAfor() (LATESET = BIT_5)
#define mstepperAoff() (LATECLR = BIT_6)
#define mstepperAon() (LATESET = BIT_6)
int old = 0;
void InitializePorts(void)
{
TRISEBits.TRISE2 = 1 ; // make as a input for encoder
TRISEBits.TRISE3 = 1 ; // make as a input for encoder
TRISEBits.TRISE4 = 0 ; // make as an output for stepper motor
TRISEBits.TRISE5 = 0 ; // make as an output for stepper motor
TRISEBits.TRISE6 = 0 ; // make as an output for stepper motor
return;
}
void InitializeStepper(void)
{
mstepperAoff(); // stepper off
mstepperAfor() ; // stepper forward
mstepperAena() ; // stepper enable
return;
}
void Encoder (void)
{
int new = 0 ;
int value = 0 ;
new = PORTE ; // read the current port status
value = new ^ old ; // flag each port bit that has changed
if(value & 0x08) // if bit 3 has changed since last time
{
if (new & 0x08) // if bit 3 is set
{
if (new & 0x04) // if bit 2 is set
{
mstepperAfor() ; // stepper forward
}
else // else bit 3 is clear
{
mstepperArev() ; // stepper reverse
}
stepperAhalf();
mstepperAon(); // stepper on
Delay(20);
mstepperAoff(); // stepper off
}
}
old = new; // save current port status
return;
}
void main(void)
{
InitializePorts();
InitializeStepper();
while(1)
{
Encoder();
}
}
More questions.
- What's the stepper motor driver? What's the meaning of it's signals?
yes i want .- I don't fully understand what you are doing with the encoder. Do you want the stepper motor follow the encoder movement?
- The code doesn't look like a good quadrature decoder algorithm. It would be expect to process all encoder signal edges, you are only counting one out of four.
yes i am using discrete H-bridge sir.
It's L298 and L297 based and this configuration u can easily see there is a five control pin as i described above
Not actually clear. L297 can be switched between full and half step mode. So what's the purpose of the "quarter" and "eight" output codes, how they are connected? Also the function of mstepperAon() and mstepperAoff() isn't self explanatory. I can just guess that in constrast to their naming they are controlling the L297 clock input?It's L298 and L297 based and this configuration u can easily see there is a five control pin as i described above .
What's particularly unclear? Encoder signal edge means that one of the two quadrature encoder signals is changing state, either rising or falling. A good decoder algorithm is acting on any signal change. counting 4 steps for a full encoder period.i don't understand with this sentence ? please tell me more detail
According to your code, only rising edges of bit are counted.my decoder algorithm is change on any out of two encoder pin pulse stream on driver working perfectly .
mstepperAon() and mstepperAoff() are controlling the STEP input?but i have a another driver A3977
mstepperAon() and mstepperAoff() are controlling the STEP input?