Embedded Controllers Using C and Arduino

In order for a processor to operate on data held in memory, the data must first be
...... As an exercise, alter one of your previous programs to utilize command line
.... from various manufacturers including the 8088, Z80, 6502, 6809 and 68000 to
 ...

Part of the document


Embedded Controllers
Using C and Arduino / 2E

[pic]

James M. Fiore













Embedded Controllers

Using C and Arduino


by

James M. Fiore







Version 2.0.3, 20 September 2017



Note: The .doc version of this title is no longer being updated.
It has been replaced by .odt (open document text) and .pdf formats.









This Embedded Controllers Using C and Arduino, by James M. Fiore is
copyrighted under the terms of a Creative Commons license:

[pic]


This work is freely redistributable for non-commercial use, share-alike
with attribution



Published by James M. Fiore via dissidents




[pic]





For more information or feedback, contact:




James Fiore, Professor


Electrical Engineering Technology


Mohawk Valley Community College


1101 Sherman Drive


Utica, NY 13501


jfiore@mvcc.edu


www.mvcc.edu/jfiore





Cover art by the author

Introduction


This text is designed to introduce and expand upon material related to the
C programming language and embedded controllers, and specifically, the
Arduino development system and associated Atmel ATmega microcontrollers.
It is intended to fit the time constraints of a typical 3 to 4 credit hour
course for electrical engineering technology and computer engineering
technology programs, although it could also fit the needs of a hardware-
oriented course in computer science. As such, the text does not attempt to
cover every aspect of the C language, the Arduino system or Atmel AVR
microcontrollers. The first section deals with the C language itself. It is
assumed that the student is a relative newcomer to the C language but has
some experience with another high level language, for example, Python. This
means concepts such as conditionals and iteration are already familiar and
the student can get up and running fairly quickly. From there, the Arduino
development environment is examined.

Unlike the myriad Arduino books now available, this text does not simply
rely on the Arduino libraries. As convenient as the libraries may be, there
are other, sometimes far more efficient, ways of programming the boards.
Many of the chapters examine library source code to see "what's under the
hood". This more generic approach means it will be easier for the student
to use other processors and development systems instead of being tightly
tied to one platform.

All Atmel schematics and data tables are derived from the latest version
(October, 2014) of the Atmel 328P documentation which may be found at
http://www.atmel.com/devices/ATMEGA328P.aspx This serves as the final word
on the operation and performance of the 328P and all interested parties
should become familiar with it.

There is a companion lab manual to accompany this text. Other OER (Open
Educational Resource) lab manuals in this series include DC and AC
Electrical Circuits, Computer Programming with Python and Semiconductor
Devices. OER texts and lab manuals are available for Operational Amplifiers
and Linear Integrated Circuits, and introductory Semiconductor Devices.
Please check my web sites for the latest versions.


A Note from the Author

This text is used at Mohawk Valley Community College in Utica, NY, for our
ABET accredited AAS program in Electrical Engineering Technology.
Specifically, it is used in our second year embedded controllers course. I
am indebted to my students, co-workers and the MVCC family for their
support and encouragement of this project. While it would have been
possible to seek a traditional publisher for this work, as a long-time
supporter and contributor to freeware and shareware computer software, I
have decided instead to release this using a Creative Commons non-
commercial, share-alike license. I encourage others to make use of this
manual for their own work and to build upon it. If you do add to this
effort, I would appreciate a notification.


"When things get so big, I don't trust them at all
You want some control-you gotta keep it small"

- Peter Gabriel
Table of Contents

1. Course Introduction . . . . 8
2. C Memory Organization . . . . 10
3. C Language Basics . . . . . 14
4. C Language Basics II . . . . 24
5. C Storage Types and Scope . . . 32
6. C Arrays and Strings . . . . . 36
7. C Conditionals and Looping . . . 40
8. C Pointers . . . . . . 48
9. C Look-Up Tables . . . . . 52
10. C Structures . . . . . . 56
11. C Linked Lists* . . . . . 60
12. C Memory* . . . . . 64
13. C File I/O* . . . . . . 68
14. C Command Line Arguments* . . . 72
15. Embedded Programming . . . . 74
16. Hardware Architecture . . . . 78
17. AVR ATmega 328P Overview** . . . 84
18. Bits & Pieces: includes and defines . . 90
19. Bits & Pieces: Digital Output Circuitry . . 98
20. Bits & Pieces: Digital Input Circuitry . . 102
21. Bits & Pieces: pinMode . . . . 106
22. Bits & Pieces: digitalWrite . . . . 112
23. Bits & Pieces: delay . . . . . 116
24. Bits & Pieces: digitalRead . . . . 124
25. Bits & Pieces: Analog Input Circuitry . . 132
26. Bits & Pieces: analogRead . . . . 136
27. Bits & Pieces: analogWrite . . . . 142
28. Bits & Pieces: Timer/Counters . . . 146
29. Bits & Pieces: Interrupts . . . . 154
Appendices . . . . . . 160
Index . . . . . . . 165
* Included for more complete language coverage but seldom used for small
to medium scale embedded work.
** Including modest comic relief for film noir buffs.
1. Course Introduction

1.1 Overview

This course introduces the C programming language and specifically
addresses the issue of embedded programming. It is assumed that you have
worked with some other high level language before, such as Python, BASIC,
FORTRAN or Pascal. Due to the complexities of embedded systems, we begin
with a typical desktop system and examine the structure of the language
along with basic examples. Once we have a decent grounding in syntax,
structure, and the development cycle, we switch over to an embedded system,
namely an Arduino based development system.

This course is designed so that you can do considerable work at home with
minimal cost, if you choose (entirely optional, but programming these
little beasties can be addicting so be fore warned). Along with this course
text you will need an Arduino Uno board (about $25) and a USB host cable. A
small "wall wart" power adapter for it may also be useful. There's a lot of
free C programming info on the 'net but if you prefer print books and want
more detail, you may also wish to purchase one of the many C programming
texts available. Two good titles are Kochan's book Programming in C and the
one by Deitel & Deitel C-How to Program. Whichever book you choose, make
sure that its focus is C, not C++. You will also need a desktop C compiler.
Just about any will do, including Visual C/C++, Borland, Code Warrior, or
even GCC. A couple of decent freeware compilers available on the 'net
include Pelles C and Miracle C.



1.2 Frequently Asked Questions



Why learn C language programming?


C is perhaps the most widely used development language today. That alone
is a good reason to consider it, but there's more:


. It is a modern structured language that has been standardized (ANSI).
. It is modular, allowing reuse of code.
. It is widely supported, allowing source code to be used for several
different platforms by just recompiling for the new target.
. Its popularity means that several third-party add-ons (libraries and
modules) are available to "stretch" the language.
. It has type checking which helps catch errors.
. It is very powerful, allowing you to get "close to the metal".
. Generally, it creates very efficient code (small space and fast
execution).



What's the difference between C and C++?


C++ is a superset of C. First came C, then came C++. In fact, the name
C++ is a programmer's joke because ++ is the increment operator in C.
Thus, C++ literally means "increment C", or perhaps "give me the next C".
C++ does everything C does plus a whole lot more. These extra features
don't come free and embedded applications usually cannot afford the
overhead. Consequently, although much desktop work is done in C++ as well
as C, most embedded work is done in C. Desktop development systems are
usually referred to as C/C++ systems meaning that they'll do both.
Embedded development systems may be strictly C (as is ours).



Where can I buy an Arduino development board?


The Arduino Uno board is available from a variety of sources including
Digi-Key, Mouser, Parts Express and others. Shop around!



What's the difference between desktop PC development and embedded
programming?


Desktop development focuses on applications for desktop computers. These
include things like word processors, graphing utilities, games, CAD
programs, etc. These are the things most people think of when they hear
the word "compute