Hello,
Is better to use ISR(PCINT2_vect) or attachInterrupt on pins 2, 3 on CHANGE event?
I need this to increase the resolution of an optical encoder
Is there any way to increase the speed of the code?
Here is the code with both variants in it
#define MASK_A1 0b100 //(1<<2)
#define MASK_A2 0b1000 //(1<<3)
#define MASK_B1 0b10000 //(1<<4)
#define MASK_B2 0b100000 //(1<<5)
volatile int PosA = 0;
volatile int PosB = 0;
volatile byte A1 = 0;//Status of pin 4 H or L
volatile byte A2 = 0;//Status of pin 5 H or L
volatile byte B1 = 0;//Status of pin 4 H or L
volatile byte B2 = 0;//Status of pin 5 H or L
void Setup45()
{
if (PIND & MASK_A1)//replace attachInterrupt(0, UpdateA, CHANGE);
A1 = 1;
if (PIND & MASK_A2)//replace attachInterrupt(1, UpdateB, CHANGE);
A2 = 1;
if (PIND & MASK_B1)
B1 = 1;
if (PIND & MASK_B2)
B2 = 1;
PCICR |= (1 << PCIE2);//Interrupt on pin change FROM PCINT16 to PCINT23 Digital
PCMSK2 = (1 << PCINT18) | (1 << PCINT19) | (1 << PCINT20) | (1 << PCINT21); // digital pin 4 and 5
}
void setup()
{
//in this case myPinX is the same as Bit
DDRD &= ~(1 << 2);//Set pin 2 as input
DDRD &= ~(1 << 3);//Set pin 3 as input
DDRD &= ~(1 << 4);//Set pin 4 as input
DDRD &= ~(1 << 5);//Set pin 5 as input
DDRD |= (1 << 6); //Set pin 6 as output
PORTD |= B01111100;//Turn On 20KOhm resistors on 2,3,4,5,6 (HIGH)
//Is better to use ISR(PCINT2_vect) or attachInterrupt on pins 2, 3 ?
// attachInterrupt(0, UpdateA, CHANGE);// encoder pin on interrupt 0 - pin 2
// attachInterrupt(1, UpdateB, CHANGE);// encoder pin on interrupt 1 - pin 3
Serial.begin (115200);
Serial.println("start");
}
void loop()
{
Serial.println(PosA, DEC);// debug - remember to comment out
Serial.println(PosB, DEC);// debug - remember to comment out
}
ISR(PCINT2_vect)//is the function called by the interrupt on PCINT2
{
if ((PIND & MASK_A1) != A1)//if digital PIN2 has changed
{
A1 = PIND & MASK_A1;//Set new status
if (!((PIND & MASK_B1) ^ A1))
PosA++;//If (A1 = 0 and B1 = 1) or (A1 = 1 and B1 = 0)
else
PosA--;//If (A1 = 0 and B1 = 0) or (A1 = 1 and B1 = 1)
}
if ((PIND & MASK_A2) != A2)//if digital PIN3 has changed
{
A2 = PIND & MASK_A2;//Set new status
if (!((PIND & MASK_B2) ^ A2))
PosB++;//If (A2 = 0 and B2 = 1) or (A2 = 1 and B2 = 0)
else
PosB--;//If (A2 = 0 and B2 = 0) or (A2 = 1 and B2 = 1)
}
if ((PIND & MASK_B1) != B1)//if digital PIN4 has changed
{
B1 = PIND & MASK_B1;//Set new status
if (!((PIND & MASK_A1) ^ B1))
PosA++;//If (A1 = 0 and B1 = 1) or (A1 = 1 and B1 = 0)
else
PosA--;//If (A1 = 0 and B1 = 0) or (A1 = 1 and B1 = 1)
}
if ((PIND & MASK_B2) != B2)//if digital PIN5 has changed
{
B2 = PIND & MASK_B2;//Set new status
if (!((PIND & MASK_A2) ^ B2))
PosB++;//If (A2 = 0 and B2 = 1) or (A2 = 1 and B2 = 0)
else
PosB--;//If (A2 = 0 and B2 = 0) or (A2 = 1 and B2 = 1)
}
}
// void UpdateA()
// {
// if (!((PIND & MASK_A1) ^ (PIND & MASK_B1)))
// PosA++;//If (A1 = 0 and B1 = 1) or (A1 = 1 and B1 = 0)
// else
// PosA--;//If (A1 = 0 and B1 = 0) or (A1 = 1 and B1 = 1)
// }
// void UpdateB()
// {
// if (!((PIND & MASK_A2) ^ (PIND & MASK_B2)))
// PosB++;//If (A2 = 0 and B2 = 1) or (A2 = 1 and B2 = 0)
// else
// PosB--;//If (A2 = 0 and B2 = 0) or (A2 = 1 and B2 = 1)
// }