Schweitzer Fachinformationen
Wenn es um professionelles Wissen geht, ist Schweitzer Fachinformationen wegweisend. Kunden aus Recht und Beratung sowie Unternehmen, öffentliche Verwaltungen und Bibliotheken erhalten komplette Lösungen zum Beschaffen, Verwalten und Nutzen von digitalen und gedruckten Medien.
This book provides a hands-on introductory course on concepts of C programming using a PIC® microcontroller and CCS C compiler. Through a project-based approach, this book provides an easy to understand method of learning the correct and efficient practices to program a PIC® microcontroller in C language. Principles of C programming are introduced gradually, building on skill sets and knowledge. Early chapters emphasize the understanding of C language through experience and exercises, while the latter half of the book covers the PIC® microcontroller, its peripherals, and how to use those peripherals from within C in great detail.
This book demonstrates the programming methodology and tools used by most professionals in embedded design, and will enable you to apply your knowledge and programming skills for any real-life application. Providing a step-by-step guide to the subject matter, this book will encourage you to alter, expand, and customize code for use in your own projects.
This chapter explains the structure of C programs as it pertains to embedded microcontrollers. A short example program is used to introduce the reader to each of the basic C elements.
Compilation units (sometimes called translation units), functions, statements, compound statements, expressions, and data declarations are all introduced. The unique C preprocessor concept is covered with some simple preprocessor directive samples. This chapter shows how constants, variables, operators, and function calls make up the basic components of an expression and how they are used together in the C programming language. The basics of functions calls, including passing and returning data, are included.
Examples and exercises are for the popular PICT microcontroller manufactured by Microchip.
Keywords
C program structure
Compilation unit
Translation unit
C comments
Functions
Statements
Expressions
Preprocessor
This is what C source code looks like:
This program may look very cryptic to you now. When you have finished reading this book and doing the experiments, this program and much more complex ones will no longer seem cryptic. As you read the next few chapters, you can refer to this program to see how the topics relate to this program. For now, let's make some observations about the overall look of the program.
Comments help anyone (including you) who reads your code understand what it does. There are two styles.
Comments between and (can span multiple lines, but may not be nested)
Comments between and end of line (one line only).
The compiler ignores all comments. Both styles are shown in the sample program.
C programs are made up of compilation units, sometimes called translation units. A compilation unit is a set of files that are compiled together by a compiler. For most examples in this book we will be using a single compilation unit. A compilation unit is made up of global data and functions. A function is a callable group of code that in some other languages is referred to as a procedure or subroutine. Functions are made up of local data accessible only to the function and of statements within the function.
An interesting feature of C is that it has what is referred to as a preprocessor. Think of the preprocessor as a tool that goes through the code first and makes some modifications to what is actually compiled. Preprocessor directives start with a and occupy the entire line. They will be covered in more detail in Chapter 3. In the above example the directive causes whatever lines are in the file (e3.h) to appear at this spot in the code for the compilation.
For example, if you created a file named delay.inc and put in the file one line:
delay_ms(500);
then you could replace the two delay lines in the above program with and the program would compile exactly the same. In the first step of compilation the compiler preprocessor would read the delay.inc file when it got to that line and replace the #include with where the #include were.
The preprocessor can be a powerful feature in C that can increase program readability, maximize code reuse, and significantly help in program maintenance.
As we examine the sample program shown, you see the first line is a preprocessor directive to include the e3.h file. It is very common for the first non-comment line in a program to include a file with various project- and/or hardware-specific definitions. The .h extension (for header) is frequently used for this kind of file. In our case all the declarations needed for the E3 hardware are in this include file.
Next we find a function definition for "main". All programs must have exactly one function named main(). This is where the program begins execution. When referring to a function name in this book we will follow the name with so it is clear that it is a function name. The void before the name indicates this function returns nothing and the void inside the indicates this function gets nothing from the caller. The and are grouping symbols. All functions start and end with these symbols.
Functions will be dealt with in detail in Chapter 7; however, to lay a foundation for what they are, consider some examples of a function being used:
x=sin(y); sin is a function with one argument and a return value
x=sin(y*3.1415/180); the argument may be any expression
x=180*sin(y)/3.1415; the return value may be used in an expression.
The "int i" is a data declaration for the variable named with the identifier i. int indicates the variable is an integer. In this case i may only be used inside the main() function. If this line was above the start of the function (outside the function) then i could be accessed by other functions. The range that a variable is accessible from is referred to as the variable scope. Scope will be covered in more detail in Chapter 4.
The line is a statement. Statements are executed at run time. This particular statement has three expressions within. Expressions will be covered in Chapter 5 and statements in Chapter 6. The quick overview of the statement is:
It executes the first expression once i=1
Repeats the following:
Tests the second expression and exits the loop if false i<=10
Executes the statement following the
The third expression is executed. i=i+1
In this example the four lines are executed 10 times with the variable i going from 1 to 10 and then, when 11, the loop stops because 11 <= 10 is false.
Expressions are some combination of constants, variables, operators, and function calls. Expressions always have a resulting value. Some simple examples of an operator are and the very special .
In our case since we have four statements to execute in the we need to group them together with the and . This is called a compound statement. The braces may contain zero or more statements. Without those, only the function would be called in the loop 10 times. Then the other three lines would execute once afterwards.
Each of the four lines in our loop is a function call. These functions are not defined by the programmer but rather are functions built into the compiler. Function calls are recognized by the following the function name. The expression(s) inside the of a function call is the data passed into a function. These are called arguments in the call and parameters in the function.
In C a special case of a valid statement is any expression followed by a . Note that just because it is valid does not mean it makes sense. For example, this is a valid C statement:
1+2;
However, it does not do anything. Some compilers might do the addition and that may take some time but nothing more is done. A good compiler will throw a warning on this line because the programmer might have made a typo.
A with no expression before it is a special case of a statement called the null statement. It does nothing.
In C there is not an assignment statement as in some other languages, but rather an assignment operator, the . Consider:
x=3;
This is an expression x = 3 consisting of a variable, operator, and constant. With the it makes a statement. It always assigns the value on the right side (rvalue) to the variable on the left side (lvalue).
The ms in delay_ms is milliseconds. Time units frequently used in programs...
Dateiformat: PDFKopierschutz: Adobe-DRM (Digital Rights Management)
Systemvoraussetzungen:
Das Dateiformat PDF zeigt auf jeder Hardware eine Buchseite stets identisch an. Daher ist eine PDF auch für ein komplexes Layout geeignet, wie es bei Lehr- und Fachbüchern verwendet wird (Bilder, Tabellen, Spalten, Fußnoten). Bei kleinen Displays von E-Readern oder Smartphones sind PDF leider eher nervig, weil zu viel Scrollen notwendig ist. Mit Adobe-DRM wird hier ein „harter” Kopierschutz verwendet. Wenn die notwendigen Voraussetzungen nicht vorliegen, können Sie das E-Book leider nicht öffnen. Daher müssen Sie bereits vor dem Download Ihre Lese-Hardware vorbereiten.
Bitte beachten Sie: Wir empfehlen Ihnen unbedingt nach Installation der Lese-Software diese mit Ihrer persönlichen Adobe-ID zu autorisieren!
Weitere Informationen finden Sie in unserer E-Book Hilfe.
Dateiformat: ePUBKopierschutz: Wasserzeichen-DRM (Digital Rights Management)
Das Dateiformat ePUB ist sehr gut für Romane und Sachbücher geeignet - also für „fließenden” Text ohne komplexes Layout. Bei E-Readern oder Smartphones passt sich der Zeilen- und Seitenumbruch automatisch den kleinen Displays an. Mit Wasserzeichen-DRM wird hier ein „weicher” Kopierschutz verwendet. Daher ist technisch zwar alles möglich – sogar eine unzulässige Weitergabe. Aber an sichtbaren und unsichtbaren Stellen wird der Käufer des E-Books als Wasserzeichen hinterlegt, sodass im Falle eines Missbrauchs die Spur zurückverfolgt werden kann.