100% found this document useful (2 votes)
321 views

Contents - Explore ATtiny Microcontrollers Using C and Assembly Language

This document provides an in-depth look at the 8-bit AVR architecture found in ATtiny and ATmega microcontrollers, primarily from a software and programming perspective. It teaches how to program ATtiny microcontrollers using C and assembly language in Microchip Studio (formerly Atmel Studio). Some key topics covered include the internal architecture of AVR microcontrollers, programming microcontrollers using various debugging tools, writing programs in assembly and C languages, and becoming proficient in debugging and programming AVR microcontrollers. The intended audience is those interested in learning embedded systems and gaining expertise in AVR microcontrollers.

Uploaded by

soumenmitra613
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
321 views

Contents - Explore ATtiny Microcontrollers Using C and Assembly Language

This document provides an in-depth look at the 8-bit AVR architecture found in ATtiny and ATmega microcontrollers, primarily from a software and programming perspective. It teaches how to program ATtiny microcontrollers using C and assembly language in Microchip Studio (formerly Atmel Studio). Some key topics covered include the internal architecture of AVR microcontrollers, programming microcontrollers using various debugging tools, writing programs in assembly and C languages, and becoming proficient in debugging and programming AVR microcontrollers. The intended audience is those interested in learning embedded systems and gaining expertise in AVR microcontrollers.

Uploaded by

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

books

books books

IN
-D
EP
TH
AVR Architecture and Programming AVR Architecture and Programming

using C and Assembly Language • Warwick A. Smith


Explore ATtiny Microcontrollers
Explore ATtiny
Microcontrollers
Explore ATtiny
using C and Assembly Microcontrollers
Language
using C and Assembly
Language
An in-depth look at the 8-bit AVR architecture found in ATtiny and ATmega
microcontrollers, mainly from a software and programming point of view.
Warwick A. Smith is an
Explore the AVR architecture using C and assembly language in Microchip
electronics engineer and
Studio (formerly Atmel Studio) with ATtiny microcontrollers. embedded programmer from
South Africa with experience
> Learn the details of how AVR microcontrollers work internally, in industrial, commercial and
including the internal registers and memory map of ATtiny devices. aerospace related industries.
Warwick has a wide interest
> Program ATtiny microcontrollers using an Atmel-ICE programmer/
in various fields of technology,
debugger, or use a cheap hobby programmer, or even an Arduino including embedded systems
Uno as a programmer. hardware, software, and
> Most code examples can be run using the Microchip Studio AVR information technology. His
simulator. writing style has been described
as "clear and concise" as well as
> Learn to write programs for ATtiny microcontrollers in assembly
"conversational and friendly".
language.
> See how assembly language is converted to machine code
instructions by the assembler program.
> Find out how programs written in the C programming language end
up as assembly language and finally as machine code instructions.
> Use the Microchip Studio debugger in combination with a hardware
USB programmer/debugger to test assembly and C language
programs, or use the Microchip Studio AVR simulator.
> DIP packaged ATtiny microcontrollers are used in this volume for
easy use on electronic breadboards, targeting mainly the ATtiny13(A)
and ATtiny25/45/85.
> Learn about instruction timing and clocks in AVR microcontrollers
using ATtiny devices.
> Be on your way to becoming an AVR expert with advanced
debugging and programming skills.
Elektor International Media BV
www.elektor.com

Warwick A. Smith
● This is an Elektor Publication.

Elektor is the media brand of Elektor International Media B.V.


PO Box 11, NL-6114-ZG Susteren, The Netherlands
Phone: +31 46 4389444

● All rights reserved. No part of this book may be reproduced in any material form,

including photocopying, or storing in any medium by electronic means and whether or


not transiently or incidentally to some other use of this publication, without the written
permission of the copyright holder except in accordance with the provisions of the
Copyright Designs and Patents Act 1988 or under the terms of a licence issued by the
Copyright Licencing Agency Ltd., 90 Tottenham Court Road, London, England W1P 9HE.
Applications for the copyright holder's permission to reproduce any part of the
publication should be addressed to the publishers.

