2026-04-13 13:56:47 +02:00
|
|
|
#include <avr/io.h>
|
|
|
|
|
#include <util/delay.h>
|
|
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
|
|
#define LED_PORT PORTB0
|
|
|
|
|
#define BTN_PORT PIND2
|
|
|
|
|
|
|
|
|
|
int main(void) {
|
2026-04-14 09:56:31 +02:00
|
|
|
// set pin to output
|
|
|
|
|
// DDRB is the register that set the direction of the pins (IN/OUT)
|
|
|
|
|
// we want to say that the LED_PORT pin is an OUT pin, which isnt the default
|
|
|
|
|
DDRB |= (_BV(LED_PORT));
|
2026-04-13 13:56:47 +02:00
|
|
|
|
2026-04-14 09:56:31 +02:00
|
|
|
while (true) {
|
|
|
|
|
// to read the pin value (IN pin), we read from the PINX register, and
|
|
|
|
|
// select the correct bit. Here the correct bit is the 1 << BTN_PORT1
|
|
|
|
|
bool cur = (PIND & _BV(BTN_PORT));
|
|
|
|
|
|
|
|
|
|
// depending on if the pin is set, we turn on or off the led
|
|
|
|
|
if (!cur)
|
|
|
|
|
PORTB |= (_BV(LED_PORT)); // on
|
|
|
|
|
else
|
|
|
|
|
PORTB &= ~(_BV(LED_PORT)); // off
|
|
|
|
|
|
|
|
|
|
// lets wait a bit before going again
|
|
|
|
|
_delay_ms(20.f);
|
|
|
|
|
}
|
2026-04-13 13:56:47 +02:00
|
|
|
}
|