#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();
}
}