added button handling via polling

This commit is contained in:
Sven Vogel 2023-10-27 17:57:11 +02:00
parent fa2d9edaa8
commit d331685f21
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"
// ---------------------------------------
//
// DDR set bit of port to either input or output
// PORTx set bit of port value
// PINx read voltage of bit of port D
#ifdef BUTTON_POLL
void init() {
// ---------------------------------------
// set Port B to output
// we will write through B5
SET_OUTPUT(DDRB, DDB5);
CLR_BIT(PORTB, DDB5);
// ---------------------------------------
// setup input port
// we will read through D2/D3
// 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);
}
void loop() {
// poll voltage of pin 2 of port D
if (!GET_BIT(PIND, PIND2)) {
SET_BIT(PORTB, DDB5);
}
// poll voltage of pin 3 of port D
if (!GET_BIT(PIND, PIND3)) {
CLR_BIT(PORTB, DDB5);
}
}
#endif

13
src/button_poll/button.h Normal file
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

@ -2,9 +2,11 @@
extern void init(); extern void init();
extern void loop(); extern void loop();
#include "blink/blink.h" //#include "blink/blink.h"
#include "button_poll/button.h"
#define BLINK //#define BLINK
#define BUTTON_POLL
int main() { int main() {
init(); init();