I am trying to get 2 encoders to work with a Mega 2560 board. Using pin change interrupts works fine with the uno board but unsuccessful with Mega. It appears to be a mapping problem in that the Mega ports are different. The attached code should work on either board but does not. I inserted print commands just to test for the interrupt with no success. Only one interrupt works on the Mega. Any help will be appreciated.
// Install Pin change interrupt for a pin, can be called multiple times
int Brake_Encoder_Count;
int Mark5_Encoder_Count;
void pciSetup(byte pin)
{
*digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
}
// Use one Routine to handle each group
ISR (PCINT0_vect) // handle pin change interrupt for D8 to D13 here
{
if ( bitRead(PINB, 6) == bitRead(PINB, 7)) //Pins 12 & 13
{
Brake_Encoder_Count++;
}
else
{
Brake_Encoder_Count--;
}
}
ISR (PCINT1_vect)
{
Serial.print(" PCINT1_vect");
/*
if ( bitRead(PINB, 4) == bitRead(PINB, 5)) //Pins 14 & 15
{
Mark5_Encoder_Count++;
}
else
{
Mark5_Encoder_Count--;
}*/
}
ISR (PCINT2_vect) // handle pin change interrupt for D0 to D7 here
{
Serial.print(" PCINT2_vect");
/* if ( bitRead(PINB, 4) == bitRead(PINB, 5)) //Pins 14 & 15
{
Mark5_Encoder_Count++;
}
else
{
Mark5_Encoder_Count--;
}*/
}
void setup() {
int i;
Serial.begin(9600);
// set pullups, if necessary
for (i = 0; i <= 15; i++)
digitalWrite(i, HIGH); // pinMode( ,INPUT) is default
for (i = A0; i <= A5; i++)
digitalWrite(i, HIGH);
// enable interrupt for pin...
pciSetup(13);
pciSetup(6);
pciSetup(7);
pciSetup(14);
pciSetup(15);
pciSetup(A8);
}
void loop() {
// Nothing needed
Serial.print("Brake_Encoder_Count ");Serial.println(Brake_Encoder_Count);
Serial.println(Mark5_Encoder_Count);
delay(200);
}