Lee esto, investiga un poco
http://forum.arduino.cc/index.php?topic=45239.40
#include <avr/interrupt.h>
#include <avr/io.h>
void setup() {
pinMode(10, INPUT); // UP pin of trackball
pinMode(11, INPUT); // DOWN pin of trackball
pinMode(12, INPUT); //LEFT pin of trackball
pinMode(13, INPUT); // RIGHT pin of trackball
pinMode(15, INPUT); // BUTTON pin of trackball
Serial.begin(115200);
PCICR |= (1 << PCIE0); // enable PCINT0
PCICR |= (1 << PCIE1); // enable PCINT1
PCMSK0 |= (1 << PCINT4); // mask for bit4 of port B (pin 10)
PCMSK0 |= (1 << PCINT5); // mask for bit5 of port B (pin 11)
PCMSK0 |= (1 << PCINT6); // mask for bit6 of port B (pin 12)
PCMSK0 |= (1 << PCINT7); // mask for bit7 of port B (pin 13)
PCMSK1 |= (1 << PCINT9); // mask for bit0 of port J (pin 15)
}
volatile boolean print_flag0 = false; // PCINT0 flag
volatile boolean print_flag1 = false; // PCINT1 flag
volatile byte pcint0_pins = B00000000; // variable for current port B bits
volatile byte pcint0_pinsLast = B00000000; // variable for previous port B bits
volatile byte pcint1_pins = B00000000; // variable for current port J bits
volatile byte pcint1_pinsLast = B00000000; // variable for previous port J bits
ISR(PCINT0_vect) // PCINT0 ISR
{
byte saveSREG = SREG; // Save SREG
noInterrupts(); // disable interrupts
pcint0_pins ^= ((PINB & B11110000) ^ (pcint0_pinsLast & B11110000)); // store bits that have changed
pcint0_pinsLast = pcint0_pins; // store current bits to previous bits
print_flag0 = true; // set flag to indicate PCINT0 interrupt
SREG = saveSREG; // restore SREG
}
ISR(PCINT1_vect) // same as above for PCINT1
{
byte saveSREG = SREG;
noInterrupts();
pcint1_pins ^= ((PINJ & B00000001) ^ (pcint1_pinsLast & B00000001));
pcint1_pinsLast = pcint1_pins;
print_flag1 = true;
SREG = saveSREG;
}
void loop() {
if (print_flag0 == true) { // for PCINT0
noInterrupts(); // disable interrupts
byte pci0 = pcint0_pins; // transfer value
interrupts(); // enable interrpts
Serial.print("PCINT0 = "); // prints bit values of port B
Serial.println(pci0, BIN);
print_flag0 = false; // reset flag
}
if (print_flag1 == true) { // same as above for pot J
noInterrupts();
byte pci1 = pcint1_pins;
interrupts();
Serial.print("PCINT1 = ");
Serial.println(pci1, BIN);
print_flag1 = false;
}
}
Aqui esta la table de Interrupciones por Pin en el MEGA
Also, here is a table of the available Pin Change interrupts for the Mega.
PC Interrupt# PCI Control Reg. PCI Mask Port/Bit Arduino Mega Pin
PCINT0: PCICR = PCIE0 = Bit0, PCMSK0 = Bit0, PortB Bit0, Pin = 53
PCINT1: PCICR = PCIE0 = Bit0, PCMSK0 = Bit1, PortB Bit1, Pin = 52
PCINT2: PCICR = PCIE0 = Bit0, PCMSK0 = Bit2, PortB Bit2, Pin = 51
PCINT3: PCICR = PCIE0 = Bit0, PCMSK0 = Bit3, PortB Bit3, Pin = 50
PCINT4: PCICR = PCIE0 = Bit0, PCMSK0 = Bit4, PortB Bit4, Pin = 10
PCINT5: PCICR = PCIE0 = Bit0, PCMSK0 = Bit5, PortB Bit5, Pin = 11
PCINT6: PCICR = PCIE0 = Bit0, PCMSK0 = Bit6, PortB Bit6, Pin = 12
PCINT7: PCICR = PCIE0 = Bit0, PCMSK0 = Bit7, PortB Bit7, Pin = 13
PCINT8: PCICR = PCIE1 = Bit1, PCMSK1 = Bit0, PortE Bit0, Pin = 0 (USART0) !!!Avoid using!!!
PCINT9: PCICR = PCIE1 = Bit1, PCMSK1 = Bit1, PortJ Bit0, Pin = 15
PCINT10: PCICR = PCIE1 = Bit1, PCMSK1 = Bit2, PortJ Bit1, Pin = 14
** PCINT11: PCICR = PCIE1 = Bit1, PCMSK1 = Bit3, PortJ Bit2, NOT_AVAILABLE
** PCINT12: PCICR = PCIE1 = Bit1, PCMSK1 = Bit4, PortJ Bit3, NOT_AVAILABLE
** PCINT13: PCICR = PCIE1 = Bit1, PCMSK1 = Bit5, PortJ Bit4, NOT_AVAILABLE
** PCINT14: PCICR = PCIE1 = Bit1, PCMSK1 = Bit6, PortJ Bit5, NOT_AVAILABLE
** PCINT15: PCICR = PCIE1 = Bit1, PCMSK1 = Bit7, PortJ Bit6, NOT_AVAILABLE
PCINT16: PCICR = PCIE2 = Bit2, PCMSK2 = Bit0, PortK Bit0, Pin = 62(A8)
PCINT17: PCICR = PCIE2 = Bit2, PCMSK2 = Bit1, PortK Bit1, Pin = 63(A9)
PCINT18: PCICR = PCIE2 = Bit2, PCMSK2 = Bit2, PortK Bit2, Pin = 64(A10)
PCINT19: PCICR = PCIE2 = Bit2, PCMSK2 = Bit3, PortK Bit3, Pin = 65(A11)
PCINT20: PCICR = PCIE2 = Bit2, PCMSK2 = Bit4, PortK Bit4, Pin = 66(A12)
PCINT21: PCICR = PCIE2 = Bit2, PCMSK2 = Bit5, PortK Bit5, Pin = 67(A13)
PCINT22: PCICR = PCIE2 = Bit2, PCMSK2 = Bit6, PortK Bit6, Pin = 68(A14)
PCINT23: PCICR = PCIE2 = Bit2, PCMSK2 = Bit7, PortK Bit7, Pin = 69(A15)