Arduino/src/button_poll/button.cpp

44 lines
973 B
C++

//
// 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
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);
}
}