7.3. Communications serielle Probenahme (Polling)
Beispiel für die serielle Kommunikation mit der Sampling-Methode (Polling):
/************************************************* *********
Beispiel für ein Programm für die Datenübertragung von
RS-232 Schnittstelle, Betriebssystem Probenahme (Polling).
Für Keil C51 C-Compiler und Mikrocontroller
8051.
Beschreibung: Ein Programm, das da ein Zeichen a, b, c.. in
Groß-oder Kleinschreibung auf 0 oder 1 Bit gesetzt
P1-Anschluss.
Autor: Rafael Aranda
Datum: 11/20/2009
Version: 1.0
************************************************** ********/
# Include <reg52.h> / / Fügen Sie die generischen Domains von 8052
/ / Setze die Position P1 Bit Mikrocontroller 8051
SBIT Led_0 = P1 ^ 0; / / Zuweisen der Symbol Led_0 Bit P1.0
SBIT LED_1 = P1 ^ 1; / / Zuweisen der Symbol LED_1 Bit P1.1
SBIT LED_2 = P1 ^ 2 / / Zuweisen der Symbol LED_2 Bit P1.2
SBIT Led_3 = P1 ^ 3; / / Zuweisen der Symbol Led_3 Bit P1.3
SBIT Led_4 = P1 ^ 4; / / Zuweisen der Symbol Led_4 Bit P1.4
SBIT Led_5 = P1 ^ 5; / / Zuweisen der Symbol Led_5 Bit P1.5
SBIT Led_6 = P1 ^ 6; / / Zuweisen der Symbol Led_6 Bit P1.6
SBIT Led_7 = P1 ^ 7; / / Zuweisen der Symbol Led_7 Bit P1.7
/ / Deklaration des Prototyps Funktionen
InitSerial void (void) / / Start des seriellen Ports
RecDat void (void) / / Funktion Daten empfangen
/ / Global Variablendeklaration
//------------------------------------------------ ----------
/ / Hauptprogramm
//------------------------------------------------ ----------
void main (void) {
/ / Force ersten Vorbereitung
P1 = 0, / / Setze alle Bits von P1 auf 0
InitSerial (); / / Begin Kommunikation
/ / Der Mann, der das Programm in einer Endlosschleife
while (1) {
/ / Zeigt die RI bis zur Ankunft eines neuen Byte
if (RI) {
RecDat ();
}
}
}
//------------------------------------------------ ----------
/ / Funktion Daten empfangen
//------------------------------------------------ ----------
RecDat void (void) {
unsigned char dat;
RI = 0, / / Lösche erhalten Flagge
dat = SBUF; / / Hole den Byte Empfangspuffer
SBUF = dat / / Echo der empfangenen Daten
/ / Analyse der empfangenen Daten und je nach Art
/ / Setze auf 0 oder 1 das entsprechende Bit
Schalter (dat) {
/ / Zero Port 1 Bit
Fall 'a':
Led_0 = 0, / / Bit P1.0 auf 0
break;
Fall 'b':
LED_1 = 0, / / Bit P1.1 auf 0
break;
case 'c':
LED_2 = 0, / / Bit P1.2 auf 0
break;
case 'd':
Led_3 = 0, / / Bit P1.3 auf 0
break;
Bei "e":
Led_4 = 0, / / Bit P1.4 auf 0
break;
case 'f':
Led_5 = 0, / / Bit P1.5 auf 0
break;
Fall 'g':
Led_6 = 0, / / Bit P1.6 auf 0
break;
Fall 'h':
Led_7 = 0, / / Bit P1.7 auf 0
break;
/ / Stellen Sie einen Port 1 Bit
Bei "A":
Led_0 = 1, / / Bit P1.0 auf 1
break;
Fall 'B':
LED_1 = 1, / / Bit P1.1 auf 1
break;
Fall 'C':
LED_2 = 1, / / Bit P1.2 auf 1
break;
Bei "D":
Led_3 = 1, / / Bit P1.3 auf 1
break;
Bei 'E':
Led_4 = 1, / / Bit P1.4 auf 1
break;
Fall "F":
Led_5 = 1, / / Bit P1.5 auf 1
break;
Bei "G":
Led_6 = 1, / / Bit P1.6 auf 1
break;
Bei "H":
Led_7 = 1, / / Bit P1.7 auf 1
break;
}
}
//------------------------------------------------ ----------
/ / Start des seriellen Ports
//------------------------------------------------ ----------
InitSerial void (void) {
TMOD | = 0x20 / / Timer 1 im Modus 2 (Autoreload)
TL1 = 0xFD / / 9600 Baud-Rate
TH1 = 0xFD;
SCON = 0x50, / / UART Setup-Modus 2
TR1 = 1, / / Start Timer1 und UART
}