Enabling pin change interrupt on Attiny84

Hello,
I just want to check with you experts if this small test program correctly enables pin change interrupts on pin 12 and 11 on Attiny84.

int aa;

void setup()
{
  pinMode(A1, INPUT);	   // Pin A1 is input to which a switch is connected
  digitalWrite(A1, HIGH);   // Configure internal pull-up resistor
  pinMode(A2, INPUT);	   // Pin A2 is input to which a switch is connected
  digitalWrite(A2, HIGH);   // Configure internal pull-up resistor

  PCMSK0 = 0b00000110;     // PCINT1 and PCINT2  (pin 12 and 11)
  GIFR   = 0b00000000;     // clear any outstanding interrupts
  GIMSK  = 0b00010000;     // enable pin change interrupts PCIE0=1
  SREG   = 0b10000000;     // enable SREG I-bit
}

void loop() {
  /* Nothing to do:   */
}  

ISR(PCINT0_vect) {    // Interrupt service routine. Every single PCINT8..14 (=ADC0..5) change
            // will generate an interrupt: but this will always be the same interrupt routine
  if (digitalRead(A1)==HIGH)  aa=100 ;
  if (digitalRead(A2)==HIGH)  aa=400;
}

Note for future, instead of setting SREG directly, global interrupts can be enabled by calling

sei();

And disabled by

cli();
  SREG   = 0b10000000;     // enable SREG I-bit

...accomplishes nothing. Remove it.

int aa;

...appears to be intended for sharing with loop. Make it volatile. And don't forget to protect it in loop.

OK, I'll remove SREG, thanks. Is everything else alright? I'm new to Attiny world.

...further to our replies - you are accessing the correct registers to achieve what you want, yes - but you should set the required bits with an OR bit mask, as you are also setting all the other bits to 0 (which you or may not be a problem for you). As your code grows, you'll need to set just one bit in a register without affecting the others.

Google 'avr bit manipulation 101'.

  GIFR   = 0b00000000;     // clear any outstanding interrupts

...accomplishes nothing. From the datasheet...

Alternatively, the flag can be cleared by writing a logical one to it.

You mean logical zero?

No, as Coding Badly says, those flags are cleared by writing a logical one to them (or by the ISR being called, I believe).

I thought clearing a flag means setting it to zero?

Is this the way it should look like:

  PCMSK0 = bit (PCINT1) | bit (PCINT2);
  GIMSK  = bit (PCIE0);

Depends - always read the datasheet, sometimes yes, sometimes no. As far as I'm aware, in all AVR devices the interrupt flags are cleared by writing a one.

gv445:
Is this the way it should look like:

  PCMSK0 = bit (PCINT1) | bit (PCINT2);

GIMSK  = bit (PCIE0);

If you are going to access lots of registers, not use Arduino functions etc, personally I'd say you're better off writing code in Atmel Studio without the Arduino framework. (As the IDE will finish register bit names for you etc...)

What you would do is include <stdio.h> and set GIMSK |= (1 << PCIE0)

OK, thanks for your great advise. Now everything works fine. However I got confused with pin numbering. In Arduino reference I can find that analog input pins can be used as digital pins, referred to as A0, A1, etc. So if I write

pinMode(A1, INPUT);

does it mean that I'd be using Attiny84 pin 12 as digital pin, but if I write

pinMode(9, INPUT);

I will use the same pin 12 as analog pin?
// +-/-+
// VCC 1| |14 GND
// 0 2| |13 10/A0
// 1 3| |12 9/A1/PCINT1
// RESET 4| |11 8/A2/PCINT2
// INT0 2 5| |10 7/A3
// 3/A7 6| |9 6/A4 SCK
// MOSI 4/A6 7| |8 5/A5 MISO
// +----+

No. pinMode is only appropriate when the pin is being used for digital I/O.