100% found this document useful (1 vote)
138 views

Atmel AVR Microcontroller

Microcontroller

Uploaded by

erwin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
138 views

Atmel AVR Microcontroller

Microcontroller

Uploaded by

erwin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 63

Atmel AVR Microcontroller

Primer: Programming and


Interfacing

Copyright © 2008 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.

Atmel AVR Microcontroller Primer:


Programming and Interfacing Steven F. Barrett and Daniel J. Pack

www.morganclaypool.com

ISBN: 1598295411 paperback


ISBN: 9781598295412 paperback

ISBN: 159829542X ebook


ISBN: 9781598295429 ebook

DOI: 10.2200/S00100ED1V01Y200712DCS015
A Publication in the Morgan & Claypool Publishers series

SYNTHESIS LECTURES ON DIGITAL CIRCUITS AND SYSTEMS # 15

Lecture #15
Series Editor: Mitchell A. Thornton, Southern Methodist University
Series ISSN

ISSN 1932-3166 print


ISSN 1932-3174 electronic

Atmel AVR Microcontroller Primer: Programming and Interfacing


Atmel AVR Microcontroller
Primer: Programming and
Interfacing

Steven F. Barrett
University of Wyoming

Daniel J. Pack
United States Air Force Academy

SYNTHESIS LECTURES ON DIGITAL CIRCUITS AND SYSTEMS


#15
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.
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

• high performance coupled with low power consumption,


• outstanding flash memory technology,
• reduced instruction set computer Harvard Architecture,
• single-cycle instruction execution,
• wide variety of operating voltages (1.8-5.5 VDC),
• architecture designed for the C language,
• one set of development tools for the entire AVR line, and
• in-system programming, debugging, and verification capability.

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.

ATMEL AVR MICROCONTROLLERPRIMER : 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 AVRline 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.

Approach of the book

If this is your first exposure to microcontrollers, we highly recommend


that you read first our other M&C textbook, Microcontrollers Fundamentals for
Engineers and Scientists.Itwillprovideyouthe background information necessary
to fully appreciate the contents of this textbook. This textbook picks up where
the first one left off. We have received permission from M&C to include some of
the background material from the first textbook in this text to allow for a
complete stand-alone product.

Our approach in this textbook is to provide you the fundamental skills to


quickly get up and operating with an Atmel microcontroller. We have chosen to
use the AVR ATmega16 as a representative sample of the AVR line (more on this
processor later). The knowledge you gain on the ATmega16 can be easily
translated to every other microcontroller in the AVR line.

We will use an ongoing testbench example throughout the textbook. We


will start by having you get a simple microcontroller circuit operating with a
simple menu program that interacts with external devices. As we move through
various microcontroller subsystems, we will continue to add features to the
testbench. By the end of the textbook, you will have a complete
hardware/software system that demonstrates the features of the ATmega16. You
can then use this testbench to adapt to other applications.

The M&C textbooks are designed to be short tutorials on a given topic.


Therefore, our treatment of each topic will provide a short theory section
followed by a description of the related microcontroller subsystem with
accompanying hardware and software to exercise the subsystem. In all examples,
we will use the C programming language. There are many excellent C compilers
available for the Atmel AVR line. We have chosen the ImageCraft ICC AVR
compiler for its short learning curve and ease of use.
Acknowledgment
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!

Laramie and Colorado Springs, November 2007


Steve Barrett and Daniel Pack
Content
1. Atmel AVR Architecture Overview .....................................................................................................
1

1.1 ATmega16ArchitectureOverview...................................................... 1
1.1.1 Reduced Instruction Set Computer ......................................... 1
1.1.2 Assembly Language Instruction Set ........................................ 2
1.1.3 ATmega16ArchitectureOverview ............................................ 3

1.2 Non volatile and Data Memories ...................................................... 3


1.2.1 In-System Programmable Flash EEPROM ................................. 3
1.2.2 Byte- Addressable EEPROM ..................................................... 5
1.2.3 Static Random Access Memory ............................................... 5
1.2.4 Programmable Lock Bits ........................................................ 5

1.3 Port System ...................................................................................... 6

1.4 Peripheral Features---Internal Subsystems ....................................... 8


1.4.1 Time Base................................................................................ 8
1.4.2 Timing Subsystem .................................................................. 9
1.4.3 Pulse Width Modulation Channels .......................................... 9
1.4.4 Serial Communications ........................................................... 9
1.4.4.1 Serial USART ............................................................... 9
1.4.4.2 Serial Peripheral Interface .......................................... 10
1.4.4.3 Two-Wire Serial Interface............................................ 10
1.4.5 Analog-to-Digital Converter .................................................... 10
1.4.6 Interrupts ................................................................................ 11
1.5 Physical and Operating Parameters.................................................. 11
1.5.1 Packaging ................................................................................ 11
1.5.2 Power Consumption................................................................ 11
1.5.3 Speed Grades ......................................................................... 13
1.6 Application: ATmega16 Testbench .................................................. 13
1.6.1 Hardware Configuration ......................................................... 13
1.6.2 Software Configuration .......................................................... 15

ATMEL AVR MICROCONTROLLER PRIMER : PROGRAMMING AND INTERFACING

1.7 ProgrammingtheATmega16 .............................................................. 19


1.7.1 Programming Procedure ......................................................... 20
1.8 Software Portability .......................................................................... 22
1.9 Summary ......................................................................................... 23
1.10 References and Further Reading .................................................... 23
1.11 Chapter Problems ........................................................................... 23
2. Serial Communication Subsystem ..................................................................... 25
2.1 Serial Communications .................................................................... 25
2.2 Serial Communication Terminology................................................. 25
2.2.1 Asynchronous versus Synchronous Serial Transmission ....... 26
2.2.2 Baud Rate ................................................................................ 26
2.2.3 Full Duplex.............................................................................. 26
2.2.4 Nonreturn to Zero Coding Format .......................................... 26
2.2.5 TheRS-232CommunicationProtocol ........................................ 27
2.2.6 Parity....................................................................................... 27
2.2.7 American Standard Code for Information Interchange .......... 27
2.3 Serial USART ..................................................................................... 27
2.3.1 System Overview .................................................................... 28
2.3.1.1 USART Clock Generator .............................................. 29
2.3.1.2 USART Transmitter .................................................... 30
2.3.1.3 USART Receiver........................................................... 30
2.3.1.4 USART Registers ......................................................... 30
2.3.2 System Operation and Programming ...................................... 32
2.3.3 Serial Peripheral Interface ...................................................... 34
2.3.3.1 SPI Operation .............................................................. 34
2.3.3.2 Registers .................................................................... 35
2.3.3.3 Programming .............................................................. 37
2.4 Two-Wire Serial Interface ................................................................. 38
2.5 Summary ......................................................................................... 38
2.6 References and Further Reading ...................................................... 38
2.7 Chapter Problems ............................................................................. 39
3. Analog-to-Digital Conversion ..................................................................... 41
3.1 Background Theory .......................................................................... 41
3.1.1 Analog versus Digital Signals ................................................... 42
3.1.2 Sampling, Quantization, and Encoding.................................... 44
3.1.3 Resolution and Data Rate ......................................................... 48
CONTENTS xi
3.2 Analog-To-Digital Conversion Process ............................................. 50
3.3 ADC Conversion Technologies ......................................................... 53
3.3.1 Successive Approximation ...................................................... 53
3.3.2 Integration .............................................................................. 55
3.3.3 Counter-Based Conversion ..................................................... 55
3.3.4 Parallel Conversion ................................................................. 55
3.4 The Atmel ATmega16 ADC System................................................... 55
3.4.1 Block Diagram ......................................................................... 56
3.4.2 Registers ................................................................................. 58
3.4.2.1 ADC Multiplexer Selection Register ............................ 58
3.4.2.2 ADC Control and Status Register A ............................. 59
3.4.2.3 ADC Data Registers (ADCH and ADCL)........................ 59
3.4.3 Programming the ADC ............................................................ 59
3.4.4 Digital-to-Analog Conversion.................................................. 62
3.5 Summary ......................................................................................... 63
3.6 References and Further Reading ...................................................... 63
3.7 Chapter Problems ............................................................................. 64
4. Interrupt Subsystem ................................................................................... 65
4.1 Interrupt Theory .............................................................................. 65
4.2 ATmega16 Interrupt System ............................................................ 65
4.3 Programming An Interrupt ............................................................... 66
4.4 Application ...................................................................................... 68
4.4.1 External Interrupts.................................................................. 68
4.4.2 Internal Interrupt .................................................................... 71
4.5 Summary .......................................................................................... 74
4.6 References and Further Reading ...................................................... 74
4.7 Chapter Problems ............................................................................. 74
5. Timing Subsystem ....................................................................................... 75
5.1 Overview .......................................................................................... 75
5.2 Timing-Related Terminology............................................................ 76
5.2.1 Frequency ............................................................................... 76
5.2.2 Period ...................................................................................... 76
5.2.3 Duty Cycle .............................................................................. 76
5.3 Timing System Overview.................................................................. 76
5.4 Applications ..................................................................................... 79
5.4.1 Input Capture---Measuring External Timing Event ................. 79

xii ATMEL AVR MICROCONTROLLER PRIMER: PROGRAMMING AND


INTERFACING
5.4.2 Counting Events .................................................................... 81
5.4.3 Output Compare-Generating Timing Signals to Interface
External Devices ............................................................................. 81
5.4.4 Industrial Implementation Case Study(PWM) ........................ 82
5.5 Overview of the Atmel Timers ......................................................... 83
5.6 Timer 0 System ................................................................................ 84
5.6.1 Modes of Operation ............................................................... 86
5.6.1.1 Normal Mode ............................................................. 87
5.6.1.2 Clear Timer on Compare Match ................................. 87
5.6.1.3 Phase Correct PWM Mode .......................................... 87
5.6.1.4 Fast PWM ................................................................... 87
5.6.2 Timer 0 Registers ................................................................. 87
5.6.2.1 Timer/Counter Control Register 0 ............................ 88
5.6.2.2 Timer/Counter Register ............................................ 88
5.6.2.3 Output Compare Register .......................................... 88
5.6.2.4 Timer/Counter Interrupt Mask Register .................... 90
5.6.2.5 Timer/Counter Interrupt Flag Register ..................... 91
5.7 Timer 1 ............................................................................................. 91
5.7.1 Timer 1Registers ................................................................. 91
5.7.1.1 TCCR 1A and TCCR 1B Registers ............................... 91
5.7.1.2 Timer/Counter Register 1(TCNT1H/TCNT1) ............. 91
5.7.1.3 Output Compare Register 1 Channel A (OCR1AH/
OCR1AL) ................................................................................ 94
5.7.1.4 Output Compare Register 1 Channel B (OCR1BH/OCR1BL)
.............................................................................................. 94
5.7.1.5 Input Capture Register 1(ICR1H/ICR1L) .................... 94
5.7.1.6 Timer/Counter Interrupt Mask Register (TIMSK) ....... 94
5.7.1.7 Timer/Counter Interrupt Flag Register (TIFR) ........... 94
5.8 Timer 2 ............................................................................................. 94
5.8.1 Timer/Counter Control Register 2..................................... 94
5.8.2 Timer/Counter Register (TCNT2) ...................................... 95
5.8.3 Output Compare Register (OCR2) ...................................... 95
5.8.4Timer/Counter Interrupt Mask Register TIMSK) ................. 96
5.8.5 Timer/Counter Interrupt Flag Register.............................. 96
5.9 Programming the Timer System....................................................... 96
5.9.1 Precision Delay .................................................................. 98
CONTENTS xiii

