int count1=0; // This count use in interrupt routine
int count2=0;
void interrupt() // enter when RB0 and RB1 Rising
{
if(INTCON.INT0IF==1)
{
if(portb.f0&portb.f2) // By checking B0 (YELLOW) and B2(WHITE), Position and Direction Find
{count1++; // If "+" direction
}
else // If "-" direction
{count1--;
}
INTCON.INT0IF=0;
}
if(INTCON3.INT1IF==1)
{
if(portb.f1&portb.f3) // Check RB1 and RB3 for SECOND ENCODER
{count2++;
}
else
{count2--;
}
INTCON3.INT1IF=0;
}
// Clear flag
}
void main()
{
Pwm_Init(5000);
PWM_Start();
Pwm_Change_Duty(200);
TRISB=0b11111111; // B0, B1, B2, B3 encoder inputs
TRISD=0b00000000; // Motor Control Pin
INTCON.GIE=1; // enable RB0 interrupts
INTCON.INT0IE=1;
INTCON.INT0IF=0;
INTCON2.RBPU=0; // Enable portB internal PULL-UP resistors
INTCON2.INTEDG0=1; // RB0 RB1 edge selection
INTCON2.INTEDG1=1;
// INTCON3.INT1IP=1;
INTCON3.INT1IE=1; // Enable RB1 INT
INTCON3.INT1IF=0;
portd.f1=1; // Motor drive to "+"
portd.f0=0; //motor(B-diode/// R-L298)
while(1)
{
if (count1>2096) // Count exceeds 2096(1 round), Stop
{
portd.f1=0;
portd.f0=0;
}
if(count2>2096)
{
portd.f1=0;
portd.f0=0;
}
}
}