Go Down

Topic: Need a S-I-M-P-L-E example of using PinChangeInt or ooPinChangeInt (Read 2230 times) previous topic - next topic

warren631

Jul 05, 2013, 10:14 pm Last Edit: Jul 05, 2013, 10:16 pm by warren631 Reason: 1
I need two interrupts on a Nano (interrupts on pins 2 and 3) to measure the duration of pulses from two wheels to get speed feedbacks for PID loops.  I'm already using pin 3 for PWM to a motor drive and I am changing the PWM frequency on timers 1 and 2 (pins 3,9,10,11).  So I need two software interrupts.  I have been trying to understand PinChangeInt and ooPinChangeInt (what's the difference?) and I find it tough going.  I don't know a class from a hole in the ground, let alone sub classes.  Do I have to use different ports (D,B,C) for the two interrupt pins or can I use pins 4 and 5?

I have tried to understand the examples downloaded with the libraries and the Wiki but I am dumbfounded.  Anyone know of a simple example with two software interrupts?  I learn by copying and trying.  Thanks for any help!

MarkT

[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

warren631

#2
Jul 06, 2013, 12:00 am Last Edit: Jul 06, 2013, 12:10 am by warren631 Reason: 1
That is simple, but how would I add another interrupt pin?  And what is pin 15? I don't have a input 15. Can I use pins 4 and 5? Is this OK:
Code: [Select]
/*
Copyright 2011 Lex.V.Talionis at gmail
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*/
   #include <PinChangeInt.h>
   #include <PinChangeIntConfig.h>
   
   #define PIN 4  // the pin we are interested in
   #define PIN2 5  // the pin we are interested in
   volatile byte burp=0;    // a counter to see how many times the pin has changed
   volatile byte burp2=0;    // a counter to see how many times the pin has changed
   byte cmd=0;     // a place to put our serial data
   
   void setup() {
     Serial.begin(9600);
     Serial.print("PinChangeInt test on pin ");
     Serial.println(PIN);
     Serial.print("PinChangeInt test on pin ");
     Serial.println(PIN2);
     pinMode(PIN, INPUT);     //set the pin to input
     digitalWrite(PIN, HIGH); //use the internal pullup resistor
     pinMode(PIN2, INPUT);     //set the pin to input
     digitalWrite(PIN2, HIGH); //use the internal pullup resistor
     PCintPort::attachInterrupt(PIN, burpcount,RISING); // attach a PinChange Interrupt to our pin on the rising edge
     PCintPort::attachInterrupt(PIN2, burpcount2,RISING); // attach a PinChange Interrupt to our pin on the rising edge
   // (RISING, FALLING and CHANGE all work with this library)
   // and execute the function burpcount when that pin changes
     }
   
   void loop() {
     cmd=Serial.read();  
     if (cmd=='p')
     {
       Serial.print("burpcount:\t");
       Serial.println(burp, DEC);
       Serial.print("burpcount2:\t");
       Serial.println(burp2, DEC);
     }
     cmd=0;
   }
   
   void burpcount()
   {
     burp++;
   }
   void burpcount2()
   {
     burp2++;
   }



MarkT

pin 15 on the Uno is the same as A1.

The example shows attachInterrupt being called for two pins, check with the library sources but I suspect
you just call it for any and all pins you want, and the library handles dispatching to your handlers - that would
be the obvious way to do it.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

warren631

The original example was for only one pin - 15.  I modified it above for two pins - 4 and 5.  Is that OK?  Or do the pins have to be on different vectors (whatever they are) or ports?

DuaneB

What Arduino are you using, not all of them support pin change int on every pin.

See here for an example using multiple pin change ints and the last link for a guide to which pins can be used on which Arduinos -

Background - because of the way the class is written, you dont need to know that its a class, just use a slightly different way of calling the functions -
http://rcarduino.blogspot.ae/2012/03/need-more-interrupts-to-read-more.html

Example usage
http://rcarduino.blogspot.ae/2012/04/how-to-read-multiple-rc-channels-draft.html

Usable pins
http://rcarduino.blogspot.ae/2013/04/the-problem-and-solutions-with-arduino.html

Duane B

rcarduino.blogspot.com
Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

Measureino

#6
Aug 02, 2013, 07:23 am Last Edit: Aug 02, 2013, 10:21 am by Measureino Reason: 1
Like others I#ve some problems understanding and using PinChangeInt. On my Mega2560 I need three interrupt pins. In the past I tested with two external Pins and had success.But now externals can't be used, because of usage by shields.

I want to use PintChangedInt with the analogue pins A1,A2 and A3 for interrupts with light barriers, which change when reflecting from HIGH to LOW. Is the follwoing code snippet correct for this?
Code: [Select]

#define Pin1 15
#define Pin2 16
#define Pin3 17
pinMode(Pin1, INPUT);     //set the pin to input
digitalWrite(Pin1, HIGH); //use the internal pullup resistor
pinMode(Pin2, INPUT);    
digitalWrite(Pin2, HIGH);
pinMode(Pin3, INPUT);    
digitalWrite(Pin3, HIGH);
PCintPort::attachInterrupt(Pin1, ISR_for_Pin1,FALLING); // attach
PCintPort::attachInterrupt(Pin2, ISR_for_Pin2,FALLING); // attach
PCintPort::attachInterrupt(Pin3, ISR_for_Pin3,FALLING); // attach

curlyz

Dear friends , this is my first contribute to the community.
I got to say this , Pin change is very simple that you dont even need to use a library for.
I learned form Qeewiki , all pin on atmega328 can be interrupt.
What is Pin Chang Int different from pin 2 and 3 interrupt?
1. It is grouped , meaning that when ever one pin of a group has change state , you must check which pin has change.
2. It is Pin Change , unlike pin 2 and 3 which ony interrupt on RISE, fall .... so you need to check the state of that pin to know if it was a rise or a fall
How to use?
1. Set your interrupt pin to be INPUT or INPUT_PULLUP
2. There are 3 group , check the pinout , let say i want to interrupt on pin 4, 5 and A3.
So I check the register map, 4 is PCINT20 belong to PCMSK2 , 5 is PCINT21 (PCMSK2) and A3 is PCINT11 (PCMSK1);
3. so I use PCMSK1 and PCMSK2 right ? then I tell the UNO that I use this two by setting the PCICR
Code: [Select]
PCICR |=  ( (1 << PCIE1) | (1 << PCIE2);
this mean , hey , enable interrupt on group 1 and 2.
4. Now , i define each group , which pin will trigger group interrupt
I use PCINT20 and PCINT21 (pin 4 and 5 ) which belong to PCMSK2 so
Code: [Select]
PCMSK2 |=( (1 << PCINT20) | (1 << PCINT21) ; 
The same for pin A3
Code: [Select]
PCMSK1 |= (1 << PCINT11) ;
5. So i use PCINT1 and PCINT2 right , then I will set the ISR,
Code: [Select]

ISR (PCINT1_vect)
{//THIS WILL BE CALL WHEN A3 change state
//if you read the pin right now and it is HIGH then it was a RISE, else , It was a FALL
   
}
ISR (PCINT2_vect)
{//THIS WILL BE CALL WHEN EITHER PIN 4 or 5 change state
   //THIS IS TRICKIER BECAUSE YOU MUST KNOW Which pin change state, so you need a history to compare
uint8_t changedbits;


    changedbits = PINB ^ portbhistory; // PINB if PCINT2, PIND if PCINT0, PINC for PCINT1
    portbhistory = PINB;// CHANGE HERE ALSO

    //DONT USE DIGITAL READ HERE , INSTEAD use port manipulation
//If you dont know why this is PINB0, check picture below
    if(changedbits & (1 << PINB0))
    {
        /* PCINT0 changed */
    }
   
    if(changedbits & (1 << PINB1))
    {
        /* PCINT1 changed */
    }

    if(changedbits & (1 << PINB2))
    {
        /* PCINT2 changed */
    }
}

All thanks to the Q guys , you are rock.


let say PINB0 , it is PB0 , which is pin 8
somehow if the picture dont show up , go to the source.
https://sites.google.com/site/qeewiki/books/avr-guide/external-interrupts-on-the-atmega328

larryd

Thanks for stepping into the deep end.



.
No technical PMs.
The last thing you did is where you should start looking.

Go Up