How to clear interrupt flag for PINCHANGE interrupts.

Well after going through excellent tutorial posted by Nick Gammon on Interrupts i was relieved of clearing flag for EXTERNAL interrupt by simple command as EIFR = bit (INTF0); // clear flag for interrupt 0 . However, i m using PINCHANGE interrupt on pin group of D0 to D7 (PCINT2_vect). If I wish to clear the flag for above mentioned interrupt then what should be my code? I tried EIFR= bit(PCINTF2) but it ended with error message ('PCINTF2' was not declared in this scope). I m a newbie, so I have a pretty strong hunch that I m forgetting something basic. So, please educate me on this. Much Obliged!

Pin Change Interrupt Flag Register
Name: PCIFR
Offset: 0x3B
Reset: 0x00
Property:
When addressing as I/O register: address offset is 0x1B

Bit 2 – PCIF2
Pin Change Interrupt Flag 2
When a logic change on any PCINT[23:16] pin triggers an interrupt request, PCIF2 will be set. If the I-bit
in SREG and the PCIE2 bit in PCICR are set, the MCU will jump to the corresponding interrupt vector.
The flag is cleared when the interrupt routine is executed. Alternatively, the flag can be cleared by writing
'1' to it.
Bit 1 – PCIF1
Pin Change Interrupt Flag 1
When a logic change on any PCINT[14:8] pin triggers an interrupt request, PCIF1 will be set. If the I-bit in
SREG and the PCIE1 bit in PCICR are set, the MCU will jump to the corresponding interrupt vector. The
flag is cleared when the interrupt routine is executed. Alternatively, the flag can be cleared by writing '1' to
it.
Bit 0 – PCIF0
Pin Change Interrupt Flag 0
When a logic change on any PCINT[7:0] pin triggers an interrupt request, PCIF0 will be set. If the I-bit in
SREG and the PCIE0 bit in PCICR are set, the MCU will jump to the corresponding interrupt vector. The
flag is cleared when the interrupt routine is executed. Alternatively, the flag can be cleared by writing '1' to

Thanks. So i can clear any existing PINCHANGE interrupt flag by writing 1 to it. So, can I type in EIFR = bit (PCIF2); (in my case) to clear the interrupt flag by seting it to 1? Or do I need any extra line of code to make it work?

What happend when you tried it? :wink:

Can you explain why you must manually clear the interrupt flag as it is automatically cleared when the ISR is executed?

The usual reason is to clear the flag shortly after enabling the internal pullup so any pin changes that occurred from the floating pin do not trigger a spurious interrupt.

1 Like

Yes, it worked. Thanks. DKWatson answer for your question is partly answered by Coding Badly and partly will be answered by this forum topic=59217.0 Interrupt still happening after detached. ( fix now available) - Programming Questions - Arduino Forum .