Using the UART/de
(→(**COPIED FROM ENGLISH PAGE**) =) |
(→Example) |
||
Line 129: | Line 129: | ||
| | ||
− | = <span class="f_Header"> | + | = <span class="f_Header">Beispiel</span> = |
− | + | Sie können auch das folgende Programm mit dem BASCOM Terminal Emulator ausprobieren welches verschiedene Sende- und Empfangs-Befehle demonstriert. | |
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
Line 143: | Line 143: | ||
<span style="font-family: 'Courier New';"> </span> | <span style="font-family: 'Courier New';"> </span> | ||
− | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Dim</span><span style="font-family: 'Courier New';"> Akey </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">As</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Byte</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0);">' | + | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Dim</span><span style="font-family: 'Courier New';"> Akey </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">As</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Byte</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0);">'Bytevariable deklarieren</span> |
<span style="font-family: 'Courier New';"> </span> | <span style="font-family: 'Courier New';"> </span> | ||
Line 149: | Line 149: | ||
<span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span> | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span> | ||
− | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">" | + | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">"Hallo, geben Sie irgendein alphanumerisches Zeichen ein.."</span> |
− | <span style="font-family: 'Courier New';">Akey </span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">=</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Waitkey</span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">()</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0);">'Waitkey | + | <span style="font-family: 'Courier New';">Akey </span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">=</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Waitkey</span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">()</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0);">'Waitkey wartet bis ein Zeichen von der UART empfangen wird</span> |
<span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> Akey</span> | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> Akey</span> | ||
Line 161: | Line 161: | ||
<span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span> | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span> | ||
− | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">" | + | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">"Danke! Wie Sie sehen gibt der Mikrocontroller eine Zahl aus."</span> |
− | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">" | + | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">"aber nicht das getippte Zeichen."</span> |
<span style="font-family: 'Courier New';"> </span> | <span style="font-family: 'Courier New';"> </span> | ||
Line 171: | Line 171: | ||
<span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span> | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span> | ||
− | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">" | + | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">"Probieren Sie nun die Enter-Taste.."</span> |
<span style="font-family: 'Courier New';">Akey </span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">=</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Waitkey</span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">()</span> | <span style="font-family: 'Courier New';">Akey </span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">=</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Waitkey</span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">()</span> | ||
Line 183: | Line 183: | ||
<span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span> | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span> | ||
− | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">" | + | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">"Die Zahl die Sie sehen ist der ASCII-Wert des eingetippten Zeichens."</span> |
− | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">" | + | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">"Wir müssen die Zahl wieder zurück umwandeln in das eingetippte Zeichen..."</span> |
− | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0);">' | + | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0);">'Beachten Sie die Funktion dieser Zeile..</span> |
− | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">" | + | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">"Geben Sie wieder ein alphanumerisches Zeichen ein..."</span> |
<span style="font-family: 'Courier New';">Akey </span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">=</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Waitkey</span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">()</span> | <span style="font-family: 'Courier New';">Akey </span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">=</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Waitkey</span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">()</span> | ||
− | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Chr</span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">(</span><span style="font-family: 'Courier New';">akey</span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">)</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0);">' | + | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Chr</span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">(</span><span style="font-family: 'Courier New';">akey</span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">)</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0);">'Beachten Sie was hier passiert</span> |
− | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">" | + | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">"Jetzt klappt es!"</span> |
<span style="font-family: 'Courier New';"> </span> | <span style="font-family: 'Courier New';"> </span> | ||
Line 203: | Line 203: | ||
<span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span> | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span> | ||
− | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">" | + | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">"In vielen Situationen reicht ein Tastendruck nicht aus..."</span> |
− | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">" | + | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">"Geben Sie nun Ihren Namen ein und dann Enter zum Bestätigen"</span> |
<span style="font-family: 'Courier New';"> </span> | <span style="font-family: 'Courier New';"> </span> | ||
− | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Dim</span><span style="font-family: 'Courier New';"> Inputstring </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">As</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">String</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">*</span><span style="font-family: 'Courier New';"> 12 </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0);">' | + | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Dim</span><span style="font-family: 'Courier New';"> Inputstring </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">As</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">String</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">*</span><span style="font-family: 'Courier New';"> 12 </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0);">'Zeichenkettenvariable deklarieren</span> |
<span style="font-family: 'Courier New';"> </span> | <span style="font-family: 'Courier New';"> </span> | ||
Line 219: | Line 219: | ||
<span style="font-family: 'Courier New';">Akey </span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">=</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Waitkey</span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">()</span> | <span style="font-family: 'Courier New';">Akey </span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">=</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Waitkey</span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">()</span> | ||
− | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">If</span><span style="font-family: 'Courier New';"> Akey </span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">=</span><span style="font-family: 'Courier New';"> 13 </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Then</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Goto</span><span style="font-family: 'Courier New';"> Thanks </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0);">' | + | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">If</span><span style="font-family: 'Courier New';"> Akey </span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">=</span><span style="font-family: 'Courier New';"> 13 </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Then</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Goto</span><span style="font-family: 'Courier New';"> Thanks </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0);">'Bei Enter zu thanks springen</span> |
− | <span style="font-family: 'Courier New';"> Inputstring </span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">=</span><span style="font-family: 'Courier New';"> Inputstring </span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">+</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Chr</span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">(</span><span style="font-family: 'Courier New';">akey</span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">)</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0);">' | + | <span style="font-family: 'Courier New';"> Inputstring </span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">=</span><span style="font-family: 'Courier New';"> Inputstring </span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">+</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Chr</span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">(</span><span style="font-family: 'Courier New';">akey</span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">)</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0);">'Zeichen an Zeichenkette anhängen/span> |
<span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Loop</span> | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Loop</span> | ||
Line 229: | Line 229: | ||
<span style="font-family: 'Courier New';">Thanks</span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">:</span> | <span style="font-family: 'Courier New';">Thanks</span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">:</span> | ||
− | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">" | + | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">"Vielen Dank"</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">;</span><span style="font-family: 'Courier New';"> Inputstring </span><span style="font-family: 'Courier New'; color: rgb(255, 0, 0);">;</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">" !"</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0);">'Beachten Sie die Funktion des Semikolons ; für mehrere Parameter</span> |
<span style="font-family: 'Courier New';"> </span> | <span style="font-family: 'Courier New';"> </span> | ||
Line 237: | Line 237: | ||
<span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span> | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span> | ||
− | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">" | + | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">"Sehen Sie sich den Programm-Code an und versuchen Sie zu verstehen,"</span> |
− | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">" | + | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">"wie das Programm arbeitet. Drücken Sie auch F1 während die Schreibmarke auf einem Befehl steht."</span> |
<span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span> | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span> | ||
− | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">" | + | <span style="font-family: 'Courier New'; font-weight: bold; color: rgb(0, 0, 128);">Print</span><span style="font-family: 'Courier New';"> </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 128);">"Wenn Sie alles verstanden haben dann gehen Sie weiter zum nächsten Beispielprogramm."</span> |
<span style="font-family: 'Courier New';"> </span> | <span style="font-family: 'Courier New';"> </span> |
Revision as of 00:34, 26 February 2013
Contents |
UART
Ein Universal Asynchronous Receiver and Transmitter (UART) kann benutzt werden um Daten zwischen zwei Geräten zu senden und zu empfangen. Diese zwei Geräte können sein: PC-zu-PC, PC-zu-Mikrocontroller oder Mikrocontroller-zu-Mikrocontroller. Die UART Kommunikation benutzt TTL Spannungen von +5V and 0V oder LVTTL abhängig von der Mikrocontroller Betriebsspannung.
Wenn eine Verbindung zum PC gewünscht wird dann muss das RS232-Protokoll verwendet werden. Das bedeutet, dass mit definierten Spannungen (+15V und -15V) kommuniziert werden muss. Die kann mit einem MAX232 Level-Shifter erreicht werden.
Die Hardware wird im folgenden Schaltplan dargestellt:
Der DB-9 Anschluss hat 9 Pins aber es müsse nur 3 davon benutzt werden. Die obige Grafik zeigt die Vorderansicht - gelötet wird auf der anderen Seite. Bei den meisten Anschlüssen sind die Pinbezeichnungen auch auf den Steckern und Buchsen selbst zu lesen.
Wenn der verwendete Mikrocontroller HW-UART nicht unterstützt dann kann auch Software-UART benutzt werden. Siehe unten. Wenn der Mikrocontroller einen HW-UART unterstützt dann werden die TxD und RxD Pins mit TxD und RxD im Schaltplan verbunden. Wenn der Mikrocontroller mehrere HW-UARTs hat dann werden die TxD0 und RxD0 Pins mit TxD und RxD entsprechend obigem Schaltplan verbunden.
Nun muss per Programm der Mikrocontroller initialisiert werden. Öffnen Sie dazu eine neue .bas Datei und fügen Sie die folgenden Programmzeilen am Anfang hinzu.
$regfile = "verwendeter_mikrocontroller def.dat"
$crystal = 8000000
$baud = 19200
Stellen Sie sicher, dass der benutzte Mikrocontroller mit $regfile angegeben wird - zum Beispiel der ATMega32
$regfile = "m32def.dat"
Einige Mikrocontroller können einen internen Oszillator als Taktquelle verwenden. Einige Mikrocontroller verwenden den internen Oszillator standardmäßig. Bei Benutzung eines internen Oszillators braucht man keinen (externen) Quarz.
Führen Sie diesen Schritt nur aus wenn Sie einen internen Oszillator verwenden.
Öffnen Sie den BASCOM-AVR Programmer wie folgt:
• | Wählen Sie den “Lock and Fuse Bits” Tab aus und and maximimieren Sie das Programmer-Fenster. |
• | Prüfen Sie, ob Sie den folgenden “Fusebit” Abschnitt sehen: |
"1:Divide Clock by 8 Disabled"
und
"Int. RC Osc. 8 MHz; Start-up time: X CK + X ms; [CKSEL=XXXX SUT=XX]"
Diese Einstellungen gibt es nicht bei jedem AVR Mikrocontroller. Wenn Sie die Einträge bei sich nicht haben dann ändern Sie nicht irgendwelche Fuse Bits.
Wenn diese Einstellungen verfügbar sind aber mit anderen Werten dann ändern Sie die Werte durch die Drop Down Möglichkeiten. Schließlich klicken Sie den "Program FS" Knopf und dann "Refresh" um die tatsächlichen Einstellungen zu sehen.
Verbinden Sie nun Mikrocontroller und PC mit einem Kabel mit DB-9 Anschluss
Schreiben Sie ein Testprogramm für Ihren Mikrocontroller welches folgendermaßen aussehen sollte:
$regfile = "m32def.dat" 'Verwendeter Mikrocontroller
$crystal = 8000000
$baud = 19200
Do
Print "Hallo Welt"
Waitms 25
Loop
End
Öffnen Sie nun Das BASCOM Terminal-Programm und stellen Sie die Verbindungseinstellungen unter “Terminal” -> “Settings” ein. Wählen Sie den Com-Port wie am PC und stellen Sie eine Baudrate ein von 19200, Parity none, Data bits 8, Stop bits 1, Handshake none, emulation none.
Wenn Sie das "Hallo Welt" im BASCOM Terminal Emulator Fenster angezeigt bekommen dann ist alles in Ordnung.
Beispiel
Sie können auch das folgende Programm mit dem BASCOM Terminal Emulator ausprobieren welches verschiedene Sende- und Empfangs-Befehle demonstriert.
$regfile = "m88def.dat"
$crystal = 8000000
$baud = 19200
Dim Akey As Byte 'Bytevariable deklarieren
Print "Hallo, geben Sie irgendein alphanumerisches Zeichen ein.."
Akey = Waitkey() 'Waitkey wartet bis ein Zeichen von der UART empfangen wird
Print Akey
Wait 1
Print "Danke! Wie Sie sehen gibt der Mikrocontroller eine Zahl aus."
Print "aber nicht das getippte Zeichen."
Wait 1
Print "Probieren Sie nun die Enter-Taste.."
Akey = Waitkey()
Akey = Waitkey()
Print Akey
Print "Die Zahl die Sie sehen ist der ASCII-Wert des eingetippten Zeichens."
Print "Wir müssen die Zahl wieder zurück umwandeln in das eingetippte Zeichen..."
Print 'Beachten Sie die Funktion dieser Zeile..
Print "Geben Sie wieder ein alphanumerisches Zeichen ein..."
Akey = Waitkey()
Print Chr(akey) 'Beachten Sie was hier passiert
Print "Jetzt klappt es!"
Wait 1
Print "In vielen Situationen reicht ein Tastendruck nicht aus..."
Print "Geben Sie nun Ihren Namen ein und dann Enter zum Bestätigen"
Dim Inputstring As String * 12 'Zeichenkettenvariable deklarieren
Do
Akey = Waitkey()
If Akey = 13 Then Goto Thanks 'Bei Enter zu thanks springen
Inputstring = Inputstring + Chr(akey) 'Zeichen an Zeichenkette anhängen/span>
Loop
Thanks:
Print "Vielen Dank" ; Inputstring ; " !" 'Beachten Sie die Funktion des Semikolons ; für mehrere Parameter
Wait 1
Print "Sehen Sie sich den Programm-Code an und versuchen Sie zu verstehen,"
Print "wie das Programm arbeitet. Drücken Sie auch F1 während die Schreibmarke auf einem Befehl steht."
Print "Wenn Sie alles verstanden haben dann gehen Sie weiter zum nächsten Beispielprogramm."
End
ASCII
As you could have seen in the previous example we use the PRINT statement to send something to the UART. Actually we do not send just text. We send ASCII characters. ASCII means American Standard Code for Information Interchange. Basically ASCII is a list of 127 characters.
ASCII Table (Incomplete)
Decimal Hex Binary Value
------- --- ------ -----
000 000 00000000 NUL (Null char.)
008 008 00001000 BS (Backspace)
009 009 00001001 HT (Horizontal Tab)
010 00A 00001010 LF (Line Feed)
012 00C 00001100 FF (Form Feed)
013 00D 00001101 CR (Carriage Return)
048 030 00110000 0
049 031 00110001 1
052 034 00110100 4
065 041 01000001 A
066 042 01000010 B
067 043 01000011 C
You can find a complete ASCII table here
CARRIAGE RETURN (CR) AND LINE FEED (LF)
In the previous example you can also see that a second print statement always prints the printed text to the following line. This is caused by the fact that the print statement always adds the CR and LF characters.
Basically if we state:
Print “ABC”
We send 65 66 67 13 10 to the UART. (In binary format)
The carriage return character (13) returns the cursor back to column position 0 of the current line. The line feed (10) moves the cursor to the next line.
Print “ABC” ;
When we type a semicolon ( ; ) at the end of the line...
Bascom does not send a carriage return/line feed, so you can print another text after the ABC on the same line.
Print “ABC” ; Chr(13) ;
This would send only ABC CR. The next print would overwrite the ABC.
OVERVIEW
Here are some other commands that you can use for UART communications:
Waitkey()
Waitkey will until a character is received in the serial buffer.
Ischarwaiting()
Returns 1 when a character is waiting in the hardware UART buffer.
Inkey()
Inkey returns the ASCII value of the first character in the serial input buffer.
Sends a variable or non-variable string to the UART
ANOTHER EXAMPLE
This example shows how to use Ischarwaiting to test if there is a key pressed. And if there is, read to a variable.
'Print "Press B key to start"
Dim Serialcharwaiting As Byte, Serialchar As Byte
Serialcharwaiting = Ischarwaiting() 'Check if B or b pressed then goto
If Serialcharwaiting = 1 Then
Serialchar = Inkey()
If Serialchar = 66 Or Serialchar = 98 Then
Goto MyRoutine
End If
End If
Goto Main
Myroutine:
'Statements
Main:
'Statements
End
BUFFERING SERIAL DATA
If you wish to send and receive data at high speed, you need to use serial input and serial output buffers. This buffering is implemented in BASCOM-AVR and can only be used for hardware UART’s.
To configure a UART to use buffers, you need to use the Config statement.
Config Serialout = Buffered , Size = 20
and/or
Config Serialin = Buffered , Size = 20
More information can be found in BASCOM-Help. Search topic = "config serialin". There is also a sample program “RS232BUFFER.BAS” in the samples folder if you wish a demonstration of the buffering.
SOFTWARE UART
The previous examples used the hardware UART. That means the compiler uses the internal UART registers and internal hardware (RxD(0) and TxD(0)) of the AVR. If you don’t have a hardware UART you can also use a software UART.
The Bascom compiler makes it easy to “create” additional UART’s. Bascom creates software UART’s on virtually every port pin.
Remember that a software UART is not as robust as a hardware UART, thus you can get timing problems if you have lots of interrupts in your program.
For this example we use micro controller pins portc.1 and portc.2.
Connect portc.1 to TxD and portc.2 to RxD see the schematic above.
Change the $regfile and program this example:
$regfile = "m88def.dat"
$crystal = 8000000
$baud = 19200
Dim B As Byte
Waitms 100
'Open a TRANSMIT channel for output
Open "comc.1:19200,8,n,1" For Output As #1
Print #1 , "serial output"
'Now open a RECEIVE channel for input
Open "comc.2:19200,8,n,1" For Input As #2
'Since there is no relation between the input and output pin
'there is NO ECHO while keys are typed
Print #1 , "Press any alpha numerical key"
'With INKEY() we can check if there is data available
'To use it with the software UART you must provide the channel
Do
'Store in byte
B = Inkey(#2)
'When the value > 0 we got something
If B > 0 Then
Print #1 , Chr(b) 'Print the character
End If
Loop
Close #2 'Close the channels
Close #1
End
After you have programmed the controller and you connected the serial cable, open the terminal emulator by clicking on in Bascom.
You should see the program asking for an alphanumerical input, and it should print the input back to the terminal.
Languages | English • Deutsch |
---|