Programming and Interfacing ATMEL's Avrs

Total Page:16

File Type:pdf, Size:1020Kb

Programming and Interfacing ATMEL's Avrs 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
Recommended publications
  • Look at the Code - the Source Text of Any Program, As a Type of Material
    Look at the code - the source text of any program, as a type of material. See the effects of the code as a material manifestation. “The Matter of Code” is a story about the Creative Coding practice that elimi- nates the polarisation of code and matter. It is a story of a “material” that has unique properties, a combination of physical and digital matter that does not produce a simple sum. Its emergent properties allow the creation of a unique experience, something that to designers and artists is otherwise unattainable. This “material” gives way to the creation of complex spatial installations that react to stimuli from the environment, changing the experience of space, its perceptions, and subsequently affecting human emotions. The contact of the digital and physical world has been dominated by the interaction with intermediary screens whose flat surfaces have limited the sensory experience - our understanding of the world is based on motion, action, active and bodily experience. Screens cause the observation and manipulation of symbols, that remove the person from reality. As a result any richness of touch is lost, as the hand’s natural and sophisticated ability to manipulate the object is evaded. Screens have created a distance from the environment that has shaped us, destroying the work of evolution that through hundreds of thousands of years has adapted both the mind and body to the ever changing environ- ment. However, the combination of the possibilities offered by programming, electronics and digital fabrication, will allow us to boldly overcome the barrier of the screen - as a “material” that can “blend bits and atoms” emerges.
    [Show full text]
  • Converting a Microcontroller Lab from the Freescale S12 to the Atmel Atmega32 Processor
    ASEE-NMWSC2013-0025 Converting a Microcontroller Lab From The Freescale S12 to the Atmel ATmega32 Processor Christopher R. Carroll University of Minnesota Duluth [email protected] Abstract During the summer of 2013, the laboratory supporting the microcontroller course at the University of Minnesota Duluth was completely re-implemented. For the last several years, the processor that has been used was the Freescale S12, a popular 16-bit microcontroller with a long ancestral history 1. The recent popularity of the Atmel AVR series of microcontrollers, as used in the Arduino microcomputers, for example, has prompted a change in the lab to use Atmel’s ATmega32 microcontroller, an 8-bit member of the AVR family of microcontrollers 2,3 . The new processor has a fundamentally different architecture than that used in the past, but the input/output resources available are much the same. This paper addresses issues that will be faced in the conversion when the course is taught with the new lab hardware for the first time in the Fall. At the very fundamental level, the S12 and ATmega32 differ in architecture. The S12 is a Princeton architecture computer (single memory for both program and data), while the ATmega32 is a Harvard architecture computer (separate program and data memories). The S12 is clearly a CISC machine (Complex Instruction Set Computer) while the ATmega32 is clearly a RISC machine (Reduced Instruction Set Computer). These differences will affect how the microcontroller course is taught when it is offered in the Fall using this new lab. Fortunately, however, the collection of input/output devices in the AVR microcontrollers mimics closely what is found in the S12, so that many of the existing lab exercises will be used again with only minor tweaking.
    [Show full text]
  • Javaprop – Users Guide
    JAVAPROP – USERS GUIDE Initial Creation: June 2009 – Last Revision: August 2018 - Martin Hepperle Table of Contents JavaProp – Users Guide ............................................................................................................. 1 Table of Contents ................................................................................................................... 1 1. Introduction ........................................................................................................................ 3 2. Symbols and Coefficients ................................................................................................... 4 Symbols .................................................................................................................................. 4 Coefficients ............................................................................................................................ 4 3. Propellers ............................................................................................................................ 7 How to design a propeller ...................................................................................................... 7 The Design Card ................................................................................................................. 7 The Airfoils Card ............................................................................................................... 9 How to analyze a propeller ..................................................................................................
    [Show full text]
  • 8051 Programmer
    T51prog2 MCS51 series and Atmel AVR microcontrollers ISP capable fast programmer Short description: 10692 supported devices from 149 manufacturers by 2.75 version of SW (21. Dec. 2010) small, very fast and powerful portable programmer of MCS51 series and Atmel AVR microcontrollers in-circuit serial programming (ISP) capability included program also serial EEPROMs DIL40 ZIF socket, all MCS51/AVR chips in DIL package up to 40 pins are supported without adapters connection to PC: USB port USB 2.0 full speed and USB 1.1 compatible upgradeable to SmartProg2 programmer. comfortable and easy to use control program, work with all versions of MS Windows from Windows 98 to Window 7 64-bit free SW update, download from Internet power supply, cable and software included approved by CE laboratory to meet CE requirements made in Slovakia Available accessories: Programming Adapters (Socket Converters) Diagnostic POD for ISP connector upgrade kit Xprog2 to SmartProg2 Features GENERAL T51prog2 is the next member of new generation MS Windows (from Windows 98 to Window 7 64-bit) based ELNEC specialized programmers . Programmer is capable to support all currently available microcontrollers of the MCS51 series (up to 40 pins) and the AVR microcontrollers (8-40 pins) using parallel and serial algorithms. T51prog2 has been developed in close cooperation with Atmel W&M. , therefore programmer's hardware is focused to support all current and future microcontrollers of Atmel W&M MCS51 family. T51prog2 is a small, very fast and powerful portable programmer for MCS51 series and Atmel AVR microcontrollers. T51prog also programs serial EEPROM with IIC (24Cxx), Microwire (93Cxx) and SPI (25Cxx) interface types.
    [Show full text]
  • Useless Machine Upgrade Counter
    The Useless Machine CounterTM Hobby Electronics Soldering Kit Instruction Guide For the best outcome, follow each step in order. We recommend reading this guide entirely before you get started. Tools required: Soldering iron, solder, flush cutters, flat blade screwdriver, safety glasses, masking tape. v1.1 Electronics & acrylic parts Swiss Pins Snap Male pin header switch IC Chip Power Wire Shunt Jumper PCB 7 segment displays 10k resistors Male pins & Right Angle 0.1uf Caps Front side of the PCB Nuts Screws Standoffs Back side of the PCB Note: Some components in this kit are to be soldered on the front side, and others on the back side of the PCB. Look for the white outlines printed on the PCB Short side bezel to show where the parts go. Preparing and placing the resistors Resistor bending: Bend each of the resistors like in these photos: You want to have the bend of the leg as close as possible to the body of the resistor. Resistor placing: The Basic Counter takes 3 x 10k resistors. They get installed in the spots noted as R1, R2, and R3. Slide the resistors all the way down so that they are flush with the PCB. Flare out the legs to keep them in place. Be sure that the body of the resistor is flush against the PCB. Solder the resistors in place. Make sure you don’t forget to solder any of the legs. ! Safety Glasses On! Snip off the ends of the legs. Be sure not to scratch the surface of the PCB. Keep your safety glasses on! Capacitor preparation Snip the capacitors from the paper tape.
    [Show full text]
  • 8-Bit Microcontrollers 32-Bit Microcontrollers and Application
    8-bit Microcontrollers 32-bit Microcontrollers and Application Processors QUICK REFE R ENCE GUIDE February 2009 Everywhere You Are® AVR Introduction Atmel® offers both 8-bit and 32-bit AVR®s. AVR microcontrollers and application processors deliver unmatched flexibility. AVR combines the most code-efficient architecture for C and assembly programming with the ability to tune system parameters throughout the entire life cycle of your key products. Not only do you get to market faster, but once there, you can easily and cost-effectively refine and improve your product offering. The AVR XMEGA gives you 16-bit performance and leading low-power features at 8-bit price. It’s simple: AVR works across the entire range of applications you’re working on... or want to work on. & Introduction QUICK REFERENCE GUIDE AVR Key Benefits AVR32 Key Benefits High performance High CPU performance picoPower™ technology Low power consumption High code density High data throughput High integration and scalability Low system cost Complete tool offering High reliability Atmel’s AVR is addressing the 8-bit and 16-bit market Easy to use Environment Friendly Packages For AVR and AVR32 microcontrollers and application processors, all the lead free packages are RoHS compliant, lead free, halide free and fully green. All parts are offered in fully green packaging only. Product Range Atmel microcontrollers - success through innovation Atmel offers both 8-bit and 32-bit AVR’s, and since day one the AVR philosophy has always been clear: Highest performance with no power penalty. tinyAVR 1-16 KBytes Flash, 8-32 pin packages megaAVR 4-256 KBytes Flash, 28-100 pin packages AVR XMEGA 16-384 KBytes Flash, 44-100 pin packages AVR32 UC3 16-512 KBytes Flash, 48-144 pin packages AVR32 AP7 Up to 32 KBytes On-chip SRAM, 196-256 pin packages & QUICK REFERENCE GUIDE Product Range Product Product Range Range Product Families tinyAVR® General purpose microcontrollers with up to 16K Bytes Flash program memory, 512 Bytes SRAM and EEPROM.
    [Show full text]
  • Working with Useless Machines a Look at Our Shifting Relationship with Ubiquity Through Personal Assistants By: Nadine Lessio
    Working With Useless Machines A look at our shifting relationship with ubiquity through personal assistants by: Nadine Lessio A thesis exhibition presented to OCAD University in partial fulfillment of the requirements for the degree of Master of Design in Digital Futures 49 McCaul Street, April 12 - 15 Toronto, Ontario, Canada, April, 2018 Nadine Lessio 2018 This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Canada (CC BY-NC-SA 2.5 CA) To see the license go to https://creativecommons.org/licenses/by-nc-sa/3.0/ca/ or write to Creative Commons, 171 Second Street, Suite 300, San Francisco, California 94105, USA. ii Copyright Notice This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. http://creativecommons.org/licenses/by-nc-sa/3.0/ You are free to: Share — to copy, distribute and transmit the work Remix — to adapt the work Under the following conditions: Attribution — You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). Noncommercial — You may not use this work for commercial purposes. Share Alike — If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one. With the understanding that: Waiver — Any of the above conditions can be waived if you get permission from the copyright holder. Public Domain — Where the work or any of its elements is in the public domain under applicable law, that status is in no way affected by the license.
    [Show full text]
  • 2 XII December 2014
    2 XII December 2014 www.ijraset.com Volume 2 Issue XII, December 2014 ISSN: 2321-9653 International Journal for Research in Applied Science & Engineering Technology (IJRASET) Overview and Comparative Study of Different Microcontrollers Rajratna Khadse1, Nitin Gawai2, Bagwan M. Faruk3 1Assist.Professor, Electronics Engineering Department, RCOEM, Nagpur 2,3Assist.Professor, E & Tc Engineering Department, JDIET, Yavatmal Abstract—A microcontroller is a small and low-cost computer built for the purpose of dealing with specific tasks, such as displaying information on seven segment display at railway platform or receiving information from a television’s remote control. Microcontrollers are mainly used in products that require a degree of control to be exerted by the user. Today various types of microcontrollers are available in market with different word lengths such as 8bit, 16bit, 32bit, and microcontrollers. Microcontroller is a compressed microcomputer manufactured to control the functions of embedded systems in office machines, robots, home appliances, motor vehicles, and a number of other gadgets. Therefore in today’s technological world lot of things done with the help of Microcontroller. Depending upon the applications we have to choose particular types of Microcontroller. The aim of this paper to give the basic information of microcontroller and comparative study of 8051 Microcontroller, ARM Microcontroller, PIC Microcontroller and AVR Microcontroller Keywords— Microcontroller, Memory, Instruction, cycle, bit, architecture I. INTRODUCTION Microcontrollers have directly or indirectly impact on our daily life. Usually, But their presence is unnoticed at most of the places like: At supermarkets in Cash Registers, Weighing Scales, Video games ,security system , etc. At home in Ovens, Washing Machines, Alarm Clocks, paging, VCR, LASER Printers, color printers etc.
    [Show full text]
  • An Overview of Atmega AVR Microcontrollers Used in Scientific Research and Industrial Applications
    Pomiary Automatyka Robotyka, R. 19, Nr 1/2015, 15–20, DOI: 10.14313/PAR_215/15 An Overview of ATmega AVR Microcontrollers Used in Scientific Research and Industrial Applications Wojciech Kunikowski, Ernest Czerwiński, Paweł Olejnik, Jan Awrejcewicz Department of Automation, Biomechanics and Mechatronics, Lodz University of Technology, 90–924 Łódź, 1/15 Stefanowski str. Abstract: Nowadays, microcontrollers are commonly used in many fields of industrial applications previously dominated by other devices. Their strengths such as: processing power, low cost, and small sizes enable them to become substitutes for industrial PLC controllers, analog electronic circuits, and many more. In first part of this article an overview of the Atmel AVR microprocessor family can be found, alongside with many scientific and industrial applications. Second part of this article contains a detailed description of two implementations of ATmega644PA microprocessor. First one is a controller with PID regulation that supports a DC motor driver. Second one is a differential equation solver with 4-th order Runge-Kutta method implemented. It is used for solving a torsion pendulum dynamics. Finally, some general conclusions regarding the two presented implementations are made. Keywords: Atmel, AVR, ATmega, microcontroller, torsion pendulum, PID control, DC motor, PWM control 1. Introduction the performance and compact dimensions of control units are the most important requirements. The described micropro- 1.1. Overview of some Atmel microcontrollers cessor characterizes also with very low power consumption. AVR ATmega is a family of 8-bit microprocessors from Atmel. It is the only processor in AVR family that works with 0.7 V Their features vary across models, but mostly, the following power supply.
    [Show full text]
  • Single-Board Microcontrollers
    Embedded Systems Design (0630414) Lecture 15 Single-Board Microcontrollers Prof. Kasim M. Al-Aubidy Philadelphia University Single-Board Microcontrollers: • There is a wide variety of single-board microcontrollers available from different manufacturers and suppliers of microcontrollers. • The most common microcontroller boards are: – Intel Boards: based on Intel microcontrollers. – ARM Boards: based on ARM7 microcontrollers. – Cortex Boards: based on Cortex microcontrollers. – AVR Boards: based on Atmel AVR microcontrollers. – MSP430 Boards: based on Texas Instruments microcontrollers. – PIC Boards: based on the Microchip PIC microcontrollers. – Motorola Boards: based on Motorola microcontrollers. – ARDUNIO Boards: based on Atmel AVR microcontrollers. •It is not easy to decide on which microcontroller to use in a certain application. However, Arduino is becoming one of the most popular microcontrollers used in industrial applications and robotics. •There are different types of Arduino microcontrollers which differ in their design and specifications. The following table shows comparison between the Arduino microcontrollers. Ref: http://www.robotshop.com/arduino-microcontroller-comparison.html The Arduino Uno board: Hardware design of the Arduino Uno board: Single-Board Microcontroller + ZigBee Example: Mobile Robot control using Zigbee Technology Single-Board Microcontroller Selection: The selection guide for using the suitable microcontroller includes: 1. Meeting the hardware needs for the project design; - number of digital and analog i/o lines. - size of flash memory, RAM, and EPROM. - power consumption. - clock speed. - communication with other devices. 2. Availability of software development tools required to design and test the proposed prototype. 3. Availability of the microcontroller..
    [Show full text]
  • Useless Machine – Zwecklose Maschine
    ft:pedia Issue 4/2018 Translated by: Peter King, www.procontechnology.com.au from the German article in ft:pedia issue 4/2018 from www.ftcommunity.de/ftpedia . Model Useless Machine – zwecklose Maschine Dirk Fox Everyone who designs a fischertechnik model wishes to realize a specific function. As a rule, this function serves a (mostly useful) purpose ‒ such as locomotion, transportation or pleasure. But, of course, the subtle realization of the function itself is often the intriguing thing ‒ sometimes even when it only partially fulfils its original purpose. What is more obvious than to realize the idea of a function for its own sake ‒ "purposeless", in a sense. This idea is not new, but it can be inspiring and often, it's impressive. History to turn itself off (Fig. 1 [1, 2]). For a theoretical computer scientist, it provided a The history of "Useless Machines", fascinating mechanical counter-concept to machines that have a function but no the "halting problem" ‒ the difficulty in purpose, goes back to the 1930s. The artist proving algorithmically that a program Bruno Munari (1907-1998), later an actually terminates (ends). influential industrial designer, was probably the first to construct such Useless Machines have been designed by purposeless mechanisms and to understand the fischertechnik community in numerous them as a critique of the increasing variants. A simple, but very compact domination of our world by machines. version of the self-disabling machine is presented here ‒ as well as suggestions to derive more complex variants. First, you might like to gain an impression of how this self-cancelling machine works, so take a look at the following YT video: www.youtube.be/watch?v=M6LJfsLstWs For this replica, I have created a fischertechnik designer file [4]; It can be downloaded together with a list of parts [5] from the pages of fischertechnik AG.
    [Show full text]
  • What Kinds of Art Would Survive in the Event of An
    153 ½ Stanton Street New York, New York 10002 [email protected] +1 617 678 4440 www.kaimatsumiya.com Wed – Sun 12- 6 pm “RESET” Group Exhibition Opening reception: Wednesday, February 6th, 6:30 – 9 February 6th – March 10th What kinds of art would survive in the event of an environmental apocalypse? What new kinds of art would emerge? How would our very definition of “art” change if the figurative reset button were pressed on the world as we know it? Our current group exhibition, “Reset,” explores these questions, imagining the gallery as a post-apocalyptic, dystopian laboratory in which ostensibly permanent laws of nature, technology, and culture can be reconfigured. The spirit of machines has been indispensable in rendering, modifying, and even destroying societal norms and laws, and art is certainly not immune to its consequences. The historical avant-garde was inextricable from the technological and industrial developments that defined their world, with the early French modernists referring to their own works as ‘machines’ suggesting both mechanistic dynamism and un-utilitarian creativity. The useless machine is essentially a reset button— built solely to turn itself off upon being turned on, thereby setting the process anew. The late Marvin Minsky, once a researcher at Bell Labs Inc., devoted his career to two principal pursuits: making strides in the development of artificial intelligence and building “the most profoundly useless things” he could think of. These two projects may seem incongruous, but for Minsky they became intimately intertwined through one of his best-known inventions, the “useless machine.” For Minsky, the most advanced artificial intelligence would be capable of the most intimate, intelligent, private, and quintessentially human act, suicide.
    [Show full text]