Difference between revisions of "AVR Embedded Tutorial - Random/de"

From Lazarus wiki
Jump to navigationJump to search
Line 71: Line 71:
 
Der Trick ist, es werden nicht alle Würfel bei jedem Timeraufruf inkrementiert. Der zweite Würfel nur bei jedem zweiten Aufruf, der Dritte nur beim dritten Aufruf, etc.
 
Der Trick ist, es werden nicht alle Würfel bei jedem Timeraufruf inkrementiert. Der zweite Würfel nur bei jedem zweiten Aufruf, der Dritte nur beim dritten Aufruf, etc.
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
 +
// Globale Variablen.
 
const
 
const
 
   anzWuerfel = 15;
 
   anzWuerfel = 15;
Line 77: Line 78:
  
 
   procedure Timer0_Interrupt; public Name 'TIMER0_OVF_ISR'; interrupt;
 
   procedure Timer0_Interrupt; public Name 'TIMER0_OVF_ISR'; interrupt;
 +
 +
  // Lokale Variblen.
 
   const
 
   const
 
     RandomCount = 6; // Anzahl Würfelaugen
 
     RandomCount = 6; // Anzahl Würfelaugen
Line 82: Line 85:
 
     i: integer;
 
     i: integer;
 
     rnd: array [0..anzWuerfel - 1] of byte;
 
     rnd: array [0..anzWuerfel - 1] of byte;
 +
 
   begin
 
   begin
 
+
     for i := 0 to anzWuerfel - 1 do begin
     for i := 0 to anzWuerfel - 1 do
 
    begin
 
 
       Inc(rnd[i]);
 
       Inc(rnd[i]);
       if rnd[i] > i then
+
       if rnd[i] > i then begin
      begin
 
 
         rnd[i] := 0;
 
         rnd[i] := 0;
 
         Inc(Random[i]);
 
         Inc(Random[i]);
         if Random[i] >= RandomCount then
+
         if Random[i] >= RandomCount then begin
 
           Random[i] := 0;
 
           Random[i] := 0;
 +
        end; 
 
       end;
 
       end;
 
     end;
 
     end;
Line 103: Line 105:
 
begin
 
begin
 
   // Timer 0
 
   // Timer 0
   TCCR0A := %00;               // Normaler Timer
+
   TCCR0A := %00;           // Normaler Timer
   TCCR0B := %011;             // Clock = CPU / 16.
+
   TCCR0B := %011;         // Clock = CPU / 16.
   TIMSK0 := (1 shl TOIE0);     // Enable Timer0 Interrupt.
+
   TIMSK0 := (1 shl TOIE0); // Enable Timer0 Interrupt.
  
 
   // UART
 
   // UART
Line 116: Line 118:
 
   repeat
 
   repeat
 
     UARTReadChar;
 
     UARTReadChar;
     for i := 0 to anzWuerfel - 1 do
+
     for i := 0 to anzWuerfel - 1 do begin
    begin
+
       Str(Random[i] + 1: 4, s);   // Jeder Würfel hat eine eigene Zufallszahl.
       Str(Random[i] + 1: 4, s); // Jeder Würfel hat eine eigene Zufallszahl.
 
 
       UARTSendString(s);
 
       UARTSendString(s);
 
     end;
 
     end;

Revision as of 17:53, 27 December 2017

Template:Translate

Zufallsgenerator

Vorwort

Ein Zufallsgenerator ist auf einem AVR gar nicht so einfach. Ein Random(...) aus der Unit System geht leider nicht. Dieses Tutorial zeigt, wie man wenigstens einen Würfel auf einem AVR programmieren kann.

Der Bereich der Zufallszahl muss von Anfang an bekannt sein, bei einem klassischen Würfel ist dies 6.

Für die Kommunikation über UART siehe hier:

Ein einfacher Würfel

var
  Random: byte; // Gibt die Zufallszahl zurück.

  procedure Timer0_Interrupt; public Name 'TIMER0_OVF_ISR'; interrupt;
  const
    RandomCount = 6; // Anzahl Würfelaugen
  begin
    Inc(Random);
    if Random >= RandomCount then
      Random := 0;
  end;

var
  s: ShortString;

begin
  // Timer 0
  TCCR0A := %00;               // Normaler Timer
  TCCR0B := %001;              // Clock = CPU
  TIMSK0 := (1 shl TOIE0);     // Enable Timer0 Interrupt.
  
  // UART
  UARTInit;                    // UART inizialisieren.

  // Interrupts einschalten.
  asm sei end;                 

  // Hauptschleife
  repeat
    UARTReadChar;              // Simuliert einen Tastendruck. 
    Str(Random + 1: 4, s);     // Die Zufallszahl abholen.
    UARTSendString(s);         // Zufallszahl ausgeben. 
  until 1 = 2;

end.

Mehrere Würfel

Schlechte Lösung

Man könnte auf den Gedanken kommen und bei mehreren Würfel folgendes zu machen:

    Str(Random + 1: 4, s);     // Die Zufallszahl abholen.
    UARTSendString(s);         // Zufallszahl ausgeben. 
    Str(Random + 1: 4, s);     // Ist abhängig vom ersten Wurf.
    ...

Nur bekommt man das Problem, das die Reihenfolge der Würfe voraussehbar ist. Daher ist die unbrauchbar.

Ideale Lösung

Aus diesem Grund habe ich folgenden Code, da kommt es unvorhersehbar.

Einziges Manko, es ist etwas komplizierter, dafür ist jede Zufallszahl unabhängig von den anderen. Bei diesem Beispiel sind es 15 Würfel. Der Timer musste noch etwas langsamer gestellt werden, als bei dem Beispiel mit nur einem Würfel.

Der Trick ist, es werden nicht alle Würfel bei jedem Timeraufruf inkrementiert. Der zweite Würfel nur bei jedem zweiten Aufruf, der Dritte nur beim dritten Aufruf, etc.

// Globale Variablen.
const
  anzWuerfel = 15;
var
  Random: array[0..anzWuerfel - 1] of byte;

  procedure Timer0_Interrupt; public Name 'TIMER0_OVF_ISR'; interrupt;

  // Lokale Variblen.
  const
    RandomCount = 6; // Anzahl Würfelaugen
  var
    i: integer;
    rnd: array [0..anzWuerfel - 1] of byte;

  begin
    for i := 0 to anzWuerfel - 1 do begin
      Inc(rnd[i]);
      if rnd[i] > i then begin
        rnd[i] := 0;
        Inc(Random[i]);
        if Random[i] >= RandomCount then begin
          Random[i] := 0;
        end;   
      end;
    end;
  end;

var
  s: ShortString;
  i: integer;

begin
  // Timer 0
  TCCR0A := %00;           // Normaler Timer
  TCCR0B := %011;          // Clock = CPU / 16.
  TIMSK0 := (1 shl TOIE0); // Enable Timer0 Interrupt.

  // UART
  UARTInit;

  // Interrupts einschalten.
  asm SEI end;              

  // Hauptschleife
  repeat
    UARTReadChar;
    for i := 0 to anzWuerfel - 1 do begin
      Str(Random[i] + 1: 4, s);   // Jeder Würfel hat eine eigene Zufallszahl.
      UARTSendString(s);
    end;
    UARTSendString(#13#10);
  until 1 = 2;
end.

Siehe auch