Warte Funktion viod pa ()

Gast #836089
Lesenswert?

Hallo,

ich möchte gerne, das der MCU, alle LEDs auf Port B ein schaltet und
und nach einer weil (ca 2sek) wieder alles aus schaltet. Die Funkionen 
alleine
all_on und all_off funktionieren problemlos, nur die Warte funktion pa
wird irrgend wie ignoriert.


Habt ihr einen, kleinen TIP für mich?


// MCU Typ: AT90S4433

#include <avr/io.h>



//_________warte Schleife____________________

void pa()

{
int x;  // legt varieable x fest
x=0; // hat den wert 0

  while (x<3000) // x kleiner 3000
 {
  x++;  // erhöhe um jwals um eins
 }

}

//_________alle LEDs an_______________________
void all_on()

 {

   DDRB  = 0xff;  // Register auf Ausgang
   PORTB = 0x00;  // Alle LED an
 }
//_______________alle LEDs aus_________________
void all_off()
   {

   DDRB  = 0xff;  // Register auf Ausgang
   PORTB = 0xff;  // Alle LED an

   }

//_______Hauptprogramm_________________________
int main (void)

{

  all_on(); // all LEDs an
  pa(); // kleine pause
  all_off();// Alles LEDs aus

  return 0;
}
Gast #836112
Lesenswert?

Schon mal was vom Optimierer gehört? Die Schleife hat effektiv keinen 
Sinn (so siehts der Compiler), also wird sie einfach weggelassen. 
Lösung: Zählvariable als volatile deklarieren.


>void pa() {
>*volatile* int x;  // legt varieable x fest
>x=0; // hat den wert 0
>
>  while (x<3000) // x kleiner 3000
> {
>  x++;  // erhöhe um jwals um eins
> }
>
>}
Gast #836198
Lesenswert?

Hallo,

Ich takte den MCU mit 4 MHz,
ich habe die Funktion einseln getestest, Funkion all_on, alle LEDs gehen 
an, Funktion all_off, dann gehen die alles aus , wenn ich pa() 
dazwischen
setze, wird die Funkion all_off sofort erreicht. DDRB, werde ich 
demnächst beachten.

In der tat, funktioniert das mit " volatile int x;" super

Aber, warum Leuchtten die LEDs dauen, wenn ich die Zahl größe 32000 
nehme,
erzeuge ich dann ein überlauf?
Gast #836217
Lesenswert?

So danke erstmal an Euch allen,
so Funtionerts !!!

#include <avr/io.h>

//_________warte Schleife____________________

void pa()

{
volatile int x;  // legt varieable x fest
x=0; // hat den wert 0

  while (x< 30000) // x kleiner 3000
 {
  x++;  // erhöhe um jwals um eins
 }

}

//_________alle LEDs an_______________________
void all_on()

 {
   DDRB  = 0xff;  // Register auf Ausgang
   PORTB = 0x00;  // Alle LED an
 }
//_______________alle LEDs aus_________________
void all_off()
   {
    DDRB  = 0xff;  // Register auf Ausgang
    PORTB = 0xff;  // Alle LED an
   }

//_______Hauptprogramm_________________________
int main (void)

{

  all_on(); // all LEDs an
  pa(); // kleine pause
  pa();
  pa();

  all_off();// Alles LEDs aus

  return 0;
}
Gast #843407
Lesenswert?

Hallo danke, für Eure Hilfe,
Ich bin jetzt schon ein ganses Stück weider gekommen.

:-) Danke Tobias  :-)



#include <avr/io.h>
#include <avr/delay.h>
#define F_CPU 8000000UL  // 8 MHz

//__________timer_______________

void delay_ms(int ms)
{
  int t;
  for(t=0; t<=ms; t++)
  _delay_ms(1);
}


//_________warte Schleife____________________

void pa()

{
volatile int b;    // legt varieable b fest
b=0;               // b hat den Wert 0

  while (b<30000)  // x kleiner 3000
 {
  b++;             // erhöhe um jwals um eins
 }

}

//_________alle LEDs an_______________________
void all_on()

 {

   DDRB  = 0xff;  // Register auf Ausgang
   PORTB = 0x00;  // Alle LED an
 }
//_______________alle LEDs aus_________________
void all_off()
   {

   DDRB  = 0xff;  // Register auf Ausgang
   PORTB = 0xff;  // Alle LED aus

   }

//_______Hauptprogramm_________________________
int main (void)

{
  int a;
  all_on(); // alle LEDs an
  pa();     // Mit warte Funktion
  pa();     // Mit warte Funktion
  all_off();// Alles LEDs aus
  pa();     // Mit warte Funktion
  pa();     // Mit warte Funktion


a=0;         // a hat den Wert 0

while (a<5)          // 5 x Blingen

  {
   all_on();         // alle LEDs an
   delay_ms(4000);   // Timer Funktion
   all_off();        // Alles LEDs aus
   delay_ms(4000);   // Timer Funktion
   a++;
  }

  return 0;
}

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