Trademarks are property of their respective owners and are used in an editorial fashion
with no intention of infringement of the trademark.

● Declaration

The Author and the Publisher have used their best efforts in ensuring the correctness of
the information contained in this book. They do not assume, and hereby disclaim, any
liability to any party for any loss or damage caused by errors or omissions in this book,
whether such errors or omissions result from negligence, accident or any other cause.

● British Library Cataloguing in Publication Data

A catalogue record for this book is available from the British Library

● ISBN: 978-3-89576-479-0 (print) ISBN: 978-3-89576-480-6 (e-book)

● © Copyright 2021: Elektor International Media B.V.

Elektor is part of EIM, the world's leading source of essential technical information and electronics products for pro
engineers, electronics designers, and the companies seeking to engage them. Each day, our international team
develops and delivers high-quality content - via a variety of media channels (including magazines, video, digital
media, and social media) in several languages - relating to electronics design and DIY electronics.
www.elektormagazine.com
Table of Contents

Introduction 17
Broad Overview of this Book..........................................................................................18
About the Targeted AVR Microcontrollers....................................................................18
Primary Programming Software.....................................................................................19
Why Learn Assembly Language?...................................................................................19
The Many Reasons for Learning Assembly Language...............................................19
C Programming.................................................................................................................. 21
Intended Audience.............................................................................................................21
Prerequisites......................................................................................................................21
Software Requirements.................................................................................................22
Hardware Requirements...............................................................................................22
A DIP Packaged ATtiny............................................................................................23
An Electronic Breadboard.......................................................................................23
Programmer/Debugger............................................................................................23
Atmel-ICE Basic Kit............................................................................................24
Atmel-ICE Full Kit...............................................................................................24
AVR Dragon.........................................................................................................25
AVRISP mkII........................................................................................................25
Hobbyist USB Programmers..............................................................................25
Power Supply............................................................................................................26
Jumper Links and Wires..........................................................................................27
Electronic Components............................................................................................28
Optional Test Equipment.........................................................................................28
Microchip, Atmel, Microchip Studio and Atmel Studio..............................................28
Accompanying Resources...............................................................................................29
Let’s Get Started................................................................................................................29

Chapter 1 • Overview of ATtiny Microcontrollers 31


1.1 ATtiny Microcontrollers.............................................................................................32
1.1.1 8-Pin Devices........................................................................................................33
1.1.1.1 ATtiny13 and ATtiny13A..............................................................................33
1.1.1.2 ATtiny25, ATtiny45 and ATtiny85...............................................................33
1.1.2 14-Pin ATtiny24, ATtiny44 and ATtiny84............................................................34

3
1.1.3 20-Pin Devices.....................................................................................................34
1.1.3.1 ATtiny26........................................................................................................34
1.1.3.2 ATtiny261, ATtiny461 and ATtiny861.........................................................35
1.1.3.3 ATtiny2313 and ATtiny4313........................................................................35
1.1.4 28-Pin Devices.....................................................................................................36
1.1.4.1 ATtiny48 and ATtiny88................................................................................36
1.1.4.2 ATtiny28L and ATtiny28V............................................................................36
1.2 Which ATtiny to Use...................................................................................................36
1.3 Comparison of ATtiny Devices.................................................................................37
1.4 Summary....................................................................................................................... 38

Chapter 2 • Hardware and Software Setup 39


