Timer PWM nach manuellem Setzen des Ausgangs

Gast #4064649
Lesenswert?

Hallo,

Ich betreibe an meinem Atmega2560 unter anderem am Timer3 und Timer4 
zwei LEDs mit PWM-Dimmung.

Es funktioniert auch alles so weit, nur wollte ich jetzt nach dem 
Dimmvorgang den Ausgang manuell auf HIGH oder LOW schalten und den Timer 
stoppen.
Ich habe jetzt aber das Problem, dass trotz erneut gestartetem Timer der 
Ausgang auf dem zuvor manuell zugewiesenen Wert stehen bleibt.

Wie kann ich den Timer wieder richtig starten? Ist das überhaupt 
möglich?
Gast #4065493
Lesenswert?

Erst mal vielen Dank für die vielen Antworten.

Übergang von PWM in manuell klappt ohne Probleme, ich komme leider nur 
nicht mehr zurück und weiß nicht woran es liegt...

Hier noch der Code vom Konstruktor (nur einmal im setup, ist ein 
Singleton) und meiner Schleife (wird jede Sekunde einmal durchlaufen)
1
TPWM::TPWM()
2
{
3
  // 16 Bit Fast PWM
4
  TCCR3A = 0x82;
5
  TCCR4A = 0x82;
6
  // stop timer
7
  TCCR3B = 0;
8
  TCCR4B = 0;
9
  // TOP for PWM, full 16 Bit
10
  ICR3 = 0xFFFF;
11
  ICR4 = 0xFFFF;
12

13
  SetProp(P1, 5);
14
  SetProp(P2, 6);
15

16
  pinMode(5, OUTPUT); //Timer 3
17
  PWM1(0);
18
  pinMode(6, OUTPUT); //Timer 4
19
  PWM2(0);
20
}
21

22
void TPWM::PruefeZeiten()
23
{
24
  if (!TimeSetted()) return;
25
  unsigned long t = now() % 86400;
26

27
  for (int i = 1; i <= 2; ++i)
28
  {
29
    TPWMProp& Prop = GetPWMProp(i);
30
    long OnTimeDiff = t - Prop.OnTime;
31
    long OffTimeDiff = t - Prop.OffTime;
32
    long SumDelay = (Prop.StopIndex - Prop.StartIndex - 1) * Prop.Delay;
33
    long i4Position = 0;
34

35
    if ((t >= Prop.OnTime) && (t < Prop.OffTime))
36
    {
37
      i4Position = Prop.StartIndex + (OnTimeDiff / Prop.Delay);
38
      if (i4Position > Prop.StopIndex) i4Position = Prop.StopIndex;
39
    }
40
    else
41
    {
42
      i4Position = Prop.StopIndex - (OffTimeDiff / Prop.Delay);
43
      if (i4Position < Prop.StartIndex) i4Position = Prop.StartIndex;
44
    }
45

46
    if (Prop.TimerStarted && ((i4Position >= 255) || (i4Position <= 0)))
47
    {
48
      if (Prop.Pin == 5) TCCR3B = 0;
49
      else TCCR4B = 0;
50
      Prop.TimerStarted = false;
51
    }
52

53
    if (i4Position > 255) i4Position = 255;
54
    if (i4Position < 0) i4Position = 0;
55

56
    if (i4Position >= 255)
57
    {
58
      if (!Prop.Out)
59
      {
60
        digitalWrite(Prop.Pin, HIGH);
61
        Prop.Out = true;
62
      }
63
    }
64
    else if (i4Position <= 0)
65
    {
66
      if (Prop.Out)
67
      {
68
        digitalWrite(Prop.Pin, LOW);
69
        Prop.Out = false;
70
      }
71
    }
72
    else
73
    {
74
      if (Prop.Position != i4Position)
75
      {
76
        if (!Prop.TimerStarted)
77
        {
78
          if (Prop.Pin == 5)
79
          {
80
            TCCR3B = (1 << WGM12) | (1 << WGM13) | 1;
81
          }
82
          else
83
          {
84
            TCCR4B = (1 << WGM12) | (1 << WGM13) | 1;
85
          }
86
          Prop.TimerStarted = true;
87
        }
88
        if (Prop.Pin == 5) PWM1(i4Position);
89
        else PWM2(i4Position);
90
        Prop.Position = i4Position;
91
      }
92
    }
93
  }
94
}

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren