Hallo Leute, ich möchte mir eine Schaltung basteln die Abfragt ob eine Taste kurz oder länger gedrückt wurde. Bei kurzem druck soll eine Variable erhöt werden Bei Langem Druck der Wert ins EEPROM gespeichert werden. Bisher hab ich mir folgendes zusammengestrickt:
1 | //----------------------------------------
|
2 | // Titel : Arcade Tastenprogrammierung
|
3 | //----------------------------------------
|
4 | #include <avr/io.h> |
5 | #include <avr/interrupt.h> |
6 | #include <stdint.h> |
7 | #include <avr/eeprom.h> |
8 | #include <avr/pgmspace.h> |
9 | |
10 | typedef unsigned char BYTE; |
11 | typedef unsigned int WORD; |
12 | |
13 | BYTE bPortB; |
14 | BYTE nKeyPress; |
15 | |
16 | const unsigned char Tabelle[] PROGMEM = {249, 164, 176, 153, 146, 130, 248, 128, 144}; |
17 | |
18 | #define CNTDEBOUNCE 10
|
19 | #define CNTREPEAT 200
|
20 | |
21 | #define KEY_PIN PINB
|
22 | #define KEY_PINNO PB6
|
23 | |
24 | uint8_t eeFooByte; |
25 | volatile uint8_t gKeyCounter; |
26 | |
27 | |
28 | ISR(TIMER1_COMPA_vect) |
29 | {
|
30 | uint8_t tmp_kc; |
31 | |
32 | tmp_kc = gKeyCounter; |
33 | |
34 | if (!(KEY_PIN & (1<<KEY_PINNO))) |
35 | {
|
36 | if (tmp_kc < CNTREPEAT) |
37 | {
|
38 | tmp_kc++; |
39 | }
|
40 | }
|
41 | else
|
42 | {
|
43 | tmp_kc = 0; |
44 | }
|
45 | gKeyCounter = tmp_kc; |
46 | }
|
47 | |
48 | void init(void) |
49 | {
|
50 | PORTB |= _BV(6); // Pull-Up Port B6 aktivieren |
51 | DDRB = 0xFF; // Port B als Eingang |
52 | DDRD = 0xFF; // Port D als Ausgang |
53 | TIMSK |= (1<<TOIE1); //Timer1 Interrupt aktiviert |
54 | TCCR1B = 1; //Prescaler 1 |
55 | TCNT1 = 65535-3600; //Preloader 3600 |
56 | }
|
57 | |
58 | int main (void) |
59 | {
|
60 | init(); |
61 | sei(); |
62 | |
63 | bPortB = 1; |
64 | nKeyPress = eeprom_read_byte(&eeFooByte); |
65 | |
66 | while(1) |
67 | {
|
68 | |
69 | |
70 | if ( gKeyCounter > CNTDEBOUNCE ) |
71 | {
|
72 | if (gKeyCounter == CNTREPEAT) // Code fuer "Taste lange gedrueckt" |
73 | {
|
74 | eeprom_write_byte(&eeFooByte, nKeyPress); |
75 | }
|
76 | else // Code fuer "Taste kurz gedrueckt" |
77 | {
|
78 | PORTD = pgm_read_byte(&Tabelle[nKeyPress]); |
79 | if (nKeyPress < 8) |
80 | {
|
81 | nKeyPress++; |
82 | |
83 | }
|
84 | else
|
85 | {
|
86 | nKeyPress = 0; |
87 | |
88 | }
|
89 | }
|
90 | }
|
91 | |
92 | }
|
93 | return 0; |
94 | }
|
Jedoch ist da noch irgendwo ein hund begraben. Mit den Interrupts tu ich mir etwas schwer. Ich bin sicher das da der Fehler drinn steckt.