AtmelAVR Microcontroller Primer: Programming and Interfacing Second Edition
f^r**t>*-**n*c contents Preface xv AtmelAVRArchitecture Overview 1 1.1 ATmegal64 Architecture Overview 1 1.1.1 Reduced Instruction Set Computer 1 1.1.2 Assembly Language Instruction Set 2 1.1.3 ATmegal 64 Architecture Overview 3 1.2 Nonvolatile and Data Memories 4 1.2.1 In-System Programmable Flash EEPROM 4 1.2.2 Byte-Addressable EEPROM 4 1.2.3 Static Random Access Memory 5 1.2.4 Programmable Lock Bits 5 1.3 Port System 1.4 Peripheral Features Internal Subsystems 7 1.4.1 Time Base 7 1.4.2 Timing Subsystem 8 1.4.3 Pulse Width Modulation Channels 8 1.4.4 Serial Communications 8 1.4.5 Analog-to-Digital Converter 9 1.4.6 Interrupts 1.5 Physical and Operating Parameters 10 1.5.1 Packaging 10 1.5.2 Power Consumption 1.5.3 Speed Grades 11 1.6 Application: ATmegal64Testbench 1.6.1 Hardware Configuration 11 1.6.2 Software Configuration 12 1.7 Programming the ATmegal64 16 1.7.1 Programming Procedure 16 1.8 Software 18 Portability 1.9 Summary 5 10 10 11 18
1.10 References and Further Reading 18 1.11 Chapter Problems 18 2 Serial Communication Subsystem 23 2.1 Serial Communications 23 2.2 Serial Communication Terminology 23 2.2.1 Asynchronous versus Synchronous Serial Transmission 24 2.2.2 Baud Rate 24 2.2.3 Full Duplex 24 2.2.4 Nonreturn to Zero Coding Format 24 2.2.5 The RS-232 Communication Protocol 24 2.2.6 Parity 25 2.2.7 American Standard Code for Information Interchange 26 2.3 Serial USART 26 2.3.1 System Overview 26 2.3.2 System Operation and Programming 30 2.3.3 Example: Global Positioning System 32 2.3.4 Serial Peripheral Interface 32 2.4 Two-Wire Serial Interface 36 2.5 Summary 36 2.6 References and Further Reading 36 2.7 Chapter Problems 36 3 Analog-to-Digital Conversion 39 3.1 Background Theory 39 3.1.1 Analog versus Digital Signals 40 3.1.2 Sampling, Quantization, and Encoding 42 3.1.3 Resolution and Data Rate 46 3.2 Analog-To-Digital Conversion Process 47 3.2.1 Operational Amplifiers 50 3.3 ADC Conversion Technologies 52 3.3.1 Successive Approximation 55 3.3.2 Integration 55 3.3.3 Counter-Based Conversion 55 3.3.4 Parallel Conversion 56 3.4 The AtmelATmegal64 ADC System 56 3.4.1 Block Diagram 58
xi 3.4.2 Registers 59 3.4.3 Programming the ADC 60 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 67 4.1 Interrupt Theory 67 4.2 ATmegal64 Interrupt System 67 4.3 Programming An Interrupt 68 4.4 Application 69 4.4.1 External Interrupts 70 4.4.2 Internal Interrupt 72 4.5 Summary 75 4.6 References and Further Reading 75 4.7 Chapter Problems 75 5 Timing Subsystem 77 5.1 Overview 77 5.2 Timing related terminology 78 5.2.1 Frequency 78 5.2.2 Period 78 5.2.3 Duty Cycle 78 5.3 Timing System Overview 78 5.4 Applications 82 5.4.1 Input Capture Measuring External Timing Event 82 5.4.2 Counting Events 83 5.4.3 Output Compare Generating Timing Signals to Interface External Devices 84 5.4.4 Industrial Implementation Case Study (PWM) 84 5.5 Overview of the Atmel Timers 86 5.6 Timer 0 System 87 5.6.1 Modes of Operation 87 5.6.2 Timer 0 Registers 90 5.7 Timer 1 91
5.7.1 Timer 1 Registers 93 5.8 Timer 2 97 5.9 Programming the Timer System 100 5.9.1 Precision Delay 100 5.9.2 Pulse Width Modulation 102 5.9.3 Input Capture Mode 103 5.10 Servo Motor Control with the PWM system 105 5.11 Summary 110 5.12 References 110 5.13 Chapter Problems Ill 6 AtmelAVR Operating Parameters and Interfacing 113 6.1 Operating Parameters 114 6.2 Input Devices 115 6.2.1 Switches 117 6.2.2 Switch Debouncing 118 6.2.3 Keypads 120 6.2.4 Sensors 120 6.3 Output Devices 123 6.3.1 Light-Emitting Diodes 123 6.3.2 Seven-Segment LED Displays 127 6.3.3 Tristate LED Indicator 127 6.3.4 Dot Matrix Display 127 6.3.5 Liquid Crystal Display 130 6.3.6 High-Power DC Devices 133 6.4 DC Motor Speed and Direction Control 134 6.4.1 H-bridge direction control 135 6.4.2 Servo motor interface 136 6.4.3 Stepper motor control 136 6.4.4 AC Devices 144 6.5 Summary 144 6.6 References and Further Reading 144 6.7 Chapter Problems 145 7 Embedded Systems Design 147 7.1 What is an embedded system? 147
xiii 7.2 Embedded system design process 148 7.2.1 Project Description 148 7.2.2 Background Research 148 7.2.3 Pre-Design 148 7.2.4 Design 150 7.2.5 Implement Prototype 152 7.2.6 Preliminary Testing 152 7.2.7 Complete and Accurate Documentation 153 7.3 Application: Flight Simulator Panel 153 7.4 Autonomous Maze Navigating Robots 180 7.5 Blinky602Arobot 183 7.5.1 Requirements 183 7.5.2 Circuit diagram 183 7.5.3 Structure chart 183 7.5.4 UML activity diagrams 183 7.5.5 Microcontroller code 186 7.6 Mountain. Maze Navigating Robot 194 7.6.1 Description 194 7.6.2 Requirements 194 7.6.3 Circuit diagram 194 7.6.4 Structure chart 194 7.6.5 UTvtL activity diagrams 198 7.6.6 Microcontroller code 198 7.6.7 Mountain Maze 202 7.6.8 Project extensions 202 7.7 Summary 204 7.8 References and Further Reading 204 7.9 Chapter Problems 205 A ATmegal64 Register Set 207 B ATmegal64 Header File 211 Authors'Biographies 223 Index 225