2.1 Install Microchip Studio.............................................................................................40
2.1.1 Download Page and Software Version...............................................................40
2.1.2 Newest Software Version....................................................................................40
2.1.3 Start the Installation.............................................................................................40
2.1.4 Installation Steps..................................................................................................40
2.2 8-Pin ATtiny Microcontroller LED Circuit...............................................................41
2.2.1 Circuit Power Supply...........................................................................................44
2.2.2 Decoupling Capacitor..........................................................................................45
2.3 A First Assembly Language Program.....................................................................45
2.3.1 Start a New AVR Assembler Project in Microchip Studio.................................45
2.3.2 LED Blink Assembly Code...................................................................................47
2.3.3 Build the Project...................................................................................................48
2.3.4 Fix Any Build Errors.............................................................................................49
2.3.5 Load the Program to the AVR Microcontroller...................................................49
2.3.6 Fault Finding.........................................................................................................50
2.4 A First C Program........................................................................................................50
2.4.1 Start a New GCC C Executable Project in Microchip Studio...........................50
2.4.2 LED Blink C Code................................................................................................51
2.4.3 Build the Project...................................................................................................53
2.4.4 Fix Any Build Errors.............................................................................................53
2.4.5 Load the Program to the AVR Microcontroller...................................................53
2.4.6 Fault Finding.........................................................................................................53
2.5 Summary....................................................................................................................... 54

4
Chapter 3 • Basics of Assembly Language 55
3.1 Mnemonics...................................................................................................................56
3.2 AVR Instruction Set....................................................................................................56
3.3 Opcodes and Operands.............................................................................................58
3.4 Assembly Language Comments...............................................................................58
3.5 LED Blink Program Operation..................................................................................59
3.6 Using the Debugger and Simulator.........................................................................61
3.6.1 Using the AVR Simulator and Debugger............................................................62
3.6.2 Using a Physical AVR Microcontroller and Debugger......................................64
3.6.3 Putting an AVR Back into ISP mode...................................................................67
3.7 Summary.......................................................................................................................67

Chapter 4 • Binary Numbers and Memory 69


4.1 Bits and Bytes.............................................................................................................70
4.1.1 Counting in Binary................................................................................................70
4.1.2 Bytes and Nibbles................................................................................................73
4.2 Weighted Number Systems.......................................................................................73
4.2.1 Weighted Decimal Numbers................................................................................73
4.2.2 Weighted Binary Numbers...................................................................................75
4.3 Hexadecimal Numbers...............................................................................................76
4.3.1 Counting in Hexadecimal....................................................................................76
4.3.2 Using Hexadecimal to Represent Binary...........................................................76
4.4 Calculating Number Sizes.........................................................................................79
4.4.1 Calculating the Maximum Value of a Binary Number.......................................79
4.4.2 Calculating Memory Size.....................................................................................79
4.4.3 Converting Between Bytes and Kilobytes..........................................................81
4.5 Numbers in Assembly Programs.............................................................................81
4.6 Numbers in C Programs............................................................................................82
4.6 Summary.......................................................................................................................83

Chapter 5 • AVR Registers and Memory Map 85


5.1 AVR Registers..............................................................................................................86
5.1.1 General Purpose Working Registers..................................................................86
5.1.2 I/O Registers.........................................................................................................86
5.2 Using General Purpose Registers...........................................................................86
5.2.1 Adding Two Registers..........................................................................................87

5
5.2.2 Incrementing a Register......................................................................................90
5.3 AVR Memory Map........................................................................................................91
5.3.1 Program Memory..................................................................................................92
5.3.2 Data Memory........................................................................................................93
5.3.3 EEPROM...............................................................................................................94
5.4 Accessing SRAM in Assembler Programs.............................................................94
5.4.1 SRAM Store and Load Example.........................................................................95
5.4.2 Store and Load Example in the Simulator.........................................................96
5.4.3 SRAM Start Addresses........................................................................................97
5.5 Summary....................................................................................................................... 98

Chapter 6 • AVR Internal Architecture 99


6.1 Clock Pulses and Program Counter......................................................................100
6.1.1 Clock Pulses.......................................................................................................100
6.1.2 Clock Pulse Period and Frequency..................................................................101
6.1.2.1 ATtiny13(A) Default Clock Frequency.....................................................101
6.1.2.2 ATtiny25/45/85 Default Clock Frequency...............................................101
6.1.2.3 RC Oscillator Accuracy.............................................................................102
6.1.2.4 Clock Sources and Improving Accuracy.................................................102
6.1.2.5 Calculating Clock Period from Frequency..............................................102
6.1.2.6 Calculating Clock Frequency from Period..............................................103
6.1.3 The Program Counter........................................................................................103
6.2 Microcontroller Buses..............................................................................................105
6.3 Harvard and von Neumann Architectures............................................................106
6.4 Fetching and Executing Instructions....................................................................107
6.5 Status Register..........................................................................................................109
6.5.1 Zero Flag.............................................................................................................109
6.5.2 Negative Flag......................................................................................................111
6.6 Stack and Stack Pointer..........................................................................................112
6.6.1 How the Stack Works.........................................................................................113
6.6.2 Accessing the Stack with PUSH and POP.......................................................114
6.6.3 Calling a Subroutine...........................................................................................117
6.7 LED Blink Program Fully Explained......................................................................121
6.8 AVR Instruction Encoding.......................................................................................124
6.9 Addressing Modes....................................................................................................129
6.9.1 Register Direct Addressing...............................................................................130

6
6.9.2 I/O Direct Addressing.........................................................................................131
6.9.3 Other Addressing Modes...................................................................................132
6.10 Summary................................................................................................................... 132

Chapter 7 • Arithmetic and Logic Instructions 135


7.1 Positive and Negative Numbers.............................................................................136
7.1.1 1’s Compliment Numbers..................................................................................136
7.1.2 2’s Compliment Numbers..................................................................................136
7.2 Addition....................................................................................................................... 139
7.2.1 ADD – Add without Carry...................................................................................139
7.2.2 ADC – Add with Carry........................................................................................140
7.3 Subtraction.................................................................................................................142
7.4 Logic Instructions.....................................................................................................143
7.4.1 Logical AND........................................................................................................143
7.4.2 Logical OR..........................................................................................................145
7.4.3 Logical Exclusive OR.........................................................................................147
7.5 Other Arithmetic and Logic Instructions.............................................................148
7.6 Summary..................................................................................................................... 148

Chapter 8 • Programming AVR I/O Ports 149


8.1 Instructions for Accessing I/O Registers.............................................................150
8.1.1 Reading and Writing I/O Registers with IN and OUT.....................................150
8.1.1.1 The IN Instruction......................................................................................150
8.1.1.2 The OUT Instruction..................................................................................151
8.1.2 I/O Register Bit Manipulation and Testing.......................................................152
8.1.3 Accessing I/O Registers as Data Space..........................................................152
8.2 I/O Ports...................................................................................................................... 153
8.2.1 Configuring I/O Pins as Outputs in Assembler................................................154
8.2.1.1 Five LED 8-pin ATtiny Circuit...................................................................154
8.2.1.2 Using debugWIRE or ISP/SPI to Program an ATtiny AVR....................154
Program-only Programmers............................................................................155
Peripheral Hardware Devices Interfering with Programming.......................155
Putting the AVR into debugWIRE Mode.........................................................156
8.2.1.3 Build the 5 LED ATtiny Circuit on Breadboard.......................................156
8.2.1.4 Assembly Code for the 5 LED Count Circuit..........................................157
8.2.1.5 Select the Debugger or Simulator...........................................................157

