Programming and Interfacing ATMEL's Avrs
Total Page:16
File Type:pdf, Size:1020Kb
Programming and Interfacing ATMEL® AVR® Microcontrollers Thomas Grace Cengage Learning PTR Australia • Brazil • Mexico • Singapore • United Kingdom • United States Programming and Interfacing © 2016 Cengage Learning PTR. ATMEL® AVR® Microcontrollers WCN: 01-100 Thomas Grace CENGAGE and CENGAGE LEARNING are registered trademarks of Cengage Publisher and General Manager, Learning, Inc., within the United States and certain other jurisdictions. Cengage Learning PTR: Stacy L. Hiquet ALL RIGHTS RESERVED. No part of this work covered by the copyright Manager of Editorial Services: herein may be reproduced, transmitted, stored, or used in any form or by any Heather Talbot means graphic, electronic, or mechanical, including but not limited to Product Team Manager: Emi Smith photocopying, recording, scanning, digitizing, taping, Web distribution, Project Editor/Copyeditor: Dan Foster information networks, or information storage and retrieval systems, except as permitted under Section 107 or 108 of the 1976 United States Copyright Technical Editor: Joseph Ozvold Act, without the prior written permission of the publisher. Interior Layout Tech: MPS Limited Cover Designer: Mike Tanamachi For product information and technology assistance, contact us at Indexer: Valerie Haynes Perry Cengage Learning Customer & Sales Support, 1-800-354-9706. Proofreader: Jenny Davidson For permission to use material from this text or product, submit all requests online at cengage.com/permissions. Further permissions questions can be emailed to [email protected]. Atmel and AVR are registered trademarks of Atmel Corporation or its subsidiaries, in the US and/or other countries. All images © Cengage Learning PTR unless otherwise noted. Library of Congress Control Number: 2015934729 ISBN-13: 978-1-305-50999-3 ISBN-10: 1-305-50999-4 eISBN-10: 1-305-51000-3 Cengage Learning PTR 20 Channel Center Street Boston, MA 02210 USA Cengage Learning is a leading provider of customized learning solutions with employees residing in nearly 40 different countries and sales in more than 125 countries around the world. Find your local representative at www.cengage.com. Cengage Learning products are represented in Canada by Nelson Education, Ltd. For your lifelong learning solutions, visit cengageptr.com. Visit our corporate website at cengage.com. Printed in the United States of America 1234567171615 For Evan, Cassie, and Patti Acknowledgments I would like to thank the editorial staff, and especially Joseph Ozvold, for all their time and help in bringing this book together. About the Author Thomas Grace earned B.S and M.S. degrees in Electrical and Computer Engineering from Clarkson University. Over the years, Grace has worked for IBM in Burlington, VT; Indiana University-Purdue University Indianapolis at PLSP in Batu Pahat, Malaysia; University at Buffalo at the Institute of Technology MARA in Kuala Lumpur, Malaysia; and Clarkson University. For over 25 years he has taught engineering at Broome Community College in Binghamton, NY, and owns a small electronics manufacturing company. Outside of work he enjoys time with his family, traveling, and woodworking. iv Contents Introduction .............................................. ix Chapter 1 Digital Systems.............................................1 Number Systems ...................................................... 1 Base 10 (Decimal) ...................................................1 Roman Numerals ...................................................2 Base 2 (Binary) .....................................................2 Base 8 (Octal) ......................................................2 Base 16 (Hexadecimal) ...............................................2 Base 60 (Sexagesimal)................................................2 Counting ..........................................................3 Converting Base 2 to Base 10..........................................3 Converting Base 10 to Base 2..........................................4 Converting Base 2 to Base 16..........................................5 Converting Base 16 to Base 2..........................................5 Converting Base 10 to Base 16.........................................5 Binary Coded Decimal ...............................................6 Base 2: Adding and Subtracting........................................7 Base 2: Signed and Unsigned Numbers ..................................8 Range of Numbers .................................................10 Base 16: Addition and Subtraction ....................................10 Digital Logic ........................................................ 11 Logic Gates .......................................................12 Programmable Logic Devices .........................................19 v vi Contents Timer, LM555 .....................................................19 Multiplexer .......................................................21 Current and Voltage Limitations ......................................21 FanOut..........................................................22 Resistors, SIPs, and DIPs . ..........................................23 Logic Switches.....................................................24 LEDs.............................................................25 Seven-Segment Displays. ..........................................26 Chapter 2 AVR Programming . ......................................29 Microcomputer Architecture . ......................................... 29 The AVR Family of Microcontrollers ..................................... 31 AVR Features .....................................................31 AVR CPU Registers .................................................39 AVR Development System ............................................. 42 AVR Development Board . ..........................................42 AVR Studio .......................................................47 Programming in Assembly ............................................. 48 A First Program in Assembly .........................................49 AVR Studio Assembler Notes .........................................53 Complete Instruction Set . ..........................................54 Flags ............................................................61 Looping ..........................................................65 Jumping .........................................................68 Branching ........................................................68 Direct Addressing ..................................................73 Indirect Addressing.................................................74 Stack Memory .....................................................77 Subroutines and Delays . ..........................................80 Passing Values to Subroutines ........................................88 AVR Peripherals ..................................................... 90 Digital I/O Ports....................................................91 Polling ...........................................................98 Interrupts .......................................................100 Analog to Digital Conversion........................................113 Serial Data Transmission. .........................................126 Serial Peripheral Interface (SPI) ......................................133 SPI Software Implementation .......................................138 Clocks on the ATtiny13.............................................143 Changing the Speed of the ATmega328 ...............................144 Timer Counter....................................................145 Contents vii Waveform Generation .............................................153 EEPROM on the ATmega328 in C ....................................158 Programming Language Summary ..................................... 159 Programming in Assembly ..........................................159 Programming in C.................................................161 Programming in C++ ..............................................164 In-Line Assembly ..................................................165 Chapter 3 Hardware Interfacing. ...................................167 Sensors ........................................................... 167 Digital Switch ....................................................168 Reed Switch .....................................................168 Transistor as a Switch ..............................................169 Relay ...........................................................170 Photo Resistor....................................................171 Potentiometer....................................................171 Thermal Resistor ..................................................172 Temperature Sensor, LM35 .........................................173 IR Sensors and Diodes..............................................174 Ultrasonic Sensor .................................................178 GPS Sensor ......................................................180 Touch Pad .......................................................182 Accelerometer....................................................184 Magnetic Field Sensor .............................................184 Comparator......................................................185 LCD Displays ....................................................... 188 Text Screens Using Parallel Data .....................................188 Graphics Screens Using Parallel Data..................................191 Text and Graphics Screens