Skip to content

Instantly share code, notes, and snippets.

@avr-programmierung
Created May 14, 2019 10:24
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/47fe442c6341b812d58649330383b0a2 to your computer and use it in GitHub Desktop.
Save avr-programmierung/47fe442c6341b812d58649330383b0a2 to your computer and use it in GitHub Desktop.
ATmega88 @ 1MHz Tastenzustand 03
/* tastenzustand_03.c ATmega88 @ 1MHz */
#include <avr/io.h>
#include <util/delay.h>
#define button_down !(PIND & (1<<PD2)) // Taster an PD2 = low
#define led_on PORTB |= (1<<PB0) // PB0 = High
#define led_off PORTB &= ~(1<<PB0) // PB0 = Low
uint16_t ButtonCounter = 0;
int main(void)
{
DDRB = 0xFF; // PORTB = Ausgang
DDRD = ~(1<<PD2); // PD2 = Eingang
while(1)
{
if (button_down) // wenn Taster gedrückt
{
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
}
ButtonCounter = 0; // Button counter set to 0
while (button_down) // So long as button pressed (PB2 = Int0)
{
ButtonCounter ++; // increment Button counter
_delay_ms(1);
if (ButtonCounter >= 700) // if button pressed long
{
led_off;
ButtonCounter --; // Button counter = 699
}
}
if (ButtonCounter <= 698) // if button pressed short
{
led_on;
ButtonCounter = 0; // Button counter set to 0
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment