Pages: [1]   Go Down
Author Topic: Pin change interrupts on arbitrary pins?  (Read 747 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA
Offline Offline
God Member
*****
Karma: 11
Posts: 673
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I am trying to understand the pin change interrupts that are available on Arduino.

From the AttachInterrupt docs, I see "Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3).".  Yet the Atmega328 data sheet specifies PCINTxx values for nearly every pin.  So from that, it would seem I could get an interrupt on any pin

What accounts for this difference in docs?  And can I actually put an ISR on any pin?  (Is it just that I have to do it manually for the others??)
Logged


USA
Offline Offline
Jr. Member
**
Karma: 4
Posts: 91
If you can't fix it with a hammer, it must be an electrical problem.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

afaik, only 2 pins for interrupts on 8/168/328 arduinos, however, more pins are available on the mega based boards. you would need to hardcode the interrupts, and i'm not sure if doing that would break any other functions.

something like this worked for me:

Code:
ISR(PCINT0_vect) {
  …
}

Setup() {
  //disable interrupts
  cli();
  
  //setup digital pin #8 pin-change interrupt (see datasheet)
  PCICR |= (1<<PCIE0);
  PCMSK0 |= (1<<PCINT0);
 
  //enable interrupts
  sei();
}
« Last Edit: March 20, 2011, 02:22:28 pm by JimEli » Logged


Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 207
Posts: 12910
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

From the AttachInterrupt docs, I see "Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3).".  Yet the Atmega328 data sheet specifies PCINTxx values for nearly every pin.  So from that, it would seem I could get an interrupt on any pin

What accounts for this difference in docs?

"external interrupts" versus "pin change interrupts".

"External interrupts" get their own interrupt vector and have extended functionality (hardware support for LOW, RISING, FALLING, CHANGE).  RISING, FALLING, and CHANGE do not work if the processor clock is stopped (deeper sleep modes).

"Pin change interrupts" have one vector per port (up to eight pins assigned to one vector) and have very basic functionality (hardware support for CHANGE).  Pin change interrupts always work; even in the deepest sleep modes.

Quote
And can I actually put an ISR on any pin?  (Is it just that I have to do it manually for the others??)

Yes.  Examples...

@JimEli's post above.
http://www.arduino.cc/playground/Main/PinChangeInt
http://www.arduino.cc/playground/Main/PcInt
http://www.google.com/search?q=pin+change+interrupt+site:arduino.cc%2Fforum
« Last Edit: March 20, 2011, 02:55:49 pm by Coding Badly » Logged

Seattle, WA
Offline Offline
God Member
*****
Karma: 11
Posts: 673
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

"external interrupts" versus "pin change interrupts".

Totally get it now.  Thanks!  You have been very helpful to me today smiley
Logged


Pages: [1]   Go Up
Jump to: