Created
April 17, 2018 21:02
-
-
Save lazlyhu/e6a309348ca658d63e38f8cd66d2cee8 to your computer and use it in GitHub Desktop.
Attiny interupt handle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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