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;
}
...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.
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.
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
// +----+