Forum: Compiler & IDEs Warte Funktion viod pa ()


von tobiasth (Gast)


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;
}

von M. K. (kichi)


Lesenswert?

Wie schnell taktet der Controller und woher weißt du, dass die 
Warte-Funktion ignoriert wird?

Du musst DDRB nicht jedesmal neu setzen - einmal zu Beginn reicht völlig 
aus.

von Sven P. (Gast)


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

von holger (Gast)


Lesenswert?

>*volatile* int x;  // legt varieable x fest

Bitte nicht nachmachen ;)

volatile int x;  // legt varieable x fest

Sieht doch besser aus.

von tobiasth (Gast)


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?

von holger (Gast)


Lesenswert?

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

Ja, ab 32768.
Nimm halt ein ein "unsigned int". Das geht bis 65535.

von tobiasth (Gast)


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;
}

von Jörg W. (dl8dtl) (Moderator) Benutzerseite


Lesenswert?

OK, und jetzt guckst du dir bitte die Doku zu <util/delay.h> noch an:

http://www.nongnu.org/avr-libc/user-manual/group__util__delay.html

Danach nimmst du dann einen Timer, und dein Prozessor ist wieder 99,9 %
der Zeit frei für andere Dinge. ;-)

von tobiasth (Gast)


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;
}

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.