AVR Embedded Tutorial - UART/de
From Lazarus wiki
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
Bei betätigen der Leertaste 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
UARTSendString('Hello World !'#13#10);
end;
until 1 = 2;
end.
ATtiny2313
Beim ATtiny2313 habe 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)