ATtiny85 Interrupt basierend auf PinChange Interrupt Library
Der ATtiny85 bietet einen Hardware Interrupt INT0 auf dem PIN PB2.
Dieser kann mit folgender Funktion zugewiesen werden:
attachInterrupt(0,wakeUpFunction, LOW); // use interrupt 0 (pin 2) and run
// function wakeUpFunction when pin 2 gets LOW
Wenn mehrere PINs per Interrupt ausgewertet werden sollen, muss man ein SW-Interrupt verwenden.
Im arduino Forum habe ich ein funktionierendes Beispiel gefunden: http://forum.arduino.cc/index.php?topic=65706.msg1756727#msg1756727
Folgend die Elemente in der Reihenfolge, wie sie aufgerufen werden:
Include + declarations
#include "Arduino.h" // required for eclipse IDE
#include <PinChangeInterrupt.h>
#define USVActive 0 // Pin5 - Input
#define BUTTON_DOWN 2 // Pin7 - Input
// flags to indicate if interrupt has been detected
volatile boolean flagIntUSV = false;
volatile boolean flagIntBUT = false;
setup()
pinMode(BUTTON_DOWN, INPUT_PULLUP); // ISR-PIN reading Button
pinMode(USVActive, INPUT); // ISR-Pin reading digital value
attachPcInterrupt(BUTTON_DOWN, ButtonInterrupt ,FALLING ); // assign ISR: ButtonInterrupt ()
attachPcInterrupt(USVActive, USVInterrupt ,FALLING ); // assign ISR: USVInterrupt ()
loop()
void loop() {
if (flagIntUSV) USVHandler(); // if ISR has set ISR-Flag: call dedicated function to do something
if (flagIntBUT) BUTHandler();
sleepNow(); // sleep function called here; the program will go to sleep here and wait for interrupt
}
sleep() – this function will send tiny to sleep and wait for interrupt
void sleepNow() // here we put the arduino to sleep
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // the most power savings sleep mode is set here
sleep_enable(); // enables the sleep bit in the mcucr register
// so sleep is possible. just a safety pin
attachPcInterrupt(BUTTON_DOWN, ButtonInterrupt ,FALLING );
attachPcInterrupt(USVActive, USVInterrupt ,FALLING ); // CHANGE
sleep_mode(); // here the device is actually put to sleep!!
// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
sleep_disable(); // first thing after waking from sleep:
}
ButtonInterrupt() – this function will be called if interrupt is deteced and set flag
void ButtonInterrupt(void) {
volatile long lastMicros = micros() - prevIntBUT;
prevIntBUT = micros();
if (lastMicros >10000) { // debounce 10ms
flagIntBUT=true;
}
}
BUTHandler() – this function will called from main loop –
void BUTHandler(void) {
flagIntBUT=false;
detachPcInterrupt(BUTTON_DOWN);
// do something
Serial.print("-------- Button pressed ");
attachPcInterrupt(BUTTON_DOWN, ButtonInterrupt ,FALLING );
}