//======================================
//=====PCINT(Pin Change Interrupt)=====
//======================================
int count = 0, val=0;
volatile uint8_t portbhistory = 0xFF;
void setup() {
portbhistory = PINB; // read PINB bits at power_up
Serial.begin(19200);
pinMode(8, INPUT);
pinMode(13, OUTPUT);
PCICR = B00000001;
PCMSK0 = B00000001;
Serial.print("PINB "); //debug
Serial.println(portbhistory);
}
ISR(PCINT0_vect){
uint8_t changedbits, risingbits, fallingbits, portbnow;
portbnow = PINB;
changedbits = portbnow ^ portbhistory;
risingbits = portbnow & ~portbhistory;
fallingbits = ~portbnow & portbhistory;
portbhistory = portbnow;
val = digitalRead(8);
//digitalWrite(13, val);
Serial.print("interrupt ");
Serial.println(++count);
Serial.print("changebits ");
Serial.println(changedbits, HEX);
if (fallingbits & (1 << PB0)) { // Falling Edge on PCINT10 - PB2
Serial.println("falling"); // Pin is Low, so cable is connected
}
if (risingbits & (1 << PB0)) { // Rising Edge on PCINT10 - PB2
Serial.println("rising"); // Pin is High, so cable is disconnected
}
}
void loop() {
// put your main code here, to run repeatedly:
// val = digitalRead(8);
// digitalWrite(13, val);
delay(2000);
}