Pages: 1 ... 3 4 [5]   Go Down
Author Topic: PinChangeInt library- To attach interrupts to multiple Arduino (Uno/Mega) pins  (Read 20220 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 4
Posts: 94
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

After some time, I have updated the library. There is a significant fix submitted by jrhelbert which makes the two Port J pins work properly with the Mega. See http://code.google.com/p/arduino-pinchangeint/.

Downloads available at https://bintray.com/greygnome/generic/PinChangeInt/view

Bleeding edge source available at https://github.com/GreyGnome/PinChangeInt
Logged

0
Offline Offline
Jr. Member
**
Karma: 4
Posts: 94
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Note: I don't have a Mega, so I didn't test ports J and K. If you use it, please let me know if it works. Thanks.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe somebody can help me?
I have this code, it is used to read a rotary encoder with an Arduino Uno. I want to make this work on an Arduino Mega.


Code:
  PORTC |= _BV(PORTC0) | _BV(PORTC1) | _BV(PORTC2) | _BV(PORTC3);   // enable pullup for pins
  PCMSK1 = _BV(PCINT8) | _BV(PCINT9) | _BV(PCINT10) | _BV(PCINT11); // enable button pin change interrupt A0 A1 A2 A3
  PCICR = _BV(PCIE1);
     

I can not find any help about converting this to a working Mega sketch. Can you help me?
Logged

0
Offline Offline
Jr. Member
**
Karma: 4
Posts: 94
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First you have to study the Mega pinout and figure out which pins and ports are available to you. Have you done this?

It looks like you have two rotary encoders (4 pins total). Is that correct?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried to use this lib, but sadly doesn't work as expected, so moved to http://www.geertlangereis.nl/Electronics/Pin_Change_Interrupts/PinChange_en.html . It works fine and I got all Analog pins on my Mega 2560 working as interrupt. but there I got other problem.... I lost Serial1, Serial2, Serial3.
None other than Serial0 Serial3 works with PCINT1 to PCINT23, with the ATMEL datasheet for 2560 it has stated that Serial3 (ATMEL's RXD3/TXD3) are on  PCINT9 and PCINT10 , so there are chances that Serial3 wont work with manipulated PCICR setting. Yet we have 2 other Serial Ports (Serial1 and Serial2) which haven't specified on any PCINT pins.
Please guide me about this.
This is where I Initialise Interrupts
Code:
[code]void InitialiseInterrupt(){
  cli(); // switch interrupts off while messing with their settings  

  PCICR =0x06;          // Enable PCIE2 and PCIE1
  PCMSK1 = 0b11111111; // Enabling A0 to A7 pins on Mega 2560 (PCINT23:16)
  PCMSK2 = 0b11111111; // Enabling A8 to A15 pins on Mega 2560(PCINT15:8)
  sei(); // turn interrupts back on
}

also as per the ATMEL's pdf you dont have any PCINT on ADC0...ADC7 pins. yet those are working


the full code is
Code:
#include <TimerOne.h>
#include <Encoder.h>
#include <SPI.h>

Encoder xenc(18,19);
int iPin[] = {54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69}; //{"A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","A10","A11","A12","A13","A14","A15"};
int iLast[16];
void setup()
{
  SPI.begin();
  Serial.begin(9600);
  Serial.println("Boe");
  Serial2.begin(9600);
  InitialiseIO();
  InitialiseInterrupt();
Timer1.initialize(100000);
Timer1.attachInterrupt(Timer1_tick);
}
void Timer1_tick(){
  float z = xenc.read();
digitalWrite(13,!digitalRead(13));
SPI.transfer(z);
}
void serialEvent(){
Serial2.print("A");
}
void loop() {
  /* Nothing to do: the program jumps automatically to Interrupt Service Routine "blink"
   in case of a hardware interrupt  */
}  

void InitialiseIO(){
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
  pinMode(A0, INPUT);   // Pin A0 is input to which a switch is connected
  digitalWrite(A0, HIGH);   // Configure internal pull-up resistor
  pinMode(A1, INPUT);   // Pin A1 is input to which a switch is connected
  digitalWrite(A1, HIGH);   // Configure internal pull-up resistor
  pinMode(A2, INPUT);   // Pin A2 is input to which a switch is connected
  digitalWrite(A2, HIGH);   // Configure internal pull-up resistor
   pinMode(A3, INPUT);   // Pin A2 is input to which a switch is connected
  digitalWrite(A3, HIGH);   // Configure internal pull-up resistor
   pinMode(A4, INPUT);   // Pin A2 is input to which a switch is connected
  digitalWrite(A4, HIGH);   // Configure internal pull-up resistor
   pinMode(A5, INPUT);   // Pin A2 is input to which a switch is connected
  digitalWrite(A5, HIGH);   // Configure internal pull-up resistor
   pinMode(A6, INPUT);   // Pin A2 is input to which a switch is connected
  digitalWrite(A6, HIGH);   // Configure internal pull-up resistor
   pinMode(A7, INPUT);   // Pin A2 is input to which a switch is connected
  digitalWrite(A7, HIGH);   // Configure internal pull-up resistor
  pinMode(A8, INPUT);   // Pin A0 is input to which a switch is connected
  digitalWrite(A8, HIGH);   // Configure internal pull-up resistor
  pinMode(A9, INPUT);   // Pin A1 is input to which a switch is connected
  digitalWrite(A9, HIGH);   // Configure internal pull-up resistor
  pinMode(A10, INPUT);   // Pin A2 is input to which a switch is connected
  digitalWrite(A10, HIGH);   // Configure internal pull-up resistor
   pinMode(A11, INPUT);   // Pin A2 is input to which a switch is connected
  digitalWrite(A11, HIGH);   // Configure internal pull-up resistor
   pinMode(A12, INPUT);   // Pin A2 is input to which a switch is connected
  digitalWrite(A12, HIGH);   // Configure internal pull-up resistor
   pinMode(A13, INPUT);   // Pin A2 is input to which a switch is connected
  digitalWrite(A13, HIGH);   // Configure internal pull-up resistor
   pinMode(A14, INPUT);   // Pin A2 is input to which a switch is connected
  digitalWrite(A14, HIGH);   // Configure internal pull-up resistor
   pinMode(A15, INPUT);   // Pin A2 is input to which a switch is connected
  digitalWrite(A15, HIGH);   // Configure internal pull-up resistor
  
}

void InitialiseInterrupt(){
  cli(); // switch interrupts off while messing with their settings  

  PCICR =0x06;          // Enable PCINT1 interrupt
  PCMSK1 = 0b11111111;
  PCMSK2 = 0b11111111;
  sei(); // turn interrupts back on
}
/*
ISR(PCINT1_vect) {    // Interrupt service routine. Every single PCINT8..14 (=ADC0..5) change
            // will generate an interrupt: but this will always be the same interrupt routine
  if (digitalRead(A0)==0)  Serial.println("A0");
  if (digitalRead(A1)==0)  Serial.println("A1");
  if (digitalRead(A2)==0)  Serial.println("A2");
   if (digitalRead(A3)==0)  Serial.println("A3");
  if (digitalRead(A4)==0)  Serial.println("A4");
  if (digitalRead(A5)==0)  Serial.println("A5");
   if (digitalRead(A6)==0)  Serial.println("A6");
  if (digitalRead(A7)==0)  Serial.println("A7");

}
ISR(PCINT2_vect) {    // Interrupt service routine. Every single PCINT8..14 (=ADC0..5) change
            // will generate an interrupt: but this will always be the same interrupt routine
  if (digitalRead(A8)==0)  Serial.println("A8");
  if (digitalRead(A9)==0)  Serial.println("A9");
  if (digitalRead(A10)==0)  Serial.println("A10");
   if (digitalRead(A11)==0)  Serial.println("A11");
  if (digitalRead(A12)==0)  Serial.println("A12");
  if (digitalRead(A13)==0)  Serial.println("A13");
   if (digitalRead(A14)==0)  Serial.println("A14");
  if (digitalRead(A15)==0)  Serial.println("A15");

}*/
ISR(PCINT1_vect) {
  for (int q = 0; q <= 7; q++) {
    int u = digitalRead(iPin[q]);
    String OP = "IP";

    if (iLast[q] != u) {
      OP += q;
      OP += u;
      OP += "!";
      Serial.print(OP);
    }
    iLast[q] = u;
  }

}
ISR(PCINT2_vect) {
  for (int q = 8; q <= 15; q++) {
    int u = digitalRead(iPin[q]);
    String OP = "IP";

    if (iLast[q] != u) {
      OP += q;
      OP += u;
      OP += "!";
      Serial.print(OP);
    }
    iLast[q] = u;
  }

}

[/code]
« Last Edit: September 02, 2014, 11:30:52 am by aadityadengle » Logged

0
Offline Offline
Jr. Member
**
Karma: 4
Posts: 94
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't do a Serial.print() inside an ISR. If it does work, consider yourself lucky and stop doing it as soon as possible because things are (were?) problematic when printing from an ISR (but supposedly fixed in IDE 1.5; see https://github.com/matthijskooijman/Arduino/commit/eb86af7b67407e198bac24ecd16a8c1669e3efaa ).

Regarding the rest of your question, you stated that you tried the PinChangeInt library and it didn't work for you. This thread is about the PinChangeInt library. The code you speak of was written by someone else so I don't think this is the appropriate place for this question, but perhaps someone will lend you the time and help you out.
Logged

Pages: 1 ... 3 4 [5]   Go Up
Jump to: