Skip to content

Instantly share code, notes, and snippets.

@lazlyhu
Created April 17, 2018 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lazlyhu/e6a309348ca658d63e38f8cd66d2cee8 to your computer and use it in GitHub Desktop.
Save lazlyhu/e6a309348ca658d63e38f8cd66d2cee8 to your computer and use it in GitHub Desktop.
Attiny interupt handle
#include "avr/interrupt.h"
bool value;
#define STATUS_PIN PB4 //Led marker for show the user what happened
void setup() {
pinMode(STATUS_PIN, OUTPUT);
digitalWrite(STATUS_PIN, HIGH);
GIMSK = 0b00100000; // turns on pin change interrupts
PCMSK = 0b00000100; // turn on interrupts on pins PB2
sei(); // enables interrupts
}
void loop() {
}
ISR(PCINT0_vect) {
value = !value;
digitalWrite(STATUS_PIN, value);
}
#include <PinChangeInterruptHandler.h>
#define STATUS_PIN PB4 //Led marker for show the user what happened
// Inherit from PinChangeInterruptHandler
class MyLib : PinChangeInterruptHandler {
byte inputPin;
bool led;
public:
MyLib(byte pin) : inputPin(pin) {}
void begin() {
pinMode(inputPin, INPUT);
attachPCInterrupt(digitalPinToPCINT(inputPin));
pinMode(STATUS_PIN, OUTPUT);
digitalWrite(STATUS_PIN, HIGH);
}
void stop() {
detachPCInterrupt(digitalPinToPCINT(inputPin));
digitalWrite(STATUS_PIN, LOW);
}
// Overwrite handlePCInterrupt() method
virtual void handlePCInterrupt(int8_t interruptNum, bool value) {
led = !led;
digitalWrite(STATUS_PIN, led);
}
};
MyLib mylib(PB2);
void setup() {
mylib.begin();
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment