Embedded C Programming and the Atmel AVR - Summary and Table of
Contents
Preface
This text is designed to teach both C language programming as it applies to
embedded microcontrollers and to provide knowledge in the application of the
Atmel® family of AVR RISC microcontrollers.
Intended Audience
This book is designed to serve two diverse audiences:
1. Students in Electrical and Computer Engineering, Electronic Engineering,
Electrical Engineering Technology and or Computer Engineering Technology
curricula. Two scenarios fit the book very well:
1.1 Beginning students who have not yet had a C programming course. In this case
the book will serve a 2-semester or 4-quarter sequence in which students learn C
language programming, learn how to apply C to embedded microcontroller designs
and advance to the more sophisticated embedded applications. In this instance
the programs may all be run on an embedded microcontroller with very little
hardware knowledge required. After completing Chapter 1, the C programming
chapter, it will serve as a programming reference for the balance of the
course(s).
1.2 Students who have already taken a C programming course can use the book for
a 1 semester or 2-quarter course in embedded microcontrollers. In this instance,
the students study only those portions of the C programming chapter that relate
to programming for the embedded environment and move quickly into the advanced
hardware concepts. The C chapter is organized (as are all the chapters in the
book) to provide a usable reference to look up any information they may need in
their courses.
2. Practicing engineers, technologists and technicians who want to add a new
microcontroller to their areas of expertise. The chapter on C programming may
used as needed (depending on the users level of programming experience) either
to learn needed concepts or as a reference. The chapters about the Atmel AVR
microcontroller hardware will lead such an individual through the steps of
learning a new microcontroller and serve as a reference for future projects.
Prerequisites
Some knowledge of digital systems, number systems, and logic design is required.
Preliminary versions the C programming chapter of this book have been used
successfully in a fundamental microcontrollers course (Sophomore level no
prerequisite programming class) following two semesters of basic digital logic
courses. It has also proven to be an excellent text for an advanced
microcontrollers elective course. In many cases the students have elected to
keep the book and used it as a reference through their senior project design
courses and taken it with them into industry as a useful reference.
Organization
The text is organized into logical topic units so that instructors may either
follow the text organization starting with the C language and progressing
through the AVR hardware and into more advanced topics, or they may pick and
choose the order of the topics covered to fit their particular needs. Topics are
kept separate and identified for easy selection. The chapter exercises and
laboratory exercises are also separated by topic to make it easy to select those
that apply in any particular instance.
Chapter Contents Summary
Chapter 1, Embedded C language Tutorial
The C language is covered in detail in a step-by-step method as it applies to
programming embedded microcontrollers. One or more example programs accompany
each programming concept to illustrate its use. At the conclusion of the
chapter, students are able to create C language programs to solve problems.
Chapter 2, The Atmel RISC Processors
The AVR RISC processors are covered from basic architecture through use of all
of the standard peripheral devices included in the microcontrollers. Example
programs are used to demonstrate common uses for each of the peripherals. Upon
completion of chapters 1 and 2, students are able to apply AVR RISC processors
to solve problems.
Chapter 3, Standard I/O and Preprocessor Functions
Chapter 3 introduces students to the built-in functions available in C and to
their use. Again, example programs are used to illustrate how to use the
built-in functions. Finishing chapter 3 prepares students to utilize the
built-in functions to speed their programming and problem-solution efforts.
Chapter 4, The CodeVisionAVR C Compiler and IDE
This chapter is the handbook for the use of the CVAVR compiler and its
accompanying Integrated Development Environment (IDE). Students can learn to use
the CVAVR and its IDE effectively to create and debug C programs.
Chapter 5, Project Development
This chapter focuses on the orderly development of a project using
microcontrollers. A complete wireless indoor/outdoor weather station is
developed in its entirety to illustrate the process. Students learn to
efficiently develop projects to maximize their successes.
Appendices:
Appendix A. Library Functions Reference. A complete reference to the built-in
library functions that were available at the time of publication.
Appendix B. Getting Started with CodeVisionAVR and the STK500. This is the
quick-start guide to CVAVR when used with the Atmel STK500.
Appendix C. Programming the AVR Microcontrollers. This is a guide to actually
programming the flash memory area of the AVR devices. This is used so students
may understand the programming function.
Appendix D. Development Tool Reference. This appendix contains information on
the installation and operation of TheCableAVR serial In-System-Programmer (ISP).
Appendix E. Development Tool Reference. This appendix contains information on
the MegaAVR-Dev Development board.
Appendix F. ASCII character table.
Appendix G. An assembly code instruction summary for use with the assembly code
programming examples.
Appendix H. Answers to selected excersises from various chapters are located
here.
Rationale
The advancing technology surrounding microcontrollers continues to provide
amazingly larger amounts of functionality and speed. These increases have lead
to almost universal use of high level languages such as C being used to program
even time-critical tasks that used to require assembly language programs to
accomplish. Simultaneously the microcontrollers have become easier and easier to
apply, making them excellent vehicles for educational use. Many schools have
adopted microcontroller vehicles as target device for their courses. And the
price on microcontroller development boards has dropped to the level where a
number of schools have the students buy the board as a part of their parts kitso
that each student has their own development board. Some of these courses require
C programming as a prerequisite, and others are teaching C language programming
and the application of embedded microcontrollers in an integrated manner.
This book is an answer to the need for a text that is usable in courses with and
without a C language prerequisite course and which can be used a useful
reference in later coursework. And the included CDROM contains the compiler and
other software so that students with their own development boards have
everything they need to work outside of class as well as in the school labs.
Hardware Used
Most of the programming application examples in this text were developed using
an AVR Evaluation board provided by Progressive Resources, LLC (refer to the
appendix for specifics). This board is particularly well suited for educational
use and is a good general-purpose development board. However the Atmel AVR
microcontrollers are very easy to use and may be run perfectly well by simply
plugging them into a protoboard and adding the oscillator crystal (along with 2
capacitors) and connecting 4 wires for programming. Students have been very
successful with either method. In addition there are several different
evaluation boards available as listed in the appendix.
The AT90S8535 and Atmega163 microcontrollers have been used to work out the
examples for the text. One of the major advantages of the AVR microcontrollers
is that they are very parallel in their architecture and the programming
approach for the devices. This means that the examples shown will work on
virtually any Atmel AVR microcontroller provided that it contains the
peripherals and other resources to do the job. I.e. it is not necessary to make
changes to use the code for other members of the AVR family. Consequently the
text is useful with other members of the AVR family as well.
The more common peripherals are covered by this text and may be used as
templates to apply more exotic peripherals in some of the other AVR family
members.
CDROM contents and Software used in the text
The software used with the text includes the AMEL AVR Studio (free from http://www.atmel.com/),
and the CodeVisionAVR (CVAVR) C compiler from Hp InfoTech (evaluation version
free from http://www.hpinfotech.ro/). All of the programs in the text except for
the weather station in Chapter 5 (this program is too large for the evaluation
version) and the SPI bus examples (SPI is not supported by the evaluation
version) can be compiled and run using the evaluation version of CVAVR.
The included CDROM contains the source code for all of the software examples
from the text. These can be used as references or as starting points for
specific assignments. Also included on the CDROM is the CodeVisionAVR evaluation
version compiler that was current at the time of publication. This compiler will
compile all of the example programs except as noted above. Also refer to the
website information above to obtain the latest version of the compiler. More
information about purchasing the full version is contained in the appendix or
may be found at http://www.prllc.com/.
Authors
Richard H. Barnett, PE, Ph.D.
Professor of Electrical Engineering Technology
Purdue University
Dr Barnett has been instructing in the area of embedded microcontrollers for the
past 18 years, starting with the Intel 8085, progressing to several members of
the 8051 family of embedded microcontrollers and now teaching Advanced Embedded
Microcontrollers using the Atmel AVR devices. During his summers and two
sabbatical periods, he worked extensively with multiple-processor embedded
systems, applying them in a variety of control-oriented applications. In
addition, he consults actively in the field. Prior to his tenure at Purdue
University, he spent 10 years as an engineer in the aerospace electronics
industry.
In terms of teaching, Dr. Barnett has won a number of teaching awards up to and
including the Charles B. Murphy Award as one of the best teachers at Purdue
University. He is also listed on Purdue Universitys Book of Great Teachers, a
list of the 225 most influential teachers over Purdues entire history. This is
his second text.
He may be contacted with suggestions/comments at Purdue University at
765-494-7497 or via email at rbarnett@purdue.edu.
Larry D. O'Cull
Senior Operating Member
Progressive Resources LLC
Mr. OCull received a B.S. degree from the School of Electrical Engineering
Technology at Purdue University. His career path started in the design of
software and control systems for CNC (computer numeric controlled) machine
tools. From there he moved into other opportunities in electronics engineering
and software development for vision systems, Laser-robotic machine tools,
medical diagnostic equipment, commercial and consumer products, and has been
listed as inventor/co-inventor on numerous patents.
Mr. OCull started Progressive Resources in 1995 after several years of working
in Electrical and Software Engineering and Engineering management. Progressive
Resources LLC (http://www.prllc.com) specializes in innovative commercial,
industrial and consumer product development. Progressive Resources is an Atmel
AVR consultant member.
He may be contacted with suggestions/comments via email at locull@prllc.com.
Sarah A. Cox
Director of Software Development
Progressive Resources LLC
Ms. Cox has a Bachelor of Science degree in both Computer and Electrical
Engineering from Purdue University where she focused her studies on software
design.
After a short career for a large consulting firm working on database management
systems, she was lured away by the fast pace and the infinite possibilities of
microprocessor designs. She worked independently on various pieces of medical
test equipment before becoming a partner at Progressive Resources LLC.
At Progressive Resources, Ms. Cox has developed software for projects ranging
from small consumer products to industrial products and test equipment. These
projects have spanned across automotive, medical, entertainment, child
development, public safety/education, sound and image compression, construction,
and many other fields. Along the way she has been listed as co-inventor on
numerous patent applications. She has also written the software for
in-system-programming and development tools targeting Atmel AVR processors.
She may be contacted with suggestions/comments via email at:sac@prllc.com.
--------------------------------------------------------------------------------
Table of Contents:
Chapter 1 Embedded C Language Tutorial
1.1 Objectives
1.2 Introduction
1.3 Beginning Concepts
1.4 Variables and Constants
1.4.1 Variable Types
1.4.2 Variable Scope
1.4.3 Constants
1.4.4 Enumerations and Definitions
1.4.5 Storage Classes
1.4.6 Type Casting
1.5 I/O Operations
1.6 Operators and Expressions
1.6.1 Assignment and Arithmetic Operators
1.6.2 Logical and Relational Operators
1.6.3 Increment, Decrement and Compound Assignment
1.6.4 The Conditional Expression
1.6.5 Operator Precedence
1.7 Control Statements
1.7.1 While Loop
1.7.2 Do/While Loop
1.7.3 For Loop
1.7.4 If/Else
1.7.5 Switch/Case
1.7.6 Break, Continue and Goto
1.8 Functions
1.8.1 Prototyping and Function Organization
1.8.2 Functions that Return Values
1.8.3 Recursion
1.9 Pointers and Arrays
1.9.1 Pointers
1.9.2 Arrays
1.9.3 Multidimensional Arrays
1.9.4 Pointers to Functions
1.10 Structures and Unions
1.10.1 Structures
1.10.2 Arrays of Structures
1.10.3 Pointers to Structures
1.10.4 Unions
1.10.5 typedef Operator
1.10.6 Bits and Bitfields
1.10.7 sizeof Operator
1.11 Memory Types
1.11.1 Constants and Variables
1.11.2 Pointers
1.11.3 Register Variables
1.12 Real Time Methods
1.12.1 Using Interrupts
1.12.2 State Machines
1.13 Chapter Summary
1.14 Exercises
1.15 Laboratory Activities
2 Chapter 2 The Atmel RISC Processors
2.1 Objectives
2.2 Introduction
2.3 Architectural Overview
2.4 Memory
2.4.1 FLASH Code Memory
2.4.2 Data Memory
2.4.3 EEPROM Memory
2.5 Reset and Interrupt Functions
2.5.1 Interrupts
2.5.2 Reset
2.6 Parallel I/O Ports
2.7 Timer/Counters
2.7.1 Timer/Counter Prescalers and Input Selectors
2.7.2 Timer 0
2.7.3 Timer 1
2.7.4 Timer 2
2.8 Serial Communication using the UART
2.9 Analog Interfaces
2.9.1 Analog to Digital Background
2.9.2 Analog to Digital Converter Peripheral
2.9.3 Analog Comparator Peripheral
2.10 Serial Communication using the SPI
2.11 The AVR RISC Assembly Language Instruction Set
2.12 Chapter Summary
2.13 Exercises
2.14 Laboratory Activities
3 Standard I/O and Preprocessor Functions
3.1 Objectives
3.2 Introduction
3.3 Character Input/Output Functions getchar() and putchar()
3.4 Standard Output Functions
3.4.1 Put String - puts()
3.4.2 Put Flash String putsf()
3.4.3 Print Formatted printf()
3.4.4 String Print Formatted sprintf()
3.5 Standard Input Functions
3.5.1 Get String gets()
3.5.2 Scan Formatted scanf()
3.5.3 Scan String Formatted- sscanf()
3.6 Preprocessor Directives
3.6.1 The #include Directive
3.6.2 The #define Directive
3.6.3 The #ifdef, #ifndef, #else and #endif Directives
3.6.4 The #pragma Directive
3.6.5 Other Macros and Directives
3.7 Chapter Summary
3.8 Exercises
3.9 Laboratory Activities
4 The CodeVisionAVR C Compiler and IDE
4.1 Objectives
4.2 Introduction
4.3 IDE Operation
4.3.1 Projects
4.3.1.1 Open Existing Projects
4.3.1.2 Create New Projects
4.3.1.3 Configure Projects
4.3.1.4 Close Projects
4.3.2 Source Files
4.3.2.1 Open an Existing Source File
4.3.2.2 Create a New Source File
4.3.2.3 Add an Existing File to the Project
4.3.3 Edit Files
4.3.4 Print Files
4.3.5 The Navigator
4.4 C Compiler Options
4.5 Compile and Make Projects
4.5.1 Compile a Project
4.5.2 Make a Project
4.6 Program the Target Device
4.7 CodeWizardAVR Code Generator
4.7.1 Chip Tab
4.7.2 Ports Tab
4.7.3 External IRQ Tab
4.7.4 Timers Tab
4.7.5 UART Tab
4.7.6 ADC Tab
4.7.7 Project Information Tab
4.7.8 Generate Source Code
4.8 Terminal Tool
4.9 The Atmel AVR Studio Debugger
4.9.1 Create a COFF File for AVR Studio
4.9.2 Launch AVR Studio from CodeVisionAVR
4.9.3 Open a File for Debug
4.9.4 Start, Stop, and Step
4.9.5 Set and Clear Breakpoints
4.9.6 View and Modify Registers and Variables
4.9.7 View and Modify the Machine State
4.10 Chapter Summary
4.11 Exercises
4.12 Laboratory Activities
5 Chapter 5 Project Development
5.1 Objectives
5.2 Introduction
5.3 Concept Development Phase
5.4 Project Development Process Steps
5.4.1 Definition Phase
5.4.2 Design Phase
5.4.3 Test Definition Phase
5.4.4 Build and Test the Prototype Hardware Phase
5.4.5 System Integration and Software Development Phase
5.4.6 System Test Phase
5.4.7 Celebration Phase
5.5 Project development process summary
5.6 Example Project: A Weather Monitor
5.6.1 Concept Phase
5.6.2 Definition Phase
5.6.2.1 Electrical Specification
5.6.2.2 Operational Specification
5.6.2.3 Basic Block Diagrams
5.6.3 Measurement Considerations for the Design
5.6.3.1 Temperature
5.6.3.2 Barometric Pressure
5.6.3.3 Humidity
5.6.3.4 Wind Speed
5.6.3.5 Wind Direction
5.6.3.6 Rainfall
5.6.3.7 Dew Point Computation
5.6.3.8 Wind Chill Computation
5.6.3.9 Battery Health
5.6.3.10 Real Time
5.6.4 Hardware Design - Outdoor Unit
5.6.5 Software Design - Outdoor Unit
5.6.6 Hardware Design - Indoor Unit
5.6.7 Software Design - Indoor Unit
5.6.8 Test Definition Phase
5.6.9 Build and Test Prototype Hardware Phase
5.6.10 System Integration and Software Development Phase, Outdoor Unit
5.6.11 System Integration and Software Development Phase, Indoor Unit
5.6.12 System Test Phase
5.7 Challenges
5.8 Chapter Summary
5.9 Exercises
5.10 Laboratory Activity
Acknowledgments
Appendices
A. Library Functions Reference
B. Getting Started with CodeVisionAVR and the STK500
C. Programming the AVR Microcontrollers
D. Installing and using TheCableAVR
E. The MevaAVR-Dev Development Board
F. ASCII Character Table
G. AVR Instruction Set Summary
H. Answers to Selected Exercises
Index
CDROM