AVR Embedded Tutorial - UART/de
From Lazarus wiki
Jump to navigationJump to search
Serielle Ein/Ausgabe ATmega328p (Arduino uno/nano)
Für die Seriele Ausgabe wird ein Terminal mit folgenden Einstellungen gebraucht:
- Baud Rate: 9600
- Bits: 8
- Stopbits: 1
- Parity: none
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
UARTSendString('Hello World !'#13#10);
end;
until 1 = 2;
end.
sowie