Forum: Mikrocontroller und Digitale Elektronik 2-Bit Zähler rückwerts zählen lassen


von Holzwurm (Gast)


Lesenswert?

Hallo Forum,
Ich habe mir bei Pollin das AVR-Board gehohlt und wollte den 2-Bit 
Zähler aus dem Pollin Tut zum Rückwerts zählen bringen.
Nun mein Problem
Wenn ich einen Taster drücke leuchten die Led´s wie sie wollen.
Im Anhang packe ich mal meinen code rein er ist in C.
Besten dan im vorraus für eure Hilfe
Euer Holzwurm

von Holzwurm (Gast)


Angehängte Dateien:

Lesenswert?

Ach hier noch der Code

von Klaus T. (gauchi)


Lesenswert?

Ich würde mal vermuten, dass deine Tasten prellen -> Entprellung

von Holzwurm (Gast)


Lesenswert?

Klaus T. schrieb:
> Ich würde mal vermuten, dass deine Tasten prellen -> Entprellung

ICh habe jetzt das Entprellen mit eingefügt aber es hat sich nicht 
verbessert wenn ich den Teil wegnehme wo Taster2 dazu kam dann läuft es 
problemlos.
ich bin Ratlos

von Hannes (Gast)


Lesenswert?

nur zur info, wenn du beide Signale invertierst dann zält er auch 
rückwärts...

Hannes

von Klaus B. (Gast)


Lesenswert?

Hallo,

an deinem Code fällt mir auf, daß du den Tasterzustand über das Register 
PINC abfrägst, in deiner Initialisierung aber DDRB konfigurierst. Dh. 
dein Input am Port C wird vermtl. "floaten" und deshalb irgendwelche 
Störsignale auffangen.

Sieh dir das mal an ...

Gruß

von Peter D. (peda)


Lesenswert?

1
#define   F_CPU     1e6
2
#include <util/delay.h>
3
#include <avr/io.h>
4
5
6
// LEDs sind high-active geschaltet
7
#define LED_DDR         DDRD
8
#define LED_PORT        PORTD
9
#define LED1            PD5
10
#define LED2            PD6
11
#define LED3            PD7
12
 
13
// TASTER ist high-active geschaltet
14
#define TASTER1         PC0      //hoch
15
#define TASTER2         PC1
16
#define KEY_PIN         PINC
17
18
19
uint8_t key_state;                              // debounced key state:
20
                                                // bit = 1: key pressed
21
uint8_t key_press;                              // key press detect
22
23
24
void debounce( void )                           // about every 10ms
25
{
26
  static uint8_t ct0 = 0xFF, ct1 = 0xFF;        // 8 * 2bit counters
27
  uint8_t i;
28
29
  i = KEY_PIN;                                  // read keys (high active)
30
  i ^= key_state;                               // key changed ?
31
  ct0 = ~( ct0 & i );                           // reset or count ct0
32
  ct1 = ct0 ^ (ct1 & i);                        // reset or count ct1
33
  i &= ct0 & ct1;                               // count until roll over ?
34
  key_state ^= i;                               // then toggle debounced state
35
  key_press |= key_state & i;                   // 0->1: key press detect
36
}
37
38
39
uint8_t get_key_press( uint8_t key_mask )
40
{
41
  key_mask &= key_press;                        // read key(s)
42
  key_press ^= key_mask;                        // clear key(s)
43
  return key_mask;
44
}
45
46
 
47
void ausgabe(uint8_t wert)
48
{
49
  LED_PORT = wert << 5;                         // PD5, 6, 7
50
  LED_DDR = 1<<LED1 | 1<<LED2 | 1<<LED3;
51
}
52
53
54
int main(void) 
55
{
56
  uint8_t count = 0;
57
58
  for(;;){
59
     _delay_ms( 10 );                           // debounce time
60
     debounce();
61
62
     if( get_key_press( 1<<TASTER1 ))
63
       count++;
64
     if( get_key_press( 1<<TASTER2 ))
65
       count--;
66
     ausgabe( count );
67
  }
68
}


Peter

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.