Forum: Mikrocontroller und Digitale Elektronik Tastenentprellroutine- bin ich zu doof?


von Avr_Junkie (Gast)


Lesenswert?

Hallo leute ,
ich versuche hat schon mehrere Stunden die Routine von Peter Dannegger 
zum laufen zu bringen
1
/************************************************************************/
2
/*                                                                      */
3
/*                      Debouncing 8 Keys                               */
4
/*                      Sampling 4 Times                                */
5
/*                      With Repeat Function                            */
6
/*                                                                      */
7
/*              Author: Peter Dannegger                                 */
8
/*                      danni@specs.de                                  */
9
/*                                                                      */
10
/************************************************************************/
11
 
12
#include <stdint.h>
13
#include <avr/io.h>
14
#include <avr/interrupt.h>
15
 
16
#ifndef F_CPU
17
#define F_CPU           1000000                   // processor clock frequency
18
#warning kein F_CPU definiert
19
#endif
20
 
21
#define KEY_DDR         DDRD
22
#define KEY_PORT        PORTD
23
#define KEY_PIN         PIND
24
#define KEY0            7
25
#define KEY1            6
26
#define KEY2            5
27
#define ALL_KEYS        (1<<KEY0 | 1<<KEY1 | 1<<KEY2)
28
 
29
#define REPEAT_MASK     (1<<KEY1 | 1<<KEY2)       // repeat: key1, key2
30
#define REPEAT_START    50                        // after 500ms
31
#define REPEAT_NEXT     20                        // every 200ms
32
 
33
#define LED_DDR         DDRC
34
#define LED_PORT        PORTC
35
#define LED0            3
36
#define LED1            4
37
#define LED2            5
38
 
39
volatile uint8_t key_state;                                // debounced and inverted key state:
40
                                                  // bit = 1: key pressed
41
volatile uint8_t key_press;                                // key press detect
42
 
43
volatile uint8_t key_rpt;                                  // key long press and repeat
44
 
45
 
46
ISR( TIMER0_OVF_vect )                            // every 10ms
47
{
48
  static uint8_t ct0, ct1, rpt;
49
  uint8_t i;
50
 
51
  TCNT0 = (uint8_t)(int16_t)-(F_CPU / 1024 * 10e-3 + 0.5);  // preload for 10ms
52
 
53
  i = key_state ^ ~KEY_PIN;                       // key changed ?
54
  ct0 = ~( ct0 & i );                             // reset or count ct0
55
  ct1 = ct0 ^ (ct1 & i);                          // reset or count ct1
56
  i &= ct0 & ct1;                                 // count until roll over ?
57
  key_state ^= i;                                 // then toggle debounced state
58
  key_press |= key_state & i;                     // 0->1: key press detect
59
 
60
  if( (key_state & REPEAT_MASK) == 0 )            // check repeat function
61
     rpt = REPEAT_START;                          // start delay
62
  if( --rpt == 0 ){
63
    rpt = REPEAT_NEXT;                            // repeat delay
64
    key_rpt |= key_state & REPEAT_MASK;
65
  }
66
}
67
 
68
///////////////////////////////////////////////////////////////////
69
//
70
// check if a key has been pressed. Each pressed key is reported
71
// only once
72
//
73
uint8_t get_key_press( uint8_t key_mask )
74
{
75
  cli();                                          // read and clear atomic !
76
  key_mask &= key_press;                          // read key(s)
77
  key_press ^= key_mask;                          // clear key(s)
78
  sei();
79
  return key_mask;
80
}
81
 
82
///////////////////////////////////////////////////////////////////
83
//
84
// check if a key has been pressed long enough such that the
85
// key repeat functionality kicks in. After a small setup delay
86
// the key is reported beeing pressed in subsequent calls
87
// to this function. This simulates the user repeatedly
88
// pressing and releasing the key.
89
//
90
uint8_t get_key_rpt( uint8_t key_mask )
91
{
92
  cli();                                          // read and clear atomic !
93
  key_mask &= key_rpt;                            // read key(s)
94
  key_rpt ^= key_mask;                            // clear key(s)
95
  sei();
96
  return key_mask;
97
}
98
 
99
///////////////////////////////////////////////////////////////////
100
//
101
uint8_t get_key_short( uint8_t key_mask )
102
{
103
  cli();                                          // read key state and key press atomic !
104
  return get_key_press( ~key_state & key_mask );
105
}
106
 
107
///////////////////////////////////////////////////////////////////
108
//
109
uint8_t get_key_long( uint8_t key_mask )
110
{
111
  return get_key_press( get_key_rpt( key_mask ));
112
}
113
 
114
int main( void )
115
{
116
  KEY_DDR &= ~ALL_KEYS;                // konfigure key port for input
117
  KEY_PORT |= ALL_KEYS;                // and turn on pull up resistors
118
 
119
  TCCR0 = (1<<CS02)|(1<<CS00);      // divide by 1024
120
  TIMSK = 1<<TOIE0;        // enable timer interrupt
121
 
122
  LED_PORT = 0xFF;
123
  LED_DDR = 0xFF;                     
124
 
125
  while(1){
126
    if( get_key_short( 1<<KEY1 ))
127
      LED_PORT ^= 1<<LED1;
128
 
129
    if( get_key_long( 1<<KEY1 ))
130
      LED_PORT ^= 1<<LED2;
131
 
132
                                                  // single press and repeat
133
 
134
    if( get_key_press( 1<<KEY2 ) || get_key_rpt( 1<<KEY2 )){
135
      uint8_t i = LED_PORT;
136
     
137
      i = (i & 0x07) | ((i << 1) & 0xF0);
138
      if( i < 0xF0 )
139
        i |= 0x08;
140
      LED_PORT = i;      
141
    }
142
  }
143
}

Wie aus der Definition ersichtlich hängen die 3 Leds an Pinc 5-3 und die 
Taster an Pind 7-5.
Jetz gleich zur ersten Frage , im Source steht die internen Pullup's 
sind aktiv, wie sieht jetzt eigentlich die Verdrahtung aus? Vcc 
->uC->Taster->Masse?

Zweite Frage beim drücken der Taster passiert rein garnichts, kein Rauch 
,kein blitzen gar nichts nur die Led leuchte schön hell?
Ich hab irgendwie das Bauchgefühl das da was mit den Interrupts nicht 
stimmen könnte gehe ich da richtig davon aus?
Achja das ganze läuft auf einem Mega8 mit fcpu 1Mhz
Vielen Dank schonmal fuer die Antworten im Vorraus , auch wenn die Frage 
schon 1000 mal gestellt wurde, ich hab mich jedenfalls vorher schon 
bemüht also nicht gleich hacken ;)


Gruß Thommy

von Karl H. (kbuchegg)


Lesenswert?

Avr_Junkie schrieb:

> sind aktiv, wie sieht jetzt eigentlich die Verdrahtung aus? Vcc
> ->uC->Taster->Masse?


Gegenfrage:
Wenn du den Taster drückst, stellst du dann ja mehr oder weniger eine 
direkte Verbindung von Vcc zu Masse her. Wie nennt man sowas?
Genau: Kurzschluss

Der Taster wird ganz einfach mit einem Ende am µC-Pin und am anderen 
Ende an Masse angeschlossen

von Avr_Junkie (Gast)


Lesenswert?

Danke lieber Mod :D
funktioniert jetzt... wie war das nochmal mit Wald und Bäumen?^^

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.