/*
Project: ATTiny85 Interrupt Test
Description: Shows relevant registers to set up
for various types of interrupts
Creation date: 12/25/23
Author: AnonEngineering
Inspired by
https://docs.wokwi.com/parts/wokwi-attiny85 simulate ATTINY85 on wokwi
https://wokwi.com/projects/317140124437578305 get interrupts working
https://wokwi.com/projects/318885209198035539 debounce function.
https://thewanderingengineer.com/2014/08/11/pin-change-interrupts-on-attiny85/ datasheet attiny85 ints.
https://electronics.stackexchange.com/questions/264434/attiny85-interrupt-confusion "only PB2 on attiny85 can be used to trap edge changes"
https://arduino.stackexchange.com/questions/66761/debouncing-a-button-with-interrupt
Wokwi | questions
AudioDiWhy — 12/23/2023 3:54 PM
License: https://en.wikipedia.org/wiki/Beerware
*/
#include <TinyDebug.h>
// not needed in Wokwi
//#include <avr/io.h>
//#include <avr/interrupt.h>
#define INT0_VECTOR INT0_vect
const int BTN_PIN = PB2;
const int LED_PIN = PB0;
const unsigned long DEBOUNCE_TIME = 20;
volatile bool isInterrupt = false;
int value = 0;
ISR(INT0_VECTOR) {
isInterrupt = true;
}
bool debounceButton() {
static int oldBtnState = HIGH;
bool isButtonPressed = false;
int btnState = digitalRead(BTN_PIN);
if (btnState != oldBtnState) {
oldBtnState = btnState;
if (btnState == LOW) {
isButtonPressed = true;
}
delay(DEBOUNCE_TIME);
}
return isButtonPressed;
}
void setup() {
Debug.begin();
pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
cli(); // Disable interrupts during setup
// set falling edge INT0 interrupt on PB2
GIMSK = 0b01000000; // 9.3.2 GIMSK – General Interrupt Mask Register
//GIFR = 0b01000000; // 9.3.3 GIFR – General Interrupt Flag Register
//PCMSK = 0b00000000; // 9.3.4 PCMSK – Pin Change Mask Register, any change
MCUCR = 0b00000010; // Table 9-2. Interrupt 0 Sense Control, falling
sei(); // enables interrupts
Debug.println("Ready\n");
}
void loop() {
if (isInterrupt) {
Debug.println("Interrupt!");
isInterrupt = false;
if (debounceButton()) {
value++;
Debug.print("Value: ");
Debug.println(value);
digitalWrite(LED_PIN, value % 2);
}
}
}