7
8.2.1.6 What the LED Count Assembler Code Does..........................................157
8.2.1.7 Build and Run the Program......................................................................158
8.2.1.8 How the LED Count Assembler Program Works....................................158
8.2.1.9 Using a Breakpoint in the Debugger.......................................................161
8.2.2 Configuring I/O Pins as Outputs in C...............................................................162
8.2.2.1 LED Count C Code...................................................................................162
8.2.2.2 How the LED Count C Code Works........................................................163
8.2.2.3 Running the LED Count C Code..............................................................163
8.2.2.4 LED Count C Code Disassembly.............................................................163
8.2.3 Limiting a Count Value.......................................................................................171
8.2.4 Three Ways to Toggle an LED with an AVR....................................................173
8.2.4.1 Toggling an LED by Value........................................................................173
8.2.4.2 Toggling an LED by Exclusive OR...........................................................173
8.2.4.3 Toggling an LED with the PINB Register................................................175
8.2.5 Configuring I/O Pins as Inputs..........................................................................177
8.3 Summary..................................................................................................................... 182

Chapter 9 • Assembly Language Elements 183


9.1 Instructions and Labels...........................................................................................184
9.2 The Preprocessor and Include Files.....................................................................185
9.3 Assembler Directives...............................................................................................187
9.3.1 Specifying Code and Data Locations...............................................................188
9.3.1.1 ORG Assembler Directive........................................................................188
9.3.1.2 CSEG Code Segment Directive...............................................................189
9.3.1.3 DSEG Data Segment and BYTE Directives...........................................190
9.3.1.4 ESEG EEPROM Segment Directive........................................................191
9.3.2 Reserving Memory.............................................................................................192
9.3.2.1 BYTE..........................................................................................................192
9.3.2.2 DB – Define Constant Byte......................................................................192
Modified Harvard Architecture.........................................................................192
Example Code using the DB Assembler Directive........................................192
CSEG Directive and First RJMP Instruction..................................................192
Usage of the DB Assembler Directive............................................................193
Data Formats and Null Terminating Strings...................................................193
Location of DB Data in Program Memory and Alignment.............................194
16-bit Address Pointer Registers....................................................................194

8
Loading the Z Register....................................................................................195
Reading Bytes from Program Memory...........................................................195
Running the Example Code.............................................................................196
9.3.2.3 DW – Define Constant Word....................................................................199
9.3.2.4 DD – Define Constant Double-word........................................................199
9.3.2.5 DQ – Define Constant Quad-word...........................................................199
9.3.3 Defining Names for Registers with DEF..........................................................200
9.3.4 Equating Names to Expressions using EQU and SET...................................200
9.3.5 Conditional Assembly........................................................................................201
9.4 Other Assembly Language Elements....................................................................203
9.5 Further Reading........................................................................................................204
9.6 Summary..................................................................................................................... 204

Chapter 10 • AVR Timing, Timers and Interrupts 205


10.1 Instruction Timing..................................................................................................206
10.1.1 AVR CPU Versions...........................................................................................206
10.1.2 Instruction Timing Examples...........................................................................206
10.1.2.1 The NOP Instruction...............................................................................206
10.1.2.2 16-bit and 32-bit Instruction Timing.......................................................208
10.1.2.3 Timing of Branch Instructions................................................................208
The Simulator Cycle Counter and Stop Watch..............................................210
Simulator Cycle Counter Bug..........................................................................211
10.2 Assembly Language Time Delay..........................................................................213
10.2.1 Calculating a Software Delay Subroutine Time Delay.................................213
10.2.1.1 Delay Subroutine Cycle Counter Measurement...................................214
10.2.1.2 Delay Subroutine Mathematical Formula.............................................215
10.2.2 A Better Software Delay Subroutine..............................................................226
10.2.2.1 Principle of Operation.............................................................................227
10.2.2.2 32-bit Subtraction with 8-bit Registers..................................................227
10.2.2.3 Timing of the Code..................................................................................229
10.2.2.4 Testing the Code in Microchip Studio...................................................230
10.2.3 A Changeable Software Time Delay Subroutine...........................................231
10.2.3.1 Splitting an Assembly Language Project into Files.............................234
10.2.3.2 How the wait_ms Subroutine Works.....................................................235
10.2.3.3 Limits of the wait_ms Subroutine..........................................................237
Simplifying the Formula...................................................................................238

