so habe mich nun einmal mit dem Thema Tasterentprellung auseinandergesetzt. Habe einen Code im Netz gefunden und diesen in meinen Code eingebunden. Leider besteht das Problem "ich blink wie ich will" immer noch. Was habe ich falsch gemacht?
Code:
#define F_CPU 1000000UL
#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#define LED1 0
#define LED2 1
#define LED3 2
#define LED4 3
//variablen für entprellung
#define TASTERPORT PIND
#define TASTERBIT PIND2
volatile int r = 1;
//variablen für interrupt
volatile int a = 1;
volatile int b = 2;
volatile int c;
volatile int d;
//Entprellung
char taster(void)
{
static unsigned char zustand;
char rw = 0;
if(zustand == 0 && !(TASTERPORT & (1<<TASTERBIT))) //Taster wird gedrueckt (steigende Flanke)
{
zustand = 1;
rw = 1;
}
else if (zustand == 1 && !(TASTERPORT & (1<<TASTERBIT))) //Taster wird gehalten
{
zustand = 2;
rw = 0;
}
else if (zustand == 2 && (TASTERPORT & (1<<TASTERBIT))) //Taster wird losgelassen (fallende Flanke)
{
zustand = 3;
rw = 0;
}
else if (zustand == 3 && (TASTERPORT & (1<<TASTERBIT))) //Taster losgelassen
{
zustand = 0;
rw = 0;
}
return rw;
}
int main(void)
{
//I/O-Ports einstellen
DDRB = 0b00001111;
//Einstellung Interrupt
PORTD = 0b00000100;
GIMSK = (1<<INT0);
MCUCR = (1<<ISC01) | (1<<ISC00);
//Globale interrupts aktivieren
sei();
//Wert in Variable setzen
c = 2;
while (1)
{
switch(c)
{
case 1:
PORTB |= ((1<<LED1) | (1<<LED3)); //LED1 und 3 ein
PORTB &= ~((1<<LED2) | (1<<LED4)); //LED2 und 4 aus
_delay_ms(500);
PORTB |= ((1<<LED2) | (1<<LED4)); //LED2 und 4 ein
PORTB &= ~((1<<LED1) | (1<<LED3)); //LED1 und 3 aus
_delay_ms(500);
break;
case 2:
PORTB |= ((1<<LED1) | (1<<LED2) | (1<<LED3) | (1<<LED4)); //LED1-4 ein
_delay_ms(500);
PORTB &= ~((1<<LED1) | (1<<LED2) | (1<<LED3) | (1<<LED4)); //LED1-4 aus
_delay_ms(500);
break;
}
}
}
ISR(INT0_vect)
{
if (taster == r); //Wenn Taster gedrückt
{
//switch case Abfrage
switch(c)
{
case 1: d = b;
break;
case 2: d = a;
break;
default: PORTB &= ~((1<<LED1) | (1<<LED2) | (1<<LED3) | (1<<LED4));
}
c = d;
}
}
Ist das überhaupt der richtige Code für Tasterentprellung?
Gruß
Alex
Lesezeichen