Introduction to Embedded Systems Using ANSI C and the Arduino Development Environment Synthesis Lectures on Digital Circuits and Systems 1st Edition Russell - The ebook with rich content is ready for you to download
Introduction to Embedded Systems Using ANSI C and the Arduino Development Environment Synthesis Lectures on Digital Circuits and Systems 1st Edition Russell - The ebook with rich content is ready for you to download
or textbooks at https://ebookultra.com
_____ Follow the link below to get your download now _____
https://ebookultra.com/download/introduction-to-embedded-
systems-using-ansi-c-and-the-arduino-development-
environment-synthesis-lectures-on-digital-circuits-and-
systems-1st-edition-russell/
https://ebookultra.com/download/introduction-to-embedded-systems-a-
cyber-physical-systems-approach-1st-edition-e-a-lee-and-s-a-seshia/
https://ebookultra.com/download/embedded-systems-handbook-second-
edition-embedded-systems-design-and-verification-richard-zurawski/
https://ebookultra.com/download/multi-core-embedded-systems-embedded-
multi-core-systems-1st-edition-georgios-kornaros/
Digital Communication Systems Using SystemVue 1st Edition
Dennis Silage
https://ebookultra.com/download/digital-communication-systems-using-
systemvue-1st-edition-dennis-silage/
https://ebookultra.com/download/java-software-and-embedded-
systems-1st-edition-mattis-hayes/
https://ebookultra.com/download/practical-rf-circuit-design-for-
modern-wireless-systems-active-circuits-and-systems-gilmore/
https://ebookultra.com/download/embedded-systems-handbook-1st-edition-
richard-zurawski/
https://ebookultra.com/download/testing-complex-and-embedded-
systems-1st-edition-kim-h-pries/
Introduction to Embedded Systems Using ANSI C and
the Arduino Development Environment Synthesis
Lectures on Digital Circuits and Systems 1st Edition
Russell Digital Instant Download
Author(s): Russell, David
ISBN(s): 9781608454983, 1608454983
Edition: 1
File Details: PDF, 1.67 MB
Year: 2010
Language: english
~ \ i \) I\ l;-\ '\ & ~ ·1 :\ Y I' l H) I I'll I\ I I\ II I I\\
Introduction to
Embedded Systems
Using ANSI C and the
Arduino Development Environment
David Russell
Introduction to
Embedded Systems
Using ANSI C and
the Arduino Development Environment
Copyright © 2010 by Morgan & Claypool
All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in
any form or by any means—electronic, mechanical, photocopy, recording, or any other except for brief quotations in
printed reviews, without the prior permission of the publisher.
Introduction to Embedded Systems: Using ANSI C and the Arduino Development Environment
David J. Russell
www.morganclaypool.com
DOI 10.2200/S00291ED1V01Y201007DCS030
Lecture #30
Series Editor: Mitchell A. Thornton, Southern Methodist University
Series ISSN
Synthesis Lectures on Digital Circuits and Systems
Print 1932-3166 Electronic 1932-3174
Synthesis Lectures on Digital
Circuits and Systems
Editor
Mitchell A. Thornton, Southern Methodist University
The Synthesis Lectures on Digital Circuits and Systems series is comprised of 50- to 100-page books
targeted for audience members with a wide-ranging background. The Lectures include topics that are
of interest to students, professionals, and researchers in the area of design and analysis of digital circuits
and systems. Each Lecture is self-contained and focuses on the background information required to
understand the subject matter and practical case studies that illustrate applications. The format of a
Lecture is structured such that each will be devoted to a specific topic in digital circuits and systems
rather than a larger overview of several topics such as that found in a comprehensive handbook. The
Lectures cover both well-established areas as well as newly developed or emerging material in digital
circuits and systems design and analysis.
Embedded Systems Interfacing for Engineers using the Freescale HCS08 Microcontroller II:
Digital and Analog Hardware Interfacing
Douglas H. Summerville
2009
Embedded Systems Interfacing for Engineers using the Freescale HCS08 Microcontroller I:
Assembly Language Programming
Douglas H.Summerville
2009
Pragmatic Power
William J. Eccles
2008
Pragmatic Logic
William J. Eccles
2007
David J. Russell
University of Nebraska-Lincoln
M
&C Morgan & cLaypool publishers
ABSTRACT
Many electrical and computer engineering projects involve some kind of embedded system in which
a microcontroller sits at the center as the primary source of control. The recently-developed Arduino
development platform includes an inexpensive hardware development board hosting an eight-bit
ATMEL ATmega-family processor and a Java-based software-development environment. These
features allow an embedded systems beginner the ability to focus their attention on learning how to
write embedded software instead of wasting time overcoming the engineering CAD tools learning
curve.
The goal of this text is to introduce fundamental methods for creating embedded software in
general, with a focus on ANSI C. The Arduino development platform provides a great means for
accomplishing this task. As such, this work presents embedded software development using 100%
ANSI C for the Arduino’s ATmega328P processor. We deviate from using the Arduino-specific
Wiring libraries in an attempt to provide the most general embedded methods. In this way, the reader
will acquire essential knowledge necessary for work on future projects involving other processors.
Particular attention is paid to the notorious issue of using C pointers in order to gain direct access
to microprocessor registers, which ultimately allow control over all peripheral interfacing.
KEYWORDS
embedded systems, embedded software, embedded development, microcontroller, mi-
croprocessor, ANSI C, Arduino, ATmega328P
To my best friend Jamie
and our three wonderful kids:
Gates, Gracen, and Gavin
xi
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Digital Representation of Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Digital Logic Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4 Digital Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.5 Information Representation in a Digital Processor . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.5.1 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.5.2 Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2 ANSI C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.1.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.2 Essential Elements of the Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.3 Formatted Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.4 Variables and Arithmetic Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.4.1 Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.4.2 Type Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.4.3 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.4.4 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.4.5 Relational and Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.4.6 Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.4.7 Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
2.4.8 Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
2.4.9 Conditional Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.5 Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.5.1 If-Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
2.5.2 Else-If . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
2.5.3 Switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
2.5.4 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
xii
2.5.5 Infinite Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.5.6 Miscellaneous (Please Don’t Use) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.6 Functions and Program Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
2.7 Scope Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
2.8 Pointers and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
2.8.1 Passing by Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
2.8.2 Dynamic Memory Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
2.9 Multi-dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
2.10 Function Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
2.11 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
2.11.1 Typedef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
2.12 Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
2.13 Bit-fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
2.14 Variable-length Argument Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
3 Introduction to Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
3.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
3.2 Experiments Using the Arduino Duemilanove Development Board . . . . . . . . . . . 80
3.3 Arduino Tools Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
4 Embedded Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
4.2 Debugging the Arduino Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
5 ATmega328P Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
5.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
5.2 AVR CPU Core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
6 General-Purpose Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
6.1 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
6.1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
6.1.2 Basic Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
6.1.3 Pin-muxing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
6.2 Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
6.2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
6.2.2 Internal Pull-up Resistor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
xiii
6.3 Accessing GPIO lines in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
6.3.1 Managing Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
6.3.2 Managing Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
6.4 Pertinent Register Descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
6.4.1 PORTB - The Port B Data Register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
6.4.2 DDRB - The Port B Data Direction Register . . . . . . . . . . . . . . . . . . . . . 108
6.4.3 PINB - The Port B Input Pins Address . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
6.4.4 PORTC - The Port C Data Register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
6.4.5 DDRC - The Port C Data Direction Register . . . . . . . . . . . . . . . . . . . . . 108
6.4.6 PINC - The Port C Input Pins Address . . . . . . . . . . . . . . . . . . . . . . . . . . 109
6.4.7 PORTD - The Port D Data Register . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
6.4.8 DDRD - The Port D Data Direction Register . . . . . . . . . . . . . . . . . . . . . 109
6.4.9 PIND - The Port D Input Pins Address . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookultra.com