Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Pages: [1]   Go Down
Author Topic: Pin Change Interrupt during ISR  (Read 244 times)
0 Members and 1 Guest are viewing this topic.
Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1733
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

~Tom~

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 152
Posts: 5838
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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,
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 216
Posts: 13140
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: July 07, 2014, 01:54:55 pm by Coding Badly » Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1733
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

~Tom~

Pages: [1]   Go Up
Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Jump to: