Pages: [1]   Go Down
Author Topic: Interrupt pins  (Read 923 times)
0 Members and 1 Guest are viewing this topic.
Connecticut
Offline Offline
Edison Member
*
Karma: 17
Posts: 1216
RTFD (Datasheet in our case)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Interrupts are great. But it's a bit difficult to use them in Arduino because the int numbers and pins don't line up. As a beginner, it was confusing to have to use "0" for pin 2 and "1" for pin 3. Why not add a line or two at the beginning of attachInterrupts and detachInterrupts to make the pins correspond? So then if you wanted a rising interrupt on pin2, you would do attachInterrupts(2, RISING, function); and attachInterrupts would decrement the number by 2. If we were using a mega's higher interrupts, then the formula is -pin + 23 (that's negative pin + 23)

So the complete listing:
pin   int number       how?
2       0                  -2
3       1                 -2
21       2                -pin + 23
20       3                -pin + 23
19       4                -pin + 23
18       5                -pin + 23


the only other modification needed would be to change the interrupt variable to an (signed) int to accommodate the intermediate negative numbers.

baum
« Last Edit: January 02, 2012, 10:56:54 am by baum » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you mean something like this..

Code:
void myIRQ(uint8_t pin, void (*userFunc)(void), int mode)
{
  if (1 < pin && pin < 4) attachInterrupt(pin-2, userFunc, mode);
  else if (17<pin && pin < 22) attachInterrupt(23-pin, userFunc, mode);
}

probably needs am #ifdef MEGA or so ...
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Connecticut
Offline Offline
Edison Member
*
Karma: 17
Posts: 1216
RTFD (Datasheet in our case)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes, exactly. Wouldn't that make it a tad easier for beginners to use interrupts? And isn't part of the idea of the Arduino project to create an easy-to-use board and language for beginners?

baum
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Agree, but only partly

If people are so far that they can program interrupts they should be able to write a function like the one above. The interface of aattachInterrupt() is not too difficult so that extra layer is "costly".

Alternative could be to make some defines, which have the pinnummers in their name,

#define IRQ_PIN1  0
#define IRQ_PIN2  1
#define IRQ_PIN18 2
#define IRQ_PIN19 3
#define IRQ_PIN20 4
#define IRQ_PIN21 5

Think the energy of the development team could be better focussed on documentation how to use attachinterrupt() and interrupts, volatile etc

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Connecticut
Offline Offline
Edison Member
*
Karma: 17
Posts: 1216
RTFD (Datasheet in our case)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, that is true.

baum
Logged

0
Offline Offline
God Member
*****
Karma: 25
Posts: 606
Always making something...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I just recently published a new higher performance Encoder library.  Indeed, I had to put a bunch of definitions for the pin to int mapping on various boards.  It's a shame that info isn't available as a macro or inline function from the core...

Here's that library, if anyone's interested.

http://www.pjrc.com/teensy/td_libs_Encoder.html
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17262
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just recently published a new higher performance Encoder library.  Indeed, I had to put a bunch of definitions for the pin to int mapping on various boards.  It's a shame that info isn't available as a macro or inline function from the core...

Here's that library, if anyone's interested.

http://www.pjrc.com/teensy/td_libs_Encoder.html

Thank you for your contribution Paul.

Lefty
Logged

Pages: [1]   Go Up
Jump to: