Forum: Mikrocontroller und Digitale Elektronik C Verständnisfrage


von Der Lars (Gast)


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???

von Thomas K. (tomthegeek)


Lesenswert?

Wenn ich das richtig sehe ist die Kathode der LED an deinem Pin 
angeschlossen. Du schaltest den Pin aber auf High -> Kein 
Spannungsabfall an der LED -> leuchtet nicht. Du musst den Pin auf Low 
setzten.

von NurEinGast (Gast)


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.

von Der Lars (Gast)


Lesenswert?

Taadaaaa die LED leuchtet :-) Recht herzlichen Dank.

von NurEinGast (Gast)


Lesenswert?

Glückwunsch !!

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.