Ports and interruptions in Arduino Mega - Rotary encoder library

Hi all !

I'm using an encoder library, to read a rotary encoder using 2 analog ports. The library is : GitHub - mathertel/RotaryEncoder: RotaryEncoder Arduino Library

The example given uses pins A2 and A3, but in my project I need to attach the encoder to ports A13 and A14 of my Arduino Mega.

This is the setup part of the original example "InterruptRotator":

// Setup a RoraryEncoder for pins A2 and A3:
RotaryEncoder encoder(A2, A3);

void setup()
{
  Serial.begin(57600);
  Serial.println("SimplePollRotator example for the RotaryEncoder library.");

  // You may have to modify the next 2 lines if using other pins than A2 and A3
  PCICR |= (1 << PCIE1);    // This enables Pin Change Interrupt 1 that covers the Analog input pins or Port C.
  PCMSK1 |= (1 << PCINT10) | (1 << PCINT11);  // This enables the interrupt for pin 2 and 3 of Port C.
}

I've tried to find the way to adapt this to my project as it follows :

// Setup a RoraryEncoder for pins A13 and A14:
RotaryEncoder encoder(A13, A14);

void setup()
{
  Serial.begin(57600);
  Serial.println("SimplePollRotator example for the RotaryEncoder library.");

  // You may have to modify the next 2 lines if using other pins than A13 and A14
  PCICR |= (1 << PCIE2);    // This enables Pin Change Interrupt 1 that covers the Analog input pins or Port K.
  PCMSK2 |= (1 << PCINT21) | (1 << PCINT22);  // This enables the interrupt for pin 5 and 6 of Port K.
} // setup()

But doesn't work, each time I turn one encoder step, the initial message appears 4 times as the program was restarting 4 times. The same example works for ports A2 and A3

For sure I'm missing something because I don't really know how this interrupt enable works, but I can't find more information.

Thanks for your help,

Eloi

Just a correction.
The example works fine with pins A2 and A3 in a Arduino UNO, not in a MEGA.

enoughframes:
For sure I'm missing something because I don't really know how this interrupt enable works, but I can't find more information.

I found a list of the pinChange enabled pins on the MEGA here:
https://code.google.com/p/arduino-pinchangeint/wiki/Logic#ATmega2560_Available_Pins_and_PORTS

For pins A13 and A14 you'd have to enable the bits 5 and 6 in the PORTK register to create interrupts.
And you must use the correct interrupt vector for the interrupt handling code.

For pins A13 and A14 you'd have to enable the bits 5 and 6 in the PORTK register to create interrupts.

You mean that ?

  PCICR |= (1 << PCIE2);    // This enables Pin Change Interrupt 1 that covers the Analog input pins or Port K.
  PCMSK2 |= (1 << PCINT21) | (1 << PCINT22);  // This enables the interrupt for pin 5 and 6 of Port K.

And you must use the correct interrupt vector for the interrupt handling code.

I'm not sure what does it mean . There is a part of the example code that I think is related with this. Do I have to change this ?

// The Interrupt Service Routine for Pin Change Interrupt 1
// This routine will only be called on any signal change on A2 and A3: exactly where we need to check.
ISR(PCINT1_vect) {
  encoder.tick(); // just call tick() to check the state.
}