AVR Embedded Tutorial - Simple GPIO on and off output/de

From Lazarus wiki
Revision as of 23:59, 3 November 2017 by Billyraybones (talk | contribs)
Jump to navigationJump to search

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, um einen Taster gegen GND anzuschließen. Somit kann man auf einen physikalischen PulUp-Widerstand verzichten.

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

if PINx and (1 shl Pinx) > 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.