Forum: Mikrocontroller und Digitale Elektronik RGB-Code Fehleranalyse


von Thomas (Gast)


Lesenswert?

Hi,
habe nun einen Code geschrieben um mittels eines Tasters die Farben 
einer RGB-LED umzuschalten. Dabei soll so gut wie möglich der Verlauf 
eines Farbkreises nachgebildet werden. Bei jedem Druck auf den Taster 
soll sich die Farbe um ein Stcük ändern. Das funktioniert ab und zu gut, 
dann ändert sich die Farbe jedoch plötzlich wieder auf einen absolut 
falschen Wert.
Vielleicht findet ja jemand von euch meinen Fehler.

mfg und danke Thomas
1
#include <asf.h>
2
#include <avr/io.h>
3
#include <avr/pgmspace.h>
4
5
#define F_CPU 16000000
6
7
uint8_t shortPress=5; //50ms debounce Zeit
8
uint16_t longPress=50; //500ms Wartezeit
9
volatile uint8_t i=0;
10
volatile uint8_t colorVariable=0;
11
12
13
const uint8_t RGB_Value[] PROGMEM ={      //RGB Array
14
  255, 0, 0,
15
  255, 32, 0,
16
  255, 64, 0,
17
  255, 96, 0,
18
  255, 128, 0,
19
  255, 159, 0,
20
  255, 191, 0,
21
  255, 223, 0,
22
  255, 255, 0,
23
  223, 255, 0,
24
  191, 255, 0,
25
  159, 255, 0,
26
  128, 255, 0,
27
  96, 255, 0,
28
  64, 255, 0,
29
  32, 255, 0,
30
  0, 255, 0,
31
  0, 255, 32,
32
  0, 255, 64,
33
  0, 255, 96,
34
  0, 255, 128,
35
  0, 255, 159,
36
  0, 255, 191,
37
  0, 255, 223,
38
  0, 255, 255,
39
  0, 223, 255,
40
  0, 191, 255,
41
  0, 159, 255,
42
  0, 128, 255,
43
  0, 96, 255,
44
  0, 64, 255,
45
  0, 32, 255,
46
  0, 0, 255,
47
  32, 0, 255,
48
  64, 0, 255,
49
  96, 0, 255,
50
  128, 0, 255,
51
  159, 0, 255,
52
  191, 0, 255,
53
  223, 0, 255,
54
  255, 0, 255,
55
  255, 0, 223,
56
  255, 0, 191,
57
  255, 0, 159,
58
  255, 0, 128,
59
  255, 0, 96,
60
  255, 0, 64,
61
  255, 0, 32,
62
  255, 0, 0,
63
  };
64
65
ISR (TIMER2_COMPA_vect){            //ISR Routine - Wird ca alle 10ms aufgerufen
66
  uint8_t tmp_cnt;
67
  tmp_cnt = i;
68
  if((PIND & (1<<PD4))){                  //Ist der Taster beim Aufruf gedrückt wird die Laufvariable erhöht
69
    tmp_cnt++;}
70
  if(!(PIND & (1<<PD4))){                  //Ist beim neuen Aufruf der ISR der Tasternicht gedrückt wird abgefragt wie lang er gedrückt war
71
    if((tmp_cnt>shortPress) && (tmp_cnt<longPress)){  //Wenn der Taster nur kurz gedrückt war
72
      tmp_cnt=0;
73
      colorVariable=colorVariable+3;  // Weiterschalten der Farbe
74
      ~(PINB |= (1<<PB5));}      //Pin Togglen für Feedback, bzw. Kontrolle der Entprell-Routine
75
    if(tmp_cnt>longPress){
76
      tmp_cnt=0;
77
      //langer Druck = true
78
      }
79
    tmp_cnt=0;
80
    }
81
    i=tmp_cnt;
82
  }
83
84
int main (void)
85
{
86
  board_init();
87
  
88
  DDRD |= (1<<PD6) | (1<<PD5);
89
  DDRB |= (1<<PB1) | (1<<PB5);
90
  DDRD &= ~(1<<PD4);
91
  
92
  
93
  TCCR0A |= (1<<WGM00) | (1<<WGM01) | (1<<COM0A1) |(1<<COM0B1); // fast PWM Mode, None-inverted mode (High at bottom, Low on Match)
94
  TCCR0B |= (1<<CS00);              //No Prescaler
95
  
96
  TCCR1A |= (1<<WGM10) | (1<<COM1A1) | (1<<COM1B1);
97
  TCCR1B |= (1<<WGM12) | (1<<CS10);        // Timer 1 für Fast PWM-Initialisieren
98
  
99
  TCCR2A |= (1<<WGM21);              // Timer 2 CTC Mode
100
  TCCR2B |= (1<<CS22) | (1<<CS21) | (1<<CS20);  // Clock/1024
101
  OCR2A = 156;                  // ISR Aufruf alle 10ms
102
  
103
  sei();
104
  
105
  TIMSK2 |= (1<<OCIE2A);
106
  
107
  
108
  for(;;){
109
    if(colorVariable==49){
110
      colorVariable=0;};
111
    OCR0A = pgm_read_byte (&RGB_Value[colorVariable]);      //Wert für jede Farbe aus RGB Tabelle an Compare register übergeben
112
    OCR0B = pgm_read_byte (&RGB_Value[colorVariable+1]);
113
    OCR1A = pgm_read_byte (&RGB_Value[colorVariable+2]);
114
  }
115
}

von sebastian (Gast)


Lesenswert?

Du zählst die Variable um 3 hoch, prüfst aber so:
> if(colorVariable==49)

49 ist nicht durch 3 teilbar.

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.