Skip to content

Instantly share code, notes, and snippets.

@avr-programmierung
Last active May 14, 2019 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avr-programmierung/47957c0371c6fb2d36321cd948b7a7fc to your computer and use it in GitHub Desktop.
Save avr-programmierung/47957c0371c6fb2d36321cd948b7a7fc to your computer and use it in GitHub Desktop.
ATmega88 @ 1MHz Tastenzustand 01
/* tastenzustand_01.c ATmega88 @ 1MHz */
#include <avr/io.h>
#include <util/delay.h>
#define button_down !(PIND & (1<<PIND2)) // Taster an PD2 = low
#define led_on PORTB |= (1<<PB0) // PB0 = High
#define led_off PORTB &= ~(1<<PB0) // PB0 = Low
enum button {off, on} state; // off=0, on=1
int main(void)
{
DDRB = 0xFF; // PORTB = Ausgang
DDRD = ~(1<<PIND2); // PORTD PIN0 = Eingang
while(1)
{
if (button_down) // Wenn Taster gedrückt ist
{
for (uint8_t i=0; i<10; i++) // Schleife mit 10 Durchläufen
{
_delay_us(1); // Prellzeit abwarten
if (button_down) // Wenn Taster noch immer gedrückt ist
asm ("NOP"); // nichts tun (Assembleranweisung NOP = No Operation)
else
i = 0; // setze i auf 0 zurück wenn ein High detektiert wurde
}
if (state == off) // Wenn Zustand zuvor OFF war
{
led_on; // LED ON
state = on;
}
else if (state == on) // Wenn Zustand zuvor ON war
{
led_off; // LED OFF
state = off;
}
while (button_down); // Warten bis Taster losgelassen wird
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment