6.2. Struktur eines C
Das folgende Programm zeigt den prinzipiellen Aufbau eines Programms in C mit dem Keil C51-Software.
/************************************************* *********
Beispiel Struktur eines Programms in C mit Keil C51
und Mikrocontroller 8051.
Programm, das alle Bits von Port 0 Flash
mit der Zeit.
Autor: Rafael Aranda
Datum: 20-10-2009
Version: 1.0
************************************************** ********/
# Include <reg52.h> / / Fügen Sie die generischen Domains von 8052
DelayMs void (unsigned int ms) / / Funktionsprototyp Erklärung
/ / Global Variablendeklaration
/ / Hauptprogramm
//------------------------------------------------ ----------
void main (void) {
/ / Force ersten Vorbereitung
P0 = 0, / / Setze alle Bits auf 0 P0
/ / Der Mann, der das Programm in einer Endlosschleife
while (1) {
P0 = 0x0FF / / Setze alle Bits auf 1 P0
DelayMs (200) / / 200 ms
P0 = 0x00, / / Setze alle Bits auf 0 P0
DelayMs (200) / / 200 ms
}
}
/ / Delay-Funktion
//------------------------------------------------ ----------
DelayMs void (unsigned int ms) {
/ / Verzögerung in Millisekunden für eine 11.0592 MHz Kristall
unsigned int i;
while (ms) {
i = 115;
while (i> 0) i -;
ms -;
}
}