#define LED_PIN PORTD7
#define INT0_PIN PORTD2
#define PCD5_PIN PORTD5
//#define NO_ARDUINO_LAYER // comment this line out to run with the Arduino Layer
# ifdef NO_ARDUINO_LAYER
int main() {
setup();
while (1) {
loop();
}
return 0;
}
#endif
void setup() {
EICRA = 0; // clear register
bitSet(EICRA, ISC01); // The falling edge of INT0 generates an interrupt request.
EIMSK = 0; // clear register
bitSet(EIMSK, INT0); // enable External Interrupt Request 0
bitSet(DDRD, LED_PIN); // set pin as output
bitSet(PORTD, INT0_PIN); // enable pin pull-up
bitSet(PCICR, PCIE2); // enable pin change interrupt on PORTD
bitSet(PCMSK2, PCINT21); // enable pin change interrupt on pin PORTD5
cli(); // this is the solution for the same code to operate
// with and without the Arduino Framework
}
void loop() {
if (bit_is_set(EIFR, INTF0)) { // check falling edge at INT0_PIN
bitSet(EIFR, INTF0) ; // clear INTF0 flag
bitToggle(PORTD, LED_PIN); // toggle LED level
}
if (bit_is_set(PCIFR, PCIF2)) { // check pin change at PCD5 pin
bitSet(PCIFR, PCIF2) ; // clear INTF0 flag
if (bit_is_clear(PIND, PCD5_PIN)) {
bitToggle(PORTD, LED_PIN); // toggle LED level}
}
}
}