Author Topic: pin change interrupts Moteino Mega - pinChange [solution/library]  (Read 8941 times)

Vendigroth

  • NewMember
  • *
  • Posts: 1
Re: pin change interrupts Moteino Mega - pinChange [solution/library]
« Reply #15 on: August 05, 2015, 10:25:56 PM »
Hi,

So does anyone have any of the libraries working with Mega?
I've tried PinChangeInt since Tom says it works, but I can't even get a single pin to interrupt.
I'm using PinChangeInt from https://github.com/GreyGnome/PinChangeInt with this:

Code: [Select]
#include <PinChangeInt.h>
#define PIN 20

volatile uint16_t interruptCount=0;

void interruptFunction() {
  interruptCount++;
}
void setup() {
  Serial.begin(9600);
  Serial.print("PinChangeInt");
  pinMode(PIN, INPUT);
  digitalWrite(PIN, HIGH);
  PCintPort::attachInterrupt(PIN, interruptFunction, CHANGE);
  // (RISING, FALLING, CHANGE)
  }
 
void loop() {
  delay(1000);                            // Every second,
  Serial.print("Pin was interrupted: ");
  Serial.print(interruptCount, DEC);      // print the interrupt count.
  Serial.println(" times.");
  Serial.print("Currently: ");
  int buttonState = digitalRead(PIN);
  Serial.println(buttonState);
}

In the end I'm planning to have 3 pins interrupted, and I was hoping to avoid doing it manually and figuring out which is which or having them on different ports. 

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: pin change interrupts Moteino Mega - pinChange [solution/library]
« Reply #16 on: August 06, 2015, 09:23:38 AM »
@Vendigroth, if you want to use RFM69 library AND use ATmega1284P AND use pinChange Interrupts, then, unfortunately, the code that gregcope mentioned in his post is your best bet.   To use pin 20, there are a couple of changes:

Change:
Code: [Select]
  PCMSK0 |= (1<<PCINT6);
  PCICR |= (1<<PCIE0);
to:
Code: [Select]
  PCMSK2 |= (1<<PCINT20); // You can use this snippet for interrupts on pins 16-23.
  PCICR |= (1<<PCIE2);


Change:
Code: [Select]
ISR(PCINT0_vect) {
  pinChanged = 1;
}
to:
Code: [Select]
ISR(PCINT2_vect) {
  pinChanged = 1; // If you want to know WHICH pin changed, then you need to read PORTC and differentiate it with its previous value.
}

Tom