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 );
}
Hallo,
kann der folgende Code funktionieren?
Danke
LG
Günter Osswald
#include
#define CLOCK 0 // phys. Pin13 ATtiny84
#define LOAD 1 // phys. Pin12 ATtiny84
#define RESET 2 // phys. Pin11 ATtiny84
// Flags um einen Interrupt anzuzeigen sind nur zum Entprellen erforderlich
// bzw. den Interrupthandler aus dem Hauptprogramm aufzurufen
volatile boolean flagClockUp = false;
volatile boolean flagClockDown = false;
volatile boolean flagLoad = false;
volatile boolean flagReset = false;
void setup() {
// put your setup code here, to run once:
pinMode(CLOCK, INPUT_PULLUP; // ISR-Pin für Clock-Signal
pinMode(LOAD, INPUT_PULLUP; // ISR-Pin für Load-Signal
pinMode(RESET, INPUT_PULLUP; // ISR-PIN für Reset-Signal
attachPcInterrupt(CLOCK, ClockUp, RISING); // assign ISR: ClockUp
attachPcInterrupt(CLOCK, ClockDown, FALLING); // assign ISR: ClockDown
attachPcInterrupt(LOAD, LoadActive, RISING); // assign ISR: LoadActive
attachPCInterrupt(RESET, ResetActive, RISING); // assign ISR: ResetActive
}
void loop() {
// put your main code here, to run repeatedly:
}
void ClockUp(void){
// Code für ClockUp hier
}
void ClockDown(void){
// Code für ClockDown hier
}
void LoadActive(void){
// Code für Load hier
}
void ResetActive(void){
// Code für Reset hier
}
Hallo Oswald,
Leider bin ich nicht mehr im Thema, da ich seit mehr als 3 Jahren fast nur noch Schaltungen mit dem ESP8266 und ESP32 umsetze.
Daher kann ich Deinen Code nicht bewerten und müsste mich da auch erst wieder einlesen.
LG
Thomas