Gast
#1809357
Hallo,
Ich habe mich mal an ner Software Entprellung versucht und dabei
folgenden Code produziert.
Die Idee dahinter:
1. bei einem Keypress starte ich einen Timer t (der zählt im interrupt
alle 8ms hoch)
2. nach 16ms also t==2 wird geprüft ob der Keypress noch immer besteht.
3. wenn dies zutrifft und die Taste anschließend wieder losgelassen ist,
wird ein Zähler hochgezählt.
Hab dabei leider festgestellt das mein Programm dieses nur einmal macht.
Irgendwo scheint er nach dem 1sten Hochzählen festzusitzen.
Wäre dankbar wenn mir einer sagen würde wo der Fehler ist. Compiler
schluckt den Code so wie er unten steht.
#include <avr/io.h>
#include "lcd-routines.h"
#include <avr/interrupt.h>
#include <stdlib.h>
#include <math.h>
#ifndef F_CPU
#define F_CPU 8000000L
#endif
uint16_t keypress;
char Buffer[20],keystate;
volatile uint16_t t,t2;
void WriteToLCD(void)
{
lcd_clear();
set_cursor(0,1);
lcd_string(itoa(keypress, Buffer, 10));
}
int main(void)
{
DDRB |= (0 << PB1); // set PB1 as input
PORTB |= (1 << PB1); // activate pullup for PB1
lcd_init();
TIMSK |= (1 << TOIE0); // enable overflow interrupt
sei(); //enable global interrupt
TCCR0 |= (0 << CS00) | (0 << CS01) | (1 << CS02);
//prescale of 256 overflow at 8,192ms
for(;;)
{
if ((keystate == 0) && (!(PINB & (1 << 1)))) //keypress detected
{
keystate=1;
t=0;
}
if ((t==2) && (keystate==1) && (!(PINB & (1 << 1)))) //keypress
still active after 16ms
{
keystate=2;
}
if ((keystate==2) && (PINB & (1 << 1))) //keyrelease detected =
valid keypress
{
keypress++;
WriteToLCD();
keystate=0;
}
}
}
ISR(TIMER0_OVF_vect)
{
t++; //overflow at 8,192ms
}
Gruß vollfreak