3

According to the datasheet:

enter image description here

we could think that, if we want to have a pin change interrupt for 3 pins, we have to create multiple instances:

ISR(PCINT0_vect){
   ...
}

ISR(PCINT1_vect){
   ...
}

ISR(PCINT2_vect){
   ...
}

void setup(){
  GIMSK = 0b00100000;
  PCMSK = 0b00000111; 
}

However, this does not work, and I read here and here that we have to define just one interrupt function:

ISR(PCINT0_vect){
   if (digitalRead(0) == LOW) 
     ...
   if (digitalRead(1) == LOW) 
     ...
   if (digitalRead(2) == LOW) 
     ...
}

Why is that so? What is PCINT1, 2, 3, ... made for then in this pinout schematics, if we don't have to use it?

Basj
  • 449
  • 2
  • 9
  • 23

3 Answers3

4

That is simply the difference between an external interrupt INTx and a pin change interrupt PCINT. The first is an interrupt for a single pin. The second is an interrupt for a complete group of pins. Normally this group is a complete port. As the Attiny85 only has one port, this is the case here. So the whole group is only 1 single interrupt source. Thus it only has one interrupt vector named PCINT0_vect.

A pin change interrupt triggers, when any of the pins under it's supervision changes. You can remove pins from it's supervision by masking them with the PCMSK register.

So the interrupt vector PCINT_vect will be triggered by every change on any of the pins, that are not masked out. It is up to you to check, which one changed it's level.


On bigger microcontrollers, there are more pins and mostly also most of them are connected to a pin change interrupt. Here mostly every complete port is connected to a single pin change interrupt. Thus the corresponding registers are additionally marked with a number (PCINT0_vect as the interrupt vector for the first port, ...)

chrisl
  • 16,622
  • 2
  • 18
  • 27
3

You are confusing Pin Change Interrupt pins with Pin Change Interrupt vectors. The two are very different.

Many Pin Change Interrupt pins are associated with just one Pin Change Interrupt vector.

You use the pin number (the value shown in the pinout) to decide if you want that pin to respond or not. You use the vector to respond any of the pins that are assigned to that vector.

In the ATTiny chips, since there are so few Pin Change Interrupt pins (8 or less) you only need one Pin Change Interrupt vector to handle them.

You can know if "PCINT4" corresponds to a pin or a vector by the context. If you see it on a pin then it is a pin. If you see it in code with _vect after it then it's a vector.

Majenko
  • 105,851
  • 5
  • 82
  • 139
1

You can enable/disable Pin Change INTerrupts for individual pins using the Pin Change Mask Register PCMSK.

The names on the pins map the bit in PCMSK to the physical pin.

You find more information in section 9.3.4 on p. 52 in the ATtiny85 Data Sheet

Kwasmich
  • 1,523
  • 12
  • 18