9
Calculating the Maximum Delay Time of the Subroutine.............................238
Posing Value Limits in Assembly Language..................................................240
Number Size Limits in the Assembler Program.............................................243
Solution to Flagging Out of Range Upper Values.........................................246
Flagging Out of Range High and Low Values................................................247
10.2.4 Passing a Value to a Subroutine....................................................................251
10.2.4.1 Simplest Way to Pass a Value...............................................................251
10.2.4.2 Improved Way to Pass a Value..............................................................253
10.3 Calling an Assembly Subroutine from C Code.................................................261
10.3.1 Passing a Value to an Assembly Subroutine from C....................................261
10.3.1.1 Testing the wait_ms_c Project Code.....................................................263
10.3.1.2 How the wait_ms_c Project Code Works..............................................264
10.3.2 Returning a Value from an Assembly Subroutine in C.................................266
10.4 Polled Timer Delay..................................................................................................268
10.4.1 Timer/Counter0 Registers...............................................................................268
10.4.1.1 Timer/Counter0 Register Addresses.....................................................269
10.4.1.2 Using Counter/Timer0 as a Timer.........................................................270
10.4.2 Polled Timer Assembly Program....................................................................271
10.4.2.1 The IFNDEF Directive.............................................................................271
10.4.2.2 Left Shift Operator..................................................................................272
10.4.2.3 Register Write or Read-Modify-Write....................................................273
10.4.2.4 Initializing Timer 0...................................................................................273
10.4.2.5 Polling Timer 0........................................................................................275
10.4.2.6 Running the Code in the Simulator.......................................................276
10.4.3 Polled Timer C Program..................................................................................276
10.5 Timer Interrupt Delay.............................................................................................277
10.5.1 How Interrupts Work........................................................................................278
10.5.2 The Interrupt Vector Table...............................................................................278
10.5.3 Assembly Program Timer Interrupt.................................................................279
10.5.3.1 ATtiny13(A) Timer Interrupt Project.......................................................279
The Interrupt Vector Table...............................................................................280
Main Program Code.........................................................................................282
Interrupt Service Routine Code.......................................................................282
Running the Timer Interrupt Code..................................................................283
10.5.3.2 ATtiny25/45/85 Timer Interrupt Project.................................................283
10.5.3.3 Universal Timer Interrupt Project...........................................................285

10
10.5.4 C Program Timer Interrupt..............................................................................286
10.6 Summary................................................................................................................... 288

Chapter 11 • The AVR Instruction Set 289


11.1 AVR Instruction Set Overview and Categories..................................................290
11.2 A Guided Tour through the ATtiny AVR Instruction Set..................................290
11.2.1 Arithmetic and Logic Instructions...................................................................290
11.2.1.1 Add and Subtract Instructions................................................................290
Addition Instructions.........................................................................................290
Subtraction Instructions...................................................................................293
11.2.1.2 Logical Instructions.................................................................................294
Logical AND......................................................................................................294
Logical OR.........................................................................................................294
Logical Exclusive OR.......................................................................................294
11.2.1.3 Increment and Decrement Instructions.................................................294
11.2.1.4 Sign Change Instructions.......................................................................294
11.2.1.5 Bit Set and Clear Instructions................................................................295
11.2.1.6 Register Set, Clear and Test Instructions.............................................296
11.2.2 Branch Instructions..........................................................................................297
11.2.2.1 Jump Instructions....................................................................................297
11.2.2.2 Subroutine Call Instructions...................................................................299
11.2.2.3 Return from Subroutine and Interrupt Instructions..............................300
11.2.2.4 Compare Instructions..............................................................................300
11.2.2.5 Skip Instructions......................................................................................301
11.2.2.6 Branch Instructions.................................................................................302
Branch if Bit in SREG Set or Cleared.............................................................302
Branch if Equal or Not Equal – SREG Z Flag................................................303
Branch if Carry Set or Cleared – SREG C Flag............................................303
Branch if Same or Higher, or Lower – SREG C Flag....................................303
Branch if Minus or Plus – SREG N Flag........................................................304
Branch if Greater or Equal, or Less Than Signed – SREG S Flag..............304
Branch if Half Carry Flag Set or Cleared – SREG H Flag............................304
Branch if T Bit Set or Cleared – SREG T Flag..............................................304
Branch if Overflow Flag Set or Cleared – SREG V Flag..............................304
Branch if Interrupt Enabled or Disabled – SREG I Flag...............................304
11.2.3 Bit and Bit-test Instructions.............................................................................305

