Go Down

Topic: How to create 3th interrupt (Read 5 times) previous topic - next topic

FusiveResonance

Code sample for setting interrupts on some other pins.

Refer to Section 12 - External Interrupts in  the data sheet
In addition, here's a pin map. http://arduino.cc/en/Hacking/PinMapping168

Code: [Select]
//make analog pins 5 and 6 into pin change interrupts
//this corresponds to PCINT1_vect  
PCICR |= (1 << PCIE1);
PCMSK1 |= (1 << PCINT12);
PCMSK1 |= (1 << PCINT13);

//pin change interrupt on digital pin 9
//this corresponds to PCINT0_vect
PCICR |= (1 << PCIE0);
PCMSK0 |= (1 << PCINT1);

//pin change interrupt on digital pin 4
//this corresponds to PCINT2_vect
PCICR |= (1 << PCIE2);
PCMSK2 |= (1 << PCINT20);


You will notice that I have given examples which use each of the corresponding masks and pin change interrupt enable bits.


Now, that you've configured the board for interrupts, we may now tell it what to do when an interrupt occurs. In your posted code example, we simply had one interrupt service routine run, but now that we have the board configured for interrupts on 3 pins, lets setup 3 different interrupt service routines.

Code: [Select]
 ISR_0 ( PCINT0_vect )
{
     //do something
}

ISR_1 ( PCINT1_vect )
{
     //do something else
}

ISR_2 ( PCINT2_vect )
{
     //do something different
}


EriSan500

Thanks FusiveResonance for the wonderfull explanation.  ;)

Still need to try this, was a bit bussy with real life (family and things).

Can this be modified to have FALLING or RISING interupts, or do I simply check inside the ISR the pins for HIGH or LOW?

Greetings,
EriSan500

FusiveResonance

Quote
Can this be modified to have FALLING or RISING interupts, or do I simply check inside the ISR the pins for HIGH or LOW?


No, this is strictly for pin CHANGE interrupts.

Yes, you will have to implement your own system of detecting rising vs falling.

Mind you, remember to keep your interrupt service routine short, as when in a heavy ISR block, you run the risk of missing another interrupt. Depending on the device you have hooked up on this pin, consider debouncing.

Finally, turn off interrupts when you're inside an ISR, as you shouldn't allow an interrupt while you're already servicing one.

Code: [Select]


#define atomic(off) PCICR = 0x07 //sets the pin change interrupt control register to B00000111
#define atomic(on) PCICR = 0x00 //sets the pin change interrupt control register to B00000000



Now in your ISR you may write something like:

Code: [Select]

ISR ( PCINT1_vect)
{
  atomic(on); //masks all interrupts. ie. sets the interrupt mask

  //some code

  atomic(off); //resets the interrupt control registers to enable interrupts
}


Be warned, depending on whether or not you are actually using all 3 pin change interrupt capabilities, you might need to modify the hex values in the atomic(on) define statement above. Please see section 12.2.4 in the atmel data sheet for more details.

Some good reading, although doesn't have anything on pin change interrupts, but I believe that you will need to include the headers that they include in their code snippet. http://gonium.net/md/2006/12/20/handling-external-interrupts-with-arduino/

Good luck. Let me know if you have issues, as I haven't tested the above code.

Coding Badly

Quote
Finally, turn off interrupts when you're inside an ISR, as you shouldn't allow an interrupt while you're already servicing one.

This is not necessary.  The processor automatically handles it for you.

FusiveResonance

Quote

This is not necessary.  The processor automatically handles it for you.


Wow. never knew that. Thanks.

Go Up