Guys, I have problem which is the interrupt routine seems is not working. This is 4X quadrature encoding.Can help me to check whether there is any error in my programming code? The interrupt is used to count the position of the motor. Motor used is SPG30E-200K and it is connected to PIC as in the schematic diagram in the detail of the motor.
- CODE: SELECT_ALL_CODE
#define IN3 PortC.F4 // IN3 and IN4 is refering to the ports of L293D motor driver
#define IN4 PortC.F7
#define A PortB.F1 // A & B is the ports of encoder from the DC motor
#define B PortB.F2
#define EN2 CCPR2L
unsigned long int volt1,volt2,state0=0,state1=0;
signed long int CountValue=0;
void interrupt (void){
if(INTCON3.INT1IF==1)
{
INTCON2.INTEDG1^=1;
INTCON3.INT1IF=0;
}
else if (INTCON3.INT2IF==1)
{
INTCON2.INTEDG2^=1;
INTCON3.INT2IF=0;
}
state1=((B*2)+A);
switch(state1)
{
case 0:
if(state0==1) CountValue++;
else CountValue--;
break;
case 1:
if(state0==3) CountValue++;
else CountValue--;
break;
case 2:
if(state0==0) CountValue++;
else CountValue--;
case 3:
if(state0==2) CountValue++;
else CountValue--;
break;
}
State0=state1;
}
void main(void)
{
TRISA = 0b11100111;
TRISB = 0b00000110;
TRISC = 0b00000000;
ADCON0 = 0b01000001;
ADCON1 = 0b11000000;
CCP2CON = 0b00001100;
PR2 = 0xFF;
T2CON = 0b00000101;
INTCON2.INTEDG1 = 1;
INTCON2.INTEDG2 = 1; //Enable Change on INT1 interupt
INTCON3.INT1IP = 1;
INTCON3.INT2IP = 1; //Enable Change on INT1 interupt LOW PRIORITY
INTCON3.INT1IF = 1;
INTCON3.INT2IF = 1; //Clear flag
INTCON3.INT1IE = 1;
INTCON3.INT2IE = 1; //Enable INT1 interupt source
INTCON.GIEL = 1; //Enable All low priority interupts
INTCON.GIEH = 1; //Enable All interupts
INTCON.RBIE = 1; //Enable Change on PortB interupt
while(1)
{
volt1 = ADC_Read(0);
delay_ms(20);
volt2 = ADC_Read(1);
delay_ms(20);
if(volt1>=205 || volt2>=205)
{
if (volt1<=(volt2+5)&&volt1>=(volt2-5))
delay_ms(5000);
else if (volt1>volt2)
{
IN3=1;
IN4=0;
delay_ms(20);
EN2=200;
IN3=0;
delay_ms(100);
}
else if(volt1<volt2)
{
IN4=1;
IN3=0;
delay_ms(20);
EN2=200;
IN4=0;
delay_ms(100);
}
}
else
{
if(CountValue==0)
delay_ms(15000);
else if(CountValue > 0)
{
do
{
IN3=1;
IN4=0;
EN2=200;
IN3=0;
delay_ms(20);
}
while(CountValue!=0);
}
else if(CountValue < 0)
{
do
{
IN3=0;
IN4=1;
EN2=200;
IN4=0;
delay_ms(20);
}
while(CountValue!=0);
}
}
}
}