5.9.2 Pulse Width Modulation ..................................................... 99


5.9.3 Input Capture Mode ........................................................... 101
5.10 Summary ........................................................................................ 103
5.11 References and Further Reading .................................................... 103
5.12 Chapter Problems ........................................................................... 104
6. Atmel AVR Operating Parameters and Interfacing ................................... 105
6.1 Operating Parameters....................................................................... 106
6.2 Input Devices ................................................................................... 107
6.2.1 Switches ........................................................................... 109
6.2.2 Switch Debouncing ........................................................... 110
6.2.3 Keypads ............................................................................ 111
6.2.4 Sensors ............................................................................ 111
6.2.4.1 Digital Sensors ..................................................... 111
6.2.4.2 Analog Sensors .................................................... 114
6.3 Output Devices ................................................................................. 114
6.3.1 Light Emitting Diodes ....................................................... 115
6.3.2 Seven-Segment LED Displays............................................ 117
6.3.3 Tristate LED Indicator. ..................................................... 117
6.3.4 Dot Matrix Display............................................................ 120
6.3.5 Liquid Crystal Display ...................................................... 120
6.3.6 High-Power DC Devices .................................................... 124
6.4 DC Motor Speed and Direction Control ............................................ 125
6.4.1 DC Motor Operating Parameters....................................... 126
6.4.2 AC Devices........................................................................ 126
6.5 Application: Flight Simulator Panel .................................................. 127
6.6 Summary .......................................................................................... 157
6.7 References and Further Reading ...................................................... 158
6.8 Chapter Problems ............................................................................. 158
A. ATmega16 Register Set .............................................................................. 159

B. ATmega16 Header File ................................................................................ 161

Author Biography ............................................................................................ 177

Index ................................................................................................................ 179


CHAPTER 1

Atmel AVR Architecture Overview

Objectives: After reading this chapter, the reader should be able to


• provide an overview of the RISC architecture of the ATmega16,
• describe the different ATmega16 memory components and their
applications,
• explain the ATmega16 internal subsystems and their applications,
• highlight the operating parameters of the ATmega16, and
• summarize the special ATmega16 features.

1.1 ATmega16 ARCHITECTURE OVERVIEW
In this section, we describe the overall architecture of the Atmel AVR
ATmega16. We begin with an introduction to the concept of the reduced
instruction set computer (RISC) and briefly describe the Atmel Assembly
Language Instruction Set. A brief introduction is warranted because we will be
programming mainly in C throughout the course of the book. We then provide a
detailed description of the ATmega16 hardware architecture.

1.1.1 Reduced Instruction Set Computer


In our first Morgan & Claypool (M&C) [1] textbook, we described a
microcontroller as an entire computer system contained within a single
integrated circuit or chip. Microcontroller operation is controlled by a user-
written program interacting with the fixed hardware architecture resident within
the microcontroller. A specific microcontroller architecture can be categorized
as accumulator-based, register-based, stack-based, or a pipeline architecture.
The Atmel ATmega16 is a register-based architecture. In this type of
architecture, both operands of an operation are stored in registers collocated
with the central processing unit (CPU). This means that before an operation is
performed, the computer loads all necessary data for the operation to its CPU.
The result of the operation is also stored in a register. During program execution,
the CPU interacts with the register set and minimizes slower memory accesses.
Memory accesses are typically handled as background operations.
2 ATMEL AVRMICROCONTROLLERPRIMER: PROGRAMMINGANDINTERFACING
Coupled with the register-based architecture is an instruction set based on
the RISC concept. A RISC processor is equipped with a complement of very simple
and efficient basic operations.More complex instructions are built up from these
very basic operations. This allows for efficient program operation. The Atmel
ATmega16 is equipped with 131 RISC-type instructions. Most can be executed in
a single clock cycle. The ATmega16 is also equipped with additional hardware to
allow for the multiplication operation in two clock cycles. In many other
microcontroller architectures, multiplication typically requires many more clock
cycles. For additional information on the RISC architecture, the interested reader
is referred to Hennessy and Patterson [3].

The Atmel ATmega16 [2] is equipped with 32 general purpose 8-bit


registers that are tightly coupled to the processor’s arithmetic logic unit within
the CPU. Also, the processor is designed following the Harvard Architecture
format. That is, it is equipped with separate, dedicated memories and buses for
program and data information. The register-based Harvard Architecture coupled
with the RISC-based instruction set allows for fast and efficient program
execution and allows the processor to complete an assembly language instruction
every clock cycle. Atmel indicates the ATmega16 can execute 16 million
instructions per second when operating at a clock speed of 16 MHz

1.1.2 Assembly Language Instruction Set


An instruction set is a group of instructions a machine ‘‘understands’’ to execute.
A large number of instructions provide flexibility but require more complex
hardware. Thus, an instruction set is unique for a given hardware and cannot be
used with another hardware configuration. Atmel has equipped the ATmega16
with 131 different instructions.

For the most efficient and fast execution of a given microcontroller,


assembly language should be used. Assembly language is written to efficiently
interact with a specific microcontroller’s resident hardware. To effectively use
the assembly language, the programmer must be thoroughly familiar with the
low-level architecture details of the controller. Furthermore, the learning curve
for a given assembly language is quite steep and lessons learned do not always
transfer to another microcontroller.

We will program the Atmel ATmega16 using the C language throughout the
text. The C programming language allows for direct control of microcontroller
hardware at the register level while being portable to other microcontrollers in
the AVR line. When a C program is compiled during the software development
process, the program is first converted to assembly language and then to the
machine code for the specific microcontroller. We must emphasize that
programming in C is not better than assembly language or vice versa. Both
approaches have their inherent advantages and disadvantages. We have chosen
to use C in this textbook for the reasons previously discussed.

ATMEL AVR ARCHITECTUREOVERVIEW 3

1.1.3 ATmega16 Architecture Overview


We have chosen the ATmega16 as a representative of the Atmel AVR line of
microcontrollers. Lessons learned with the ATmega16 may be easily adapted to all other
processors in the AVR line. A block diagram of the Atmel ATmega16’s architecture is
provided in Figure 1.1.

As can be seen from the figure, the ATmega16 has external connections for power
supplies (VCC, GND, AVCC, and AREF), an external time base (XTAL1 and XTAL2) input
pins to drive its clocks, processor reset (active low RESET), and four 8-bit ports (PA0-PA7,
PC0-PC7, PB0-PB7, and PD0-PD7), which are used to interact with the external world. As
we shall soon see, these ports may be used as general purpose digital input/output (I/O)
ports or they may be used for the alternate functions. The ports are interconnected with
the ATmega16’s CPU and internal subsystems via an internal bus. The ATmega16 also
contains a timer subsystem, an analog-to-digital converter (ADC), an interrupt subsystem,
memory components, and a communication subsystem.

In the next several subsections, we briefly describe each of these internal


subsystems shown in the figure. Detailed descriptions of selected subsystem operation
and programming are provided later in this book. We cannot cover all features of the
microcontroller because of limited space. Instead, we focus on the primary functional
components of the ATmega16 to fulfill the purpose of this book as a basic primer to the
ATmega16.

1.2 NONVOLATILE AND DATA MEMORIES


The ATmega16 is equipped with three main memory sections: flash electrically erasable
programmable read-only memory (EEPROM), static random access memory (SRAM), and
byte addressable EEPROM for data storage. We discuss each memory component in turn.

1.2.1 In-System Programmable Flash EEPROM


Bulk programmable flash EEPROM is used to store programs. It can be erased and
programmed as a single unit. Also, should a program require a large table of constants,
it may be included as a global variable within a program and programmed into flash
EEPROM with the rest of the program. Flash EEPROM is nonvolatile, meaning memory
contents are retained when microcontroller power is lost. The ATmega16 is equipped
with 16K bytes of onboard reprogrammable flash memory. This memory component is
organized into 8K locations, with 16 bits at each location.

The flash EEPROM is in-system programmable. In-system programmability means


the microcontroller can be programmed while resident within a circuit. It does not have
to be removed from the circuit for programming. Instead, a host personal computer (PC)
connected via a cable to a microcontroller downloads the program to the microcontroller.
Alternately, the microcontroller

4 ATMEL AVR MICROCONTROLLER PRIMER: PROGRAMMING AND INTERFACING


FIGURE 1.1: Atmel AVR ATmega16 block diagram. Figure used with permission of Atmel.

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.

1.2.2 Byte-Addressable EEPROM


Byte-addressable memory is used to permanently store and recall variables during
program execution. It too is nonvolatile. It is especially useful for logging system
malfunctions and fault data during program execution. It is also useful for storing data
that must be retained during a power failure but might need to be changed periodically.
Examples where this type of memory is used are found in applications to store system
parameters, electronic lock combinations, and automatic garage door electronic unlock
sequences. The ATmega16 is equipped with 512 bytes of EEPROM.

1.2.3 Static Random Access Memory


SRAM is volatile. That is, if the microcontroller loses power, the contents of SRAM memory
are lost. It can be written to and read from during program execution. The ATmega16 is
equipped with 1000 bytes (actually 1120) of SRAM. A small portion (96 locations) of the
SRAM is set aside for the general-purpose registers used by the CPU and also for the I/O
and peripheral subsystems aboard the microcontroller. A complete ATmega16 register
listing and accompanying header file is provided in Appendices A and B, respectively.
During program execution, RAM is used to store global variables, support dynamic
memory allocation of variables, and provide a location for the stack (to be discussed
later).

1.2.4 Programmable Lock Bits


To provide for memory security from tampering, the ATmega16 is equipped with six
memory lock bits. These lock bits are programmed using the Atmel STK500 programming
board. The lock bits may be configured for the following options:

• No memory lock features enabled.


• No further programming of memory is allowed using parallel or serial
programming techniques.
• No further programming or verification of memory is allowed using parallel or
serial programming techniques.
6 ATMEL AVRMICROCONTROLLERPRIMER: PROGRAMMINGANDINTERFACING

1.3 PORT SYSTEM


The Atmel ATmega16 is equipped with four 8-bit general-purpose, digital I/O ports
designated PORTA, PORTB, PORTC, and PORTD. All of these ports also have alternate
functions, which will be described later. In this section, we concentrate on the basic
digital I/O port features.

As shown in Figure 1.2, each port has three registers associated with it:

• Data Register (PORTx)---used to write output data to the port,


FIGURE 1.2: ATmega16 port configuration registers: (a) port-associated registers and (b)
port pin configuration.

ATMEL AVR ARCHITECTUREOVERVIEW 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

DDRC=0xff; //set PORTC as output


PORTC=0x00; //initialize low
DDRD=0xff; //set PORTD as output
PORTD=0x00; //initialize low
}
8 ATMEL AVR MICROCONTROLLER PRIMER: PROGRAMMING AND INTERFACING

To read the value from a port pin configured as input, the following code could
be used. Note the variable used to read the value fromthe input pins is declared as an
unsigned char because both the port and this variable type are 8 bits wide.

unsigned char new_PORTB; //new values of PORTB

:
:
:

new_PORTB = PINB; //read PORTB

1.4 PERIPHERAL FEATURES---INTERNAL SUBSYSTEMS


In this section, we provide a brief overview of the peripheral features of the
ATmega16. It should be emphasized that these features are the internal subsystems
contained within the confines of the microcontroller chip. These built-in features allow
complex and sophisticated tasks to be accomplished by the microcontroller.

1.4.1 Time Base


The microcontroller is a complex synchronous state machine. It responds to
program steps in a sequential manner as dictated by a user-written program. The
microcontroller sequences through a predictable fetch--decode--execute sequence. Each
unique assembly language program instruction issues a series of signals to control the
microcontroller hardware to accomplish instruction related operations.

The speed at which a microcontroller sequences through these actions is


controlled by a precise time base called the clock. The clock source is routed throughout
the microcontroller to provide a time base for all peripheral subsystems. The ATmega16
may be clocked internally, using a user-selectable resistor capacitor (RC) time base, or
externally. The RC internal time base is selected using programmable fuse bits. We will
discuss how to do this in the application section of this chapter. You may choose an
internal fixed clock operating frequency of 1, 2, 4, or 8 MHz

To provide for a wider range of frequency selections, an external time source may
be used. The external time sources, in order of increasing accuracy and stability, are an
external RC network, a ceramic resonator, or a crystal oscillator. The system designer
chooses the time base frequency and clock source device appropriate for the application
at hand.

ATMEL AVR ARCHITECTURE OVERVIEW 9

1.4.2 Timing Subsystem


The ATmega16 is equipped with a complement of timers that allows the user to generate
a precision output signal, measure the characteristics (period, duty cycle, frequency) of
an incoming digital signal, or count external events. Specifically, the ATmega16 is
equipped with two 8-bit timer/counters and one 16-bit counter. We discuss the operation,
programming, and application of the timing systemin Chapter 5 of the text.

1.4.3 Pulse Width Modulation Channels


A pulse width modulated, or PWM, signal is characterized by a fixed frequency and a
varying duty cycle. Duty cycle is the percentage of time a repetitive signal is logic high
during the signal period. It may be formally expressed as

duty cycle (%) = (on time/period) × (100%).

The ATmega16 is equipped with four PWM channels. The PWM channels coupled
with the flexibility of dividing the time base down to different PWMsubsystemclock
source frequencies allows the user to generate a wide variety of PWMsignals, from
relatively high-frequency, low-duty cycle signals to relatively low-frequency, high-duty
cycle signals. PWMsignals are used in a wide variety of applications, including controlling
the position of a servo motor and controlling the speed of a DC motor. We discuss the
operation, programming, and application of the PWMsystem in Chapter 5 of the text.

1.4.4 Serial Communications


The ATmega16 is equipped with a host of different serial communication subsystems,
including the Universal Synchronous and Asynchronous Serial Receiver and Transmitter
(USART), the Serial Peripheral Interface (SPI), and the Two-Wire Serial Interface (TWI). What
all of these systems have in common is the serial transmission of data. In a serial
communications transmission scheme, data are sent a single bit at a time from
transmitter to receiver.

1.4.4.1 Serial USART.


The serial USART is used for full duplex (two-way) communication between a receiver and
transmitter. This is accomplished by equipping the ATmega16 with independent
hardware for the transmitter and receiver. The USART is typically used for asynchronous
communication. That is, there is not a common clock between the transmitter and
receiver to keep them synchronized with one another. To maintain synchronization
between the transmitter and receiver, framing start and stop bits are used at the
beginning and end of each data byte in a transmission sequence.

10 ATMEL AVRMICROCONTROLLERPRIMER: PROGRAMMINGANDINTERFACING

The ATmega16 USART is quite flexible. It has the capability to be set to a


variety of data transmission rates known as the baud (bits per second) rate. The
USART may also be set for data bit widths of 5 to 9 bits with one or two stop bits.
Furthermore, the ATmega16 is equipped with a hardware-generated parity bit
(even or odd) and parity check hardware at the receiver. A single parity bit allows
for the detection of a single bit error within a byte of data. The USART may also
be configured to operate in a synchronous mode. We discuss the operation,
programming, and application of the USART in Chapter 2 of the text.

1.4.4.2 Serial Peripheral Interface. The ATmega16 SPI can also be used for two-
way serial communication between a transmitter and a receiver. In the SPI system,
the transmitter and receiver share a common clock source. This requires an
additional clock line between the transmitter and receiver but allows for higher
data transmission rates as compared with the USART.

The SPI may be viewed as a synchronous 16-bit shift register with an 8-bit
half residing in the transmitter and the other 8-bit half residing in the receiver.
The transmitter is designated the master because it provides the synchronizing
clock source between the transmitter and the receiver. The receiver is designated
as the slave. We discuss the operation, programming, and application of the SPI
in Chapter 2 of the text.

1.4.4.3 Two-Wire Serial Interface. The TWI subsystem allows the system
designer to network a number of related devices (microcontrollers, transducers,
displays, memory storage, etc.) together into a system using a two-wire
interconnecting scheme. The TWI allows a maximum of 128 devices to be
connected together. Each device has its own unique address and may both
transmit and receive over the two-wire bus at frequencies up to 400 kHz. This
allows the device to freely exchange information with other devices in the
network within a small area.

1.4.5 Analog-to-Digital Converter

The ATmega16 is equipped with an eight-channel ADC subsystem. The ADC


converts an analog signal from the outside world into a binary representation
suitable for use by the microcontroller. The ATmega16 ADC has 10-bit resolution.
This means that an analog voltage between 0 and 5 V will be encoded into one of
1024 binary representations between (000) 16 and (3FF) .This provides the
ATmega16 with a voltage resolution of approximately 4.88 mV. We discuss the
operation, programming, and application of the ADC in Chapter 3 of the text.
ATMEL AVR ARCHITECTUREOVERVIEW 11

1.4.6 Interrupts
The normal execution of a program step follows a designated sequence of instructions.
However, sometimes, this normal sequence of events must be interrupted to respond to
high-priority faults and status both inside and outside the microcontroller. When these
higher-priority events occur, the microcontroller must temporarily suspend normal
operation and execute event specific actions called an interrupt service routine. Once the
higher priority event has been serviced, the microcontroller returns and continues
processing the normal program.

The ATmega16 is equipped with a complement of 21 interrupt sources. Three of


the interrupts are provided for external interrupt sources, whereas the remaining 19
interrupts support the efficient operation of peripheral subsystems aboard the
microcontroller. We discuss the operation, programming, and application of the interrupt
system in Chapter 4 of the text.

1.5 PHYSICAL ANDOPERATINGPARAMETERS


In this section, we provide data on the physical layout and operating parameters of the
ATmega16 microcontroller. As a system designer, it is important to know the various
physical and operating parameter options available to select the best option for a given
application.

1.5.1 Packaging
The ATmega16 comes in three different packaging styles: a 40-pin plastic dual in-line
package (DIP), a 44-lead thin quad flat pack package, and a 44-pad quad flat
nonlead/microlead frame package. The Pinout Diagram for the different packaging
options are provided in Figure 1.3.

1.5.2 Power Consumption


The ATmega16 is available at two different operating voltage ranges. The ATmega16L
operates at supply voltages from 2.7 to 5.5 VDC, whereas the ATmega16 operates at
supply voltages from 4.5 to 5.5 VDC. In the application examples that follow, we will use
a standard laboratory 5-VDC power supply and also discuss a method of providing a 5-
VDC supply using an off-the-shelf 9-VDC battery.

The current draw for the microcontroller is quite low. For example, when the
ATmega16L is actively operating at 3 MHz from a 3-VDC power source, the current draw
is 1.1 mA. When placed in the idle mode, the microcontroller current draw reduces to less
than 0.35 mA. Finally, in the power-down mode, the microcontroller will draw less than
1 µA of current from the voltage source.
To minimize power consumption, the microcontroller can be placed into various
low current sleep modes. There are six different sleep modes available to the system
designer. The

12 ATMEL AVRMICROCONTROLLERPRIMER: PROGRAMMINGANDINTERFACING


FIGURE 1.3: Atmel AVR ATmega16 Pinout Diagram: (a) 40-pin plastic DIP and (b) thin quad
flat pack/microlead frame. Figure used with permission of Atmel.

ATMEL AVR ARCHITECTURE OVERVIEW 1

Microcontroller is placed in sleep mode using the SLEEP command and


‘‘wakened’’ from SLEEP when an interrupt occurs. Additionally, power
consumption can be further reduced by operating the microcontroller at the
lowest practical clock frequency for a given application.

1.5.3 Speed Grades


The ATmega16 is available at two different speed grades. The ATmega16L
operates from 0 to 8 MHz, whereas the ATmega16 operates from 0 to 16 MHz. As
previously discussed, the operating speed of the microcontroller is set by the
time base chosen for the processor. One might believe that faster microcontroller
operation is always better. This is not the case. The system designer must
determine the minimum practical speed of microcontroller operation for a given
application. The microcontroller’s power consumption is directly related to
operating speed. That is, the faster the operating speed of the microcontroller,
the higher its power consumption. This becomes especially critical in portable,
battery-operated embedded systems applications.

That completes our brief introduction to the features of the ATmega16. In


the next section, we apply what we have learned in developing a testbench for
the ATmega16.

1.6 APPLICATION: ATmega16 TESTBENCH

In the application sections throughout the text, we use a running example---an


ATmega16 Testbench. The purpose of the Testbench is to illustrate the operation
of selected ATmega16 subsystems working with various I/O devices. Most
importantly, the Testbench will serve as a template to develop your own
applications.
In this chapter, we provide the hardware configuration of a barebones
testbench and a basic software framework to get the system up and operating.
We will connect eight debounced tact switches to PORTB and an eight-channel
tristate light-emitting diode (LED) array to PORTA. The software will check for a
status change on PORTB. When the user depresses one of the tact switches, the
ATmega16 will detect the status change and the corresponding LED on PORTA
will transition from red to green.

1.6.1 Hardware Configuration


Provided in Figure 1.4 is the basic hardware configuration for the Testbench. We
will discuss in detail the operation of the I/O devices in Chapter 6.
PORTA is configured with eight tact (momentary) switches with
accompanying debouncing hardware. We discuss the debounce circuit in detail
in Chapter 6. PORTB is equipped with an eight-channel tristate LED indicator. For
a given port pin, the green LED will illuminate for a logic high, the red LED for a
logic low, and no LEDs for a tristate high-impedance state. We discuss this circuit
in detail in Chapter 6.
14 ATMEL AVRMICROCONTROLLERPRIMER: PROGRAMMINGANDINTERFACING
FIGURE 1.4: ATmega16 Testbench hardware.

ATMEL AVR ARCHITECTUREOVERVIEW 15

Aside from the input hardware on PORTB and the output display hardware on PORTA of
the controller, there are power (pins 10, 30, and 32) and ground (pins 11 and 31)
connections. A standard 5-VDC power supply may be used for the power connections. For
portable applications, a 9-VDC battery equipped with a 5-VDC regulator (LM340-05 or
uA7805) may be used as a power source. Pins 9 through 11 have a resistor (1 M), two
capacitors (1.0 µF), and a tact switch configured to provide a reset switch for the
microcontroller. We use a ZTT 10-MHz ceramic resonator as the time base for the
Testbench. It is connected to pins 12 (XTAL2) and 13 (XTAL1) of the ATmega16.

1.6.2 Software Configuration


The Testbench software is provided below. The program contains the following sections:
• Comments
• Include Files: We have included the Image C raft ICC AVR include file for the
ATmega16
• (iom16v.h). This file provides the software link between the names of the
ATmega16
• hardware registers and the actual hardware locations. When a register is used by
name in
• the program, reference is made to the contents of that register.
• Function Prototypes
• Global Variables
• Main Program: We begin the main program by calling the function to initialize the
ports and then enter a continuous loop. Within the loop body, the ATmega16
monitors for a status change on PORT B. When the user depresses one of the tact
switches connected to PORT B, a change of status is detected and the appropriate
LED is illuminated on PORT A.
• Function Definition

//*************************************************************
//file name: testbench.c
//function: provides test bench for ATMEL AVR ATmega16 controller
//target controller: ATMEL ATmega16
//
//ATMEL AVR ATmega16 Controller Pin Assignments
//Chip Port Function I/O Source/Dest Asserted Notes
//Pin 1 PB0 to active high RC debounced switch
//Pin 2 PB1 to active high RC debounced switch
//Pin 3 PB2 to active high RC debounced switch
16 ATMEL AVRMICROCONTROLLERPRIMER: PROGRAMMINGANDINTERFACING

//Pin 4 PB3 to active high RC debounced switch


//Pin 5 PB4 to active high RC debounced switch
//Pin 6 PB5 to active high RC debounced switch
//Pin 7 PB6 to active high RC debounced switch
//Pin 8 PB7 to active high RC debounced switch
//Pin 9 Reset
//Pin 10 VDD
//Pin 11 Gnd
//Pin 12 Resonator
//Pin 13 Resonator
//Pin 14 PD0 to tristate LED indicator
//Pin 15 PD1 to tristate LED indicator
//Pin 16 PD2 to tristate LED indicator
//Pin 17 PD3 to tristate LED indicator
//Pin 18 PD4 to tristate LED indicator
//Pin 19 PD5 to tristate LED indicator
//Pin 20 PD6 to tristate LED indicator
//Pin 21 PD7 to tristate LED indicator
//Pin 22 PC0
//Pin 23 PC1
//Pin 24 PC2
//Pin 25 PC3
//Pin 26 PC4
//Pin 27 PC5
//Pin 28 PC6
//Pin 29 PC7
//Pin 30 AVcc to VDD
//Pin 31 AGnd to Ground
//Pin 32 ARef to Vcc
//Pin 33 PA7
//Pin 34 PA6
//Pin 35 PA5
//Pin 36 PA4
//Pin 37 PA3
//Pin 38 PA2
//Pin 39 PA1
ATMEL AVR ARCHITECTURE OVERVIEW 17

//Pin 40 PA0
//
//author: Steven Barrett and Daniel Pack
//created: July 12, 2007
//last revised: July 12, 2007
//***************************************************************

//include files**************************************************
#include<iom16v.h> //ImageCraft ICC AVR
//include file
//for ATmega16
//function prototypes********************************************

void initialize_ports(void); //initializes ports

//main program***************************************************
//global variables
unsigned char old_PORTB = 0x00; //present value of PORTB
unsigned char new_PORTB; //new values of PORTB
void main(void)
{
initialize_ports(); //initialize ports
while(1){//main loop
new_PORTB = PINB; //read PORTB

if(new_PORTB != old_PORTB){ //process change


//in PORTB input
switch(new_PORTB){ //PORTB asserted high

case 0x01: //PB0 (0000_0001)


PORTD=0x00; //turn off all LEDs PORTD
PORTD=0x01; //turn on PD0 LED (0000_0001)
break;

case 0x02: //PB1 (0000_0010)


PORTD=0x00; //turn off all LEDs PORTD
18 ATMEL AVRMICROCONTROLLERPRIMER: PROGRAMMINGANDINTERFACING

PORTD=0x02; //turn on PD1 LED (0000_0010)


break;

case 0x04: //PB2 (0000_0100)


PORTD=0x00; //turn off all LEDs PORTD
PORTD=0x04; //turn on PD2 LED (0000_0100)
break;

case 0x08: //PB3 (0000_1000)


PORTD=0x00; //turn off all LEDs PORTD
PORTD=0x08; //turn on PD3 LED (0000_1000)
break;

case 0x10: //PB4 (0001_0000)


PORTD=0x00; //turn off all LEDs PORTD
PORTD=0x10; //turn on PD4 LED (0001_0000)
break;

case 0x20: //PB5 (0010_0000)


PORTD=0x00; //turn off all LEDs PORTD
PORTD=0x20; //turn on PD5 LED (0010_0000)
break;

case 0x40: //PB6 (0100_0000)


PORTD=0x00; //turn off all LEDs PORTD
PORTD=0x40; //turn on PD6 LED (0100_0000)
break;

case 0x80: //PB7 (1000_0000)


PORTD=0x00; //turn off all LEDs PORTD
PORTD=0x80; //turn on PD7 LED (1000_0000)
break;

default:; //all other cases


} //end switch(new_PORTB)
} //end if new_PORTB
ATMEL AVR ARCHITECTUREOVERVIEW 19

old_PORTB=new_PORTB; //update PORTB


} //end while(1)
} //end main
//***************************************************************
//function definitions
//***************************************************************

//***************************************************************
//initialize_ports: provides initial configuration for I/O ports
//***************************************************************

void initialize_ports(void)
{
DDRA=0xff; //set PORTA[7:0] as output
PORTA=0x00; //initialize PORTA[7:0] low

DDRB=0x00; //PORTB[7:0] as input


PORTB=0x00; //disable PORTB
//pull-up resistors

DDRC=0xff; //set PORTC as output


PORTC=0x00; //initialize low
DDRD=0xff; //set PORTD as output
PORTD=0x00; //initialize low
}
//***************************************************************

1.7 PROGRAMMINGTHE ATmega16

Programming the ATmega16 requires several hardware and software tools. We


briefly mention required components here. Please refer to the manufacturer’s
documentation for additional details.

Software tools: Throughout the text, we use the ImageCraft ICC AVR
compiler. This is an excellent, user-friendly compiler. There are other excellent
compilers available. The compiler is used to translate the source file (testbench.c)
into machine language for loading into the ATmega16. We use Atmel’s AVR Studio
to load the machine code into the ATmega16.

20 ATMEL AVR MICROCONTROLLER PRIMER: PROGRAMMING AND INTERFACING

Hardware tools: We use Atmel’s STK500 AVR Flash MCU Starter Kit
(STK500) for programming the ATmega16. The STK500 provides the interface
hardware between the host PC and the ATmega16 for machine code loading. The
STK500 is equipped with a complement of DIP sockets, which allows for
programming all of the microcontrollers in the Atmel AVR line.

1.7.1 Programming Procedure

In this section, we provide a step-by-step procedure to program the ATmega16


DIP using the STK500 AVR Flash MCU Starter Kit. Please refer to Figure 1.5. It
should be noted that the STK500 will be used to program the microcontroller,
which will then be placed in the Testbench circuit.

1. Load AVR Studio (free download from www.atmel.com)


2. Ensure that the STK500 is powered down.
3. Connect the STK500 as shown in Figure 1.5.
4. Insert the ATmega16 into the red 40-pin socket. Note the location of pin 1
in Figure 1.5.
5. Power up the STK500.
6. Start up AVR Studio on your PC.
7. Pop-up window ‘‘Welcome to AVR Studio’’ should appear. Close this
window by clicking on the ‘‘Cancel button.’’
8. Click on the ‘‘AVR icon.’’ It looks like the silhouette of an integrated circuit.
It is on the second line of the toolbar about halfway across the screen.
9. This should bring up a STK500 pop-up window with six tabs (Program,
Fuses, Lockbits, Advanced, Board, and Auto). At the bottom of the Program
tab window, verify that the STK500 was autodetected. Troubleshoot as
necessary to ensure STK500 was autodetected by AVR Studio.
10. Set all tab settings:
{ Program:
{ Select device: ATmega16
{ Programming mode: ISP, Erase Device Before Programming, Verify
Device after Programming
{ Flash: Input HEX file, Browse and find machine code file: your file
name.hex
{ EEPROM: Input HEX file, Browse and find machine code file: yourfile-
name.EEP
{ Fuses: Set the following fuses
{ Boot flash section size = 128
ATMEL AVR ARCHITECTUREOVERVIEW 21

FIGURE 1.5: Programming the ATmega16 with the STK500.


22 ATMEL AVRMICROCONTROLLERPRIMER: PROGRAMMINGANDINTERFACING

{ Brown out detection at Vcc = 4.0 V


{ External Crystal/Resonator High Frequency; start-up time 16K CK + 64 ms
{ Lock bits:
{ Mode 1
{ Application Protection Mode 1
{ Boot Loader Protection Mode 1
{ Advanced: N/A
{ Board: N/A
{ Auto:
{ Erase Device
{ Program Flash
{ Verify Flash
{ Program Fuses
{ Verify Fuses
{ Read Fuses
11. Programming step:
{ Program Tab: click program
{ Fuse Tab: click program (do not forget this step---otherwise, processor runs
very slow!)
12. Power down the STK500. Remove the programmed chip from the STK500 board
and place it in the Testbench circuit.

1.8 SOFTWARE PORTABILITY

The software techniques discussed in the textbook are based on the ATmega16;
however, the developed software may be easily ported for use with other Atmel
AVR microcontrollers. To ease the transition to another microcontroller, it is
suggested using a direct bit assignment technique. Instead of setting an entire
register content at once, selected bits may be set. The individual bit definitions
for the ATmega16 are provided in Appendix B.

For example, to set the UCSRB register, the following individual bit
assignments may be used:

UCSRB = (1<<TXEN)|(1<<RXEN); //Enable transmit and receive

as opposed to:

UCSRB = 0x08; //Enable transmit and receive


ATMEL AVR ARCHITECTURE OVERVIEW 23

When transporting code, the header file for the specific microcontroller must be
used, and also, the interrupt vector numbers may require change.

1.9 SUMMARY

In this chapter, we provided a brief overview of the ATmega16 microcontroller,


a representative sample of the AVR microcontrollers. Information presented in
this chapter can be readily applied to other microcontrollers in the AVR line. We
then provided the Testbench hardware and software that we use throughout the
text to illustrate peripheral subsystemoperation aboard the ATmega16. In
upcoming chapters, we provide additional details on selected ATmega16
subsystems.

1.10 REFERENCES ANDFURTHER READING

1. S Barrett and D Pack, Microcontroller Fundamentals for Engineers and


Scientists,Morgan& Claypool, San Rafael, CA, 2006.
doi:10.2200/S00025ED1V01Y200605DCS001
2. Atmel 8-bit AVR Microcontroller with 16K Bytes In-SystemProgrammable
Flash, ATmega16, ATmega16L, data sheet: 2466L-AVR-06/05, Atmel, San
Jose, CA.
3. J Hennessy and D Patterson, Computer Architecture: A Quantitative
Approach,3rded,Morgan Kaufman, San Francisco, CA, 2003.

1.11 CHAPTER PROBLEMS

1. Question : What is a RISC processor?


2. Question : How does the ATmega16 interact with the external world?
3. Question : What are the different methods of applying a clock source to the
ATmega16? List the inherent advantages of each type.
4. Question : Describe the three different types of memory components aboard
the ATmega16. What is each used for?
5. Question : Describe the three registers associated with each port.
6. Question : With a specific port, can some port pins be declared as output pins
while others as input pins?
7. Question : Describe the serial communication features aboard the ATmega16.
Provide a suggested application for each.
8. Question : What is the purpose of the ADC system?
9. Question : What is the purpose of the interrupt system?
10. Question : What is the purpose of the PWM system?
11. Question : What is the best clock speed to operate the ATmega16 at for a
specific application?
12. Question : Sketch a flow chart or UML activity diagramfor the testbench.c
program.
CHAPTER 2

Serial Communication Subsystem


Objectives: After reading this chapter, the reader should be able to

• describe the differences between serial and parallel communication,


• provide definitions for key serial communications terminology,
• describe the operation of the USART,
• program the USART for basic transmission and reception,
• describe the operation of the SPI,
• program the SPI for basic transmission and reception, and
• describe the purpose of the two-wire interface (TWI).

2.1 SERIAL COMMUNICATIONS

Microcontrollers must often exchange data with other microcontrollers or


peripheral devices. Data may be exchanged by using parallel or serial techniques.
With parallel techniques, an entire byte of data is typically sent simultaneously
from the transmitting device to the receiver device. Although this is efficient
from a time point of view, it requires eight separate lines for the data transfer
[1].

In serial transmission, a byte of data is sent a single bit at a time. Once 8 bits
have been received at the receiver, the data byte is reconstructed. Although this
is inefficient froma time point of view, it only requires a line (or two) to transmit
the data.

The ATmega16 is equipped with a host of different serial communication


subsystems, including the serial USART, SPI, and TWI. What all of these systems
have in common is the serial transmission of data. Before discussing the different
serial communication features aboard the ATmega16, we review serial
communication terminology.

2.2 SERIAL COMMUNICATIONTERMINOLOGY

In this section, we review common terminology associated with serial


communication.
26 ATMEL AVRMICROCONTROLLERPRIMER: PROGRAMMINGANDINTERFACING

2.2.1 Asynchronous versus Synchronous Serial Transmission


In serial communications, the transmitting and receiving device must be
synchronized to one another and use a common data rate and protocol.
Synchronization allows both the transmitter and receiver to be expecting data
transmission/reception at the same time. There are two basic methods of
maintaining ‘‘sync’’ between the transmitter and receiver: asynchronous and
synchronous.

In an asynchronous serial communication system, such as the USART


aboard the ATmega16, framing bits are used at the beginning and end of a data
byte. These framing bits alert the receiver that an incoming data byte has arrived
and also signals the completion of the data byte reception. The data rate for an
asynchronous serial system is typically much slower than the synchronous
system, but it only requires a single wire between the transmitter and receiver.

A synchronous serial communication system maintains ‘‘sync’’ between


the transmitter and receiver by employing a common clock between the two
devices. Data bits are sent and received on the edge of the clock. This allows data
transfer rates higher than with asynchronous techniques but requires two lines,
data and clock, to connect the receiver and transmitter.

2.2.2 Baud Rate

Data transmission rates are typically specified as a baud or bits per second rate.
For example, 9600 baud indicates data are being transferred at 9600 bits per
second.

2.2.3 Full Duplex

Often, serial communication systems must both transmit and receive data. To do
both transmissionand reception simultaneously requires separate hardware for
transmission and reception. A single duplex system has a single complement of
hardware that must be switched from transmission to reception configuration. A
full duplex serial communication system has separate hardware for transmission
and reception.

2.2.4 Nonreturn to Zero Coding Format

There are many different coding standards used within serial communications.
The important point is the transmitter and receiver must use a common coding
standard so data may be interpreted correctly at the receiving end. The Atmel
ATmega16 [2] uses a nonreturn to zero coding standard. In nonreturn to zero,
coding a logic 1 is signaled by a logic high during the entire time slot allocated
for a single bit, whereas a logic 0 is signaled by a logic low during the entire time
slot allocated for
a single bit.
SERIAL COMMUNICATIONSUBSYSTEM 27

2.2.5 The RS-232 Communication Protocol

When serial transmission occurs over a long distance, additional


techniques may be used to ensure data integrity. Over long distances, logic levels
degrade and may be corrupted by noise. At the receiving end, it is difficult to
discern a logic high from a logic low. The RS-232 standard has been around for
some time. With the RS-232 standard (EIA-232), a logic 1 is represented with a -
12-VDC level, whereas a logic 0 is represented by a +12-VDC level. Chips are
commonly available (e.g., MAX232) that convert the 5- and 0-V output levels from
a transmitter to RS-232- compatible levels and convert back to 5- and 0-V levels
at the receiver. The RS-232 standard also specifies other features for this
communication protocol.

2.2.6 Parity
To further enhance data integrity during transmission, parity techniques
may be used. Parity is an additional bit (or bits) that may be transmitted with the
data byte. The ATmega16 uses a single parity bit. With a single parity bit, a single-
bit error may be detected. Parity may be even or odd. In even parity, the parity
bit is set to 1 or 0, such that the number of 1’s in the data byte including the
parity bit is even. In odd parity, the parity bit is set to 1 or 0, such that the number
of 1’s in the data byte including the parity bit is odd. At the receiver, the number
of bits within a data byte including the parity bit are counted to ensure that parity
has not changed, indicating an error, during transmission.

2.2.7 American Standard Code for Information Interchange


The American Standard Code for Information Interchange (ASCII) is a
standardized seven-bit method of encoding alphanumeric data. It has been in use
for many decades, so some of the characters and actions listed in the ASCII table
are not in common use today. However, ASCII is still the most common method
of encoding alphanumeric data. The ASCII code is provided in Figure 2.1. For
example, the capital letter ‘‘G’’ is encoded in ASCII as 0x47. The ‘‘0x’’ symbol
indicates the hexadecimal number representation. Unicode is the international
counterpart of ASCII. It provides standardized 16-bit encoding format for the
written languages of the world. ASCII is a subset of Unicode. The interested reader
is referred to the Unicode home page website at www.unicode.org for additional
information on this standardized encoding format.

2.3 SERIAL USART


The serial USART provide for full duplex (two-way) communication between a
receiver and transmitter. This is accomplished by equipping the ATmega16 with
independent hardware for the transmitter and receiver. The USART is typically
used for asynchronous communication. That is, there is not a common clock
between the transmitter and receiver to keep them synchronized with
28 ATMEL AVR MICROCONTROLLER PRIMER: PROGRAMMING AND INTERFACING

FIGURE 2.1: ASCII Code. The ASCII code is used to encode alphanumeric
characters. The ‘‘0x’’ indicates hexadecimal notation in the C programming
language.

one another. To maintain synchronization between the transmitter and receiver,


framing start and stop bits are used at the beginning and end of each data byte
in a transmission sequence. The Atmel USART also has synchronous features.
Space does not permit a discussion of these USART enhancements.

The ATmega16 USART is quite flexible. It has the capability to be set to a variety
of data transmission or baud (bits per second) rates. The USART may also be set
for data bit widths of 5 to 9 bits with one or two stop bits. Furthermore, the
ATmega16 is equipped with a hardware-generated parity bit (even or odd) and
parity check hardware at the receiver. A single parity bit allows for the detection
of a single bit error within a byte of data. The USART may also be configured to
operate in a synchronous mode. We now discuss the operation, programming,
and application of the USART. Because of space limitations, we cover only the
most basic capability of this flexible and powerful serial communication system.
SERIAL COMMUNICATIONSUBSYSTEM 29

FIGURE 2.2: Atmel AVR ATmega16 USART block diagram. Figure used with
permission of Atmel.

2.3.1 SystemOverview
The block diagram for the USART is provided in Figure 2.2. The block diagram
may appear a bit overwhelming, but realize there are four basic pieces to the
diagram: the clock generator, the transmission hardware, the receiver hardware,
and three control registers (UCSRA, UCSBR, and UCSRC). We discuss each in turn.

2.3.1.1 USART Clock Generator. The USART Clock Generator provides the clock
source for the USART system and sets the baud rate for the USART. The baud rate
is derived from the
30 ATMEL AVRMICROCONTROLLERPRIMER: PROGRAMMINGANDINTERFACING

overall microcontroller clock source. The overall system clock is divided by the USART
baud rate registers UBRR[H:L] and several additional dividers to set the baud rate. For the
asynchronous normal mode (U2X bit = 0), the baud rate is determined using the following
expression:

baud rate=(system clock frequency)/(2(UBRR + 1)),

where UBRR is the content of the UBRRH and UBRRL registers (0--4095). Solving for UBRR
yields

UBRR=((system clock generator)/(16 × baud rate))- 1

2.3.1.2 USART Transmitter. The USART transmitter consists of a Transmit Shift Register.
The data to be transmitted are loaded into the Transmit Shift Register via the USART I/O
Data Register (UDR). The start and stop framing bits are automatically appended to the
data within the Transmit Shift Register. The parity is automatically calculated and
appended to the Transmit Shift Register. Data are then shifted out of the Transmit Shift
Register via the TxD pin a single bit at a time at the established baud rate. The USART
transmitter is equipped with two status flags: the USART Data Register Empty (UDRE) and
the transmit complete (TXC) flags. The UDRE flag sets when the transmit buffer is empty,
indicating it is ready to receive new data. This bit should be written to a zero when writing
the USART Control and Status Register A (UCSRA). The UDRE bit is cleared by writing to
the UDR. The TXC flag bit is set to logic 1 when the entire frame in the Transmit Shift
Register has been shifted out and there are no new data currently present in the transmit
buffer. The TXC bit may be reset by writing a logic 1 to it.

2.3.1.3 USARTReceiver. The USART Receiver is virtually identical to the USART


Transmitter except for the direction of the data flow, which is reversed. Data are received
a single bit at a time via the RxD pin at the established baud rate. The USART receiver is
equipped with the receive complete (RXC) flag. The RXC flag is logic 1 when unread data
exist in the receive buffer.

2.3.1.4 USART Registers. In this section, we discuss the register settings for controlling
the USART system. We have already discussed the function of the UDR and the USART
baud rate registers (UBRRH and UBRRL). Note: The USART Control and Status Register C
(UCSRC) and the USART baud rate register high (UBRRH) are assigned to the same I/O
location in the memory map (Figure 2.3). The URSEL bit (bit 7 of both registers)
determines which register
SERIAL COMMUNICATIONSUBSYSTEM 31

FIGURE 2.3: USART registers.

is being accessed. The URSEL bit must be 1 when writing to the UCSRC register
and 0 when writing to the UBRRH register. UCSRA. This contains the RXC, TXC,
and the UDRE bits. The function of these bits has already been discussed. UCSRB.
This contains the receiver and transmitter enable bits (RXEN and TXEN,
respectively). These bits are the ‘‘on/off’’ switch for the receiver and transmitter,
respectively. The UCSRB register also contains the UCSZ2 bit. The UCSZ2 bit in
the UCSRB register and the UCSZ[1:0] bits contained in the UCSRC register
together set the data character size.
32 ATMEL AVRMICROCONTROLLERPRIMER: PROGRAMMINGANDINTERFACING

UCSRC. This allows the user to customize the data features to the application at
hand. It should be emphasized that both the transmitter and receiver be
configured with the same data features for proper data transmission. The UCSRC
contains the following bits:

• USART mode select (UMSEL): 0, asynchronous operation; 1, synchronous


operation
• USART parity mode (UPM[1:0]): 00, no parity; 10, even parity; 11, odd parity
• USART stop bit select (USBS): 0, one stop bit; 1, two stop bits
• USART character size (data width) (UCSZ[2:0]): 000, 5 bits; 001, 6 bits; 010;
7 bits; 011, 8 bits; 111, 9 bits

2.3.2 SystemOperation and Programming
The basic activities of the USART system consist of initialization, transmission,
and reception. These activities are summarized in Figure 2.4. Both the transmitter
and receiver must be initialized with the same communication parameters for
proper data transmission. The transmission and reception activities are similar
except for the direction of data flow. In transmission, we monitor for the UDRE
flag to set, indicating the data register is empty. We then load the data for
transmission into the UDR register. For reception, we monitor for the RXC bit to
set, indicating there are unread data in the UDR register. We then retrieve the
data from the UDR register.

FIGURE 2.4: USART activities.


SERIAL COMMUNICATIONSUBSYSTEM 33

To program the USART, we implement the flow diagrams provided in


Figure 2.4.Inthe sample code provided, we assume the ATmega16 is operating at
10 MHz and we desire a baud rate of 9600, asynchronous operation, no parity,
one stop bit, and eight data bits.

To achieve 9600 baud with an operating frequency of 10 MHz requires that


we set the UBRR registers to 64, which is 0x40.

/*************************************************************
//USART_init: initializes the USART system
//************************************************************
void USART_init(void)

{
UCSRA = 0x00; //control register initialization
UCSRB = 0x08; //enable transmitter
UCSRC = 0x86; //async, no parity, 1 stop bit,

//8 data bits


//Baud Rate initialization

UBRRH = 0x00;
UBRRL = 0x40;
}

//************************************************************
//USART_transmit: transmits single byte of data
//************************************************************

void USART_transmit(unsigned char data)


{
while((UCSRA & 0x20)==0x00) //wait for UDRE flag
{
;
}
UDR = data; //load data to UDR for transmission
}
34 ATMEL AVRMICROCONTROLLERPRIMER: PROGRAMMINGANDINTERFACING

//************************************************************
//USART_receive: receives single byte of data
//************************************************************

unsigned char USART_receive(void)


{
while((UCSRA & 0x80)==0x00) //wait for RXC flag
{
;
}
data = UDR; //retrieve data from UDR
return data;
}

//************************************************************

2.3.3 Serial Peripheral Interface

The ATmega16 SPI also provides for two-way serial communication between a
transmitter and a receiver. In the SPI system, the transmitter and receiver share
a common clock source. This requires an additional clock line between the
transmitter and receiver but allows for higher data transmission rates as
compared with the USART. The SPI system allows for fast and efficient data
exchange between microcontrollers or peripheral devices. There are many SPI-
compatible external systems available to extend the features of the
microcontroller. For example, a liquid crystal display (LCD) or a digital-to-analog
converter (DAC) could be added to the microcontroller using the SPI system.

2.3.3.1 SPI Operation. The SPI maybe viewed as asynchronous 16- bit shift
register with an 8-bit half residing in the transmitter and the other 8-bit half
residing in the receiver as shown in Figure 2.5. The transmitter is designated the
master because it provides the synchronizing clock source between the
transmitter and the receiver. The receiver is designated as the slave. A slave is
chosen for reception by taking its slave select ( SS) line low. When the SS line is
taken low, the slave’s shifting capability is enabled. SPI transmission is initiated
by loading a data byte into the master configured SPI Data Register (SPDR). At that
time, the SPI clock generator provides clock pulses to the master and also to the
slave via the SCK pin. A single bit is shifted out of the master designated shift
register on the Master Out Slave In (MOSI) microcontroller pin on every
SERIAL COMMUNICATIONSUBSYSTEM 35

FIGURE 2.5: SPI overview.

SCK pulse. The data are received at the MOSI pin of the slave designated device.
At the same time, a single bit is shifted out of the Master In Slave Out (MISO) pin
of the slave device and into the MISO pin of the master device. After eight master
SCK clock pulses, a byte of data has been exchanged between the master and
slave designated SPI devices. Completion of data transmission in the master and
data reception in the slave is signaled by the SPI Interrupt Flag (SPIF) in both
devices. The SPIF flag is located in the SPI Status Register (SPSR) of each device.
At that time, another data byte may be transmitted.

2.3.3.2 Registers. The registers for the SPI system are provided in Figure 2.6. We
will discuss each one in turn. SPI Control Register. The SPI Control Register (SPCR)
contains the ‘‘on/off’’ switch for the SPI system. It also provides the flexibility
for the SPI to be connected to a wide variety of devices with different data
formats. It is important that both the SPI master and slave devices be configured
for compatible data formats for proper data transmission. The SPCR contains the
following bits:

• SPI Enable (SPE) is the ‘‘on/off’’ switch for the SPI system. A logic 1 turns
the systemon and logic 0 turns it off.
36 ATMEL AVRMICROCONTROLLERPRIMER: PROGRAMMINGANDINTERFACING

FIGURE 2.6: SPI registers.

• Data Order (DORD) allows the direction of shift from master to slave to be
controlled. When the DORD bit is set to 1, the least significant bit (LSB) of
the SPDR is transmitted first. When the DORD bit is set to 0, the Most
Significant Bit (MSB) of the SPDR is transmitted first. • The Master/Slave
Select (MSTR) bit determines if the SPI system will serve as a master (logic
1) or slave (logic 0).
• The Clock Polarity (CPOL) bit allows determines the idle condition of the
SCK pin. When CPOL is 1, SCK will idle logic high, whereas when CPOL is 0,
SCK will idle logic 0.
• TheClockPhase(CPHA)determinesifthedatabitwillbesampledontheleading(
0) or trailing (1) edge of the SCK.
• The SPI SCK is derived from the microcontroller’s systemclock source. The
systemclock is divided down to form the SPI SCK. The SPI Clock Rate Select
(SPR[1:0]) bits and the Double SPI Speed (SPI2X) bit are used to set the
division factor. The following divisions may be selected using SPI2X, SPR1,
and SPR0:

{ 000: SCK = systemclock/4


{ 001: SCK = systemclock/16
{ 010: SCK = systemclock/64
{ 011: SCK = systemclock/1284
{ 100: SCK = systemclock/2
{ 101: SCK = systemclock/8
{ 110: SCK = systemclock/32
{ 111: SCK = systemclock/64
SERIAL COMMUNICATIONSUBSYSTEM 37

SPI Status Register. This contains the SPIF. The flag sets when eight data
bits have been transferred from the master to the slave. The SPIF bit is cleared by
first reading the SPSR after the SPIF flag has been set and then reading the SPDR.
The SPSR also contains the SPI2X bit used to set the SCK frequency.

SPI Data Register. As previously mentioned, writing a data byte to the SPDR
initiates SPI transmission.

2.3.3.3 Programming. To program the SPI system, the system must first be
initialized with the desired data format. Data transmission may then commence.
Functions for initialization, transmission, and reception are provided below. In
this specific example, we divide the clock oscillator frequency by 128 to set the
SCK clock frequency.

//************************************************************
//spi_init: initializes spi system
//************************************************************

void spi_init(unsigned char control)


{
DDRB = 0xA0; //Set SCK (PB7), MOSI (PB5) for output,
others to input
//Configure SPI Control Register (SPCR)
SPCR = 0x53; //SPIE:0,SPE:1,DORD:0,MSTR:1,CPOL:0,CPHA:0,
SPR:1,SPR0:1
}

//************************************************************
//spi_write: Used by SPI master to transmit a data byte
//************************************************************

void spi_write(unsigned char byte)


{
38 ATMEL AVRMICROCONTROLLERPRIMER: PROGRAMMINGANDINTERFACING

SPDR = byte;
while (!(SPSR & 0x80));
}

//************************************************************
//spi_read: Used by SPI slave to receive data byte
//************************************************************

unsigned char spi_read(void)


{
while (!(SPSR & 0x80));

return SPDR;
}

//************************************************************

2.4 TWO-WIRE SERIAL INTERFACE

The TWI subsystem allows the system designer to network a number of related
devices (microcontrollers, transducers, displays, memory storage, etc.) together
into a system using a two-wire interconnecting scheme. The TWI allows a
maximum of 128 devices to be connected together. Each device has its own
unique address and may both transmit and receive over the two-wire bus at
frequencies up to 400 kHz. This allows the device to freely exchange information
with other devices in the network within a small area. Space does not permit a
detailed discussion of this advanced serial communication system.

2.5 SUMMARY
In this chapter, we have discussed the differences between parallel and serial
communications and key serial communication-related terminology. We then, in
turn, discussed the operation of USART, SPI, and TWI serial communication
systems. We also provided basic code examples to communicate with the USART
and SPI systems.

2.6 REFERENCES ANDFURTHER READING


1. S Barrett and D Pack, Microcontroller Fundamentals for Engineers and
Scientists,Morgan& Claypool, San Rafael, CA, 2006.
doi:10.2200/S00025ED1V01Y200605DCS001
2. Atmel 8-bit AVR Microcontroller with 16K Bytes In-System Programmable
Flash, ATmega16, ATmega16L*, data sheet: 2466L-AVR-06/05, Atmel, San
Jose, CA.

2.7 CHAPTER PROBLEMS


1. Question : Summarize the differences between parallel and serial
conversion.
2. Question : Summarize the differences among the USART, SPI, and TWI
methods of serial communication.
3. Question : Draw a block diagram of the USART system, label all key
registers, and all keys USART flags.
4. Question : Draw a block diagram of the SPI system, label all key
registers, and all keys USART flags.
5. Question : If an ATmega16 microcontroller is operating at 12 MHz, what
is the maximum transmission rate for the USART and the SPI?
6. Question : What is the ASCII encoded value for ‘‘Claypool’’?
7. Question : Draw the schematic of a system consisting of two ATmega16
that will exchange data via the SPI system. The system should include RS-
232 level shifting.
8. Question : Write the code to implement the system described in the
question above.
9. Question : Add USART and SPI features to the testbench.
CHAPTER 3

Analog-to-Digital Conversion

Objectives: After reading this chapter, the reader should be able to


• explain the difference between analog and digital signals,
• illustrate the ADC process,
• assess the quality of ADC using the metrics of sampling rate, quantization
levels, number
• of bits used for encoding, and dynamic range,
• design signal conditioning circuits to interface sensors with ADCs,
• describe the key registers used during an ATmega16 ADC,
• describe the steps to perform an ADC with the ATmega16, and
• program the ATmega16 to perform an ADC.

A microcontroller is used to process information from the natural world, decide


on a course of action based on the information collected, and then issue control
signals to implement the decision. Because much of the information from the
natural world is analog or continuous in nature and the microcontroller is a
digital or discrete-based processor, a method to convert an analog signal to
digital is required [1]. An ADC system performs this task, whereas a DAC
performs the conversion in the opposite direction. We will discuss both types of
converters in this chapter.

In the first section, we present the fundamental concepts associated with


the ADC process. In the following section, we discuss the conversion process
itself, followed by a presentation of different hardware implementations of the
process. Much of these early sections contain the same material you will find in
our text, Microcontroller Fundamentals for Engineers and Scientists.Wethen
review the basic features of the ATmega16 ADC system, followed by a system
description and a discussion of key ADC registers. We conclude our discussion
of the ADC with several illustrative code examples. We conclude the chapter with
a discussion of the DAC process.

3.1 BACKGROUNDTHEORY
Before we discuss the ADC process, we need to familiarize you with underlying
theories that support the process. We start with some definitions on analog and
digital signals.
42 ATMEL AVRMICROCONTROLLERPRIMER: PROGRAMMINGANDINTERFACING

3.1.1 Analog versus Digital Signals


A signal is a collection of values representing the state of a physical variable. The
collection can be as small as only one value or can have as many values as you
wish. In engineering, we usually arrange the values in order, for example, over
time or over a spatial axis, to display the information. The time and spatial
variables are called independent variables, because they are not affected by the
physical variables of interests. For example, we measure the temperature change
over time. The temperature measured is dependent on the time, not the other
way around. Figure 3.1 shows an altitude trajectory of a bird flying over a short
period. The signal shows how the altitude of the bird changes continuously.
Figure 3.2 shows a grayscale image of a six-legged robot. The image
captured the light intensities of the scene using a charge-coupled device camera.
If we move from the left to the right on the image and observe the intensity
changes, we can find vertical edges by detecting signal
ANALOG-TO-DIGITALCONVERSION 43

FIGURE 3.2: A photo of a walking robot.

intensity changes in the spatial axis. The same analysis can be performed as we
move from the top to the bottom of the image.

Analog signals are those whose physical variable values change


ontinuously over their independent variable. Most physical variables, your
speech, movement of stars, and the music you hear at a concert are analog
signals, signals that we find all around us. Digital signals, on the other hand, have
their physical variables defined only for discrete instances over their
independent variables. Although it may look continuous to human eyes, the
photo example shown in Figure 3.2 is a discrete signal because pixels that make
up a camera image cannot capture all space within the camera frame. The image
is only a finite composition of intensity values seen by a discrete number of
pixels.

Digital signals are important because all signals represented in digital


systems, computers, and microcontrollers are in digital forms. The important
task is how to faithfully represent analog
44 ATMEL AVRMICROCONTROLLERPRIMER: PROGRAMMINGANDINTERFACING

Signals using digital signals. For example, human voices must be converted
to corresponding digital signals before they can be routed by digital switching
circuits in telephone communication systems. Similarly, voice commands to
robots must be converted to a digital form before robots can understand the
command.
As shown in the examples above, we live in an analog world; that is,
physical variables are analog signals. It is precisely this reason why the ADC is
so very important in any digital systems that interact with an analog
environment.

3.1.2 Sampling, Quantization, and Encoding


In this subsection, we present three important processes associated with the
ADC. We first start with the subject of sampling. Imagine yourself as a
photographer in an Olympic diving stadium. Your job is taking a sequence of
pictures of divers jumping off from a diving board 10 meters above the surface
of the diving pool. Your goal is to put the sequence of pictures together to
reconstruct the motion of each diver. The sequence of pictures makes up samples
of divers’ motions. If a diver tries a complex motion and you want to faithfully
reconstruct his motion, you must take enough pictures from the start to the end
of the dive. If a diver makes a simple routine dive, you only need to take a few
pictures over the period of the dive. Two very different cases of motions
generated by a diver is shown in Figure 3.3. The same time sequence is used to
capture samples for both motions. As can be seen from figure, frame (a) motion
cannot be regenerated from the samples, whereas the motion shown in frame (b)
can clearly be reconstructed from the same number of samples used to capture
both motions.

Sampling is the process of taking ‘‘snapshots’’ of a signal over time.


Naturally, when we sample a signal, we want to sample it in an optimal fashion
such that we can capture the essence of the signal while minimizing the use of
resources. In essence, we want to minimize the number of samples while
faithfully reconstructing the original signal from the samples. As can be deduced
from our discussion above, the rate of change in a signal determines the number
of samples required to faithfully reconstruct the signal, provided that all adjacent
samples are captured with the same sample timing intervals.

Harry Nyquist from Bell Laboratory studied the sampling process and
derived a criterion that determines the minimum sampling rate for any
continuous analog signals. His, now famous, minimum sampling rate is known
as the Nyquist sampling rate, which states that one must sample a signal at least
twice as fast as the highest frequency content of the signal of interest. For
example, if we are dealing with the human voice signal that contains frequency
components that span from about 20 Hz to 4 kHz, the Nyquist sample theorem
tells us that we must sample the signal at least at 8 kHz, 8000 ‘‘snapshots’’ every
second. Engineers who work for telephone companies must deal with such issues.
For further study on the Nyquist sampling rate, refer to Barrett
ANALOG-TO-DIGITALCONVERSION 45

and Pack [2] listed in the References section. Sampling is important because when
we want to represent an analog signal in a digital system, such as a computer, we
must use the appropriate sampling rate to capture the analog signal for a faithful
representation in digital systems.

Now that we understand the sampling process, let us move on to the


second process of the ADC, quantization. Each digital system has a number of
bits, which it uses as the basic units to represent data. A bit is the most basic unit
where single binary information, 1 or 0, is represented. A nibble is made up of 4
bits put together. A byte is 8 bits.

In the previous section, we tacitly avoided the discussion of the form of


captured signal samples. When a signal is sampled, digital systems need some
means to represent the captured
46 ATMEL AVR MICROCONTROLLER PRIMER : PROGRAMMING AND INTERFACING

samples. The quantization of a sampled signal is how the signal is


represented as one of quantization level. Suppose you have a single bit to
represent an incoming signal. You only have two different numbers, 0 and 1. You
may say that you can distinguish only low from high. Suppose you have 2 bits.
You can represent four different levels, 00, 01, 10, and 11. What if you have 3
bits? You now can represent eight different levels: 000, 001, 010, 011, 100, 101,
110, and 111. Think of it as follows. When you had 2 bits, you were able to
represent four different levels. If we add one more bit, that bit can be 1 or 0,
making the total possibilities 8. Similar discussion can lead us to conclude that
given n bits, we have 2 ndifferent numbers or levels one can represent.

Figure 3.4 shows how n bits are used to quantize a range of values. In many
digital systems, the incoming signals are voltage signals. The voltage signals are
first obtained from physical signals with the help of transducers, such as
microphones, angle sensors, and infrared sensors. The voltage signals are then
conditioned to map their range with the input range of a digital system, typically
0to5V.InFigure3.4, n bits allow you to divide the input signal range of a digital
system into

FIGURE 3.4: Quantization.


ANALOG-TO-DIGITALCONVERSION 47
ANALOG-TO-DIGITALCONVERSION 47

2n different quantization levels. As can be seen from the figure, higher


quantization levels means better mapping of an incoming signal to its true value.
If we only had a single bit, we can only represent levels 0 and 1. Any analog signal
value in between the range had to be mapped either as level 0 or level 1, not
many choices. Now imagine what happens as we increase the number of bits
available for the quantization levels. What happens when the available number
of bits is 8? How many different quantization levels are available now? Yes, 256.
How about 10, 12, or 14? Notice also that as the number of bits used for the
quantization levels increases for a given input range the ‘distance’ between two
adjacent levels decreases with a factor of a polynomial.

Finally, the encoding process involves converting a quantized signal into


a digital binary number. Suppose again we are using 8 bits to quantize a sampled
analog signal. The quantization levels are determined by the 8 bits, and each
sampled signal is quantized as one of 256 quantization levels. Consider the two
sampled signals shown in Figure 3.5. The first sample is mapped to quantization
level 2 and the second one is mapped to quantization level 198. Note the amount
48 ATMEL AVRMICROCONTROLLERPRIMER: PROGRAMMINGANDINTERFACING

of quantization error introduced for both samples. Now consider Figure 3.5. The
same signal is sampled at the same time but quantized using a less number of
bits. Note that the quantization error is inversely proportional to the number of
bits used to quantize the signal. Once a sampled signal is quantized, the encoding
process involves representing the quantization level with the available bits. Thus,
for the first sample, the encoded sampled value is 0000 0001, whereas the
encoded sampled value for the second sample is 1100 0110. As a result of the
encoding process, sampled analog signals are now represented as a set of binary
numbers. Thus, the encoding is the last necessary step to represent a sampled
analog signal into its corresponding digital form, shown in Figure 3.6.

3.1.3 Resolution and Data Rate


Resolution is a measure used to quantize an analog signal. In fact, resolution is
nothing but the ‘‘distance’’ between two adjacent quantization levels we
discussed earlier. Suppose again we have
ANALOG-TO-DIGITALCONVERSION 49

a range of 5 V and 1 bit to represent an analog signal. The resolution in this case
is 2.5 V, a very poor resolution. You can imagine how your TV screen will look if
you only had only two levels to represent each pixel, black and white. The
maximum error, called the resolution error, is 2.5 V for the current case, 50% of
the total range of the input signal. Suppose you now have 4 bits to represent
quantization levels. The resolution now becomes 1.25 V, or 25% of the input
range. Suppose you have 20 bits for quantization levels. The resolution now
becomes 4.77 × 10 , 9.54 × 10-5 % of the total range. The discussion we presented
simply illustrates that as we increase the available number of quantization levels
within a range, the distance between adjacent levels decreases, reducing the
quantization error of a sampled signal. As the number grows, the error decreases,
making the representation of a sampled analog signal more accurate in the
corresponding digital form. The number of bits used for the quantization is
directly proportional to the resolution of a system. You now should understand
the technical background when you watch high-definition television
broadcasting.

Now let us move onto the discussion of the data rate. The definition of the
data rate is the amount of data generated by a system per some time unit.
Typically, the number of bits or the number of bytes per second is used as the
data rate of a system. We just saw that the more bits we use for the quantization
levels, the more accurate we can represent a sampled analog signal. Why not use
the maximum number of bits current technologies can offer for all digital
systems, when we convert analog signals to digital counterparts? It has to do with
the cost involved. In particular, suppose you are working for a telephone
company and your switching system must accommodate 100,000 customers. For
each individual phone conversation, suppose the company uses an 8-kHz
sampling rate and you are using 10 bits for the quantization levels for each
sampled signal. If all customers are making out-of-town calls, what is the number
of bits your switching system must process to accommodate all calls? The answer
will be 100,000 × 8000 × 10, or 8 billion bits per every second! You will need
some major computing power to meet the requirement. For such reasons, when
designers make decisions on the number of bits used for the quantization levels,
they must consider the computational burden the selection will produce on the
computational capabilities of a digital system versus the required
systemresolution.

You will also encounter the term dynamic range when you consider finding
appropriate ADCs. The dynamic range is a measure used to describe the signal to
noise ratio. The unit used for the measurement is decibel, which is the strength
of a signal with respect to a reference signal. The greater the decibel number, the
stronger the signal is compared with a noise signal. The definition of the dynamic
range is 20 log 2 1b ,whereb is the number of bits used to convert analog signals

For the sake of our discussion, we ignore other overheads involved in processing a phone call such as
multiplexing, demultiplexing, and serial-to-parallel conversion.
50 ATMEL AVRMICROCONTROLLERPRIMER: PROGRAMMINGANDINTERFACING

to digital signals. Typically, you will find 8 to 12 bits used in commercial ADCs,
8
translating the dynamic range from 20 log 2 dB to 20 log 212 dB (Oppenheim and Schafer
[3]).

3.2 ANALOG-TO-DIGITALCONVERSIONPROCESS
The goal of the ADC process is to accurately represent analog signals as digital signals.
Toward this end, three signal processing procedures, sampling, quantization, and
encoding, described in the previous section must be combined together. Before the ADC
process takes place, we first need to convert a physical signal into an electrical signal
with the help of a transducer. A transducer is an electrical and/or mechanical system that
converts physical signals into electrical signals or electrical signals to physical signals.
Depending on the purpose, we categorize a transducer as an input transducer or an
output transducer. If the conversion is from physical to electrical, we call it an input
transducer. The mouse, the keyboard, and the microphone for your PC all fall under this
category. A camera, an infrared sensor, and a temperature sensor are also input
transducers. The output transducer converts electrical signals to physical signals. The
computer screen and the printer for your computer are output transducers. Speakers and
electrical motors are also output transducers. Therefore, transducers play the central part
for digital systems to operate in our physical world by transforming physical signals to
and from electrical signals.

In addition to transducers, we also need signal conditioning circuitry before we


apply the ADC process or its opposite process of DAC process. The signal conditioning
circuitry is called the transducer interface. The objective of the transducer interface
circuit is to scale and shift the electrical signal range to map the output of the input
transducer to the input of the ADC. Figure 3.7 shows the transducer interface circuit using
an input transducer.

The output of the input transducer is first scaled by constant K.Inthefigure,weusea


microphone as the input transducer whose output ranges from -5VDC to + 5VDC.The input
to the ADC ranges from 0 to 5 VDC. The box with constant K maps the output range of the
input transducer to the input range of the converter. Naturally, we need to multiply all
input signals by 1/2 to accommodate the mapping. Once the range has been mapped, the
signal now needs to be shifted. Note that the scale factor maps the output range of the
input transducer as -2.5 to +2.5 VDC instead of 0 to 5 VDC. The second portion of the
circuit shifts the range by 2.5 VDC, thereby completing the correct mapping. Actual
implementation of the circuit components is accomplished using amplifiers with some
feedback loops.

In general, the scaling and bias process may be described by two equations :

V2 max = (V2 max x K ) + B

V2 min = (V2 min x K ) + B


ANALOG-TO-DIGITALCONVERSION 51

FIGURE 3.7: A block diagram of the signal conditioning for an ADC. The range of the sensor voltage
output is mapped to the ADC input voltage range. The scalar multiplier maps the magnitudes of
the two ranges, and the bias voltage is used to align two limits.

The variable V min represents the maximum output voltage from the input transducer.
This voltage occurs when the maximum physical variable (X 1max ) is presented to the input
transducer. This voltage must be scaled by the scalar multiplier (K ) and then have a DC
offset bias voltage (B) added to provide the voltage V 2max max to the input of the ADC
converter.

Similarly, The variable V min represents the minimum output voltage from the input
transducer. This voltage occurs when the minimum physical variable (X1min ) is presented
to the input transducer. This voltage must be scaled by the scalar multiplier (K ) and then
have a DC offset bias voltage (B) added to produce voltage V2 min to the input of the ADC
converter.

Usually, the values of V1max and V1min are provided with the documentation for the
transducer. Also, the values of V 2max and V 2min are known. They are the high and low
reference voltages for the ADC system (usually 5 and 0 VDC for a microcontroller). We
thus have two equations and two unknowns to solve for K and B. The circuits to scale by
K and add the offset B are usually implemented with operational amplifiers. We refer
interested readers to Thomas and Rosa [4] listed in the References section.

Once a physical signal has been converted to its corresponding electrical signal
with the help of an input transducer and the output of the transducer mapped correctly
to the input of the ADC, the ADC process can start. The first step of the ADC process is
the sampling of the analog signal. When selecting a converter, one must consider the type
of physical signal that is being converted to properly ensure the sampling rate. As
discussed in the previous section, using the proper sampling rate is the first step that
determines whether an analog signal will be represented correctly in digital
52 ATMEL AVRMICROCONTROLLERPRIMER: PROGRAMMINGANDINTERFACING

systems. What this means for the reader is to select an ADC that can handle a required
conversion rate. Because most microcontrollers now come with a built-in ADC, one must
study the user manual portion discussing the conversion rate and make sure that the
required sampling rate for the application falls under the advertised conversion rate. For
example, if you need to convert the signal representing a person’s blood pressure, the
sampling rate with 100 Hz (100 samples per second) will suffice. On the other hand, if
you are dealing with human voice, you need at least an 8-kHz sampling rate capacity (see
Enderle et al. [5] for details).

Once the analog signal has been sampled, the quantization process takes place.
For this process, again one must decide how much quantization error can be allowed. At
one extreme where you are only concerned with finding out only two states, say on and
off, quantization error of 1 V is not important. We can operate safely with 2 bits with
maximum quantization error of 1.25 V. On the other hand, if we can only operate with
maximum quantization error of 0.01 V, we need to choose a converter with, at minimum,
10 bits (about 5 mV). To determine the number of bits and its corresponding maximum
quantization error, we use the following equation.

𝑟𝑎𝑛𝑔𝑒
Resolution =
2𝑏

Thus, we can determine the number of bits that will meet the error requirement using the
equation above.

Once the quantization level has been determined, we can now encode it using the
available bits. As seen in the previous section, the process is a simple conversion of a
decimal number (quantization level) to a binary number. Note that the binary number
should use all available bits. For example, quantization level 4 using 8 bits is converted
as 0000 0100, not 100.

In summary, the ADC process has three steps for completion: sampling,
quantization, and encoding. In the next section, we delve into four different technologies
used to implement the ADC process.

Example 3.1 A photodiode is a semiconductor device that provides an output


current corresponding to the light impinging on its active surface. The photodiode is
used with a transimpedance amplifier to convert the output current to an output voltage.
A photodiode/transimpedance amplifier provides an output voltage of 0 V for maximum
rated light intensity and -2.50 VDC of output voltage for the minimum rated light
intensity. Calculate the required values of K and B for this light transducer, so it may be
interfaced to a microcontroller’s ADC system.

You might also like