#529 ATtiny Pin Change Interrupts
How to use pin change interrupts with the ATtiny85
Here’s a quick demo..
The ATtiny only has one hardware interrupt pin (PB0) but since this is often used for other purposes (like I²C), handling interrupts on other pins needs to be done using the pinc change interrupt (see 9.2 External Interrupts in the datasheet).
This example demonstrates pin change interrupts being used to detect and debounce a pushbutton. On every change, and LED is toggled.
Setting up Pin Change Interrupts
Two steps required:
The relevant bits of the Pin Change Mask Register (PCMSK) register are set to enable interrupts for pins PCINT0 to PCINT5
Enable Pin Change Interrupt by setting bit PCIE in the General Interrupt Mask Register (GIMSK) register
The ATtiny hardware support for the Arduino IDE includes a number of macros to help generalise the register control, including:
digitalPinToPCMSKbitfor setting PCMSK register
digitalPinToPCICRbitfor setting GIMSK register
Pin definitions, with specific reference to the 8-pin PDIP8 ATtiny85:
|Interrupt pin||Port bit||PDIP8 Pin|
The PinChangeInterrupts.ino sketch is a simple demonstration of pin change interrupts. It uses the ATtiny register macros to setup the registers accordingly.
The ATtiny85 is programmed using the Arduino ISP sketch running on an Arduino Uno. See LEAP#070 ProgrammingWithArduinoISP for more info.