|
|
Line 1: |
Line 1: |
− | {{Translate}}
| |
| | | |
− | =Einfache GPIO Ein und Aus-gabe=
| |
− |
| |
− | ==Direkte Portmanipulation==
| |
− | 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, 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.
| |
− | <syntaxhighlight lang="pascal">if PINx and (1 shl Pinx) > 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.
| |
− |
| |
− | ==Beispiel ATtiny2313==
| |
− | <syntaxhighlight lang="pascal">
| |
− | 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.
| |
− | </syntaxhighlight>
| |
− |
| |
− | ==Vereinfachung Portzugriffe==
| |
− | Wen man auf Ports zugreift und nicht immer die '''and''' und '''or''' Verknüpfungen schrieb will, kann auch folgende Proceduren/Funktionen verwenden. Für andere Ports kann man diese sehr einfach anpassen.
| |
− | ===pinMode===
| |
− | Entspricht in etwa '''pinMode(...''' von Arduino.
| |
− | <syntaxhighlight lang="pascal">
| |
− | procedure ModePortD(Pin: byte; Value: Boolean);
| |
− | begin
| |
− | if Value then begin
| |
− | DDRD := DDRD or (1 shl Pin);
| |
− | end else begin
| |
− | DDRD := DDRD and not (1 shl Pin);
| |
− | end;
| |
− | end;
| |
− | </syntaxhighlight>
| |
− |
| |
− | ===digitalWrite===
| |
− | Entspricht in etwa '''digitalWrite(...''' von Arduino.
| |
− | <syntaxhighlight lang="pascal">
| |
− | procedure WritePortD(Pin: byte; Value: Boolean);
| |
− | begin
| |
− | if Value then begin
| |
− | PORTD := PORTD or (1 shl Pin);
| |
− | end else begin
| |
− | PORTD := PORTD and not (1 shl Pin);
| |
− | end;
| |
− | end;
| |
− | </syntaxhighlight>
| |
− |
| |
− | ===digitalRead===
| |
− | Entspricht in etwa '''digitalRead(...''' von Arduino.
| |
− | <syntaxhighlight lang="pascal">
| |
− | function ReadPortB(bit: byte): Boolean;
| |
− | begin
| |
− | Result := PINB and (1 shl bit) <> 0;
| |
− | end;
| |
− | </syntaxhighlight>
| |
− |
| |
− | [[Category:AVR]] {{AutoCategory}}
| |