Siegesmund | Embedded C Programming | E-Book | www.sack.de
E-Book

E-Book, Englisch, 424 Seiten

Siegesmund Embedded C Programming

Techniques and Applications of C and PIC MCUS
1. Auflage 2014
ISBN: 978-0-12-801470-7
Verlag: Elsevier Science & Techn.
Format: EPUB
Kopierschutz: 6 - ePub Watermark

Techniques and Applications of C and PIC MCUS

E-Book, Englisch, 424 Seiten

ISBN: 978-0-12-801470-7
Verlag: Elsevier Science & Techn.
Format: EPUB
Kopierschutz: 6 - ePub Watermark



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. - A complete introduction to C programming using PIC microcontrollers, with a focus on real-world applications, programming methodology and tools - Each chapter includes C code project examples, tables, graphs, charts, references, photographs, schematic diagrams, flow charts and compiler compatibility notes to channel your knowledge into real-world examples - Online materials include presentation slides, extended tests, exercises, quizzes and answers, real-world case studies, videos and weblinks

Mark Siegesmund is a software engineer with over 30 years of experience in embedded sytems within the military, industrial and commercial sectors. He is the founder of Custom Computer Services, Inc. (CCS). CCS specializes in embedded software and hardware, offering development tools for Microchip MCUs and DSCs, as well as a line of embedded Ethernet development.
Siegesmund Embedded C Programming jetzt bestellen!

Autoren/Hrsg.


Weitere Infos & Material


2;Half Title
;2
3;Title Page;4
4;Copyright;5
5; Contents;6
6; Introduction;16
7;Chapter 1 C Overview and Program Structure;18
7.1;C Source Code;18
7.2;Comments;18
7.3;Program Structure;19
7.4;C Preprocessor Directives;19
7.5;Functions;19
7.6;Declarations;20
7.7;Statements and Expressions;20
7.8;Time;21
7.9;Typing Accuracy;21
7.10;Text Formatting;22
7.11;Compatibility Notes;22
7.12;Summary;22
7.13;Quiz;25
8;Chapter 2 Constants;28
8.1;Bits, Bytes, Etc.;28
8.1.1;Bits;28
8.1.2;Nibbles;28
8.1.3;Bytes;28
8.1.4;Syntax of C Constants;29
8.1.5;Binary;29
8.1.6;Decimal;31
8.1.7;Signed Integers;31
8.1.8;Hexadecimal;32
8.1.9;Octal;34
8.1.10;Floating Point;35
8.1.11;Fixed Point;35
8.1.12;Characters;35
8.1.13;String of Characters;36
8.1.14;True and False;37
8.1.15;Const;38
8.2;Tri-Graph Sequences;38
8.3;Compatibility Notes;40
8.3.1;Design Documentation;40
8.3.2;Summary;40
8.4;Quiz;42
9;Chapter 3 Preprocessor Directives;44
9.1;Standard Preprocessor Directives;44
9.2;#define id text;44
9.3;#include


Chapter 1

C Overview and Program Structure


Abstract


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 PIC™ microcontroller manufactured by Microchip.

Keywords

C program structure

Compilation unit

Translation unit

C comments

Functions

Statements

Expressions

Preprocessor

C Source Code


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


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.

Program Structure


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.

C Preprocessor Directives


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.

Functions


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.

Declarations


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.

Statements and Expressions


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).

Time


The ms in delay_ms is milliseconds. Time units frequently used in programs...



Ihre Fragen, Wünsche oder Anmerkungen
Vorname*
Nachname*
Ihre E-Mail-Adresse*
Kundennr.
Ihre Nachricht*
Lediglich mit * gekennzeichnete Felder sind Pflichtfelder.
Wenn Sie die im Kontaktformular eingegebenen Daten durch Klick auf den nachfolgenden Button übersenden, erklären Sie sich damit einverstanden, dass wir Ihr Angaben für die Beantwortung Ihrer Anfrage verwenden. Selbstverständlich werden Ihre Daten vertraulich behandelt und nicht an Dritte weitergegeben. Sie können der Verwendung Ihrer Daten jederzeit widersprechen. Das Datenhandling bei Sack Fachmedien erklären wir Ihnen in unserer Datenschutzerklärung.