From 0e60b9ba972b37dff95a67bcf6710dbe176aec21 Mon Sep 17 00:00:00 2001 From: Andrea Manni Date: Fri, 3 Feb 2017 20:03:09 +0100 Subject: [PATCH] interrupts --- .../base/base.ino} | 8 +++- .../interrupts/debounce/debounce.ino | 44 ++++++++++++++++++ .../interrupts/doppia_ISR/doppia_ISR.ino | 38 ++++++++++++++++ .../interrupt_base_volatile.ino | 45 +++++++++++++++++++ .../input_interrupt_0/input_interrupt_0.ino | 31 ------------- .../input_interrupt_1_debounce.ino | 41 ----------------- 6 files changed, 134 insertions(+), 73 deletions(-) rename advanced_projects/{interrupt_base/interrupt_base.ino => interrupts/base/base.ino} (81%) create mode 100644 advanced_projects/interrupts/debounce/debounce.ino create mode 100644 advanced_projects/interrupts/doppia_ISR/doppia_ISR.ino create mode 100644 advanced_projects/interrupts/interrupt_base_volatile/interrupt_base_volatile.ino delete mode 100644 interrupts/input_interrupt_0/input_interrupt_0.ino delete mode 100644 interrupts/input_interrupt_1_debounce/input_interrupt_1_debounce.ino diff --git a/advanced_projects/interrupt_base/interrupt_base.ino b/advanced_projects/interrupts/base/base.ino similarity index 81% rename from advanced_projects/interrupt_base/interrupt_base.ino rename to advanced_projects/interrupts/base/base.ino index 6118c0b..9b23dc6 100644 --- a/advanced_projects/interrupt_base/interrupt_base.ino +++ b/advanced_projects/interrupts/base/base.ino @@ -1,4 +1,10 @@ -// sketch 03_01_interrupts +/* + * Interrupt base + * + * Utilizzo di un interrupt ala Arduino per intercettare + * la pressione di un bottone. + * + */ int ledPin = 13; diff --git a/advanced_projects/interrupts/debounce/debounce.ino b/advanced_projects/interrupts/debounce/debounce.ino new file mode 100644 index 0000000..0857296 --- /dev/null +++ b/advanced_projects/interrupts/debounce/debounce.ino @@ -0,0 +1,44 @@ +/* + Interrupts : input deboounce + + Utilizzare un interrupt per intercettare + l'input di un interruttore momentaneo. + + Debounce software nella funzione richiamata. + Per debounce hardware: http://www.all-electric.com/schematic/debounce.htm + + */ + +int ledPin = 13; +volatile int state = LOW; + +void setup() +{ + pinMode(ledPin, OUTPUT); + pinMode(2, INPUT_PULLUP); + attachInterrupt(0, blink, FALLING); +} + +void loop() +{ + //digitalWrite(ledPin, state); + delay(10000); // Mette in pausa Arduino per 10sec +} + +// Funzioni +void blink() +// Modifica dello stato del LED +{ + static unsigned long last_interrupt_time = 0; + // This is not supposed to be global + // as it's olny accesed inside this ISR + + // If interrupts come faster than 20ms, assume it's a bounce and ignore + if (millis() - last_interrupt_time > 20) + { // Azione da intraprendere + state = !state; + digitalWrite(ledPin, state); + last_interrupt_time = millis(); + } +} + diff --git a/advanced_projects/interrupts/doppia_ISR/doppia_ISR.ino b/advanced_projects/interrupts/doppia_ISR/doppia_ISR.ino new file mode 100644 index 0000000..8577def --- /dev/null +++ b/advanced_projects/interrupts/doppia_ISR/doppia_ISR.ino @@ -0,0 +1,38 @@ +/* + * Interrupt doppia ISR + * + * Utilizzo di un interrupt ala Arduino per intercettare + * la pressione di un bottone. + * + * Doppia ISR: la prima ISR intercetta FALLING + * e riconfigura l'interrupt con RISING a una seconda + * ISR che a sua volta ripristina il comportamento + * precedente. + */ + +int ledPin = 13; + +void setup() +{ + pinMode(ledPin, OUTPUT); + pinMode(2, INPUT_PULLUP); + attachInterrupt(0, eventoFall, FALLING); + // Partiamo intercettanto un interrupt FALLING +} + +void loop() +{ +// Tutto avviene nelle ISRs +} + +void eventoFall() // Sempre VOID +{ + digitalWrite(ledPin, HIGH); + attachInterrupt(0, eventoRise, RISING); +} + +void eventoRise() // Sempre VOID +{ + digitalWrite(ledPin, LOW); + attachInterrupt(0, eventoFall, FALLING); +} diff --git a/advanced_projects/interrupts/interrupt_base_volatile/interrupt_base_volatile.ino b/advanced_projects/interrupts/interrupt_base_volatile/interrupt_base_volatile.ino new file mode 100644 index 0000000..6dff6fe --- /dev/null +++ b/advanced_projects/interrupts/interrupt_base_volatile/interrupt_base_volatile.ino @@ -0,0 +1,45 @@ +/* + * Interrupt doppia ISR + * + * Utilizzo di un interrupt ala Arduino per intercettare + * la pressione di un bottone. + * + * Doppia ISR: la prima ISR intercetta FALLING + * e riconfigura l'interrupt con RISING a una seconda + * ISR che a sua volta ripristina il comportamento + * precedente. + */ + +const byte ledPin = 13; +const byte interruptPin = 2; // Arduino 328 / 168 +volatile boolean state = 0 ; // volatile evita che la variabile possa +// avere valori diversi in SRAM e in un registro, +// evita che il compilatore in fase di ottimizzazioe la consideri +// come const nel loop() + +void setup() +{ + pinMode(ledPin, OUTPUT); + pinMode(interruptPin, INPUT_PULLUP); // Vedi: digitalPinToInterrupt() + attachInterrupt(0, eventoFall, FALLING); + // Partiamo intercettanto un interrupt FALLING +} + +void loop() +{ + digitalWrite(ledPin, state); + // La variabile state viene richiamata fuori dalla ISR + // quindi deve essere dichiarata volatile +} + +void eventoFall() // Sempre VOID +{ +state = HIGH ; + attachInterrupt(0, eventoRise, RISING); +} + +void eventoRise() // Sempre VOID +{ +state = LOW ; + attachInterrupt(0, eventoFall, FALLING); +} diff --git a/interrupts/input_interrupt_0/input_interrupt_0.ino b/interrupts/input_interrupt_0/input_interrupt_0.ino deleted file mode 100644 index 80d1d17..0000000 --- a/interrupts/input_interrupt_0/input_interrupt_0.ino +++ /dev/null @@ -1,31 +0,0 @@ -/* - Interrupts : input - - Utilizzare un interrupt per intercettare - l'input di un interruttore momentaneo - - */ - -int ledPin = 13; -volatile int state = LOW; - -void setup() -{ - pinMode(ledPin, OUTPUT); - pinMode(2, INPUT_PULLUP); - attachInterrupt(0, blink, FALLING); -} - -void loop() -{ - //digitalWrite(ledPin, state); - delay(10000); // Mette in pausa Arduino per 10sec -} - -// Funzioni -void blink() -// Modifica dello stato del LED -{ - state = !state; - digitalWrite(ledPin, state); -} diff --git a/interrupts/input_interrupt_1_debounce/input_interrupt_1_debounce.ino b/interrupts/input_interrupt_1_debounce/input_interrupt_1_debounce.ino deleted file mode 100644 index 9361345..0000000 --- a/interrupts/input_interrupt_1_debounce/input_interrupt_1_debounce.ino +++ /dev/null @@ -1,41 +0,0 @@ -/* - Interrupts : input deboounce - - Utilizzare un interrupt per intercettare - l'input di un interruttore momentaneo. - - Debounce software nella funzione richiamata. - Per debounce hardware: http://www.all-electric.com/schematic/debounce.htm - - */ - -int ledPin = 13; -volatile int state = LOW; - -void setup() -{ - pinMode(ledPin, OUTPUT); - pinMode(2, INPUT_PULLUP); - attachInterrupt(0, blink, FALLING); -} - -void loop() -{ - //digitalWrite(ledPin, state); - delay(10000); // Mette in pausa Arduino per 10sec -} - -// Funzioni -void blink() -// Modifica dello stato del LED -{ - static unsigned long last_interrupt_time = 0; - // If interrupts come faster than 200ms, assume it's a bounce and ignore - if (millis() - last_interrupt_time > 200) - { // Azione da intraprendere - state = !state; - digitalWrite(ledPin, state); - } - last_interrupt_time = millis(); -} - -- 2.39.2