Difference between revisions of "AVR Embedded Tutorial - Simple GPIO on and off output/de"

From Lazarus wiki
Jump to navigationJump to search
Line 1: Line 1:
 
{{Translate}}
 
{{Translate}}
  
==Erster Blink-Beispiel==
+
==Direkte Portmanipulation==
Dieses Beispiel zeigt, wie man mit einem ATtiy2313 programmiert.
+
Bevor man einen GPIO-Pin auf '''HIGH''' schalten kann, muss man diesen als Ausgang konfigurieren. Dies geht über DDRx.
 +
<syntaxhighlight lang="pascal">DDRx := DDRx  or (1 shl Pinx);</syntaxhighlight>
 +
 
 +
Pin auf HIGH:
 +
<syntaxhighlight lang="pascal">PORTx := PORTx or (1 shl Pinx);</syntaxhighlight>
 +
Pin auf LOW:
 +
<syntaxhighlight lang="pascal">PORTx := PORTx and not (1 shl Pinx);</syntaxhighlight>
 +
 
 +
Lässt man DDRx auf LOW, dann wird mittels PORTx ein PulUp-Widerstand aktiviert. Dies wird gebraucht, wen man einen Taster, welcher gegen GND angeschlossen wird. Somit kann man auf einen physikalischen PulUp-Widerstand verzichten.
 +
 
 +
Den Status des GPIO-Pins kann man mittels PINx abfragen.
 +
<syntaxhighlight lang="pascal">if PINB and (1 shl Pin) > 0 then Pin_ist_HIGH;</syntaxhighlight>
 +
 
 +
 
 +
==Blink-Beispiel==
 +
Dieses Beispiel zeigt, wie man mit einem ATtiy2313 ein GPIO anspricht.
 
Dazu werden 2 LEDs an Pin 0 und 1 von PortD angeschlossen.
 
Dazu werden 2 LEDs an Pin 0 und 1 von PortD angeschlossen.
  

Revision as of 21:39, 3 November 2017

Template:Translate

Direkte Portmanipulation

Bevor man einen GPIO-Pin auf HIGH schalten kann, muss man diesen als Ausgang konfigurieren. Dies geht über DDRx.

DDRx := DDRx  or (1 shl Pinx);

Pin auf HIGH:

PORTx := PORTx or (1 shl Pinx);

Pin auf LOW:

PORTx := PORTx and not (1 shl Pinx);

Lässt man DDRx auf LOW, dann wird mittels PORTx ein PulUp-Widerstand aktiviert. Dies wird gebraucht, wen man einen Taster, welcher gegen GND angeschlossen wird. Somit kann man auf einen physikalischen PulUp-Widerstand verzichten.

Den Status des GPIO-Pins kann man mittels PINx abfragen.

if PINB and (1 shl Pin) > 0 then Pin_ist_HIGH;


Blink-Beispiel

Dieses Beispiel zeigt, wie man mit einem ATtiy2313 ein GPIO anspricht. Dazu werden 2 LEDs an Pin 0 und 1 von PortD angeschlossen.

Beispiel ATtiny2313

program Project1;
const
  DP0 = 0;      // Pin0 PortD
  DP1 = 1;      // Pin1 PortD
  sl = 150000;  // Verzögerung

  procedure mysleep(t: int32); // Ein einfaches Delay.
  var
    i: Int32;
  begin
    for i := 0 to t do begin
      asm
               NOP;
      end;
    end;
  end;

begin
  // Pin 0 und 1 von PortD auf Ausgabe.
  DDRD := DDRD  or (1 shl DP0) or (1 shl DP1);

  repeat
    // LED wechseln
    PORTD := PORTD or (1 shl DP0);      // Pin0 ein
    PORTD := PORTD and not (1 shl DP1); // Pin1 aus
    mysleep(sl);                        // Pause

    // LED wechseln
    PORTD := PORTD or (1 shl DP1);      // Pin1 ein
    PORTD := PORTD and not (1 shl DP0); // Pin0 aus
    mysleep(sl);                        // Pause
  until 1 = 2;
end.

sowie