11
11.2.3.1 Bit Set and Clear Instructions................................................................305
11.2.3.2 Shift, Rotate and Swap Instructions......................................................305
Logical Shift Instructions.................................................................................305
Rotate through Carry Instructions...................................................................305
Logical Shift and Rotate Code Example........................................................305
Led Chaser Logical Shift Code Example.......................................................306
Arithmetic Shift and Swap Instructions..........................................................310
11.2.3.3 Status Register Bit Set and Clear Instructions.....................................310
Set or Clear SREG Bit.....................................................................................310
Store or Load SREG T Bit to/from Register...................................................310
Set or Clear Carry Flag – SREG C Bit...........................................................311
Set or Clear Negative Flag – SREG N Bit......................................................311
Set or Clear Zero Flag – SREG Z Bit.............................................................311
Set or Clear Global Interrupt Flag Bit – SREG I Bit......................................311
Set or Clear Sign Flag – SREG S Bit.............................................................312
Set or Clear Two’s Compliment Overflow Flag – SREG V Bit.....................312
Set or Clear T Bit in SREG – SREG T Bit......................................................312
Set and Clear Half Carry Flag – SREG H bit.................................................312
11.2.4 Data Transfer Instructions...............................................................................312
11.2.4.1 Move/Copy Instructions..........................................................................312
11.2.4.2 Load Immediate Instruction....................................................................313
11.2.4.3 Load Indirect Instructions.......................................................................313
11.2.4.4 Store Indirect Instructions......................................................................314
11.2.4.5 Load Indirect with Displacement............................................................315
11.2.4.6 Store Indirect with Displacement...........................................................315
11.2.4.7 Load Direct from SRAM..........................................................................315
11.2.4.8 Store Direct to SRAM..............................................................................316
11.2.4.9 Load Program Memory Instructions......................................................316
11.2.4.10 Store Program Memory........................................................................316
11.2.4.11 Port IN and OUT Instructions...............................................................316
11.2.4.12 PUSH and POP Instructions................................................................317
11.2.5 MCU Control Instructions................................................................................317
11.3 Entire AVR Instruction Set....................................................................................318
11.3.1 Number of AVR Instructions............................................................................318
11.3.1.1 Total Number of AVR Instructions..........................................................318
11.3.1.2 Number of ATtiny AVR Instructions........................................................318

12
11.3.1.3 Discrepancies in Documentation...........................................................318
11.3.2 Other AVR Instructions....................................................................................319
11.3.2.1 Arithmetic and Logic Instructions...........................................................319
11.3.2.2 Branch Instructions.................................................................................319
11.3.2.3 Data Transfer Instructions......................................................................320
11.3.3 Putting Instructions into Perspective..............................................................321
11.3.3.1 The Full AVR Instruction Set..................................................................321
11.3.3.2 The Plain AVR CPU.................................................................................321
11.3.3.3 Reduced AVRrc Core..............................................................................321
11.3.3.4 AVRe and AVRxt Cores...........................................................................321
11.3.3.5 AVRe+ CPU..............................................................................................322
11.3.3.6 AVRxm CPU.............................................................................................322

