AVR Embedded Tutorial - Simple GPIO on and off output/de
Deutsch · English
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.
- Pin auf Ausgabe:
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);- Pin umschalten:
PORTx := PORTx xor (1 shl Pinx);Lässt man DDRx auf LOW, dann wird mittels PORTx ein PullUp-Widerstand aktiviert. Dies wird gebraucht, um einen Taster gegen GND anzuschließen. Somit kann man auf einen externen PullUp-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.
Wechselblinker 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 asm nop 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.Pin Umschalten ATtiny2313
Wen man den Pin umschalten will, egal wie sein Ursprungs zustand ist, kann man die mit einer xor Verknüpfung lösen. Das es auch einen Wechselblinker, wird DP0 zuerst auf HIGH gestellt.
begin
// Pin 0 und 1 von PortD auf Ausgabe.
DDRD := DDRD or (1 shl DP0) or (1 shl DP1);
PORTD := PORTD or (1 shl DP0); // Pin0 ein
repeat
// LED wechseln
PORTD := PORTD xor (1 shl DP0); // Pin0 umschalten
PORTD := PORTD xor (1 shl DP1); // Pin1 umschalten
mysleep(sl); // Pause
until 1 = 2;
end.Vereinfachung Portzugriffe
Wen man auf Ports zugreift und nicht immer die and, or und xor Verknüpfungen schreiben will, kann auch folgende Proceduren/Funktionen verwenden. Die Beispiele zeigen dies für PORTD. Für andere Ports kann man diese sehr einfach anpassen.
pinMode
Entspricht in etwa pinMode(... von Arduino.
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;digitalWrite
Entspricht in etwa digitalWrite(... von Arduino.
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;digitalSwitch
Schaltet den Pin um, gibt es nicht beim Arduino.
procedure WritePortD(Pin: byte);
begin
PORTD := PORTD xor (1 shl Pin);
end;digitalRead
Entspricht in etwa digitalRead(... von Arduino.
function ReadPortD(bit: byte): Boolean;
begin
Result := PIND and (1 shl bit) <> 0;
end;Pin direkt ansprechen
Wen man mit absolute eine bitpacked Array, Record oder Set mit dem Port verknüpft, kann man direkt auf jeden Pin zugreifen, ohne lästigen or und and not Verknüpfungen.
Der kompiliert Code, ist genau gleich gross, wie auf die herkömmliche Methode mit or und and not. Eleganter wird es kaum mehr gehen.
Die folgenden drei Beispiele sprechen die ersten 4 Pins von PORTD an, welche 4 LEDs ansteuern.
Bitpacked Array
var
LEDPort: bitpacked array [0..7] of boolean absolute PORTD;
begin
LEDPort[0] := True;
LEDPort[1] := True;
LEDPort[2] := False;
LEDPort[3] := False;Bitpacked Record
var
LEDPort: bitpacked record
red, green, yellow, blue: boolean;
end absolute PORTD;
begin
LEDPort.red := True;
LEDPort.green := True;
LEDPort.yellow := False;
LEDPort.blue := False;Set
var
LEDPortS: set of (green, yellow, red, blue) absolute PORTD;
begin
LEDPortS := [yellow, red]; // gelb, rot ein
LEDPortS := LEDPortS + [green, blue] - [red]; // grün, blau ein; rot aus
LEDPortS := LEDPortS - [green, blue] + [red]; // grün, blau aus; rot einDDR und PORT
DDRx kann man ähnlich lösen, wie bei PORTx. Dazu am besten die LEDs als type deklarieren.
type
TLed = bitpacked record
green, yellow, red, blue: boolean;
end;
var
LedPORT: TLed absolute PORTD;
LedDDR: TLed absolute DDRD;
begin
LedDDR.green := True;
LedDDR.red := True;
repeat
LedPORT.green := True;
LedPORT.red := False;
...PINx kann man ähnlich lösen.
Arduino Pin PD0 & PD1
Dieser Beschrieb bezieht sich auf den Arduino Uno/Nano, es können auch andere AVRs betroffen sein.
Die Pins PD0 & PD1 von PORTD werden auch für die UART-Schnittstelle gebraucht und sind somit blockiert. Wen man diese Pin trotzdem nutzen will, muss man sie wieder frei geben.
UCSR0B := 0; // UART-Funktionen deaktivieren.Hinweis: Die LEDs RX und TX leuchten negiert, da Anode der LED an Vcc angeschlossen ist.
Genauere Infos über UART hier:
- UART - Serielle Ein und Ausgabe über UART (COM-Port).
Siehe auch
- Übersichtseite AVR Embedded Tutorial
Autor: Mathias
From the Free Pascal wiki, page "AVR Embedded Tutorial - Simple GPIO on and off output/de", as it stood on 2020-01-26 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.
Edited on wiki.freepascal.org by 3 people, 2017–2020
From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.
| When | Who | What changed |
|---|---|---|
| Mathias | Created page | |
| Mathias | Beispiel-Code-ATtiny2313 | |
| Mathias | Erster Blink-Beispiel | |
| Mathias | Direkte Portmanipulation | |
| Mathias | ||
| Mathias | Mathias moved page Einfache Port-Ausgabe/de to Einfache GPIO Ein und Aus-Ausgabe/de (minor) | |
| Billyraybones | (minor) | |
| Mathias | Beispiel ATtiny2313 | |
| Mathias | ||
| Mathias | Mathias moved page Einfache GPIO Ein und Aus-Ausgabe/de to Simple GPIO on and off output (minor) | |
| Mathias | Mathias moved page Simple GPIO on and off output to AVR Embedded Tutorial - Simple GPIO on and off output (minor) | |
| Mathias | Mathias moved page AVR Embedded Tutorial - Simple GPIO on and off output to AVR Embedded Tutorial - Simple GPIO on and off output/de (minor) | |
| Mathias | ||
| Mathias | Blanked the page | |
| Mathias | Undo revision 113656 by Mathias (talk) | |
| Mathias | ||
| Mathias | Undo revision 113658 by Mathias (talk) | |
| Mathias | digitalRead | |
| Mathias | Direkte Portmanipulation | |
| Mathias | Beispiel ATtiny2313 | |
| Mathias | Einfache GPIO Ein und Aus-gabe | |
| Mathias | Vereinfachung Portzugriffe | |
| Mathias | Bit direkt ansprechen | |
| Mathias | Bit direkt ansprechen | |
| Mathias | Bitpacked Record | |
| Mathias | Bit direkt ansprechen | |
| Mathias | Bit direkt ansprechen | |
| Mathias | Bit direkt ansprechen | |
| Mathias | digitalSwitch | |
| Mathias | Pin Umschalten ATtiny2313 | |
| Mathias | Set | |
| Mathias | Siehe auch | |
| Mathias | Direkte Portmanipulation (minor) | |
| Mathias | Wechselblinker ATtiny2313 (minor) | |
| Mathias | Pin Umschalten ATtiny2313 (minor) | |
| Mathias | Vereinfachung Portzugriffe (minor) | |
| Mathias | Pin direkt ansprechen (minor) | |
| Mathias | Siehe auch | |
| Mathias | Arduino Pin 0 & 1 | |
| Mathias | Siehe auch (minor) | |
| Mathias | DDR und PORT | |
| Mathias | Pin direkt ansprechen (minor) | |
| Trev | Fixed template loop; syntax highlighting; modded categories ro German; added language category (minor) | |
| Trev | digitalRead: Changed PORTB to PORTD; PINB to PIND as per other examples (minor) |
Talk about this page
Nobody has opened a bench for this page yet. Sign in to open one.
