/*************************************************************
Program: Pin Change Interrupt Test
Coder: Jackie
Purpose:
*************************************************************/
#define rLED 6 //PH3 0000 1000 0x08
#define gLED 5 //PE3 0000 1000 0x08
#define SW1 14 //PJ1 0000 0010 0x02 PCINT 10
//PCICR |= 0x01 - bit1(PCIE1) enabled to actavate PCINT[15:8]
//PCMSK2 |= 0x04 - bit3(PCINT10) enabled
//PCIFR(flag register) any logic change on PCINT10 will set bit1(PCIF1) and execute ISR
//PCIFR resets to zero after ISR is executed
void setup() {
Serial.begin(9600);
pinMode(rLED,OUTPUT);
pinMode(gLED,OUTPUT);
pinMode(SW1,INPUT_PULLUP);
//PCI Enable falls under PCICR(PCI Control Register)
//PCIE[2:0] - the num is the bit value - only uses bit0:2
//bit2 enables PCINT 23:16 - PCI2 Interrupt Vector
//bit1 enables PCINT 15:8 - PCI1 Interrupt Vector
//bit0 enables PCINT 7:0 - PCI0 Interrupt Vector
//the wanted PCINT pin is enabled individual using PCMSK(PC Mask) register
//PCMSK2 has PCINT[23:16] and corresponds to bit 7:0 of PCMSK2
//PCMSK[1:0] is similar
//enable on pin set, clear to disable
//PCIFR - PCI Flag Registory -
//unlike external interrupts, PCI doesn't have ISCn[1:0] to set the edge type
//a bit more complicated way of setting rising/falling/dual edge interrupt request
//any logic changes on PCINT[23:16](PCIE2) pins will trigger an interrupt request and PCIF2 becomes set
//it will then execute the ISR(PCINTn_vect) and the flag bit will be cleared
//flag bit can be cleared by writing a 1 to PCIFn
cli();
PCICR |= 0x02; //bit1(PCIE1) enabled to actavate PCINT[15:8]
PCMSK1 |= 0x04; //bit2(PCINT10) enabled
sei();
} //void setup end
void loop() {
PINE |= 0x08;
Serial.println("DELAY START");
delay(1000);
Serial.println("DELAY END\n------------");
} //void loop end
//ISR vector# uses same number as PCICR(PCIE#)
ISR(PCINT1_vect) {
cli();
if (!(digitalRead(SW1))) { //toggle LED on falling edge only (logic 0)
if (PINH & 0x08) digitalWrite(rLED,LOW);
else digitalWrite(rLED,HIGH);
}
sei();
} //ISR(PCINT10_vect) end