C Verständnisfrage

Gast #1895058
Lesenswert?
• ▲
▼
Moin Moin,

Ich habe mir für mein Entwicklerboard einen Testcode gedownloadet. es 
sieht so aus:
1
/*  Sample program for Olimex AVR-P28 with ATMega8 processor
2
 *  Blinks the led with a speed ~2Hz using a simple delay loop.
3
 *  Compile with AVRStudio+WinAVR (gcc version 3.4.6)
4
 */
5

6
#define  __AVR_ATmega8__  1
7

8
#include "avr/io.h"
9

10
void Initialize(void)
11
{
12
  PORTB = 0x0;
13
  PORTC = 1<<5;  /* turn the LED off */
14
  PORTD = 0x0;
15

16
  DDRB = 0x0;
17
  DDRC = 1<<5;  /* PC5 as output - the LED is there */
18
  DDRD = 0x0;
19

20
}
21

22
/*  state = 0 -> Led Off
23
 *  state = 1 -> Led On
24
 *  state !=[0,1] -> Led Toggle 
25
 */
26
void LedSet(unsigned char state)
27
{
28
  switch (state)
29
  {
30
    case 0:
31
      PORTC = 0xff;
32
      break;
33
    case 1:
34
      PORTC = 0x00;
35
      break;
36
    default:
37
      if (PORTC = 0xff) 
38
        PORTC = 0x00;
39
      else
40
        PORTC = 0xff;
41
  }
42
  
43
}
44

45

46
int main(void)
47
{
48
  int i;
49

50
  Initialize();
51

52
  while (1)
53
  {
54
    LedSet(1);
55
    for (i=60000;i;i--);
56
    LedSet(0);
57
    for (i=60000;i;i--);
58
  }
59
  return 0;
60
}

Effekt: meine LED auf dem Board blinkt. Ich verstehe ja auch warum die 
blinkt.
Was ich aber nicht verstehe... Wenn ich den Code wie folgt umänder, 
müsste sie, nach meinem verständnis dauerhaft leuchten. tut sie aber 
nicht. Um ehrlich zu sein macht die LED dann garnichts mehr.
(Geändert habe ich beim unteren LedSet die 1 in eine 0)
1
/*  Sample program for Olimex AVR-P28 with ATMega8 processor
2
 *  Blinks the led with a speed ~2Hz using a simple delay loop.
3
 *  Compile with AVRStudio+WinAVR (gcc version 3.4.6)
4
 */
5

6
#define  __AVR_ATmega8__  1
7

8
#include "avr/io.h"
9

10
void Initialize(void)
11
{
12
  PORTB = 0x0;
13
  PORTC = 1<<5;  /* turn the LED off */
14
  PORTD = 0x0;
15

16
  DDRB = 0x0;
17
  DDRC = 1<<5;  /* PC5 as output - the LED is there */
18
  DDRD = 0x0;
19

20
}
21

22
/*  state = 0 -> Led Off
23
 *  state = 1 -> Led On
24
 *  state !=[0,1] -> Led Toggle 
25
 */
26
void LedSet(unsigned char state)
27
{
28
  switch (state)
29
  {
30
    case 0:
31
      PORTC = 0xff;
32
      break;
33
    case 1:
34
      PORTC = 0x00;
35
      break;
36
    default:
37
      if (PORTC = 0xff) 
38
        PORTC = 0x00;
39
      else
40
        PORTC = 0xff;
41
  }
42
  
43
}
44

45

46
int main(void)
47
{
48
  int i;
49

50
  Initialize();
51

52
  while (1)
53
  {
54
    LedSet(0);
55
    for (i=60000;i;i--);
56
    LedSet(0);
57
    for (i=60000;i;i--);
58
  }
59
  return 0;
60
}

Meine Frage ist nun: Wieso leuchtet die LED nicht mehr???
Gast #1895076
Lesenswert?
• ▲
▼
> Meine Frage ist nun: Wieso leuchtet die LED nicht mehr???

Weil Du Ihr gesagt hast, sie soll nicht mehr leuchten.


Oben bei LedSet steht's doch

    /*  state = 0 -> Led Off
     *  state = 1 -> Led On

Mit LedSet(0); schaltet sie also folgerichtig AUS.
Dann wartest Du (       for (i=60000;i;i--);   )
Dann schaltest Du sie AUS  (   LedSet(0);   )

usw.

Das Programm tut genau das was es soll.

Antwort schreiben

Hinweis: der ursprüngliche Beitrag ist mehr als 6 Monate alt. Bitte hier nur auf die ursprüngliche Frage antworten, für neue Fragen einen neuen Beitrag erstellen.

Formatierung
  • [c]C-Code[/c]
  • [code]Code in anderen Sprachen, ASCII-Zeichnungen[/code]
  • [math]Formel in LaTeX-Syntax[/math]
  • [[Titel]] – Link zu einem Artikel
  • Verweis auf einen anderen Beitrag: Rechtsklick auf dessen Beitrags-ID (#8078628), „Adresse kopieren“, in den Text einfügen

Mehr zur Formatierung

Längeren Sourcecode als Anhang, nicht in den Text. JPG nur für Fotos und Scans; Zeichnungen und Screenshots als PNG oder GIF. Siehe Bildformate.

Schon registriert? Anmelden

Wird nicht angezeigt. Wir schicken dir einen Link; sobald du damit bestätigst, erscheint dein Beitrag.

Mit dem Abschicken legst du einen kostenlosen Account an und erkennst die Nutzungsbedingungen und die Datenschutzerklärung an.