Go Down

Topic: Pin Change Interrupt during ISR (Read 246 times) previous topic - next topic

Tom Carpenter

Something not entirely clear from the ATTiny861 datasheet (and probably similar for most AVRs) is what happens when a pin change occurs during the pin change ISR. An example might help.
Say there are two pins which are enabled for generating a pin change interrupt, and that they are part of the same bank so use the same interrupt vector:
Code: [Select]

ISR (PCINT_vect){  //so a pin change has occurred

  //<---- (1) what happens if a pin change occurs *here* (before reading PINA, but after entering the ISR)

  byte current = PINA; //lets assume that both belong to Port A
  byte changed = old ^ current; //and that 'old' is a global containing the old value for PINA, then we know which have changed.
  old = current; //back up for next time
  //So at this point we have a 'changed' variable which has ones for each bit which has changed state
 
  //<---- (2) what happens if a pin change occurs *here* (or for that matter any time after reading PINA)

  ... //other code - e.g. if statements to perform specific tasks depending on what changed
}


There are two places there where I am not entirely clear on the behaviour of the ATTiny at these points. Will the ISR be called again after it returns the first time? Or will the changes be lost?

Obviously if the interrupt flag is not set again, then it isn't an issue for (1) as that is already checked, however it causes an issue for (2) as that is not checked yet.
If the interrupt flag is set again, then that is fine for (2) as it needs checking, but it is a bit wasteful for (1) as it has already been checked [won't do any harm as we are backing up the last known value].

I'm hoping that another interrupt is triggered, but want to be sure.
~Tom~

fungus


There are two places there where I am not entirely clear on the behaviour of the ATTiny at these points. Will the ISR be called again after it returns the first time? Or will the changes be lost?


I think it should be called again instantly,
No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Coding Badly

#2
Jul 07, 2014, 08:43 pm Last Edit: Jul 07, 2014, 08:54 pm by Coding Badly Reason: 1
Quote
Bit 5 - PCIF: Pin Change Interrupt Flag
When a logic change on any PCINT15 pin triggers an interrupt request, PCIF becomes set (one). If the I-bit in SREG and the PCIE bit in GIMSK are set (one), 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 a logical one to it.


Quote
If an interrupt condition occurs while the corresponding interrupt enable bit is cleared, the Interrupt Flag will be set and remembered until the interrupt is enabled, or the flag is cleared by software.


Quote
When the AVR exits from an interrupt, it will always return to the main program and execute one more instruction before any pending interrupt is served.


Quote
Will the ISR be called again after it returns the first time?


Almost.  When the current invocation returns, a single machine instruction is executed then the ISR is called.

Tom Carpenter

That's fine then. Just wanted to make sure the pin change interrupt wasn't a strange one (or at least no more so than it already is).
~Tom~

Go Up