From 3a8fa2a56e3730aed1e5baf6aabeea6e1526436e Mon Sep 17 00:00:00 2001 From: servostar Date: Fri, 27 Oct 2023 17:58:21 +0200 Subject: [PATCH] added button handling via interrupts --- src/button_interupt/button.cpp | 47 ++++++++++++++++++++++++++++++++++ src/button_interupt/button.h | 13 ++++++++++ src/main.cpp | 6 +++-- 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/button_interupt/button.cpp create mode 100644 src/button_interupt/button.h diff --git a/src/button_interupt/button.cpp b/src/button_interupt/button.cpp new file mode 100644 index 0000000..f32fa2a --- /dev/null +++ b/src/button_interupt/button.cpp @@ -0,0 +1,47 @@ +// +// Created by servostar on 27.10.23. +// + +#include "button.h" + +#ifdef BUTTON_INTERRUPT + +ISR(INT0_vect) +{ + PORTB = 0; +} + +ISR(INT1_vect) +{ + PORTB = 0xff; +} + +void init() { + // set B as output + DDRB = 0xff; + PORTB = 0x00; + + // set Port D to input (bit 2/3, remember bit 0/1 are for UART) + SET_INPUT(DDRD, DDD2); + SET_INPUT(DDRD, DDD3); + // put 5V on input (make it pull up resistor) + SET_BIT(PORTD, DDD2); + SET_BIT(PORTD, DDD3); + + // configure interrupt mode + SET_INTERRUPT_MODE(0, FALLING); + SET_INTERRUPT_MODE(1, FALLING); + + // enable interrupt 0 and 1 + ENABLE_INTERRUPT(0); + ENABLE_INTERRUPT(1); + + // enable interrupts globally + sei(); +} + +void loop() { + +} + +#endif \ No newline at end of file diff --git a/src/button_interupt/button.h b/src/button_interupt/button.h new file mode 100644 index 0000000..286d28d --- /dev/null +++ b/src/button_interupt/button.h @@ -0,0 +1,13 @@ +// +// Created by servostar on 27.10.23. +// + +#ifndef ARDUINO_BUTTON_H +#define ARDUINO_BUTTON_H + +#include "prelude.h" + +void init(); +void loop(); + +#endif //ARDUINO_BUTTON_H diff --git a/src/main.cpp b/src/main.cpp index 84bbf39..0001e4c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,10 +3,12 @@ extern void init(); extern void loop(); //#include "blink/blink.h" -#include "button_poll/button.h" +//#include "button_poll/button.h" +#include "button_interupt/button.h" //#define BLINK -#define BUTTON_POLL +//#define BUTTON_POLL +#define BUTTON_INTERRUPT int main() { init();