added button handling via interrupts

This commit is contained in:
Sven Vogel 2023-10-27 17:58:21 +02:00
parent d331685f21
commit 3a8fa2a56e
3 changed files with 64 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -3,10 +3,12 @@ extern void init();
extern void loop(); extern void loop();
//#include "blink/blink.h" //#include "blink/blink.h"
#include "button_poll/button.h" //#include "button_poll/button.h"
#include "button_interupt/button.h"
//#define BLINK //#define BLINK
#define BUTTON_POLL //#define BUTTON_POLL
#define BUTTON_INTERRUPT
int main() { int main() {
init(); init();