Atmel AVR Microcontroller Primer Programming and Interfacing 1st Edition Steven F. Barrett download
Atmel AVR Microcontroller Primer Programming and Interfacing 1st Edition Steven F. Barrett download
https://ebookfinal.com/download/atmel-avr-microcontroller-primer-
programming-and-interfacing-1st-edition-steven-f-barrett/
https://ebookfinal.com/download/embedded-c-programming-and-the-atmel-
avr-2nd-edition-richard-h-barnett/
https://ebookfinal.com/download/8051-microcontroller-architecture-
programming-and-application-1st-ed-edition-mahalakshmi/
https://ebookfinal.com/download/ecological-modernisation-and-
japan-1st-edition-brendan-f-d-barrett/
Real Time C Efficient Object Oriented and Template
Microcontroller Programming 2nd Edition Unknown
https://ebookfinal.com/download/real-time-c-efficient-object-oriented-
and-template-microcontroller-programming-2nd-edition-unknown/
https://ebookfinal.com/download/attachment-play-and-authenticity-a-
winnicott-primer-1st-edition-steven-tuber/
https://ebookfinal.com/download/learn-java-fundamentals-a-primer-for-
java-development-and-programming-1st-edition-jeff-friesen/
https://ebookfinal.com/download/cross-platform-game-programming-game-
development-1st-edition-steven-goodwin/
https://ebookfinal.com/download/programming-in-prolog-fifth-edition-w-
f-clocksin/
Atmel AVR Microcontroller Primer Programming and
Interfacing 1st Edition Steven F. Barrett Digital Instant
Download
Author(s): Steven F. Barrett, Daniel Pack, Mitchell Thornton
ISBN(s): 9781598295412, 1598295411
Edition: 1
File Details: PDF, 10.20 MB
Year: 2007
Language: english
1 of 188
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.
DOI: 10.2200/S00100ED1V01Y200712DCS015
A Publication in the Morgan & Claypool Publishers series
Lecture #15
Series Editor: Mitchell A. Thornton, Southern Methodist University
Series ISSN
Daniel J. Pack
United States Air Force Academy
M
&C Mor gan &Cl aypool Publishers
4 of 188
iv
ABSTRACT
This textbook provides practicing scientists and engineers a primer on the Atmel AVR microcon-
troller. Our approach is to provide the fundamental skills to quickly get up and operating with this
internationally popular microcontroller. The Atmel ATmega16 is used as a representative sample
of the AVR line. The knowledge you gain on the ATmega16 can be easily translated to every
other microcontroller in the AVR line. We cover the main subsystems aboard the ATmega16,
providing a short theory section followed by a description of the related microcontroller subsystem
with accompanying hardware and software to exercise the subsytem. In all examples, we use the
C programming language. We conclude with a detailed chapter describing how to interface the
microcontroller to a wide variety of input and output devices.
KEYWORDS
Atmel microcontroller, microcontroller, ATmega16, Atmel AVR, microcontroller interfacing
5 of 188
v
Preface
In 2006, Morgan & Claypool Publishers (M&C) released our textbook Microcontrollers Fundamen-
tals for Engineers and Scientists. The purpose of this textbook was to provide practicing scientists
and engineers a tutorial on the fundamental concepts and the use of microcontrollers. The text-
book presented the fundamental concepts common to all microcontrollers. Our goals for writing
this follow-on book are to present details on a specific microcontroller family---the Atmel AVR
Microcontroller.
Why Atmel? There are many excellent international companies that produce microcontrollers.
As Atmel states, ‘‘Atmel Corporation is an industry leader in the design and manufacture
of advanced semiconductors, with focus on microcontrollers, nonvolatile memory, logic, radio
frequency components and sensors.’’ Some of the highlights of the Atmel AVR line include
Although all of these features are extremely important, we have chosen to focus on the Atmel
AVR line of microcontrollers for this primer for a number of other related reasons:
• The learning curve for Atmel microcontrollers is gentle. If this is your first exposure to
microcontrollers, you will quickly come up to speed on microcontroller programming and
interfacing. If you already know another line of processors, you can quickly apply your
knowledge to this powerful line of 8-bit processors.
• It is relatively inexpensive to get started with the Atmel AVR microcontroller line.
The microcontrollers themselves are inexpensive, and the compilers and programming
hardware and software are relatively inexpensive.
6 of 188
vi ATMEL AVR MICROCONTROLLER PRIMER: PROGRAMMING AND INTERFACING
• The AVR line provides a full range of processing power, from small 8-pin processors to
complex 100-pin processors. The same compiler and programming hardware may be used
with a wide variety of microcontrollers.
• Many of the AVR microcontrollers are available in dual inline package, which makes them
readily useable on a printed circuit board prototype (e.g., senior design projects).
• Many of the microcontrollers in the AVR line are pin-for-pin compatible with one another.
This allows you to easily move up and down the AVR line as your project becomes better
defined.
• Atmel has documentation available for every microcontroller at your fingertips. Simply
visit www.atmel.com. Furthermore, Atmel customer support is good and responsive.
• There is worldwide interest in the AVR microcontroller line. We would be remiss to not
mention AVR Freaks. This is a dedicated, international group of AVR experts who share
their expertise online with other high-power users and novices alike.
Acknowledgments
Space does not permit us to thank everyone who has provided encouragement along the way. We
thank Joel Claypool and John Enderle for inviting us to participate in their efforts to develop a
series of short tutorial textbooks on select engineering topics. We also thank Atmel and ImageCraft
for their permission to use their copyrighted material and screenshots throughout the text. We
especially thank Helen Perlegos of Atmel for her assistance is securing appropriate permission to
use Atmel material within the text.
Most of all, we thank our families. We acknowledge our parents. Thank you, Moms
(Eleanore and Jackie), and thank you, Dad (Frank), for always believing in me (S.B.). Thank you,
Moms (Young Shin and Rana), and thank you, Dads (Sung Bock and Chong Kon), for your
encouragement and unfailing support (D.P.). Finally, our work could not have come to fruition
without the sacrifices of our family members: Cindy, Heidi, Heather, Jon R., Christine, Jon B.,
Andrew, and Graham. As always, without you none of this would matter. We love you!
Contents
CHAPTER 1
FIGURE 1.1: Atmel AVR ATmega16 block diagram. Figure used with permission of Atmel.
17 of 188
ATMEL AVR ARCHITECTURE OVERVIEW 5
can be programmed outside its resident circuit using a flash programmer board. We will use this
technique throughout the book. Specifically, we will use the Atmel STK500 AVR Flash MCU
Starter Kit for programming the ATmega16. This inexpensive development board (less than $100)
is readily available from a number of suppliers.
a)
Port x Data Register - PORTx
7 0
Port x Data Direction Register - DDRx
7 0
Port x Input Pins Address - PINx
7 0
b)
FIGURE 1.2: ATmega16 port configuration registers: (a) port-associated registers and (b) port pin
configuration.
19 of 188
ATMEL AVR ARCHITECTURE OVERVIEW 7
• Data Direction Register (DDRx)---used to set a specific port pin to either output (1) or
input (0), and
• Input Pin Address (PINx)---used to read input data from the port.
Figure 1.2(b) describes the settings required to configure a specific port pin to either input
or output. If selected for input, the pin may be selected for either an input pin or to operate in the
high-impedance (Hi-Z) mode. In Hi-Z mode, the input appears as high impedance to a particular
pin. If selected for output, the pin may be further configured for either logic low or logic high.
Port pins are usually configured at the beginning of a program for either input or output, and
their initial values are then set. Usually, all eight pins for a given port are configured simultaneously.
A code example is provided below to show how ports are configured. Note that because we are
using the C programming language with a compiler include file, the register contents are simply
referred to by name. Note that the data direction register (DDRx) is first used to set the pins as
either input or output, and then the data register (PORTx) is used to set the initial value of the
output port pins.
//***************************************************************
//initialize_ports: provides initial configuration for I/O ports
//***************************************************************
void initialize_ports(void)
{
DDRA=0xfc; //set PORTA[7:2] as output, PORTA[1:0]
//as input (1111_1100)
PORTA=0x03; //initialize PORTA[7:2] low, PORTA[1:0]
//current source
DDRB=0xa0; //PORTB[7:4] as output, set PORTB[3:0] as input
PORTB=0x00; //disable PORTB pull-up resistors
:
:
:
Vow, vau, subst. gelofte, eed; Vow verb. beloven, eene gelofte
doen, bezweren, plechtig betuigen, wijden: To make a vow = een
gelofte doen; She has received (taken) the vows = heeft de
kloostergeloften afgelegd.
Wabash, wôbaš.
Waddington, wodiŋt’n.
Waddle, wod’l, subst. waggelende, schommelende gang; Waddle
verb. waggelen; Waddler.
Waddy, wadi, wodi, knots, stok; Waddy verb. met een knots
doodslaan.
Wallis, wolis.
Walpole, wolpoul.
Walworth, wolwəth.
Wandsworth, wonzwəth.
Wanley, wonli.
Wapping, wopiŋ.
Warble, wöb’l, subst. gekweel, lied; gezwel op den rug van een
paard; Warble verb. kweelen, zingen, trillers slaan; Warbler =
kweeler, zangvogel.
Warham, wor’m.
Warsaw, wösô.
Wart, wöt, wrat: Wart-hog = soort van Amer. wild zwijn; Wart-
wort = kroontjeskruid; Wartless = zonder wratten; Warty = vol
wratten, wratachtig.
Wasp, wosp, wesp: He has his head full of wasps = hij heeft
allerlei kuren; Wasp-waisted = met wespentaille; Waspish = met
eene wespentaille, prikkelbaar, giftig; [633]Waspish-headed =
prikkelbaar, opvliegend; subst. Waspishness.
Wassail, wosil, wasil, subst. drinkgelag; gekruid bier met wijn met
Kerstmis of Nieuwjaar en opgediend in den Wassail-bowl;
Wassail verb. drinken (op het welzijn van); Wassail-cup = beker
waaruit wassail werd gedronken; Wassailer = drinkebroer;
Wassailers = lieden, die met Kerstmis zingend rondgaan.
Waterloo, wôtelû.
Wattle, wot’l, subst. horde van twijg of teen, deklat, takje, teenen
twijgje, lel van haan of kalkoen, baarddraad van visschen, soort v.
acacia (Australië); Wattle verb. met teenen twijgjes binden of
vlechten, met eene horde omringen; Wattle-bark = bast der
Austral. acacia; Wattle-bird = soort bijenwolf; Wattle-work =
werk van gevlochten teen; Wattled; Wattling and daubing = het
bouwen van hutten van gevlochten twijgen en leem.
Waugh, wô.
Waverley, weivəli.
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.
ebookfinal.com