Author Topic: [AVR] pin change interupts (PCINT)  (Read 2327 times)

0 Members and 1 Guest are viewing this topic.

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17814
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
[AVR] pin change interupts (PCINT)
« on: December 19, 2015, 04:14:22 pm »
I'm looking into the pin change interupts on AVR's. If i have understood correctly the interupts are grouped every 8 pins so any one of the 8 pins will generate an interupt that will run the same code, so how do I know which pin was the source of the interupt ? presumably I will then have to run a check on each pins state (or rather on each pin that has the interupt enabled) to work it out and presumably the pin state must remain for long enough fo me to enter the routine and check the states of the pins.
 

Offline obiwanjacobi

  • Frequent Contributor
  • **
  • Posts: 988
  • Country: nl
  • What's this yippee-yayoh pin you talk about!?
    • Marctronix Blog
Re: [AVR] pin change interupts (PCINT)
« Reply #1 on: December 19, 2015, 04:21:49 pm »
Yes. In addition you can filter some pins so they don't generate an interrupt. So make the number of pins as small as possible and when an interrupt comes, go see which pin it was...

There are also external interrupt pins - but only a couple, usually.

Be careful with connecting buttons/keyboards etc for the bounce may cause more interrupts than you'd expect. Not recommended - at the very least debounce in hardware (RC).

[2c]
Arduino Template Library | Zalt Z80 Computer
Wrong code should not compile!
 

Offline FreddyVictor

  • Regular Contributor
  • *
  • Posts: 164
  • Country: gb
Re: [AVR] pin change interupts (PCINT)
« Reply #2 on: December 19, 2015, 05:19:25 pm »
presumably I will then have to run a check on each pins state (or rather on each pin that has the interupt enabled) to work it out and presumably the pin state must remain for long enough fo me to enter the routine and check the states of the pins.
it's actually relatively straightforward to spot the changes using a technique like this:

Code: [Select]
#define PIN0   0
ISR(PCINT1_vect)
{
uint8_t pinc = PINC & PCMSK1; // mask so we only get the pins we are interested in !
uint8_t pinc_change = pinc ^ pinc_old; // flag those pins which have changed
pinc_old = pinc; // save port state

// repeat this for all pins required..
if ( pinc_change & (1<<PIN0) )
{
if ( pinc & (1<<PIN0) ) {
// hi
} else {
// lo
}
}
}
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf