Skip to content

Instantly share code, notes, and snippets.

@avr-programmierung
Created May 14, 2019 10:09
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/bc960f85ab55117d3baf44adb8420cb8 to your computer and use it in GitHub Desktop.
Save avr-programmierung/bc960f85ab55117d3baf44adb8420cb8 to your computer and use it in GitHub Desktop.
ATmega88 @ 8MHz speed button
/*
* speed_button_1.c
* Controller: ATmega88 @ 8MHz
*/
#include <avr/io.h>
int main(void)
{
DDRB = 0xFF; // Richtungsregister PORTB auf Ausgang
DDRD &= ~(1<<PD2); // Richtungsregister PORTD PinD2 auf Eingang
uint8_t tastenzustand = 0; // Flag für Tastenzustand definieren
uint32_t x=0; // Datentyp für x = unsigned long (Wertebereich 0 bis 4 294 967 295)
while(1)
{
if (!(PIND & (1<<PD2))) // wenn PinD2 = low (gedrückt)
{
PORTB |= (1<<PB0); // LED1 ON
x++; // Zählvariable x um 1 erhöhen
tastenzustand = 1; // Flag für "Taster wurde gedrückt" setzen
}
else if ((PIND & (1<<PD2)) && (tastenzustand == 1)) // wenn PinD2 = high und Taster losgelassen
{
PORTB &= ~(1<<PB0); // LED1 OFF
tastenzustand = 0; // Flag für den Tastenzustand zurücksetzen
if ((x > 5000) && (x <= 40000)) // wenn x > 5000 (Tasterprellen abwarten) und <= 40000
{
PORTB |= (1<<PB7); // LED4 ON (Anzeige für Superschnell!)
x=0; // Zählvariable x zurücksetzen
}
else if ((x > 40000) && (x <= 60000)) // wenn x > 40000 und <= 60000
{
PORTB |= (1<<PB2); // LED3 ON (Anzeige für Schnell!)
x=0; // Zählvariable x zurücksetzen
}
else if ((x > 60000) && (x <= 80000)) // wenn x > 60000 und <= 80000
{
PORTB |= (1<<PB1); // LED2 ON (Anzeige für Langsam!)
x=0; // Zählvariable x zurücksetzen
}
else (x > 300000) // wenn x > 300000 (Langer Tastendruck -> Neues Spiel)
{
x=0; // Zählvariable x zurücksetzen
PORTB &= ~((1<<PB7)|(1<<PB2)|(1<<PB1)); // Bit 1,2 und 7 löschen (LED 2,3 und 4 OFF)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment