Forum: Mikrocontroller und Digitale Elektronik Term vereinfachen


von Guido (Gast)


Lesenswert?

Hallo,

in meinem Code fällt mir die ganze Zeit eine bestimmte Routine ins Auge, 
die einfach so hingeklatscht aussieht. Hat vielleicht jemand ne Idee wie 
man diese ein bißchen kompakter bekommt?
1
void battery_routine(void)
2
{
3
    if(release_battery_cut_off == on)
4
    {
5
      if((battery_count >= 100)&&(battery_count <= 113))
6
      {
7
        PORTB &= ~(1 << PB4);
8
        PORTC &= ~(1 << PC4);
9
        PORTB &= ~(1 << PB3);
10
      }
11
12
      if((battery_count >= 125)&&(battery_count <= 138))
13
      {
14
        PORTB &= ~(1 << PB4);
15
        PORTC &= ~(1 << PC4);
16
        PORTB &= ~(1 << PB3);
17
      }
18
19
      if((battery_count >= 151)&&(battery_count <= 164))
20
      {
21
        PORTB &= ~(1 << PB4);
22
        PORTC &= ~(1 << PC4);
23
        PORTB &= ~(1 << PB3);
24
      }
25
26
      if((battery_count >= 114)&&(battery_count <= 124))
27
      {
28
        PORTB |= (1 << PB4);
29
        PORTC |= (1 << PC4);
30
        PORTB |= (1 << PB3);
31
      }
32
33
      if((battery_count >= 139)&&(battery_count <= 150))
34
      {
35
        PORTB |= (1 << PB4);
36
        PORTC |= (1 << PC4);
37
        PORTB |= (1 << PB3);
38
      }
39
40
      if((battery_count >= 165)&&(battery_count <= 180))
41
      {
42
        PORTB |= (1 << PB4);
43
        PORTC |= (1 << PC4);
44
        PORTB |= (1 << PB3);
45
      }
46
47
      if(battery_count >= 180)
48
      {
49
        PORTB |= (1 << PB4);
50
        PORTC |= (1 << PC4);
51
        PORTB |= (1 << PB3);
52
      }
53
54
      if(battery_count <= 100)
55
      {
56
        PORTB |= (1 << PB4);
57
        PORTC |= (1 << PC4);
58
        PORTB |= (1 << PB3);
59
      }
60
    }
61
}

von Andreas (Gast)


Lesenswert?

Hallo,

erst einmal gibt es einen Fehler in der Funktion.
Bei battery_count = 100 werden erst die Pins gelöscht und dann wieder 
gesetzt. Beim Wert 180 ist es ähnlich unschön, ist jedoch wirkungslos, 
da Pins jedesmal gesetzt werden.
Zur Frage:
VerODERe doch die einzelnen Bedingungen.
if ( ((battery_count >= 100)&&(battery_count <= 113))
  || ((battery_count >= 125)&&(battery_count <= 138))
  || ((battery_count >= 151)&&(battery_count <= 164)) )
{
  PORTB &= ~(1 << PB4);
  PORTC &= ~(1 << PC4);
  PORTB &= ~(1 << PB3);
}

if ( ((battery_count >= 114)&&(battery_count <= 124))
  || ((battery_count >= 139)&&(battery_count <= 150))
  || ((battery_count >= 165)&&(battery_count <= 180))
  || (battery_count > 180)
  || (battery_count <= 100) )
{
  PORTB |= (1 << PB4);
  PORTC |= (1 << PC4);
  PORTB |= (1 << PB3);
}

von Patrick (Gast)


Lesenswert?

Vieleicht so:
1
void battery_routine(void)
2
{
3
    if(release_battery_cut_off == on)
4
    {
5
      if(((battery_count >= 100)&&(battery_count <= 113))
6
          | ((battery_count >= 125)&&(battery_count <= 138))
7
          | ((battery_count >= 114)&&(battery_count <= 124)))
8
      p = 1;
9
            
10
      if(((battery_count >= 114)&&(battery_count <= 124))
11
          | ((battery_count >= 139)&&(battery_count <= 150))
12
          | ((battery_count >= 165)&&(battery_count <= 180))
13
          | (battery_count >= 180) | (battery_count <= 100))
14
      p = 0;
15
      
16
      switch (p)
17
      {
18
       case 0: PORTB |= (1 << PB4);
19
               PORTC |= (1 << PC4);
20
               PORTB |= (1 << PB3);
21
               break;
22
       case 1: PORTB &= ~(1 << PB4);
23
               PORTC &= ~(1 << PC4);
24
               PORTB &= ~(1 << PB3);
25
               break;
26
      }
27
    }
28
}

Oder geht auch einfach
1
else p = 0;
?

von Andreas (Gast)


Lesenswert?

Ups,

(battery_count <= 100)

muss natürlich

(battery_count < 100)

sein.

von Patrick (Gast)


Lesenswert?

Da war einer schneller ;-)

von Funkenschlosser (Gast)


Lesenswert?

void PortOn()
{
 PORTC &= ~(1 << PC4);
 PORTB &= ~(1 << PB4) | (1 << PB3);
}

void PortOff()
{
 PORTC |= (1 << PC4);
 PORTB |= (1 << PB4) | (1 << PB3);
}

void battery_routine(void)
{
    if(release_battery_cut_off == on)
    {
      if(( battery_count > = 100 ) && ( battery_count <= 113 ))
      {
        PortOn()
      }

      if(( battery_count >= 125 ) && ( battery_count <= 138 ))
      {
        PortOn()
      }

      if(( battery_count >= 151 ) && ( battery_count <= 164 ))
      {
        PortOn()
      }

      if(( battery_count >= 114 ) &&(battery_count <= 124 ))
      {
        PortOff()
      }

      if(( battery_count >= 139 )&& ( battery_count <= 150 ))
      {
        PortOff()
      }

      if(( battery_count >= 165 ) && ( battery_count <= 180 ))
      {
        PortOff()
      }

      if( battery_count >= 180 )
      {
        PortOff()
      }

      if( battery_count <= 100 )
      {
        PortOff()
      }
    }
}

Nicht effizienter, aber etwas übersichtlicher.

von Luther B. (luther-blissett)


Lesenswert?

Wenn ich das richtig sehe, dann kommt immer entweder PortOn oder 
PortOff, daher:
1
       if((( battery_count > = 100 ) && ( battery_count <= 113 ))
2
         ||(( battery_count >= 125 ) && ( battery_count <= 138 ))
3
         ||(( battery_count >= 151 ) && ( battery_count <= 164 )))
4
          PortOn();
5
       else 
6
          PortOff();

von Karl H. (kbuchegg)


Lesenswert?

Sortiere mal die Abfragen abfsteigend nach den Wertebereichen
1
      if(battery_count >= 180)
2
      {
3
        PORTB |= (1 << PB4);
4
        PORTC |= (1 << PC4);
5
        PORTB |= (1 << PB3);
6
      }
7
8
9
      if((battery_count >= 165)&&(battery_count <= 180))
10
      {
11
        PORTB |= (1 << PB4);
12
        PORTC |= (1 << PC4);
13
        PORTB |= (1 << PB3);
14
      }
15
16
      if((battery_count >= 151)&&(battery_count <= 164))
17
      {
18
        PORTB &= ~(1 << PB4);
19
        PORTC &= ~(1 << PC4);
20
        PORTB &= ~(1 << PB3);
21
      }
22
23
      if((battery_count >= 139)&&(battery_count <= 150))
24
      {
25
        PORTB |= (1 << PB4);
26
        PORTC |= (1 << PC4);
27
        PORTB |= (1 << PB3);
28
      }
29
30
      if((battery_count >= 125)&&(battery_count <= 138))
31
      {
32
        PORTB &= ~(1 << PB4);
33
        PORTC &= ~(1 << PC4);
34
        PORTB &= ~(1 << PB3);
35
      }
36
37
38
      if((battery_count >= 114)&&(battery_count <= 124))
39
      {
40
        PORTB |= (1 << PB4);
41
        PORTC |= (1 << PC4);
42
        PORTB |= (1 << PB3);
43
      }
44
45
      if((battery_count >= 100)&&(battery_count <= 113))
46
      {
47
        PORTB &= ~(1 << PB4);
48
        PORTC &= ~(1 << PC4);
49
        PORTB &= ~(1 << PB3);
50
      }
51
52
      if(battery_count <= 100)
53
      {
54
        PORTB |= (1 << PB4);
55
        PORTC |= (1 << PC4);
56
        PORTB |= (1 << PB3);
57
      }


Dadurch kannst du jetzt jeweils die Obergrenze in den Bereichen
unter den Tisch fallen lassen und die einzelnen Abfragen mit jeweils
einem else-if aneinanderhängen
1
      if(battery_count >= 180)
2
      {
3
        PORTB |= (1 << PB4);
4
        PORTC |= (1 << PC4);
5
        PORTB |= (1 << PB3);
6
      }
7
8
9
      else if( battery_count >= 165 )
10
      {
11
        PORTB |= (1 << PB4);
12
        PORTC |= (1 << PC4);
13
        PORTB |= (1 << PB3);
14
      }
15
16
      else if( battery_count >= 151 )
17
      {
18
        PORTB &= ~(1 << PB4);
19
        PORTC &= ~(1 << PC4);
20
        PORTB &= ~(1 << PB3);
21
      }
22
23
      else if( battery_count >= 139 )
24
      {
25
        PORTB |= (1 << PB4);
26
        PORTC |= (1 << PC4);
27
        PORTB |= (1 << PB3);
28
      }
29
30
      else if( battery_count >= 125 )
31
      {
32
        PORTB &= ~(1 << PB4);
33
        PORTC &= ~(1 << PC4);
34
        PORTB &= ~(1 << PB3);
35
      }
36
37
38
      else if( battery_count >= 114 )
39
      {
40
        PORTB |= (1 << PB4);
41
        PORTC |= (1 << PC4);
42
        PORTB |= (1 << PB3);
43
      }
44
45
      else if( battery_count >= 100 )
46
      {
47
        PORTB &= ~(1 << PB4);
48
        PORTC &= ~(1 << PC4);
49
        PORTB &= ~(1 << PB3);
50
      }
51
52
      else 
53
      {
54
        PORTB |= (1 << PB4);
55
        PORTC |= (1 << PC4);
56
        PORTB |= (1 << PB3);
57
      }

Jetzt sieht man auch, dass der Fall 'zwischen 165 und 180'
identisch ist zum Fall 'größer 180'. Hier kann also die
Fallunterscheidung raus. Dazu dann noch die beiden bereits
vorgeschlagenen Hilfsfunktionen
1
      if( battery_count >= 165 )
2
        PortOn();
3
4
      else if( battery_count >= 151 )
5
        PortOff();
6
7
      else if( battery_count >= 139 )
8
        PortOn();
9
10
      else if( battery_count >= 125 )
11
        PortOff();
12
13
      else if( battery_count >= 114 )
14
        PortOn();
15
16
      else if( battery_count >= 100 )
17
        PortOff();
18
19
      else 
20
        PortOn();

von Guido (Gast)


Lesenswert?

Super, vielen Dank für die ganzen Antworten!

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.