Professional Documents
Culture Documents
Beginner en Avr Assembly
Beginner en Avr Assembly
Assembly Language of
ATMEL-AVR-Microprocessors
by
Gerhard Schmidt
http://www.avr-asm-tutorial.net
February 2011
History:
Added page on assembler concept in February 2011
Added chapter on code structures in April 2009
Additional corrections and updates as of January 2008
Corrected version as of July 2006
riginal version as of !ecember 200"
Avr-Asm-Tutorial 1 http://www.avr-asm-tutorial.net
Content
1 Why learning Assembler?......................................................................................................................1
2 The concept behind the language assembler in micro-controllers.........................................................2
2.1 The hardware of micro-controllers.................................................................................................2
2.2 How the C! wor"s.......................................................................................................................2
2.# $nstructions in assembler................................................................................................................#
2.% &ifference to high-le'el languages.................................................................................................#
2.( Assembler is not machine language...............................................................................................#
2.) $nterpreting and assembler..............................................................................................................%
2.* High le'el languages and Assembler..............................................................................................%
2.+ What is really easier in assembler?................................................................................................(
# Hardware for A,--Assembler-rogramming.......................................................................................)
#.1 The $. $nterface of the A,- processor family.............................................................................)
#.2 rogrammer for the C-arallel-ort.............................................................................................)
#.# /0perimental boards.......................................................................................................................*
#.#.1 /0perimental board with an ATtiny1#...................................................................................*
#.#.2 /0perimental board with an AT12.2#1#3ATmega2#1#........................................................+
#.% -eady-to-use commercial programming boards for the A,--family............................................1
#.%.1 .T4222...................................................................................................................................1
#.%.2 .T4(22...................................................................................................................................1
#.%.# A,- &ragon.........................................................................................................................12
% Tools for A,- assembly programming...............................................................................................11
%.1 The editor.....................................................................................................................................11
%.1.1 A simple typewriter..............................................................................................................11
%.1.2 .tructuring assembler code...................................................................................................12
%.2 The assembler...............................................................................................................................1(
%.# rogramming the chips.................................................................................................................1)
%.% .imulation in the studio................................................................................................................1)
( What is a register?................................................................................................................................21
(.1 &ifferent registers.........................................................................................................................22
(.2 ointer-registers............................................................................................................................22
(.2.1 Accessing memory locations with pointers..........................................................................22
(.2.2 -eading program flash memory with the 5 pointer..............................................................22
(.2.# Tables in the program flash memory....................................................................................2#
(.2.% Accessing registers with pointers.........................................................................................2#
(.# -ecommendation for the use of registers.....................................................................................2%
) orts.....................................................................................................................................................2(
).1 What is a ort?.............................................................................................................................2(
).2 Write access to ports.....................................................................................................................2(
).# -ead access to ports......................................................................................................................2)
).% -ead-6odify-Write access to ports..............................................................................................2)
).( 6emory mapped port access........................................................................................................2)
).) &etails of rele'ant ports in the A,-............................................................................................2)
).* The status register as the most used port......................................................................................2*
).+ ort details....................................................................................................................................2+
* .-A6..................................................................................................................................................21
*.1 What is .-A6?...........................................................................................................................21
*.2 7or what purposes can $ use .-A6?...........................................................................................21
*.# How to use .-A6?......................................................................................................................21
*.#.1 &irect addressing..................................................................................................................21
*.#.2 ointer addressing.................................................................................................................#2
*.#.# ointer with offset.................................................................................................................#2
*.% !se of .-A6 as stac"..................................................................................................................#2
*.%.1 &efining .-A6 as stac"......................................................................................................#2
*.%.2 !se of the stac".....................................................................................................................#1
*.%.# Common bugs with the stac" operation................................................................................#1
+ 8umping and branching........................................................................................................................##
+.1 Controlling se9uential e0ecution of the program.........................................................................##
+.2 :inear program e0ecution and branches.......................................................................................#%
+.# Timing during program e0ecution................................................................................................#(
+.% 6acros and program e0ecution....................................................................................................#(
+.( .ubroutines...................................................................................................................................#(
Avr-Asm-Tutorial 2 http://www.avr-asm-tutorial.net
+.) $nterrupts and program e0ecution.................................................................................................#*
1 Calculations..........................................................................................................................................#1
1.1 ;umber systems in assembler......................................................................................................#1
1.1.1 ositi'e whole numbers <bytes= words= etc.>........................................................................#1
1.1.2 .igned numbers <integers>....................................................................................................#1
1.1.# ?inary Coded &igits= ?C&...................................................................................................#1
1.1.% ac"ed ?C&s........................................................................................................................%2
1.1.( ;umbers in A.C$$-format....................................................................................................%2
1.2 ?it manipulations.........................................................................................................................%2
1.# .hift and rotate.............................................................................................................................%1
1.% Adding= subtracting and comparing..............................................................................................%2
1.%.1 Adding and subtracting 1)-bit numbers................................................................................%2
1.%.2 Comparing 1)-bit numbers...................................................................................................%2
1.%.# Comparing with constants....................................................................................................%2
1.%.% ac"ed ?C& math.................................................................................................................%#
1.( 7ormat con'ersion for numbers....................................................................................................%#
1.(.1 Con'ersion of pac"ed ?C&s to ?C&s= A.C$$ or ?inaries...................................................%#
1.(.2 Con'ersion of ?inaries to ?C&............................................................................................%%
1.) 6ultiplication...............................................................................................................................%%
1.).1 &ecimal multiplication.........................................................................................................%%
1.).2 ?inary multiplication............................................................................................................%%
1.).# A,- assembler program......................................................................................................%(
1.).% ?inary rotation......................................................................................................................%)
1.).( 6ultiplication in the studio...................................................................................................%)
1.* Hardware multiplication...............................................................................................................%+
1.*.1 Hardware multiplication of +-by-+-bit binaries....................................................................%+
1.*.2 Hardware multiplication of a 1)- by an +-bit-binary............................................................%1
1.*.# Hardware multiplication of a 1)- by a 1)-bit-binary............................................................(2
1.*.% Hardware multiplication of a 1)- by a 2%-bit-binary............................................................(2
1.+ &i'ision........................................................................................................................................(#
1.+.1 &ecimal di'ision...................................................................................................................(#
1.+.2 ?inary di'ision......................................................................................................................(%
1.+.# rogram steps during di'ision..............................................................................................(%
1.+.% &i'ision in the simulator......................................................................................................((
1.1 ;umber con'ersion......................................................................................................................()
1.12 &ecimal 7ractions.......................................................................................................................(*
1.12.1 :inear con'ersions..............................................................................................................(*
1.12.2 /0ample 1@ +-bit-A&-con'erter with fi0ed decimal output................................................(+
1.12.# /0ample 2@ 12-bit-A&-con'erter with fi0ed decimal output..............................................(1
12 roAect planning.................................................................................................................................)2
12.1 How to plan an A,- proAect in assembler.................................................................................)2
12.2 Hardware considerations............................................................................................................)2
12.# Considerations on interrupt operation........................................................................................)2
12.#.1 ?asic re9uirements of interrupt-dri'en operation...............................................................)1
12.#.2 /0ample for an interrupt-dri'en assembler program..........................................................)1
12.% Considerations on timing............................................................................................................)#
11 Anne0.................................................................................................................................................)%
11.1 $nstructions sorted by function...................................................................................................)%
11.2 &irecti'es and $nstruction lists in alphabetic order....................................................................))
11.2.1 Assembler directi'es in alphabetic order............................................................................))
11.2.2 $nstructions in alphabetic order...........................................................................................)*
11.# ort details..................................................................................................................................)1
11.#.1 .tatus--egister= Accumulator flags....................................................................................)1
11.#.2 .tac"pointer........................................................................................................................)1
11.#.# .-A6 and /0ternal $nterrupt control................................................................................)1
11.#.% /0ternal $nterrupt Control...................................................................................................*2
11.#.( Timer $nterrupt Control......................................................................................................*2
11.#.) Timer3Counter 2..................................................................................................................*1
11.#.* Timer3Counter 1..................................................................................................................*2
11.#.+ Watchdog-Timer.................................................................................................................*#
11.#.1 //-B6............................................................................................................................*#
11.#.12 .erial eripheral $nterface .$..........................................................................................*%
11.#.11 !A-T...............................................................................................................................*(
Avr-Asm-Tutorial 3 http://www.avr-asm-tutorial.net
11.#.12 Analog Comparator..........................................................................................................*(
11.#.1# $3B orts............................................................................................................................*)
11.% orts= alphabetic order................................................................................................................*)
11.( :ist of abbre'iations...................................................................................................................**
Avr-Asm-Tutorial 1 http://www.avr-asm-tutorial.net
!hy learning Assembler"
Assembler or other languages# that is the $uestion% &hy should ' learn another language# if ' already learned other
programming languages( )he best argument* +hile you live in France you are able to get through by spea,ing -nglish# but
you +ill never feel at home then# and life remains complicated% .ou can get through +ith this# but it is rather
inappropriate% 'f things need a hurry# you should use the country/s language%
0any people that are deeper into programming A12s and use higher3level languages in their daily +or, recommend that
beginners start +ith learning assembly language% )he reason is that sometimes# namely in the follo+ing cases*
if bugs have to be analy4ed#
if the program e5ecutes different than designed and e5pected#
if the higher3level language doesn/t support the use of certain hard+are features#
if time3critical in line routines re$uire assembly language portions#
it is necessary to understand assembly language# e% g% to understand +hat the higher3level language compiler produced%
&ithout understanding assembly language you do not have a chance to proceed further in these cases%
Short and easy
Assembler instructions translate one by one to e5ecuted machine instructions% )he processor needs only to e5ecute +hat
you +ant it to do and +hat is necessary to perform the tas,% 6o e5tra loops and unnecessary features blo+ up the
generated code% 'f your program storage is short and limited and you have to optimi4e your program to fit into memory#
assembler is choice 1% 7horter programs are easier to debug# every step ma,es sense%
Fast and quick
8ecause only necessary code steps are e5ecuted# assembly programs are as fast as possible% )he duration of every step is
,no+n% )ime critical applications# li,e time measurements +ithout a hard+are timer# that should perform e5cellent# must
be +ritten in assembler% 'f you have more time and don/t mind if your chip remains 999 in a +ait state type of operation#
you can choose any language you +ant%
Assembler is easy to learn
't is not true that assembly language is more complicated or not as easy to understand than other languages% :earning
assembly language for +hatever hard+are type brings you to understand the basic concepts of any other assembly
language dialects% Adding other dialects later is easy% As some features are hard+are3dependent optimal code re$uires
some familiarity +ith the hard+are concept and the dialect% &hat ma,es assembler sometimes loo, complicated is that it
re$uires an understanding of the controller/s hard+are functions% Consider this an advantage* by learning assembly
language you simultaneously learn more about the hard+are% ;igher level languages often do not allo+ you to use special
hard+are features and so hide these functions%
)he first assembly code does not loo, very attractive# +ith every 100 additional lines programmed it loo,s better% <erfect
programs re$uire some thousand lines of code of e5ercise# and optimi4ation re$uires lots of +or,% )he first steps are hard
in any language% After some +ee,s of programming you +ill laugh if you go through your first code% 7ome assembler
instructions need some months of e5perience%
AVRs are ideal for learning assembler
Assembler programs are a little bit silly* the chip e5ecutes anything you tell it to do# and does not as, you if you are sure
over+riting this and that% All protection features must be programmed by you# the chip does e5actly anything li,e it is told#
even if it doesn/t ma,e any sense% 6o +indo+ +arns you# unless you programmed it before%
)o correct typing errors is as easy or complicated as in any other language% 8asic design errors# the more tric,y type of
errors# are also as complicated to debug li,e in any other computer language% 8ut* testing programs on A)0-: chips is very
easy% 'f it does not do +hat you e5pect it to do# you can easily add some diagnostic lines to the code# reprogram the chip
and test it% 8ye# bye to you -<20 programmers# to the =1 lamps used to erase your test program# to you pins that don/t
fit into the soc,et after having them removed some do4en times%
Changes are no+ programmed fast# compiled in no time# and either simulated in the studio or chec,ed in3circuit% 6o pin is
removed# and no =1 lamp gives up >ust in the moment +hen you had your e5cellent idea about that bug%
Test it!
8e patient doing your first steps? 'f you are familiar +ith another @high3levelA language* forget it for the first time% 8ehind
every assembler language there is a certain hard+are concept% 0ost of the special features of other computer languages
don/t ma,e any sense in assembler%
)he first five instructions are not easy to learn# after that your learning speed rises fast% After you had your first lines* grab
the instruction set list and lay bac, in the bathtub# +ondering +hat all the other instructions are li,e%
7erious +arning* !on/t try to program a mega3machine to start +ith% )his does not ma,e sense in any computer language#
and >ust produces frustration% 7tart +ith the small B;ello +orldC3li,e e5amples# e% g% turning some :-!s on and off for a
certain time# then e5plore the hard+are features a bit deeper%
2ecommendation* Comment your subroutines and store them in a special directory# if debugged* you +ill need them again
in a short time%
;ave success?
Avr-Asm-Tutorial 2 http://www.avr-asm-tutorial.net
# The concept behind the language
assembler in micro-controllers
Attention? )hese pages are on programming micro3controllers# not on <Cs +ith :inu53 or &indo+s operating systems and
similar elephants# but on a small mice% 't is not on programming -thernet mega3machines# but on the $uestion +hy a
beginner should start +ith assembler and not +ith a comple5 high3level language%
)his page sho+s the concept behind assembler# +hat those familiar +ith high3level languages have to give up to learn
assembler and +hy assembler is not machine language%
2.1 The hardware of micro-controllers
&hat has the hard+are to do +ith assembler( 0uch# as can be seen from the follo+ing%
)he concept behind assembler is to ma,e the hard+are resources of the processor accessible% 2esources means all
hard+are components# li,e
D the central processing unit @C<=A and its math servant# the arithmetic and logic unit @A:=A#
D the diverse storage units @internal and e5ternal 2A0# --<20 storageA#
D the ports that control characteristics of port3bits# timers# A! converters# and other devices%
Accessible means directly accessible and not via drivers or other interfaces# that an operating system provides% )hat
means# you control the serial interface or the A! converter# not some other layer bet+een you and the hard+are% As
a+ard for your efforts# the complete hard+are is at your command# not only the part that the compiler designer and the
operating system programmer provides for you%
2.2 How the CPU works
0ost important for understanding assembler is to understand ho+ the C<= +or,s% )he C<= reads instructions @instruction
fetchA from the program storage @the flashA# translates those into e5ecutable steps and e5ecutes those% 'n A12s# those
instructions are +ritten as 16 bit numbers to the flash storage# and are read from there @first stepA% )he number read then
translates @second stepA e% g% to transporting the content of the t+o registers 20 and 21 to the A:= @third stepA# to add
those @fourth stepA and to +rite the result into the register 20 @fifth stepA% 2egisters are simple 8 bit +ide storages that can
directly be tied to the A:= to be read from and to be +ritten to%
)he coding of instructions is demonstrated by some e5amples%
CPU operation Code (binary Code (he!
7end C<= to sleep 1001%0101%1000%1000 9E88
Add register 21 to register 20 0000%1100%0000%0001 0C01
7ubtract register 21 from register 20 0001%1000%0000%0001 1801
&rite constant 1F0 to register 216 1110%1010%0000%1010 -A0A
0ultiply register 2" +ith register 22 and +rite the result to registers 21 @078A and
20 @:78A
1001%1100%0011%0010 9C"2
7o# if the C<= reads he5 9E88 from the flash storage# it stops its operation and does not fetch instructions any more% !on/t
be afraid# there is another mechanism necessary before the C<= e5ecutes this% And you can +a,e up the C<= from that%
Executing instructions
'f the C<= reads he5 0C01# 20 and 21 is added and the result is +ritten to register 20% )his is e5ecuted li,e demonstrated in
the picture%
First the
instruction
+ord @16 bitA
is read from
the flash and
translated to
e5ecutable
steps @1A%
)he ne5t step
connects the
registers to
the A:=
inputs# and
adds their content @2A%
6e5t# the result is +ritten to the register @"A%
Avr-Asm-Tutorial 3 http://www.avr-asm-tutorial.net
'f the C<= reads he5 9C2" from the flash# the registers 2" and 22 are muliplied and the result is +ritten to 21 @upper 8 bitsA
and 20 @lo+er 8 bitsA% 'f the A:= is not e$uipped +ith hard+are for multiplication @e% g% in an A)tiny1"A# the 9C2" does
nothing at all% 't doesn/t even open an error +indo+ @the tiny1" doesn/t have that hard+areA?
'n principle the C<= can e5ecute 6E#E"6 @163bitA different instructions% 8ut because not only 1F0 should be +ritten to a
specific register# but values bet+een 0 and 2EE to any register bet+een 216 and 2"1# this load instruction re$uires 2E6D16
G H#096 of the 6E#E"6 theoretically possible instructions% )he direct load instruction for the constant c @cF%%c0A and registers
r @r"%%r0# rH is al+ays 1 and not encodedA is coded li,e this*
8it 1E 1H 1" 12 11 10 9 8 F 6 E H " 2 1 0
:!' 2#C 1 1 1 0 cF c6 cE cH r" r2 r1 r0 c" c2 c1 c0
&hy those bits are placed li,e this in the instruction +ord remains A)0-:/s secret%
Addition and subtraction re$uire "2D"2 G 1#02H combinations and the target registers 20%%2"1 @tH%%t0A and source registers
20%%2"1 @sH%%s0A are coded li,e this*
8it 1E 1H 1" 12 11 10 9 8 F 6 E H " 2 1 0
A!! 2t#2s 0 0 0 0 1 1 sH tH t" t2 t1 t0 s" s2 s1 70
7=8 2t#2s 0 0 0 1 1 0 sH tH t" t2 t1 t0 s" s2 s1 s0
<lease# do not learn these bit placements# you +ill not need them later% Just understand ho+ an instruction +ord is coded
and e5ecuted%
2." #nstr$ctions in assembler
)here is no need to learn 163bit numbers and the cra4y placement of bits +ithin those# because in assembler you/ll use
human3readable abbreviations for that# so3called mnemonics# an aid to memory% )he assembler representation for he5
9E88 is simply the abbreviation I7:--<I% 'n contrast to 9E88# 7:--< is easy to remember% -ven for someone li,e me that
has difficulties in remembering its o+n phone number%
Adding simply is IA!!I% For naming the t+o registers# that are to be added# they are +ritten as parameters% @6o# not in
brac,ets% C programmers# forget those brac,ets% .ou don/t need those in assembler%A 7imply type IA!! 20#21I% )he line
translates to a single 16 bit +ord# 0C01% )he translation is done by the assembler%
)he C<= only understands 0C01% )he assembler translates the line to this 16 bit +ord# +hich is +ritten to the flash storage#
read from the C<= from there and e5ecuted% -ach instruction that the C<= understands has such a mnemonic% And vice
versa* each mnemonic has e5actly one corresponding C<= instruction +ith a certain course of actions% )he ability of the
C<= determines the e5tent of instructions that are available in assembler% )he language of the C<= is the base# the
mnemonics only represent the abilities of the C<= itself%
2.% &ifference to hi'h-le(el lan'$a'es
;ere some hints for high3level programmers% 'n high3level languages the constructions are not depending from the
hard+are or the abilities of a C<=% )hose constructions +or, on very different processors# if there is a compiler for that
language and for the processor family available% )he compiler translates those language constructions to the processor/s
binary language% A J) in 8asic loo,s li,e a J0< in assembler# but there is a difference in the +hole concept bet+een
those t+o%
A transfer of program code to another processor hard+are does only +or, if the hard+are is able to do the same% 'f a
processor C<= doesn/t have access to a 16 bit timer# the compiler for a high3level language has to simulate one# using an 83
bit timer and some time3consuming code% 'f three timers are available# and the compiler is +ritten for only t+o or a single
timer# the available hard+are remains unused% 7o you totally depend on the compiler/s abilities# not on the C<=/s abilities%
Another e5ample +ith the above sho+n instruction I0=:I% 'n assembler# the target processor determines if you can use
this instruction or if you have to +rite a multiplication routine% 'f# in a high3level language# you use a multiplication the
compiler inserts a math library that multiplies every ,ind of numbers# even if you have only 83by383bit numbers and 0=:
alone +ould do it% )he lib offers an integer# a long3+ord and some other routines for multiplications that you don/t need% A
+hole pac,age of things you don/t really need% 7o you run out of flash in a small tiny A12# and you change to a mega +ith
"E unused port pins% r an 5mega# >ust to get your elefant lib +ith superfluous routines into the flash% )hat is +hat you get
from a simple IDI# +ithout even being as,ed%
2.) *ssembler is not machine lan'$a'e
8ecause assembler is closer to the hard+are than any other language# it is often called machine language% )his is not e5act
because the C<= only understands 16 bit instruction +ords in binary form% )he string IA!! 20#21I cannot be e5ecuted%
And assembler is much simpler than machine language% 7imilarities bet+een machine language and assembler are a
feature# not a bug%
Avr-Asm-Tutorial 4 http://www.avr-asm-tutorial.net
2.+ #nterpretin' and assembler
&ith an interpreter the C<= first translates the human3readable code into binary +ords that can be e5ecuted then% )he
interpreter +ould
D first read the te5t stream IA G A K 8I @nine characters of one byte eachA#
D strip the four blan,s from the te5t#
D locate the variables A and 8 @location in registers or in 72A0# precisionLlength# etc%A#
D identify the plus sign as operator#
D prepare a machine e5ecutable se$uence that is e$uivalent to the formulation in the te5t%
'n the conse$uence# probably a simple machine code li,e IA!! 20#21I @in AssemblerA +ould result% 8ut most probably the
resulting machine code +ould be multiple +ords long @read and +rite variables fromLto 72A0# 163bit3integer adding#
register savingLrestoring on stac,# etc%# etc%A%
)he difference bet+een the interpreter and the assembling is that# after assembling# the C<= gets its favored meal#
e5ecutable +ords# directly% &hen interpreting the C<= is# during most of the time# performing the translation tas,%
)ranslation probably re$uires 20 or 200 C<= steps# before the three or four +ords can be e5ecuted% -5ecution speed so is
more than lame% &hile this is no problem if one uses a fast cloc, speed# it is inappropriate in time critical situations# +here
fast response to an event is re$uired% 6o one ,no+s +hat the C<= is >ust doing and ho+ long this re$uires%
6ot having to thin, about timing issues leads to the inability of the human programmer to resolve timing issues# and
missing information on timing ,eeps him unable to do those things# if re$uired%
2., Hi'h le(el lan'$a'es and *ssembler
;igh level languages insert additional nontransparent separation levels bet+een the C<= and the source code% An e5ample
for such an nontransparent concept are variables% )hese variables are storages that can store a number# a te5t string or a
single 8oolean value% 'n the source code# a variable name represents a place +here the variable is located# and# by
declaring variables# the type @numbers and their format# strings and their length# etc%A%
For learning assembler# >ust forget the high level language concept of variables% Assembler only ,no+s bits# bytes# registers
and 72A0 bytes% )he term IvariableI has no meaning in assembler% Also# related terms li,e ItypeI are useless and do not
ma,e any sense here%
;igh level languages re$uire you to declare variables prior to their first use in the source code# e% g% as 8yte @83bitA# double
+ord @163bitA# integer @1E3bit plus 1 sign bitA% Compilers for that language place such declared variables some+here in the
available storage space# including the "2 registers% 'f this placement is selected rather blind by the compiler or if there is
some priority rule used# li,e the assembler programmer carefully does it# is depending more from the price of the
compiler% )he programmer can only try to understand +hat the compiler IthoughtI +hen he placed the variable% )he
po+er to decide has been given to the compiler% )hat IrelievesI the programmer from the trouble of that decision# but
ma,es him a slave of the compiler%
)he instruction IA G A K 8I is no+ type3proofed* if A is defined as a character and 8 a number @e% g% G 2A# the formulation
isn/t accepted because character codes cannot be added +ith numbers% <rogrammers in high level languages believe that
this type chec, prevents them from programming nonsense% )he protection# that the compiler provides in this case by
prohibiting your type error# is rather useless* adding 2 to the character IFI of course should yield a I;I as result# +hat
else( Assembler allo+s you to do that# but not a compiler%
Assembler allo+s you to add numbers li,e F or H8 to add and subtract to every byte storage# no matter +hat type of thing
is in the byte storage% &hat is in that storage# is a matter of decision by the programmer# not by a compiler% 'f an operation
+ith that content ma,es sense is a matter of decision by the programmer# not by the compiler% 'f four registers represent a
"23bit3value or four A7C'' characters# if those four bytes are placed lo+3to3high# high3to3lo+ or completely mi5ed# is >ust up
to the programmer% ;e is the master of placement# no one else% )ypes are un,no+n# all consists of bits and bytes
some+here in the available storage place% )he programmer has the tas, of organi4ing# but also the chance of optimi4ing%
f a similar effect are all the other rules# that the high level programmer is limited to% 't is al+ays claimed that it is saver
and of a better overvie+ to program anything in subroutines# to not >ump around in the code# to hand over variables as
parameters# and to give bac, results from functions% Forget most of those rules in assembler# they don/t ma,e much sense%
Jood assembler programming re$uires some rules# too# but very different ones% And# +hat/s the best* most of them have
to be created by yourself to help yourself% 7o* +elcome in the +orld of freedom to do +hat you +ant# not +hat the
compiler decides for you or +hat theoretical professors thin, +ould be good programming rules%
;igh level programmers are addicted to a number of concepts that stand in the +ay of learning assembler* separation in
different access levels# in hard+are# drivers and other interfaces% 'n assembler this separation is complete nonsense#
separation +ould urge you to numerous +or,arounds# if you +ant to solve your problem in an optimal +ay%
8ecause most of the high level programming rules don/t ma,e sense# and because even puristic high level programmers
brea, their o+n rules# +henever appropriate# see those rules as a nice cage# preventing you from being creative% )hose
$uestions don/t play a role here% -verything is direct access# every storage is available at any time# nothing prevents your
access to hard+are# anything can be changed 3 and even can be corrupted% 2esponsibility remains by the programmer
only# that has to use his brain to avoid conflicts +hen accessing hard+are%
)he other side of missing protection mechanisms is the freedom to do everything at any time% 7o# smash your ties a+ay to
start learning assembler% .ou +ill develop your o+n ties later on to prevent yourself from running into errors%
Avr-Asm-Tutorial 5 http://www.avr-asm-tutorial.net
2.- .hat is really easier in assembler/
All +ords and concepts that the assembler programmer needs is in the datasheet of the processor* the instruction and the
port table% !one? &ith the +ords found there anything can be constructed% 6o other documents necessary% ;o+ the timer
is started @is +riting I)imer%7tart@8AI someho+ easier to understand than I:!' 216#0502M and C=) )CC20#216I(A# ho+ the
timer is restarted at 4ero @IC:2 216M and C=) )CC20#216IA# it is all in the data sheet% 6o need to consult a more or less
good documentation on ho+ a compiler defines this or that% 6o special# compiler3designed +ords and concepts to be
learned here# all is in the datasheet% 'f you +ant to use a certain timer in the processor for a certain purpose in a certain
mode of the 1E different possible modes# nothing is in the +ay to access the timer# to stop and start it# etc%
&hat is in a high level language easier to +rite IA G A K 8I instead of I0=: 216#21FI( 6ot much% 'f A and 8 aren/t defined
as bytes or if the processor type is tiny and doesn/t understand 0=:# the simple 0=: has to be e5changed +ith some
other source code# as designed by the assembler programmer or copyLpasted and adapted to the needs% 6o reason to
import an nontransparent library instead# >ust because you/re to la4y to start your brain and learn%
Assembler teaches you directly ho+ the processor +or,s% 8ecause no compiler ta,es over your tas,s# you are completely
the master of the processor% )he re+ard for doing this +or,# you are granted full access to everything% 'f you +ant# you can
program a baud3rate of HE%HE bps on the =A2)% A speed setting that no &indo+s <C allo+s# because the operating system
allo+s only multiples of FE @&hy( 8ecause some historic mechanical teletype +riters had those special mechanical gear
bo5es# allo+ing $uic, selection of either FE or "00 bps%A% 'f# in addition# you +ant 1 and a half stop bytes instead of either 1
or 2# +hy not programming your o+n serial device +ith assembler soft+are% 6o reason to give things up%
&ho is able to program in assembler has a feeling for +hat the processor allo+s% &ho changes from assembler to a higher
level language later on# e% g% in case of very comple5 tas,s# has made the decision to select that on a rational basis% 'f
someone s,ips learning assembler he has to do +hat he can# stic,s to the available libraries and programs creative
+or,arounds for things that the compiler doesn/t allo+# and in a +ay that assembler programmers +ould laugh at loud%
)he +hole +orld of the processor is at the assembler programmer/s command# so +hy do complicated and highly sensitive
+or,arounds on something you can formulate in a nice# lean# esthetic +ay(
Avr-Asm-Tutorial 6 http://www.avr-asm-tutorial.net
$ %ard&are for AVR-Assembler-
'rogramming
:earning assembler re$uires some simple hard+are e$uipment to test your programs# and see if it +or,s in practice%
)his section sho+s t+o easy schematics that enable you to home bre+ the re$uired hard+are and gives you the necessary
hints on the re$uired bac,ground% )his hard+are really is easy to build% ' ,no+ nothing easier than that to test your first
soft+are steps% 'f you li,e to ma,e more e5periments# leave some more space for future e5tensions on your e5perimental
board%
'f you don/t li,e the smell of soldering# you can buy a ready3to3use board# too% )he available boards are characteri4ed in
this section belo+%
".1 The #0P #nterface of the *12 processor family
8efore going into practice# +e have to learn a fe+ essentials on the serial programming mode of the A12 family% 6o# you
don/t need three different voltages to program and read an A12 flash memory% 6o# you don/t need another pre3
programmed microprocessor to program the A12s% 6o# you don/t need 10 'L lines to tell the chip +hat you li,e it to do%
And you don/t even have to remove the A12 from the soc,et on your your e5perimental board# before programming it% 't/s
even easier than that%
All this is done by a build3in interface in the A12 chips# that enable you to +rite and read the content of the program flash
and the built3in3--<20% )his interface +or,s serially and needs only three signal lines*
7CN* A cloc, signal that shifts the bits to be +ritten to the memory into an internal shift register# and that
shifts out the bits to be read from another internal shift register#
07'* )he data signal that sends the bits to be +ritten to the A12#
0'7* )he data signal that receives the bits read from the A12%
)hese three signal pins are internally connected to the programming machine only if you change
the 2-7-) @sometimes also called 27) or restartA pin to 4ero% ther+ise# during normal operation
of the A12# these pins are programmable 'L lines li,e all the others%
'f you li,e to use these pins for other purposes during normal operation# and for in3system3
programming# you/ll have to ta,e care# that these t+o purposes do not conflict% =sually you then
decouple these by resistors or by use of a multiple5er% &hat is necessary in your case# depends
from your use of the pins in the normal operation mode% .ou/re luc,y# if you can use them for in3
system3programming e5clusively%
6ot necessary# but recommendable for in3system3programming is# that you supply the
programming hard+are out of the supply voltage of your system% )hat ma,es it easy# and
re$uires t+o additional lines bet+een the programmer and the A12 board% J6! is the common
ground or negative pole of the supply voltage# 1)J @target voltageA the supply voltage @usually
KE%0 voltsA% )his adds up to 6 lines bet+een the programmer hard+are and the A12 board% )he
resulting '7<6 connection# as defined by A)0-:# is sho+n on the left%
7tandards al+ays have alternative standards# that +ere used earlier% )his is the technical basis
that constitutes the adapter industry% 'n our case the alternative standard +as designed as
'7<10 and +as used on the 7)N200 board# sometimes also called CA6!A interface% 't/s still a very
+idespread standard# and even the more recent 7)NE00 board is e$uipped +ith it% '7<10 has an
additional signal to drive a red :-!% )his :-! signals that the programmer is doing his >ob% A
good idea% Just connect the :-! to a resistor and clamp it the positive supply voltage%
".2 Pro'rammer for the PC-Parallel-Port
6o+# heat up your soldering iron and build up your programmer% 't is a $uite easy schematic and +or,s +ith standard
parts from your +ell3sorted e5periments bo5%
.es# that/s all you need to program an A12% )he 2E3pin plug goes into the parallel port of your <C# the 103pin '7< goes to
your A12 e5perimental board% 'f your bo5 doesn/t have a FH:72HE# you can also use a FH;C2HE @+ith no hard+are
changesA or a FH:72HHLFH;C2HH @by changing some pins and signalsA% 'f you use ;C# don/t forget to tie unused inputs
either to J6! or the supply voltage# other+ise the buffers might produce e5tra noise by capacitive s+itching%
)he necessary program algorithm is done by the '7< soft+are% 8e a+are that this parallel port interface is not supported by
A)0-:/s studio soft+are any more% 7o# if you +ant to program your A12 directly from +ithin the studio# use different
programmers% )he 'nternet provides several solutions%
Avr-Asm-Tutorial 7 http://www.avr-asm-tutorial.net
'f you already have a programming board# you +ill not need to build this programmer# because you/ll find the '7< interface
on some pins% Consult your handboo, to locate these%
"." 3!perimental boards
.ou probably +ant to do your first programming steps +ith a self3made A12 board% ;ere are t+o versions offered*
A very small one +ith an A)tiny1"# or
a more complicated one +ith an A)9072"1" or A)mega2"1"# including a serial 272"2 interface%
$($( E)perimental board &ith an ATtiny$
)his is a very small board that allo+s e5periments +ith the A)tiny1"/s internal hard+are% )he picture sho+s
the '7<10 programming interface on the left# +ith a programming :-! attached via a resistor of "90 hms#
the A)tiny1" +ith a pull3up of 10, on its 2-7-) pin @pin 1A#
the supply part +ith a bridge rectifier# to be supplied +ith 9%%1E1 from an AC or !C source# and a small E1
regulator%
)he A)tiny1" re$uires no e5ternal O)A: or cloc, generator# because it +or,s +ith its internal 9%6 0;4 2C generator and# by
default# +ith a cloc, divider of 8 @cloc, fre$uency 1%2 0;4A%
)he hard+are can be build on a small board li,e the one sho+n in the picture% All pins of the tiny1" are accessible# and
e5ternal hard+are components# li,e the :-! sho+n# can be easily plugged in%
Avr-Asm-Tutorial 8 http://www.avr-asm-tutorial.net
)his board allo+s the use of the
A)tiny1"/s hard+are components li,e
'L3ports# timers# A! converters# etc%
$($(# E)perimental board &ith an AT*+,#$$-ATmega#$$
For test purposes# +ere more 'L3pins or a serial communication interface is necessary# +e can use a A)9072"1" or
A)mega2"1" on an e5perimental board% )he schematic sho+s
a small voltage supply for connection to an AC transformer and a voltage regulator E1L1A#
a O)A: cloc, generator @here +ith a 10 0;4 O)A:# all other fre$uencies belo+ the ma5imum for the 2"1" +ill
also +or,A#
the necessary parts for a safe reset during supply voltage s+itching#
the '7<3<rogramming3'nterface @here +ith a '7<10<'63connectorA%
7o that/s +hat you need to start +ith% Connect other peripheral add3ons to the numerous free 'L pins of the 2"1"%
)he easiest output device can be a :-!# connected via a resistor to the positive supply voltage% &ith that# you can start
+riting your first assembler program s+itching the :-! on and off%
'f you
do not need the serial communication interface# >ust s,ip the hard+are connected to pins 2L" and 1HL16#
Avr-Asm-Tutorial 9 http://www.avr-asm-tutorial.net
do not need hard+are handsha,e signals# s,ip the hard+are on the pins 1HL16 and connect 2)7 on the 93pin3
connector over a 2%2, resistor to K91%
'f you use an A)mega2"1" instead of an A)9072"1"# the
follo+ing changes are resulting*
the e5ternal O)A: is not necessary# as the A)mega has
an internal 2C cloc, generator# so >ust s,ip all
connections to pins H and E#
if you +ant to use the e5ternal O)A: instead of the
build3in 2C as cloc, source# you +ill have to program
the fuses of the A)mega accordingly%
".% 2eady-to-$se commercial pro'rammin' boards for the *12-
family
'f you do not li,e home3bre+ed hard+are# and if have some e5tra money left that you don/t ,no+ +hat to do +ith# you
can buy a commercial programming board% !epending from the amount of e5tra money you/d li,e to spend# you can select
bet+een more or less costly versions% For the amateur the follo+ing selection criteria should be loo,ed at*
price#
<C interface @preferably =78# less convenient or durable* 93pin 272"2# re$uiring additional soft+are* interfaces for
the parallel port of the <CA#
support reliability for ne+er devices @updates are re$uired from time to time# other+ise you sit on a nearly dead
horseA#
hard+are features @depends on your foreseeable re$uirements in the ne5t five yearsA%
)he follo+ing section describes the three standard boards of A)0-:# the 7)N200# the 7)NE00 and the !ragon% )he
selection is based on my o+n e5periences and is not a recommendation%
$(.( ,T/#++
)he 7)N200 from A)0-: is a historic board% 'f you grab a used one you/ll get
a board +ith some soc,ets @for 8# 20# 28 and H0 pin devicesA#
eight ,eys and :-!s# hard connected to port ! and 8#
an :C! standard 1H3pin interface#
an option for attaching a 283pin 72A0#
a 272"2 interface for communication#
a cable interface for a <C parallel port on one side and a 103pin '7< on the other side%
;igh voltage programming is not supported%
)he board cannot be programmed from +ithin the 7tudio# the programming soft+are is no longer maintained# and you
must use e5ternal programs capable of driving the <C parallel port%
'f someone offers you such a board# ta,e it only for free and if you/re used to operate soft+are of the necessary ,ind%
$(.(# ,T/0++
-asy to get is the 7)NE00 @e% g% from A)0-:A% 't has the follo+ing hard+are*
7oc,ets for programming most of the A12 types @e% g% 1H3pin devices or )PF< pac,ages re$uire additional
hard+areA#
serial and parallel programming in normal mode or +ith high voltage @;1 programming brings devices bac, to
life even if their 2-7-) pin has been fuse3programmed to be normal port inputA#
'7<6<'63 and '7<10<'63connection for e5ternal 'n37ystem3<rogramming#
programmable oscillator fre$uency and supply voltages#
plug3in s+itches and :-!s#
a plugged 272"2C3connector @=A2)A#
a serial Flash3--<20 @only older boards have thisA#
access to all port pins via 103pin connectors%
A ma>or disadvantage of the board is that# before programming a device# several connections have to be made manually
Avr-Asm-Tutorial 10 http://www.avr-asm-tutorial.net
+ith the delivered cables%
)he board is connected to the <C using a serial port @C05A% 'f your laptop doesn/t have a serial interface# you can use one
of the common =783to37erial3'nterface cables +ith a soft+are driver% 'n that case the driver must be ad>usted to use
bet+een C01 and C08 and a baud rate of 11E, to be automatically detected by the 7tudio soft+are%
<rogramming is performed and controlled by recent versions of A12 studio# +hich is available for free from A)0-:/s +eb
page after registration% =pdates of the device list and programming algorithm are provided +ith the 7tudio versions# so
the support for ne+er devices is more li,ely than +ith other boards and programming soft+are%
-5periments can start +ith the also supplied A12 @older versions* A)9078E1E# ne+er boards versions include different
typesA% )his covers all hard+are re$uirements that the beginner might have%
$(.($ AVR 1ragon
)he A12 dragon is a very small board% 't has an =78 interface# +hich also supplies the board and the 63pin3'7< interface%
)he 63pin3'7<3'nterface is accompanied by a 203pin ;1 programming interface% )he board is prepared for adding some
soc,ets on board# but doesn/t have soc,ets for target devices and other hard+are on board%
)he dragon is supported by the 7tudio soft+are and is a updated automatically%
'ts price and design ma,es it a nice gift for an A12 amateur% )he bo5 fits nicely in a ro+ +ith other precious and carefully
designed bo5es%
Avr-Asm-Tutorial 11 http://www.avr-asm-tutorial.net
. Tools for AVR assembly programming
Four basic programs are necessary for assembly programming% )hese tools are*
the editor#
the assembler program#
the chip programing interface# and
the simulator%
)+o different basic routes are possible*
1% anything necessary in one pac,age#
2% each tas, is performed +ith a specific program# the results are stored as specific files%
=sually route Q1 is chosen% 8ut because this is a tutorial# and you are to understand the underlying mechanism first# +e
start +ith the description of route Q2 first% )his sho+s the +ay from a te5t file to instruction +ords in the flash memory
%.1 The editor
.(( A simple type&riter
Assembler programs are +ritten +ith an editor% )he editor >ust has to be able to create and edit A7C'' te5t files% 7o#
basically# any simple editor does it%
7ome features of the editor can have positive effects*
-rrors# that the assembler later detects# are reported along +ith the line number in the te5t file% :ine numbers are
also a po+erful invention of the computer3age +hen it comes to discussions on your code +ith someone else%
)yping errors are largely reduced# if those errors are mar,ed +ith colors% 't is a nice feature of an editor to
highlight the components of a line in different colors% 0ore or less intelligent recognition of errors ease typing%
8ut this is a feature that ' don/t really miss%
'f your editor allo+s the selection of fonts# chose a font +ith fi5ed spacing# li,e Courier% ;eaders loo, nicer +ith
that%
.our editor should be capable of recogni4ing line ends +ith any combination of characters @carriage returns# line
feeds# bothA +ithout producing unacceptable screens% Another item on the +ish list for &ido+s 201"%
'f you prefer shooting +ith cannons to ,ill sparro+s# you can use a mighty +ord processing soft+are to +rite assembler
programs% 't might loo, nicer# +ith large bold headings# gray comments# red +arnings# changes mar,ed# and reminders on
)o3!o/s in e5tra bubble fields% 7ome disadvantages here* you have to convert your te5t to plain te5t at the end# losing all
your nice design +or,# and your resulting te5t file should not have a single control byte left% ther+ise this single byte +ill
cause an error message# +hen you assemble the te5t% And remember* :ine numbers here are only correct on page one of
your source code%
7o# +hatever te5t program you chose# it/s up
to you% )he follo+ing e5amples are +ritten in
+avrasm# an editor provided by A)0-: in
earlier days%
'n the plain editor field +e type in our
directives and assembly instructions% 't is
highly recommended that lines come
together +ith some comments @starting
+ith RA% :ater understanding of +hat +e/ve
planned here +ill be helpful in later
debugging%
6o+ store the program te5t# named to
something%asm into a dedicated directory#
using the file menu% )he assembly program is
complete no+%
'f you/d li,e to see +hat synta53highlighting means# ' have a snapshot of such an A12 editor here%
)he editor recogni4es instructions automatically and uses different colors @synta5 highlightingA to signal user constants and
typing errors in those instructions @in blac,A% 7toring the code in an %asm file provides nearly the same te5t file# colors are
not stored in the file%
Avr-Asm-Tutorial 12 http://www.avr-asm-tutorial.net
!on/t try to find this editor or its
authorR the editor is history and no
longer maintained%
.((# ,tructuring assembler code
This page shows the basic structure of an assembler program. These structures are typical for A,-
assembler. This te0t discusses
comments#
header informations#
code at program start and
the general structure of programs%
Comments
)he most helpful things in assembler programs are comments% 'f you need to understand older code that you +rote#
sometimes years after# you +ill be happy about having some or more hints +hat is going on in that line% 'f you li,e to ,eep
your ideas secret# and to hide them against yourself and others* don/t use comments% A comment starts +ith a semicolon%
All that follo+s behind on the same line +ill be ignored by the compiler% 'f you need to +rite a comment over multiple
lines# start each line +ith a semicolon% 7o each assembler program should start li,e that*
<ut comments around all parts of the program# be it a complete subroutine or a table% &ithin the comment mention the
special nature of the routine# pre3conditions necessary to call or run the routine% Also mention the results of the
subroutine in case you later +ill have to find errors or to e5tend the routine later% 7ingle line comments are defined by
adding a semicolon behind the command on the line% :i,e this*
L1I R23+)+A /ere somethin' is loa(e(
M4V R53R2 an( #opie( somewhere else
Things to be ritten on to!
<urpose and function of the program# the author# version information and other comments on top of the program should
be follo+ed by the processor type that the program is +ritten for# and by relevant constants and by a list +ith the register
names% )he processor type is especially important% <rograms do not run on other chip types +ithout changes% )he
instructions are not completely understood by all types# each type has typical amounts of --<20 and internal 72A0% All
these special features are included in a header file that is named 5555def%inc# +ith 5555 being the chip type# e% g% 2"1"#
tn2"2"# or m8E1E% )hese files are available by A)0-:% 't is good style to include this file at the beginning of each program%
)his is done li,e that*
(64LI,T 0on1t list the )ollowin' in the list )ile
(I6CL71E 8m900def(inc8 2mport o) the )ile
(LI,T .wit#h list on a'ain
)he path# +here this file can be found# is only necessary if you don/t +or, +ith A)0-:/s 7tudio% f course you have to
include the correct path to fit to your place +here these files are located% !uring assembling# the output of a list file listing
the results is s+itched on by default% ;aving listing ob might result in very long list file @D%lstA if you include the header file%
)he directive %6:'7) turns off this listing for a +hile# :'7) turns it on again% :et/s have a short loo, at the header file% First
these files define the processor type*
(1EVICE ATME:A900 The tar'et (evi#e t,pe
)he directive %!-1'C- advices the assembler to chec, all instructions if these are available for that A12 type% 't results in an
error message# if you use code se$uences that are not defined for this type of processor% .ou don/t need to define this
+ithin your program as this is already defined +ithin the header file% )he header file also defines the registers O;# O:# .;#
.:# S; and S:% )hese are needed if you use the 163bit3pointers O# . or S to access the higher or lo+er byte of the pointer
separately% All port locations are also defined in the header file# so <2)8 translates to a he5 number +here this port is
Avr-Asm-Tutorial 13 http://www.avr-asm-tutorial.net
located on the defined device% )he port/s names are defined +ith the same names that are used in the data sheets for the
respective processor type% )his also applies to single bits in the ports% 2ead access to port 8# 8it "# can be done using its bit
name <'68"# as defined in the data sheet% 'n other +ords* if you forget to include the header file you +ill run into a lot of
error messages during assembly% )he resulting error messages are in some cases not necessarily related to the missing
header file% thers things that should be on top of your programs are the register definitions you +or, +ith# e% g%*
(1E; mpr < R2 0e)ine a new name )or re'ister 316
)his has the advantage of having a complete list of registers# and to see +hich registers are still available and unused%
2enaming registers avoids conflicts in the use of these registers and the names are easier to remember% Further on +e
define the constants on top of the source file# especially those that have a relevant role in different parts of the program%
7uch a constant +ould# e% g%# be the Otal fre$uency that the program is ad>usted for# if you use the serial interface on
board% &ith
(E=7 f> < .++++++ 4Tal )re5uen#, (e)inition
at the beginning of the source code you immediately see for +hich cloc, you +rote the program% 1ery much easier than
searching for this information +ithin 1H82 lines of source code%
Things that should be done at !rogram start
After you have done the header# the program code should start% At the beginning of the code the reset3 and interrupt3
vectors @their function see in the J=0< sectionA are placed% As these re$uire relative >umps# +e should place the respective
interrupt service routines right behind% 'n case of A)mega types +ith larger flash memory J=0< instructions can be used
here# so be careful here% )here is some space left then for other subroutines# before +e place the main program% )he main
program al+ays starts +ith initiali4ation of the stac, pointer# setting registers to default values# and the init of the
hard+are components used% )he follo+ing code is specific for the program%
Tool for structuring of !rogram code
)he described standardi4ed structure is included in a program +ritten for &indo+s perating 7ystems# +hich can be
do+nloaded at http*LL+++%avr3asm3do+nload%deLavrThead%4ip%
=n4ip the e5ecutable file# and
simply run it% 't sho+s this* ;ere
you can choose *Ttiny by
clic,ing on it# and then select
*Ttiny1" in the dropdo+n field
*12-Type%
.ou are no+ as,ed to navigate
to its respective include3file
tn1"def%inc% 7ho+ the program
the +ay +here the header file is
located%
;ere you can enter your desired
multi purpose register# the
output configuration on ports A
and 8# if available# and if you
+ant to use interrupts%
Clic, Update to fill the +indo+
+ith your code frame%
Clic, CopyToClipboard# if you
+ant to paste this code into
your code editor# or
.riteTo4ile to +rite this to an
assembler code file instead%
'f you don/t ,no+ +hat it is for
and +hat to do# press the Help
button%
)his produces the follo+ing
code*
66666666666666666666666666666666666666666666
6 7A(( &ro8e#t title here9 6
6 7A(( more in)o on so)tware version here9 6
6 :";20<< +, 7A(( "op,ri'ht 2n)o here9 6
66666666666666666666666666666666666666666666
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
/ A 3 0 * A 3 B 2 > F ? 3 G A T 2 ? >
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
& ? 3 T . A > 0 & 2 > .
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
" ? > . T A > T . T ? " / A > - B
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
F 2 4 K 0 B 3 2 = B 0 " ? > . T A > T .
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
3 B - 2 . T B 3 0 B F 2 > 2 T 2 ? > .
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
. 3 A G 0 B F 2 > 2 T 2 ? > .
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
.0.B-
.?3- 040060
Format: @a+el: .JDTB > reserve > J,tes )rom @a+el:
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
3 B . B T A > 0 2 > T = B " T ? 3 .
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
.".B-
.?3- I0000
r8mp Gain 3eset ve#tor
reti 2nt ve#tor 1
reti 2nt ve#tor 2
reti 2nt ve#tor 3
reti 2nt ve#tor 4
reti 2nt ve#tor 5
reti 2nt ve#tor 6
reti 2nt ve#tor 7
reti 2nt ve#tor 8
reti 2nt ve#tor 9
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
2 > T B 3 3 A & T . B 3 = 2 " B .
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
G A 2 > & 3 ? - 3 A G 2 > 2 T
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
Avr-Asm-Tutorial 15 http://www.avr-asm-tutorial.net
Gain:
2nit sta#$
l(i rmp% @?*:3AGB>0; 2nit @.J sta#$
out .&@%rmp
2nit &ort J
l(i rmp%:1LL00J2;M:1LL00J1;M:1LL00J0; 0ire#tion o) &ort J
out 003J%rmp
7A(( all other init routines here9
l(i rmp%1LL.B ena+le sleep
out G"A"3%rmp
sei
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
& 3 ? - 3 A G @ ? ? &
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
@oop:
sleep 'o to sleep
nop (umm, )or wa$e up
r8mp loop 'o +a#$ to loop
3e'ister (e)initions
Avr-Asm-Tutorial 62 http://www.avr-asm-tutorial.net
.BHA rsre' E 315 savin' the status (urin' interrupts
.BHA rmp E 316 Temporar, re'ister outsi(e interrupts
.BHA rimp E 317 Temporar, re'ister insi(e interrupts
.BHA r)l' E 318 Fla' re'ister )or #ommuni#ation
.BHA +int0 E 0 Fla' +it )or si'nalin' 2>T0-.ervi#e
.BHA +t#0 E 1 Fla' +it )or si'nalin' T"0-?ver)low
...
2.3-Ta+le
.".B-
.?3- I0000
r8mp main 3eset ve#tor% e<e#ute( at start-up
r8mp isrNint0 2>T0-ve#tor% e<e#ute( on level #han'es on the 2>T0 input line
reti unuse( interrupt
reti unuse( interrupt
r8mp isrNt#0N?ver)low T"0-?ver)low-ve#tor% e<e#ute( in #ase o) a T"0 over)low
reti unuse( interrupt
reti unuse( interrupt
... other int ve#tors
main:
l(i rmp%/2-/:3AGB>0; set sta#$ re'ister
out .&/%rmp
l(i rmp%@?*:3AGB>0;
out .&@%rmp
... other thin's to (o
2>T Bna+le )or T"0 over)lows
l(i rmp%1LLT?2B0 ?ver)low 2nterrupt Bna+le Timer 0
out T2G.O%rmp set interrupt-mas$ o) the timer
l(i rmp%:1LL".00;M:1LL".02; pres#aler +, 1024
out T""30%rmp start timer
2>T Bna+le o) the 2>T0 input
l(i rmp%:1LL.B;M:1LL2."00; .@BB&-Bna+le an( 2>T0 int on all level #han'es
out G"A"3%rmp to the #ontrol re'ister
l(i rmp%1LL2>T0 ena+le 2>T0 interrupts
out -2"3%rmp to the interrupt #ontrol re'ister
set interrupt status )la'
sei set interrupt )la'
loop:
sleep pro#essor to sleep
nop (umm, )or wa$e-up
s+r# r)l'%+int0 2>T0 )la' not set
r#all ma#heNint0 han(le 2>T0 event
s+r# r)l'%+t#0 T"0-?ver)low )la' not set
r#all ma#heNt#0 han(le T"0 over)low
r8mp loop 'o +a#$ to sleep