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]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
#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]