Hey everyone, after hours of searching I am still very confused as to how to attach interrupts to pins. I am trying to make an interrupt on DP8; this is the part of my code I am having trouble with:
I realize that, so I'm trying to add additional pins using Atmega commands, in the same way that the Arduino Mega gains additional interrupts. As I understand it, all IO pins on Atmega are covered by Pin Change Interrupts.
I believe the problem is this: there are two types of pin related interrupts, and attachInterrupt() only handles one kind. The wrong one, for your application.
The kind that attachInterrupt() handles is the External Interrupt Request, which is bound to the INT0 and INT1 pins.
The kind you seem to be requiring is a Pin Change interrupt, a different beast. Any pin can be configured to generate this kind of interrupt; you set PCICR, PCIFR, and the appropriate PCMSKn.
Searching on pin-change interrupt here in the forum may be helpful as will be a look at the data sheet for the '328 at Section 12.2/page 71 from this url:
Hmm, that makes sence. The closest replacement I can find for attachInterrupt() is in the third link I posted, where this code is claimed to work for pin 5:
ISR(PCINT2_vect)
{
if (PIND & B00100000) // evaluates to TRUE when PortD bit #5 (Arduino digital pin #5) is HIGH
{
counter1++;
}
}
Unfortunately, I have no idea what the binary part means as it seems to correspond to pin #4.
Your example is NOT an "interrupt". It is simply an example of "polling". i.e. just going out at some specified time and saying "does this equal that?" Very much different than an interrupt. They could just as easily have said: if digitalRead(5) Then counter1++
In the context of the persons code, it is. "ISD()" is apparently an AVR interrupt command, run on "PCINT2_vect", the Atmega equivalent of pin 5, in the attached libraries.
Your other examples show extentions to the Arduino base functionality. Apparently there are features in the Atmel chip which the Arduino basic language doesn't implement. So they have extentions that you can use to get to those other features. But you must use the special code and syntax. You can't simply use attachInterrupt() as it only supports the pins defined by the definition page.
All of them do. Interrupt pins aren't "hard-wired", like you said, into the Atmega; that's how the Arduino Mega can use the same chip and have more native interrupts. And yes, I understand that I can't use attachInterrupt(), I have to use some variation of ISR(). That's what I'm asking about.
If you want some sample code that uses pin change interrupts on arbitrary, specifiable pins, take a look at the OrangutanPulseIn code from the Pololu AVR Library. Once you download the library, you can find the source code in the directory:
libpololu-avr/src/OrangutanPulseIn
The library code is documented in the command reference; this might help you better understand what it's doing.
Unfortunately, I have no idea what the binary part means as it seems to correspond to pin #4.
I don't understand you're confusion here. The statement is checking to see if the fifth bit of the digital input register for port D (PIND) is set:
PIND & B00100000
Why are you interpreting this as pin 4?
One key thing to note about pin change interrupts is that there is only one ISR per port, so if you have configured multiple pins from the same port to generate pin change interrupts, you need to do some extra computation in the ISR to figure out which pin actually changed. External interrupts are nicer because you automatically know which pin caused the interrupt and what edge was detected.