Go Down

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

EriSan500

Dec 04, 2009, 10:19 am Last Edit: Dec 04, 2009, 10:20 am by erisan500 Reason: 1
Hi all,

can someone post some code on how to create a 3th interrupt (and evt more) with the possibility to set RISING or FALLING?

I went through the datasheet of the 328 but i'm not getting it.  :-[

Thanks in advance,
EriSan500

CaptainObvious

3rd*

And I don't believe there is a 3rd interrupt available on the 328.  If you want more interrupts, you'll need to upgrade. I know for sure the 1280 (Arduino Mega) has like 5 or 6 interrupts?

What are you trying to do with the interrupts exactly? There may be a way to utilize just 1 interrupt to handle multiple buttons.

For example.. if that's what you're doing.. using multiple buttons, you can use an analog input pin, set resistors on each of the buttons, and use analog-read to get button pushes. This way, you only use 1 pin for like.. 3 buttons or how many ever you're using, and anytime one of those 3 buttons is pushed, it interrupts, but only using 1 arduino pin.

http://www.nuelectronics.com/estore/index.php?main_page=product_info&cPath=1&products_id=2

Here's an example of a shield using that, it only uses analog pin 0 to read all the buttons.

EriSan500

CaptainObvious,

have a look at http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1257584961 it explains why i (we) need 3 interrupts.

EriSan500


EriSan500

ok, i guess you are reffering to this piece of code:

Code: [Select]

#include <avr/interrupt.h>  
#include <avr/io.h>

#define LEFT false
#define RIGHT true

int mPin1 = 5; // H-bridge leg 1
int mPin2 = 6; // H-bridge leg 2
int mEnable = 7;

boolean volatile mDirection = RIGHT;
int volatile mSpeed = 200;

ISR( PCINT1_vect )
{
if( ( ( PINC & _BV( PC4 ) ) && mDirection == LEFT ) ||
    ( ( PINC & _BV( PC5 ) ) && mDirection == RIGHT ) )
    switch_direction();
}

void setup() {
pinMode( mPin1, OUTPUT );
pinMode( mPin2, OUTPUT );
pinMode( mEnable, OUTPUT );

//make analog pins 5 and 6 into pin change interrupts  
PCICR |= (1 << PCIE1);
PCMSK1 |= (1 << PCINT12);
PCMSK1 |= (1 << PCINT13);  

digitalWrite( mEnable, HIGH );
set_motor();
}

void loop() {  
}

void set_motor()
{
boolean speedPin = ( mDirection ) ? mPin1 : mPin2;
boolean unusedPin = ( mDirection ) ? mPin2 : mPin1;

analogWrite( unusedPin, 0 );
analogWrite( speedPin, mSpeed );  
}

void switch_direction()
{
mDirection = !mDirection;  
set_motor();
}


Can someone explain the following pieces:

Code: [Select]

ISR( PCINT1_vect )
{
if( ( ( PINC & _BV( PC4 ) ) && mDirection == LEFT ) ||
    ( ( PINC & _BV( PC5 ) ) && mDirection == RIGHT ) )
    switch_direction();
}

especialy the "PCINT1_vect" and "PINC & _BV(PC4)"

and this:
Code: [Select]

//make analog pins 5 and 6 into pin change interrupts  
PCICR |= (1 << PCIE1);
PCMSK1 |= (1 << PCINT12);
PCMSK1 |= (1 << PCINT13);

i figured out i have to look in the datasheet to find out that analog pin 5 = PCINT12 and 6 = PCINT13. Can i also assign digital pins in this way?



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.

Arnonh

I am getting this error with your code
Code: [Select]


error: '__vector_3' was not declared in this scope

EriSan500

I gave it a try too, and i'm also getting an error:

Code: [Select]

#include <avr/interrupt.h>  
#include <avr/io.h>


void setup() {
//pin change interrupt on digital pin 5
//this corresponds to PCINT0_vect  
PCICR |= (1 << PCIE0);
PCMSK0 |= (1 << PCINT21);

//pin change interrupt on digital pin 6
//this corresponds to PCINT1_vect
PCICR |= (1 << PCIE1);
PCMSK1 |= (1 << PCINT22);

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

[glow]ISR_0( PCINT0_vect )[/glow]
{
     //do something
}

ISR_1( PCINT1_vect )
{
     //do something else
}

ISR_2( PCINT2_vect )
{
     //do something different
}

void loop()
{

}


error pointing to highlighted line:
Code: [Select]
error: expected constructor, destructor, or type conversion before '(' token

Any idea what i might be doing wrong?

Greetings,
EriSan500

EriSan500

#12
Dec 07, 2009, 02:33 pm Last Edit: Dec 07, 2009, 02:33 pm by erisan500 Reason: 1
ok, made some progress (dunno if this is correct though).

I changed
Code: [Select]
ISR_0( PCINT0_vect )
{
     //do something
}
ISR_1( PCINT1_vect )
{
     //do something
}
ISR_2( PCINT2_vect )
{
     //do something
}

to
Code: [Select]
ISR( PCINT0_vect )
{
     //do something
}
ISR( PCINT1_vect )
{
     //do something
}
ISR( PCINT2_vect )
{
     //do something
}


and it compiled without errors.
Will test this as soon as my new arduino's arrive.

Do i use DigitalRead(pin) inside the ISR to check if the port is HIGH or LOW ? Or is there a better way?

All i need to happen inside the ISR is to increase a counter.

Greetings,
EriSan500

FusiveResonance

#13
Dec 07, 2009, 04:14 pm Last Edit: Dec 07, 2009, 04:22 pm by FusiveResonance Reason: 1
Yes you may use digitalRead inside the ISR, but if you're finding it too slow you might have to ready the binary data directly from the register.

Don't forget to declare any variables used by the ISR as volatile.

Also, at the end of your setup() method, you'll want to call sei(). This will set the global interrupt enable bit. In case you ever want to reset the bit, (thereby turning off interrupts), you may use cli().

EriSan500

I finally got my 2nd arduino and got a chance to test this.

And yes, it does not work as expexted. See below code, if you spot the mistake I made, please let me know.

Code: [Select]

#include <avr/interrupt.h>  
#include <avr/io.h>


void setup() {
 pinMode(5, INPUT);
 Serial.begin(9600);

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

//pin change interrupt on digital pin 6
//this corresponds to PCINT1_vect
PCICR |= (1 << PCIE1);
PCMSK1 |= (1 << PCINT22);

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

ISR( PCINT0_vect )
{
     //do something
Serial.println("interrupt on 5");
}

ISR( PCINT1_vect )
{
     //do something else
}

ISR( PCINT2_vect )
{
     //do something different
}

void loop()
{

}


Go Up