Contents - Explore ATtiny Microcontrollers Using C and Assembly Language
Contents - Explore ATtiny Microcontrollers Using C and Assembly Language
books books
IN
-D
EP
TH
AVR Architecture and Programming AVR Architecture and Programming
Warwick A. Smith
● This is an Elektor Publication.
● All rights reserved. No part of this book may be reproduced in any material form,
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.
A catalogue record for this book is available from the British Library
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
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
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
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
6
6.9.2 I/O Direct Addressing.........................................................................................131
6.9.3 Other Addressing Modes...................................................................................132
6.10 Summary................................................................................................................... 132
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
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
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
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
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
14
B.5.1 Alternate LED Blink Assembly Program..........................................................363
B.5.2 Alternate LED Blink C Program........................................................................363
Index 367
15