Hi, I'm in a project that uses pin change interrupts and I have one or two questions..
first, I have this function:
ISR (PCINT0_vect)
{
// one of pins D8 to D13 has changed
}
Is it possible to know which pin has changed ?
And is it possible to attach the same interrupt on multiple pins (Like D8 and D9)?
Thanks.
Yes, you read the register to see which caused the PCINT.
There's a register for each of the 4 ports.
See about 1/3 of the way down
1 Like
Is it possible to know which pin has changed ?
Take a look at the Bitwise XOR(^) and Bitwise AND (&) operators. You can use the & mask to select the pins of interest on the Port, and if you are concerned with more than one pin, the ^ to determine which bits have changed.
And is it possible to attach the same interrupt on multiple pins (Like D8 and D9)?
Yes, if the two pins are on the same Port, you enable the pin change interrupt on the group of pins on the port by setting a bit on the Pin Change Interrupt Control Register (PCICR), and then you enable the specific pins in that group with bits in the PinChangeMaskRegister (PCMSKX).
PCICR |= (1<<PCIE0);//enable group interrupts on PORTB PCINT[7:0]
PCMSK0 |= (1<<PCINT0);//enable interrupt pin 8
PCMSK0 |= (1<<PCINT1);//enable interrupt pin 9
If you are not going to use a pin change interrupt library, the the ATMega 328 data sheet will be invaluable.
Or just do a digitalRead() on the pin that interests you. The port and bitmask stuff is only necessary if you are really squeezed for speed and need to optimise.
Thanks guys for your answers.
But I still don't have the answer to my question. What register I have to read to know which pin caused the PCINT ? Just reading the gpio port status is not enough to know which pin has changed
But I still don't have the answer to my question. What register I have to read to know which pin caused the PCINT ? Just reading the gpio port status is not enough to know which pin has changed
Try following the suggestion from MorganS to just use digitalRead() on the pin or pins of interest.
Was a pin HIGH and it went LOW? Was a pin LOW and it went to HIGH? If you can't figure out how to tell which pin changed, post some simple test code which indicates what problem you are having.
cattledog:
Try following the suggestion from MorganS to just use digitalRead() on the pin or pins of interest.
Just reading the gpio port status is not enough to know which pin has changed. If there is multiple pins used on the same interrupt, I don't know witch one have triggered the interrupt. The digitalRead only tell me the pin x is HI or LOW.. I don't have the information of the last state of each pins, and I don't plan to do it..
fantasiiio:
Hi, I'm in a project that uses pin change interrupts and I have one or two questions..
first, I have this function:
ISR (PCINT0_vect)
{
// one of pins D8 to D13 has changed
}
Is it possible to know which pin has changed ?
And is it possible to attach the same interrupt on multiple pins (Like D8 and D9)?
Thanks.
GreyGnome has already written a pretty good library to handle pinchange interrupts EnableInterrupts
check out his code.
Chuck.
fantasiiio:
Just reading the gpio port status is not enough to know which pin has changed. If there is multiple pins used on the same interrupt, I don't know witch one have triggered the interrupt. The digitalRead only tell me the pin x is HI or LOW.. I don't have the information of the last state of each pins, and I don't plan to do it..
Correct. The pin-change interrupts cannot tell you which pin it was from the 'port' of 8 pins. If you need to know, then you need to store that within your program.
I don't have the information of the last state of each pins, and I don't plan to do it..
You may not plan to do it, but if you don't do it, you won't be able to tell if a state has changed.
What is your problem with saving the last state of a pin?
cattledog:
You may not plan to do it, but if you don't do it, you won't be able to tell if a state has changed.
What is your problem with saving the last state of a pin?
It's was just... not planed.. lol
I was sure the mcu could know which pin has triggered the interrupt... but apparently not
And thanks to chucktodd for the link to the library !