Sie sind auf Seite 1von 7

Kapitel 1

1 Einleitung

Nun endlich schreiben Sie Ihr erstes komplettes ´C´-Programm, durch das der
CC03er mit seiner Umwelt in Kontakt tritt.
Dazu benötigen Sie zunächst einmal die ´C´-Funktion „printf(…)“ zur Ausgabe
von Texten und Zahlen. Verschiedene Steuerzeichen ermöglichen es Ihnen danach,
die Bildschirmgestaltung optimal für Ihre Zwecke und nach Ihren Vorstellungen
aufzubauen.
Jetzt kommen Sie auch mit einem etwas unangenehmen Kapitel in Berührung: mit
Programmfehlern und deren Beseitigung.
Danach geht es weiter mit Zahlen und Zahlensystemen. Sie lernen, welche Daten
bzw. Variablen-Typen es unter ´C´ gibt und welche Rechenfunktionen vorhanden
sind.
Dann betrachten wir die umgekehrte Richtung: Wie gelangen Eingabe-Informatio
nen von der Tastatur zum CC03er?
Sie lernen auch neue ´C´-Strukturen kennen, wie die if-Anweisung, die switch
case-Konstruktion, die while-, die do … while- und die for-Schleife.
Logische Operatoren und Verknüpfungen runden die Betrachtungen zu ´C´ ab.
Selbstverständlich behandeln wir wieder Themen rechts und links vom Mikrocon
troller, wie der Boot-Modus, ESC-Steuersequenzen, logische Grundverknüpfungen
etc.
Chapter 2
2 Lesson 5 'C' Practice 1 - Building a 'C' program
In this lesson you will first learn some basic facts about the

Programming language 'C': about the basic structure of a 'C' program, over which

Language elements of 'C' and beyond the functionality of the standard

'C' libraries.

2.1 General

Take a look at the world of microcontroller programming languages You can see very quickly that the
front runner is the language 'C', the Meanwhile, the most original microcontroller language, the
assembler, on has displaced many areas.

Today's 'C' compilers are getting more powerful in terms of:

- The code optimization, that is, the translated programs are getting shorter and shorter and
exploiting the subtleties of microcontroller-specific hardware programming to the last,
- The running speed, i.e. the programs are running faster and faster and reach almost the
execution speeds of optimal written assembler programs.

"Remember!"

The programming language 'C'

The programming language 'C' does not come with a professional programmer

The industry nowadays more over and also for the committed

Practitioners from other technical fields who want to use microcontrollers are engaging with 'C'

Got to.

Beginners, however, must be aware that the first one Getting into 'C' can sometimes be a bit bumpy.

For us as authors of this course this has the consequence:

- At first we introduce you to "C" only as far as it does for the first

successful steps into the 8051er world is necessary: practical,

low in frustration and ready to run without too much theoretical ballast.

- You can then get to know, exploit and program all the functions of our 8051er with your 'C'
knowledge. More effective for writing

Programs generally completely pass this knowledge on!

- We will familiarize you step by step with the higher ordinations of 'C' do.
The thing started with 'C' in the seventies of the last century

Messrs. Brian W. Kernigham and Dennis M. Ritchie, who use 'C' as a language, among others.

developed for programming the operating system UNIX.

From then on,'C' gradually turned into a universal programming language that finally emerged in
1988 in its basic elements of the American National

Standard Institute (ANSI) was quasi internationally standardized. This is how the core language ANSI-
C was born.

Over time, when microcontrollers were born, the ANSI-C core has been enhanced with
microcontroller-specific commands to optimally support the properties of these computational
devices. On the microcontroller level you need i.Allg. no large database or Windows applications, but
for example fast-running I / O routines to on-site processes too control and regulate. These aspects
were then supplemented by 'C' for Microcontroller taken into account.

Therefore, we subsequently rely on the ANSI-C core and gradually add microcontroller (8051er) -
specific peculiarities.

2.2 The basic structure of a 'C' program

Let's start immediately with a look at the basic structure of one

'C' program, Table 2.1:

Table 2.1: The basic structure of a C program

Instructions for the preprocessor (1)

# ...

Definition of global variables (2)

-------------------------------------------------- ----------------------------------------------

Definition of self-written functions (3)

-------------------------------------------------- ----------------------------------------------

Start of the main program Start the main function main ():

void main (void) (4)

To solve the desired program task:

Definition of local variables

Calling C standard library functions

Call of self-written functions

} (5)

End of the main program End of main function main ().


There are five different areas in which to build such a 'C' program:

(1): instructions for the preprocessor


The preprocessor is a kind of "preprocessing program" that first processes your program text before
the 'C' compiler (translator) starts the actual translation work.

Thus, e.g. The preprocessor automatically replaces the comments in the program with blanks, or
special program libraries with already completed functions are integrated.

Such direct instructions to the preprocessor always start with the "#"-Character.

More on that later.

(2): Global variables


These are variables that are valid throughout the program, so to speak can be accessed from
anywhere.

(3): self-written functions


Such program parts make up the essential performance of 'C',

because you are able to make your programs modular and this exactly to your needs.

(4): start the main function main


This is where your actual main program starts by entering the required

local variables, the commands to execute, and the function calls.

(5): end of main function main


Your main program ends with the last "}" bracket.

From now on, you should always have this structure in front of your inner eye, because

We will gradually fill these individual blocks.

2.3 The language elements of 'C'


To develop your own 'C' program, you need certain, standardized Language elements, which we
want to introduce briefly to you first and briefly which will be explained in more detail in the
individual lessons.

Basically, a distinction is made between those specified in ANSI-C,

internationally standardized elements and the later added microcontroller specific components,
which are not internationally standardized.
Comments
are essential in every program to

Explain program sequences, so you can later through the program

"Through increases".

names
within the program are very important because you identify your variables,

Constants and function calls.

data types
are numbers known from mathematics:

- integers: 1, 2, 3, -5, -1075, ...

- floating-point numbers: 3.1415, 125.78, -3456.789, ...

Also in 'C' these types of numbers are distinguished.

In addition, there are character types, bit types and so-called strings

(Text strings) consisting of pure ASCII characters.

Variables and constants


are the objects that are processed in your program. They can have different scope.

Keywords (reserved words)


are words from the 'C' vocabulary that you are not allowed to use as your own name. In other words,
keywords are 'C' command words, e.g.

"If" for programming a conditional branch. They are therefore not allowed

Name a variable with the name if, because then error messages occur.

operators
are shortcuts (one or two pieces) that use certain operations

Realize numbers or (computational) expressions, for example:

- the addition of two numbers, indicated by the "+" operator: a + b

- the comparison of two numbers by the "> (greater)" operator: a> b

- the logical combination of two statements by the "&& (AND)" -

Operator: "If ((a> 25) && (a <32)) then ..."

- Etc.
instructions
of which there are three different types:

a) Simple instructions:

c=a+b

The variable c is assigned the sum of the variables a and b.

b) 'own' (internal) functions. The actual commands of'C' will be

referred to as functions (more precisely, as library functions, since they are all in

certain libraries are summarized):

y = sin (x);

printf ("Hello, you there!")

c) Functions written by you (these are also called subprograms):

mw_ein (); // Reading in measured values

mw_aw (); // Evaluation of measured values

Program constructs in 'C'


serve for the structured structure of the program. Here are instructions

for program loops, conditional branches, etc.

The following are these program elements:

- the block statement

- the "for" loop

- the "if ... else" query

- the "while" loop

- the "do ... while" loop

- the "switch ... case" distinction

- the "break" statement

- the "continue" statement

- the "return" statement

Fields and structures


are one or more dimensional arrangements consisting of elements same or different data type. They
are also called vectors in mathematics or matrices.

string processing
At this point, the user will find functions for handling and processing Strings, however, e.g. not so
common in process automation be used.
Microcontroller specific adjustments
Supplement the ANSI-C standard so that 'C' programs can make the most of the special features of
the microcontroller and the user

e.g. a powerful real - time behavior on the immediate field level

Provide process automation.

interrupts
The processing of unpredictable program interruptions (so-called interrupts) represents one of the
most essential and performance-enhancing features of any microcontroller.

Accordingly, a microcontroller programming language must also be in the

Be able to optimally support such "exceptional situations", i. programmatically to realize and work
off as quickly as possible.

pointer
Working with Pointers is another essential feature of 'C',

which we will also discuss in detail.

Preprocessor directives
are commands to the preprocessor that do a certain "preprocessing" of the program text before the
'C' compiler translates that program text.

2.4 The Standard Libraries of 'C'


The standard functions of 'C', i. the normal command set of ANSI-C, are summarized in so-called
function libraries that are used in the Purchase of the 'C' compiler or the IDE are included.

Each library includes a specific area of expertise. So there are e.g. a Library for purely mathematical
functions, one with functions for string processing, one for input / output functions, etc.

So you never need to "reinvent the wheel", but you can already Use ready-made functions for your
purposes.

In the respective lessons, we will then introduce you to the scope of the most important libraries and
work with them.