Chapter 12 • Software Tools and Settings 323


12.1 AVR Assembler Programs.....................................................................................324
12.1.1 The AVRASM2 Assembler...............................................................................324
12.1.1.1 Building a Project with AVRASM2.........................................................324
12.1.1.2 AVRASM2 on the Command Line..........................................................326
12.1.1.3 AVRASM2 Options in Microchip Studio................................................327
12.1.1.4 List File Options......................................................................................328
12.1.2 The AVR-AS Assembler...................................................................................329
12.1.3 The AVRA Assembler.......................................................................................329
12.2 The GNU C Toolchain.............................................................................................329
12.3 Where to from Here?..............................................................................................330
12.3.1 Objectives Achieved........................................................................................330
12.3.2 What was Not Covered....................................................................................332
12.3.3 Other AVR Microcontrollers.............................................................................332
12.3.4 Assembly Language Resources.....................................................................332

Appendix A • External Programmer Setup 333


A.1 Hobby USB Programmer Capabilities..................................................................334
A.1.1 Differences Between Hobby Programmers and the Atmel-ICE.....................334
A.1.2 Programming Interfaces....................................................................................335
A.1.3 Using Hobby USB Programmers with this Book.............................................335
A.2 Overview of External Programmer Setup............................................................336
A.3 External Programmer Setup...................................................................................336

13
A.3.1 Install a Driver....................................................................................................336
A.3.1.1 USBasp Driver..........................................................................................336
A.3.1.2 USBtinyISP Driver....................................................................................339
A.3.1.3 Arduino Uno Sketch..................................................................................341
A.3.2 Download and Install avrdude..........................................................................342
A.3.3 Build an AVR Circuit and Connect a Programmer..........................................343
A.3.3.1 Connecting a USBasp..............................................................................344
10-pin Male Header..........................................................................................344
10-pin Female Connector on Ribbon Cable..................................................344
6-pin Adapter (ISP/SPI)...................................................................................345
A.3.3.2 Connecting a USBtinyISP........................................................................345
6-pin Male Header............................................................................................345
6-pin Female Connector on Ribbon Cable.....................................................345
A.3.3.3 Connecting an Arduino Uno ArduinoISP................................................345
A.3.4 Programming Parameters.................................................................................346
A.3.4.1 Documentation for avrdude.....................................................................346
A.3.4.2 Parameters for avrdude Explained.........................................................346
A.3.4.3 Parameters for a USBasp........................................................................349
A.3.4.4 Parameters for a USBtinyISP..................................................................350
A.3.4.5 Parameters for an ArduinoISP.................................................................350
A.3.5 Microchip Studio External Tool Setup..............................................................350
A.3.5.1 Open an Assembly or C Project..............................................................351
A.3.5.2 Add an External Tool in Microchip Studio...............................................351
USBasp..............................................................................................................352
USBtinyISP........................................................................................................352
ArduinoISP (Arduino Uno with ArduinoISP sketch loaded)..........................352
A.3.5.3 Add a Toolbar Button for the External Tool............................................353
A.3.6 Testing the Programmer....................................................................................355
A.3.7 Programming Problems and Solutions............................................................355

Appendix B • Alternate Circuits and Programs 357


B.1 8-Pin PDIP ATtiny13/25/45/85.................................................................................358
B.2 14-Pin PDIP ATtiny24/44/84....................................................................................359
B.3 20-Pin PDIP ATtiny26/261/461/861 and ATtiny2313/4313..................................360
B.4 28-Pin PDIP ATtiny48/88..........................................................................................361
B.5 Alternate Programs..................................................................................................362

14
B.5.1 Alternate LED Blink Assembly Program..........................................................363
B.5.2 Alternate LED Blink C Program........................................................................363

Appendix C • The ASCII Table 365


C.1 Printable Characters................................................................................................365
C.2 ASCII Table.................................................................................................................365

Index 367

15

You might also like