AVR Embedded Tutorial - UART/de

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

Template:Translate

ATmega328p (Arduino uno/nano)

Für die serielle Ausgabe wird ein Terminal mit folgenden Einstellungen gebraucht:

Baud Rate 9600
Bits 8
Stopbits 1
Parity none

Beim Betätigen von [space] im Terminal-Programm, sollte ein "Hello World !" zurück kommen.

program Project1;
const
  CPU_Clock = 16000000; // Taktfrequenz Arduino, default 16MHz.
  Baud      = 9600;     // Baudrate
  Teiler    = CPU_Clock div (16 * Baud) - 1;

  procedure UARTInit;
  begin
    UBRR0 := Teiler;

    UCSR0A := (0 shl U2X0);
    UCSR0B := (1 shl TXEN0) or (1 shl RXEN0);
    UCSR0C := (%011 shl UCSZ0);
  end;

  procedure UARTSendChar(c: char);
  begin
    while UCSR0A and (1 shl UDRE0) = 0 do begin
    end;
    UDR0 := byte(c);
  end;

  function UARTReadChar: char;
  begin
    while UCSR0A and (1 shl RXC0) = 0 do begin
    end;
    Result := char(UDR0);
  end;

  procedure UARTSendString(s: ShortString);
  var
    i: integer;
  begin
    for i := 1 to length(s) do begin
      UARTSendChar(s[i]);
    end;
  end;

begin
  UARTInit;
  repeat
    if UARTReadChar = #32 then begin         // space
      UARTSendString('Hello World !'#13#10); 
    end;
  until 1 = 2;
end.

ATtiny2313

Beim ATtiny2313 haben die Register eine andere Bezeichnung als beim ATmega328p. Meistens entfällt die 0 in der Bezeichnung. UBRR0 muss man in UBRRH und UBRRL zerlegen.

UBRRH := Teiler shr 8;
UBRRL := Byte(Teiler);

UCSRB := (1 shl TXEN) or (1 shl RXEN);
UCSRC := (%011 shl UCSZ);

(Der Code wurde nicht getestet)