0% found this document useful (0 votes)
337 views

Embedded C and Atmel Avr

Uploaded by

tronguyen
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
337 views

Embedded C and Atmel Avr

Uploaded by

tronguyen
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 427

fl*AA

(~)

c 1I@!i Atmel
Richard Barnett Larry O'Cull Sarah Cox

AVR

Richard Barnett.Larry Embedded

O'Cuil,Sarah

Cox

C Programming

and the Armel A VR

ElSBN: 1-4018-1206-6 Copyright © 2003 by Delmar Learning, published


by Thomson

a division of Thomson learning(a

Learning. Learning Asia Pte Ltd).

Original language All Rights reserved.

division of Thomson

~nffi~~~~.~~rn~.mm~.~~~w. g~~no
Tsinghua Chinese University edition. Press is authorized hy Thomson Learning to publish and distribute exclusively this Simplified Hong Kong, This edition is authorized for sale in the People's export of this edition Republic of China only (excluding of the Copyright or retrieval Macao SAR and Taiwan). publication Unauthorized is a violation Act. No part of this system, without the

may be reproduced

or distributed

by any means, or stored in a database

prior written permission

of the publisher.

4: 'r _X mJ {4; Of 1M~r=!WLlJ tfJ m- n 7=;-J HI!'{i Jt m~1X ii1

*A 7- ,'-UIIR1+ J!R #: f-B Il& ~ tr


Q

.l1l r*l C1~gM9=< i]J ~i-tE, ~~n ~ )JIJITi&~& rl~ Fi~li£:l{) ~ ~ ~

Ltt/ll€;;$: {j(~![IT l.:j:l '$ A ~:!-t {U ~ *~.H!l;fY:i¥l* tHli P#.t1&:W:tJm&!'i&~tl.~1T


0

~.~~W~~~~nM~~,
981-243-982-x

~~~ffMn~~~~~ff*~~ffW$*.
~

Jt~T rn!'dX~}r-J*~tR*r"J
1II-iiJ :ft II&~ §(CI P)J& m

ff k~-,%

f:

01-2003-2176

*~MW~.~$*~~~tt~*m~~~, ~~~.~mm~~
!tl:Art

c ~l~!oj

Atmei AVR/(~)~ C Programming

~t¥,

c*rt.- $~*,

f,ijf~~~i¥-.

-~~R-:

1f!f$*~ W li&fi, 20U3

~.44 flJi k;

Embedded

and the Atmel A VR

ISBN 7-302-06955-7

1.*'..

II

.CD~.. ·®t;···@fi!iJ-·' III.CD-$-fJ-t'i(t~i-l


t\(

n{rL, AVR®C

*6-:f_!£ff:W-i-t

IV.CDTP368.I@TP312

l-p 13'I1\fi4>: ~ IHEr CIP

lli+* ~(2003)~
en

064032

~J
iJl::

~tg m'$ *<!f:$liIf)eM


010 62770%9

*f

http://wWw.tllp.com.

.. , 100084

$. til. ,

010-62770175

.gP.ll!l~:

mfiBfUI: W~
3t .fii ~Iin.:Mi

MWmit: ~~ iiiit ij!: it: f;# Pi


fIJ
7f
jj;~ ~; ~t

*1liI:
w tit jlE £~ ~IJJ

Jiut-

&: ff

.Iti
~

:if: *'f $ ·:tH!:i.t1t;l1 ~ t. f.( M h I'JT *: 13,}X260 E!l~. 27 $., iX, 2003 1f: 9 }j m 1 JltZ 2003
~:

684 =r.'j: 9 }J ~ I i1i:_

fp ,ijlrJ

ISBN 7-302 06%~,-7,'TP • 5] 37


1 ~·IO()O

EP

It:

*~::ffXi#~ c ilh~fitJ.AAtaf2it~~Et'~~fjl:JJlt, RIse *:Ylltr,tr2.1f1rj~B<lfEl*j;U tHo

if~1#;7

mJti Atmel

0i1J 1:.1'=i¥J AVR

-I-H£ fl' X1M ~ /f'; raJ Ii'~ if Jj'ij & i l i¥J i~ • ~ -~w;:t'f~Et!.-fit • .f1L IN~ Jt!-¥If¥~ ~~ _ _L~tL*~ Jt!--fI~N*lkit.ml~
0

tt*~~oo~~.*~m~*~~~~~~:
.~~~rlcffi*~~~:

~~~~~~~~M.4~*~~f¥~~*~~ff, ~~ ¥It 1lT 5t ~ 3] c i€f)H~ufl. ¥} ;y :s:j '!ia-M 1:tJt~ mt £;! 1ff~3 W: it 4' b), A:it mffl C ift -j§- fP Jm 12: -1-7: :>J JI[iJ! rf. , ftP.1fJ mt ~ IdJ, '+ j.J £ ~ ?It ~ A 3:\ mffl WJ¥ , i!.lI:~f¥ ff @_ *.m ~~:iE tr f£ll'*A~11}H~~~$-4', rmJL:>f~iiff~@!ft*rtiRo 1 ~(UXAA c l*Er~it):m~~)§ 15 r_tlli1i~J~~~._:~ • ~J.i c itH~fl¥J~~: *~-arIJ,1''Fj;J--~M9XMt-*tl~f¥~~*o 1tik#'t~{£r.

~~~Qml_~~3~~§~~~.~~.~.~~gc • m~~~~£ID~m#I~~~~m~.w~~~Jti~~~I~~~tt*A~om.~
5Jl- ffjj 1£)
e-

~~R~~~~ml~~~~*AA~~~~~~W,~Fn~~~~~*A~~~*

~OO~~,l.m~~~~2~~~.~~n~~~.~mM~~~~.~~ft* m

~~3.

~~~m&m~*~~m~~~~~.~mMo

W ff

Atmel A VR

tIltr~1M:HU:!itt ~Q ~R 1¥J.1t. ~ ~J ~ ij;;~-t:?: YJ -1'-ii I¥J~

~~~~~fflT~*~~~~*M~~~.*~.W~OOffl •• ~(ffi~f*~=~.*~.~
If -t·T
0

~~~*~~~.~*m~~.~*~~moml§~~ffi~~mBd~~~.~~3~

~.W~~)o~~*~~~*M~~m.~~.~.w~ftUom$~M,~.Y~W~~ fi~~m~*$> ~*~~mag.~~~~#~~.~.M, ~~~~re~~~.~~m~

• lJ •

itXAi\ c iiliH'i1j Atmel

AVR

ffiA p;j tYJ 4ij-1'-±Jm~~{~L{itM!:v.. ~ px~1*EtJ ttlltEOOEi<J~)j ~_tfJL~)] x-{ ffi Jli ±:~ I¥J, J3: ~ nr !J~ ij;~ }j 'if ~ ;.g -&Ht!! fE A 1*@ m ~ ffffl '2 f(j
~Q

Mcm~*~,

*~&*m~3~~m~.*~#~$#~. M~~~~~~ft*~~mffiM~**m,
~€£AVROO~#.
0

~mA~~g~~.a&m~~mmaA~OO~¥~~
~
o

EI ~MH

M 1 lit ~Ait

e i.~~ #i.{l;

*.W~M*~Tc~~.#~$.M~~~~ffl~*A~.~~.OO.~~Q4ij~~~ .~m~~~~~~~~~~mOOffl~~*~~*.m. ~~.m~~~Cill~~~*fi~


IPJ H!:[
Q

M 2.

AtmelRise ~JlH
p;j

~tJ 1->,r 19: I¥J ~ mffl ~ *Ju!ftk [b]Jm T


0

*.

;tJ.Nlif 7mf2:t~~tf.J¥*Fu~&;It
0

~ $1¥J4ij- 'l'tFm~r-& jpJ:l$~Uffl7J'{9rJ~r¥*W iG ~ 7 ~ulID W3it. ~?¥: ~ z Fn. ~ ~ J5i it<~ IJ. ;fIJ ffl AVR RISC • ~ $IJ3
0

~ 3 if:

*i:tt

110 ~Qfj!~Jlfi§ l'l

~.~~~frffiC~~~~~~.Hffl~o~~,&~~ffl~~fi~*~~~~~$~~
~ffl~o~d.3.~~~g,

.m~~ffl$~.~*~ft.~*.~.~~.~~ha
3liitfH~~.EJt:*~1;i\:ij[
0

CodeVisionAVR .wft&;t:t:~~JfZUf~E81fffl¥:lll"L ft*ita..p. i:ir~~J3tmfilJfiJ fflodeVisionA VR ~ IDE *~~M13.HtlOiJa]ii\ C c iif~mff

*~~
~5i!

~ 4 if:

CodeVisionAVR C

IIIf§1f~

*it.~MhlTmm.~~.~~§OO*£Mfi~~M*~~~~.Ei<JX~~~~*~. ~*~*~~~-tl~~E$~~, ~~~~M~~~~KR "~~B. ~~~~*E8~~Q


1If.f~

1}f13R B CodeVisionAVR 1O STK500 An


ZjJ-~,,*i!{tfflmm
0

~~AW ••• ~ S*~*~iliM~~~. m~m~~~~~$$.~~~m.~.M~


*ffl3J{_RzflJffl
CodeVisonA VR]fist Atmel STK500

pHi{ CAVR ViH~$~$~jU~ ~£ AVR W:'i-S'J FLASH #fm$!K~~~jOO~f'Fm1¥1. ~tt~ t~1¥1 nJ g1, ii:f~ I!IJ EIi,ffHE t8 )]i If , iJif3J{_0 :ti~~o {tffl CableAVR *ffl J'.:l ¥$ frrBr 911 {iiI t: ~fiJ {tffl Cable A VR ~f4:& 1IJ!1lj; " Il1H:rt E MegaA VR-DEV rr 1tt& MegaA VR-DEV JT-.tst:tR E8~ ~ f5;fQfiiH~l
0
0

.o

_,_ n

• IJJ •

IlfHJt

F ASCII H

*1'{f~

IlfHKG AVR fI;f4Ht~[,~,

±;g:~7J"19IJ~ff~ wr ffl fIJEEJ1[ttilri.€i-~·t~4!¥Jffi~,~.!a

PfH*

£iHt~:S:],*~Q

m~ ••
i!:J_U
0

~ftA

11'J£ ~~if:m
o

*~.OOtt*~~~~£.~m~, ~W~~.~~ft.~~$OO~n~~~~
ra'lW:ift §(#IJ9P C iH*),*~Jm··-~X--Ja-tfll]~*1~Pf!(fl{f*.

~ mr

~a-t, .~~B~mm~m.*.W~7. t~ H fJ'1 fj '7: tit .fir i~ ~ '7 fZ ,mHR ~ N #5tl ft1t it!!. ~~ (fl~ lJft ~ ffl ':h frJ

~#~*R~m~.m~*%~a ~~~B~~~~7

~~£#,±;g:~~~~~.~~n~~~~*~.o~~~~~¥~C~~~&~.~~~~ lllU C $ tP A_-r\.~ %1). FfJM #Hift --&d1HK Ie *-1=5~JE 7 tJ. LW3#~~I¥J~*. *WJtJ.fFAI:¥it¥JJJB~~~(fl:£5~
e

**

-~~~~~~*~, m~¥~mm~~*¥~~B~ff~fiW~~~~
0 ~

~~B~~-# 7~ rr& ~ it~ 7f ~ fR , ft

1'$0

-*-t5 <P }.: iff; 5ttili~mffl ~ 1?Jj =U £ ffi Progressive

Resources, LLC i,\. tl] tlif1t

IY~ VR ~~b\~ A
0

7f 1t

.(l~Wli#.~i.~lfj!#* E)JfbtI¥J ~ ~~ff~fRtIH~ff$X¥1tm. 1E.~1~~1Ff:JJ!m ff2t. Atmel AVRWNftd.~F'ffl'~~if:ffl e reE:tm~~~H&_t, 5'~j](f--·1'-fai**~ft. JL--rE{r,W; 1:').& 41*

*~l¥Ji§lrf Br2{E.~tUfl: AT90S8535QX.-lJ Atmega163 _t~~b\tt., AVR -*_9u1tii~$tl~Ef. -::*:tit ,~~, 1::if] B'~ ~ ~& ~.fir IYJ 11 j\ .1~ tElfbi. LSl!lt. m~ffl EFJ#lJ -T -aT gAtE {:EM Atmel ~ tiIi~ AVR mtJ~$IJW_t;iEtT, fJ.~'B_t j]j~#JElEf.Jfrf-tl~$'£~-&B 16'~:h AVR -*3'~;1tit!!.f}{J~W
Q

m~.~(fl.m~.m~~~~ffNmM.~#~~.~~~~mn~~*.

~.a~~wo -*~&mm~AVR~~I¥J~~.~~ft. ~~.


-*~~~7.~m~*~.

7J~~-mfRo

§~~AVR¥~~~~~~~.*~~.-*~EEJ~~~~*

-*~£3m~~~N~~EEJ~~.&~*.£~~~<p-+A.~. ~~~~N~.it!!.~ ~A~.~$~.~.~•• J~ID~~~~~::*:.ftEEJ~Wom~m*mffi.-~~.M~ 9=J Jj[S---tWftfltm


<>

*- is f(.j ~:;ff

Richard H. Barnett

I1!TT.~bZ*fj:~

fR*l::*:$

·lV •

IlXA..rt

C tIij;jj1-'=j Atmel AVR

m±f±l1±1¥J 18 :q::,,- - Kh\$llXAj:tMtnm~~@1:1;'1U8f!(~I1= )A Intel 0ilJ [j<J t 8085 :Jft!&. jlJ 805'1 ffi37~.Aj.IJi~*~~. rmtEOOU;flJm Atrnel AVR N~M.~glHlxAAmUg:*ll ~ ttl tt ff I it fE ~ fX~n:!fi j~ ptj 1-1FXM ~, ftl!!..... rl:tmM • $ ft1 W ~ *A Jt lVf ~I t1= • 1f itvt1fJJ.ili.ffl fiJ1§..fflID lu] t=;::ffilJ ErJJitJij q:r ~ 5J:7~. ftBif,f9-t&:tmiE...tj£4Jl!~JA~~iilJI ft tE:!:l3t±:* ~ffJ{HWHAJ, fIB.~ T 10 :q:: I¥JAA~ ~ -f I \1v.I~~1jJ iEq&~ JJTIii. Barnett IW± a;Jt~~ T 1~ $ :!tIm, 1E.:rfi t1=:tJ £8 f±:*"F r.tl JiM- ~~YIP ffij$IH~7 Charles B. Murphy ~ 1m jriJ 111 A J8 tt..A eg ~Q ill:X ~J1j¥}iff y.... i3: 1'- j;Q il ~ Wp-¥ Jlfr .l:R >,~:J¥: itli:31~ $ 7 .E81±A$r:pM:1fmJll&JEB 225 UI.~~iPo*-KH:ftfr!:=JEBm.---:*~*~ ~o :~U[rff 1:ffriJ Xi J\!.QX;1'f ~iX, n:r bl,3I;ij::E8:t± j,: ~ ffJ Et! 13 765-494-7497 lflftBltt 1EPI tJ 1:tiE tt!. T m~1i~; [email protected] f~
Barnett
0

*!Jf.~

~~*.

Larry D. O'Cull

~~~l'F~
Progressive Resources LLC O'Cull5'G Numeric Controlled.

0- J'1j
tH.t:f!I!.:.¥:~+~1:V: ftf?Jf AAit:h CNC(Computer ~tM;:f41tIEi!~tl*~a<J. Fo*# J1:i] ~ -=f ..L~I¥JJt1tE@i
j)Jt
0

1:.M18 tl:*~ i¥J El! -=f Ifltt*:~

it·~,fftJi~~~~)iX.ffi.19:

•. n~~#~~~*m~19:4H~~#,
O'Cull

Tft~~m.~~o ~~, ftBif&$~~~OO~~~QX;~~~fi~#n


c o

~~~*m

.• ftm.~.ffi., ~"~~&*~~#

tEM$ T JL 1f:A'~ ~-=ffO~itt l~I1'F ~&I~~JmI1tJEi. F 1995 ~P.X;JL 7 Progressive Resources 0 ;;] Progressive Resources LLC 0- R] (http./zprllc.comj'e nM. *-.~ft, I ~~~m~r1~ ffJ~rru~7f'ii._I ft *i~il'jifM Atmel AVR J®!rblJ1 a<J~ lfl f 3zQ~:gfffijJ~ ~E.1t*Jti~. -aJlJ.tt:i:5 ~ -f!B~fttjIJ [email protected] lDft!!Itt~II**
o a

Sarah A. Cox

0RJ
~ ftt 7f'ii.'± 1:f

C~~±tEm~.A~~~~#~~*~Nr~.mlfl~~I~~~.~~±~UI.c ~ ~ iE -1-1~:* ttl w.m IU] 0 RJ r:pI ft 11:- .E9: at fa], 11 ~:Wi P-f: it J!IL~ ~~ itt ~ TIlJ, z.J§ ~ 1&:~
~fI~19:itEntkill1*fIJ71J~tijJl!& '317 tEp'x;7g Progressive Resources LLC 0RJ 00"fffF1!fZ frY. tltB ~ ~.!IULc ~ T 1~$ J~)f m~ 19:.ffi.rr~ it J iJ\ ~ IT Progressive Resources 0~ $, Cox .tC:tj;J.:g.f~JJI! § 3f1ttki4, ffifr,j~2itlwd\A'>]m~n~:fH I.'.!ttz:p~lfl3!~ii\W.ffi. § if%~ 7 f~ ~~:IgIG. 1E.m ~ ~i.t .. ~1T" ~5f- ~ JLli'ii.Ji ~ 0~ ~Jl:~Jm 3Z;#:/~1f~ F{flfJOO~ffimr~ jf:m.~~ ~lltz:.j~, j{t!!ll:)!1~$~If~I¥J~IRJ1m1f1lo PtlH£j;J*,tJc ttiH~~~!ft1tf. :kJ Atrnel AVR 5l.I::~~~1;JftitIJt" !m *:g ff fuJ ~ Jt!. ~ ~ @ i-~, it i4f El!T m~itt 1.t:i:5 [email protected] lfl tltB IIX 1ft If*:~
a eeo

31
~

;r. *~~$1lf:(ALU), ;ffi.J¥ ROM b'J. &. 4ftBY~-& ifffM~iU --1'- /j, JIj ffJ Z}=f If 1¥J1i&~ ;M~z- ~ ~~~~a~~~.m*&~~?£.~~I~10~~~*AWA~.~~B~**~~~~
I

*A:rt;mtf;{;j;U~8t~TIJiTJ*~hxEf!.IE4t9='*pt T cpu, ±~9h5tWWfffif ~#I¥JW:ru. it.tIL, ~1~ »;It ~ ff -- ~ It_ f¥J ttt~ -r/ - W {flo _t tk l. 1lXAAmttltlJ~Er'~1tm}Jj1:o!2mijj: Intel 0-i=lHm 8051 ~~IHjL.!f.~pg#, 110" 30 frT ~
Instruments ~National Semiconductor ~Motam Ia, Phil ips/Signetics-, Z ilog , AM D" Hitach i, Microchip 01=.'],
0

fi General
Toshiba

~&;ttftR~~ -Si6:!L$. Arntel JiX-J; FLASH ~1-i1R-*JfRl¥Jtttj'f.'W!~Ao FLASH M-#_nfl~~~ ~'11: (nonvolatilejjll ur ~:mJ:~;rti¥~ff-M@, tim'"; ffltE~W1j_tn, 1I!m:a:-=-g-0Jtj-&-i-lO PC i:t&_l JK frp # ThlLa FH-J"- ~M~~ WA:it ffE ~1fUJlA'~M~ Jj~ , JiJf ~:*: jdiJ1t 7 A tmel 1E 1i& Pi ;ttl B @l ~ 1=:I:qy.J~m.lkff, ~% AVR RlSC(Reduced Instruction Set Computing, ~1iiit!i:f~-4(Jt;)ttjL,\M

fJJ

~1¥J~M .•~~m~n~**~~M~~.~+A~N~~nQ
~ 1--~ f4:t11fQ ~ EfJ ~ A'jJ.. mmt JE =#;- r' j tt x&:li~mt~

t~:B I¥J~ rJlift ~ ~

w~ Er-J It\ IY!

,LI:~

.~@~~RWW~~~m~~h~AD~~C.W*~~~ru~~Rm~~~,
IIJ tt ~ttt. it B 1± i!jJ nIT I¥J @ ffl I:~ ff {jt ~ i
§ -mr, ~1ifffJ--1'
0 ~ ~ ~ - ~-

~~~mo~~~~~H~m~~M.£&*~H~mM,
Haiduc ~~I¥J~

c 1ft-§' 7f/i:_I~JM;;l!
0

~Rft~~ S: a! EfJ fui ~ tE r B ~uit EB~ fj~1M tt 11k MW.~~*o

CodeVisiou.A VRc 1::~ ~ HP InfoTech S.R.L Fr'0 Pavel PC

tW ~ ~ ~ il:, )t:lik ~ ~.fO iIlil m AVR .fIl Jt flk RISC m~1t1J ~ 8'-1 mt 1T, :*~ 71<. -if Ef.J ~ Itfr~ j§J (-1-- ~~j=f q:t ik~ ~ :Bitt-' ill~ •• ~~ft#~~*~~). ~H~~m~~~*H~~~~~"~~m~"OO~*, ~~ ft ffl F ~ T ~ ~ i!* -+£ I¥J Z9J tJL [email protected] n~ a J;] PC ~ £; c itt -g ~rr- , ~ it1;] mttl ft ~ ¥c~ I If $~ ~ ifi -~-~~~f I¥J t~JJ5. fl3-£ 7f J;] A:r\ 1ltt ~ 1t~~ ~ 1:3 c iit ~ 1-t o!!',..t. ~ i:& 5e?lf J'IH81J rti , l;J it3W AA f ~ErJ~*~m/J\, ~~, rlJ~&-ar£m I'J<JftWo *15ffJ § I¥J~ -Wt~7iJM~1!r-m{1t----t-H~~ tfl~~,1.'L jQJlft, *15xt~~~®:(£*AAmtflmlJ~~it _tfl$.t:i~IhA*~, -tE.~- --*#m

iK1--1C'ItUEl.~7fLtPFjjt(lDE)jjJtj.ft.if:(£

trlI¥J

Windows

mfflfiFf9=':i£1f

*~~

~J~~~o

M'

1:ii
1.1

-l8:.J\i\.
~

*:!it § f,"F
r~

c i!~~~

1
I

1.2

1.3
1.4

£*.m.~
51:': :II: ;f!:l 'ffl:lt
1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6

I
4
---4

~!it!fti:~

~itttJ11'm~ ..··· ····· ..·..·· ..··· ..·..··..······· ..····· ···· ···· ·· 1tJit
;t[4t::¥D JE

··

···.·..··· .

·· ····.· .

5
7 8

s:

·..··..·..··· · ·..··..·..·· ··
·..·..···..·..··..·..··..·..···· ··..·..·· ···· ··· ··..·..···..· ·..···.·..·· ·· . ··..·..··· ·····

f-i-f~~.M ..···..···· ~:lrt#~

1.5 1.6

!ftfu A/$ftr ttl


1.6.1

:i.E.:ro=lU:tfl:.it.:rt
M11i::ig~?q=:fIJjf*iE1f:M'··
1.6.2
1.6.3 1.6.4 1.6.5

m*

8
9 10

·..··· ·

~$ft~~.f.t
Ell$E.f,J~

~*~Ji;:.:-r,r

··..·..··

·..·..···..·..··..·..···..·..··..·..······.··· . · · · ·..·..··
···..·.. ··· ·.. ···
· ··

· ··
· ·..·

·· ·

··..·..···

··· ·····..·----.13 ..
· -

10

Eli\JI:±g~f;JlO!l: -frIi{&~.$1 ..·

14
15
16

*=ftf-::&.i:t:j\_
lli.$11t:Jt~
while Vi'EF
do/while ViU
for

··..•.. ··

··

1.7

~1ttliftiiJ
1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6

17 ··· ·· 17
18

Vi:q-;

19

if/else mil) switch/case

20
23

1:B--iU
· ·· ·

break. continue III goto i;\%1:i]·

25
29
--30 ··

1.8

B&~
1.8.1 1.8.2
1.8.3

·
Jli~lO~~m~J!.

···..···..·

···..· ·..·..···

ii~il[@l11i: ±mjJ]ffi~

32 33 37
37

·..·..·..··· ··

1.9

mftfO~m
1.9.1 1.9.2

··
··...··...··...···. .· .· .· .·

mtt·..···...·....··. ·.·....··· ··...··...··...· . · · .·. · .· .· .·

~m
~f:llll:tll· ..· ··..···..··..·..·..· ·· ·..··..·

··.·..·· .
· ·

40
42

1.9.3

• VIII •

ib!:Ait
1.9.4

c iilf:i~

Atmel AVR

fliiol S§:1&I¥.JWtt-

··..·.. ·..·.. ··

··..·.. ·..·.. ···

··

··..·····..···44

1.10

!tiftJ ~~m1*
1.10.1 1.10.2
1.10.3

··· ···.··· .

···.· .

··· ···.·48 .

~ftJ ..·····.·· ···..···· ··..·..··· ·· ··..·..··..·..·· ··..·..·· ·· ······.··..·..·..·..··..····..···· ·· ··..··..48 . . M'~J~ffi · ·· ··..·..·..·..···..···· · ··..····· ··50 m{ti]t1ifi;J~-mtt ··· ·..·..··..·..· ····.··..·..·.. 51 .

1.10.4 1.10.5 J. J 0.6 1.10.7

;l=!:;fflif··
fJz .fDV1:J!(

·· ·· ··· ··..·.. ··· ·· ··..·.. ··..···

·· ·· ···..···..·.. ·
··

· ··
·..··.. ·

·.. ·

·.. ·.. ·52


55

typedef :iE$f.1'ff

· · ··..·.. ···54

sizeofEJJ1ff

··..·..·..·..··..·..···.·········· ·· ·56 .

1.11

ffM~~~ ..···..... ..····· · ··· ···.····· ..· . ········ . ..·····


1.11.1
1.11.2

· ··· ···......... ....··.····· . · ··· · . ..·

·57

~~fIlJt~·

....··.... ·.... ··...···......... · ·. . ·..·..·..·..·..·..··..····· ·........·..·........·..·..·..··.... ·.... · ···...··57 · · ·· .

mtl ....··....·..·..·..·..·..·..···· ..····.····.·..·· ·· ·· ·· ·· ....··.·..····.··.·..·..·..·..·..· ···.·..·..59 ..··· . . ..·..·· . .. . ~ff~3!!:iit· ..·· ·· ..·..·..·..·..·..·..·..·..·..·· ·· ··· ··· ·· ·..·..·..·..·· ..·..·· ·· ·· ..·..·..·..··· ·..·59
62
· · ·..·..··..·..· ···· ·· · ·..·.. ·..·

1.11.3 1. 12 ~

at 1J?t

1.12.1

i!ffl ~ [fff
iA:$;ffL

62
71 71 73

1.13

*./J\~
~J]

1.12.2

····...·. ··· ···· ··


··· · · ··

·..·..· ·

· ·.. 65

1.14 1.15 ~ 2 jf 2.1 2.2 2.3 2.4

_tm~53·
Atmel Rise

···

·····..··· ·····..·······..··· ·····..····· ·

*~§*~

~I1H "

···
··

· ···

75 75 75 75 76 77 77 8l 82

t$.:*~.if.iJM~
1f-~#
2.4.1 2.4.2 2.4.3 FLASH

51-~······ ..··..··· ····· ····· ··············· .····· ·····.··· ··· .····· ··· · . . .

·.·
·..· ·· ·

1\~ffM$ ~~fi-M$

· ·· ..··· ·· ·· ..····· ..·..· ·· ..···.·..···.···.····· ·· ..·..·· . . . ·..·..·..·· ··


·..·· ··

EEPROM 1ffiti~

2.5

.:I: {}L;fOI-P If1TJJJ~


2.5.1 2.5.2

"P~
1l {lL liD ~ IJ

·..·..·· ·· ··
·..·.. ·..·.. ·· ·.. ·.. ··

··· ·..·..··
,

83 86 88

2.6

1flT
2.7. J
2.7.2 2.73 2.7.4

2.7 itlt-t-Bli-tli:~
itlJ>t-Hlittrt~fOO):E#F~:fU$!tI
Timer 0 Timer 1 Timer2··· ·· ·· ·· ·· ·· ·· ··..·..··..··· ··· ·

93
A~1f~ · ··..·..·· ·..·..·· ·· ··..·
·

93
94

97
108

2.8

1JEffl

UART iltrr$f.f:iM.ffi····

··· ······· ..···

····· · ····· ·

112

[j

• IX·

2.9 ~1t)*U············"·····················"···"··········
2.9.1 2.'9.2 2.9.3

······118
····.·..····.·..·· ··· ······ ..··..· ·118 . . ..···· ·..·· ···..·.. ···..·..·· ·· ··..·.. ·119

mlt$tJf41f~j;;oiJ:J, mlt!f{:Jf4~;~ nl:

2.1 0 2.11 2.12 2.13 2.14

t~W~r:~~ :f~ffl SPI i!t1f $rr:iJ! fii

·..····· ··· ··

·· ·

·..·

· ·..··122
127 130 132

A VR RIse l[~i-N-1§'t~~~

*1jt
~.3::.I

J'ra-

· · · ·..·..· · ·..· · ·· · ·..· ·


··· .. ··· .. ··•··· .. ··· .. ··· .. ·.. ··· ·.. · · ·.. ··· .. ·

·136 137

t·.tJL~:-1
.fiF;1l1/0 ~lrf~;rtf:li?EJfi

~ 3.
3.1 3.2 3.3 3.4

139

*~8tjj;·
-T11~A/!fIlijtlj ti-f't~ Ifj 8l§ ~
3.4.1 3.4.2 3.4.3 3.4.4 fl§~

·..·

··

· · ·..· · ·..·

139 ·139 145 146


··..·147

ijri§····· ..········.·····.···..···..······················ . .

········.···139 .

getcharO,fO putchan)- ..···..·..········.················ . ..··········· ..···..·..···..·


putst)

~rI:I ?t:f$I1r~
~Jt.tT~ffi~

····· ..·..··..·..···..·..···..·..····..·.. ··145

fA FLASH fiH±:!~1'iT"ffif{ PUTSFO ? n.$ ~ A tr $ ffi tr.r sprintf()

printf()· ....···· ....··..·..··· ···· ..····· ···· ···

·· ···· ..·..··· ····· ···..······

149 150 ·150


···..·..···..·..··· ·· ····

3.5

tiFi1£~Affi~······· ..····· ········ ..···.············· ..· ..··· . 3.5.1 !Ji;f!J f.f $ flJ Ji getst) 3.5.2 ~ltE #Ii m tr.r scanf()
3.5.3

* *~* ~:it.13na
m~ m~
rn~

·151

ffi f{ sscanf()

·152

3.6

ffi:1.t:$m~·· ..····· · · ···..········ ..········ ..···..···..···..···.···..···..···..········ ·..···.···········153 . ..· . ..


3.6.1 3.6.2 3.6.3 #in(;lude #define

#i fdef, #ifndef.

#else fil#endi f

m~

·..··.······· ·.···········..··· ·····153 . ·. .. .. ·· ··.·..··· ···.·..··.·..··· · ·154 . ..


·156

3.6.4 #pragma
3.6.5

·..·..···.·..· ····· ..·····..·..·..162 . ..:··· ..·


··· · ··..·..··· ··· ··· ·165
166 166

Jt.f!B*lOm~

3.7
3.8

*1jt/J"..~
~)J

3.9 jg 4. 4.1 4.2 4.3

J:m~)]
CodeVisionAVR ~~B41lI5px*~~fI C

l67

*.B_f.f · · ·..·
ij I -~4.3.1 4.3.2

· · ·..·..· ·
··..·..··..·..··

··..·..··..·..· ··· ·
· ··..·..···..·..··..·..··

· · · ·.. 168
168 ··..·..··

168

~JVtJTbt([DE)lf;tt~{t

···..······.········ . ..···..···..···..···..··

169 ··..·..··..·.. 169

J!Il:U·..·· ···..·..····.··· ··· ··· ·· .

iIiii=t1!f

·..··..·;··· ··

·..·..··

·..·.. 171

• X>

ti*A:rt: C~:fj
4.3.3 4.3.4
4.3.5

t=; Armel AVR

~~.:t1tf:·················································
ITETJJ[ftf

·····························175 ························176 ····················176


177 ··················178 ..

.:t1*~Ni.$
UART

4.4

tii i.l:il.iZl:;J]:!
ffM~mJt. ft1t
.f:'ilff~~
SRAM····················..······ ··· ..··· ..··············

4.4.1 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6

····.·178 . ····· ···· ···179 ..·..


179
··..····.·········179 .

~if

·..·· ..179 ..··

4.5

~if5fIl~Ji!i:rm
4.5.1 4.5.2

9
· · ······... .. .... .. . · ····· ·· ······.... .. · · · ··· ·

180 ···· ···· ..·..·· ..···· ..·..··· ····· ···.·..·180 . ··l82


183

;tIi;ifJ!il fI
tJ::r&;f_!l:i §

4.6

xt § t:iFi'it ~~m.
4.6.1 4.6.2 4.6.3
4.6.4

Zit
FLASH;fn EEPROM
FLASH ~~

· ·· ..·· ..··
fir ··

····· ..·..·· ···· ..·..····· 183 ..··· ..·..···


·183
185

i* ~{fL
Boot Lock Bit 0 ~ Boot Lock Bit 1

185
185

4.6.5 4.6.6 4.6.7 4.6.8 4.6.9 4.6.10

~.?';

-185

oit~Ili' ..·..·..·..··· ·· ..·..··· ·· ..·..·· ..·..··


UBl.2iflt
Program I All

·..·· ..··

·..·..·· ..·· ···.·· ..··· ..·· . ..·····


··..·..···.·..· . ··· ..·..·· ··
·..··..·..···..·..···

186
186
··· ···..··..·186

:;tttm~@B
Chip jl:qij-t Ports ~l:OO --F External IRQ

·· ·..·..···..····· ···
·· ..·..··

186 188
189

4.7

CodeWizardA VR 1't~~fflG:2J
4.7.1 4.7.2 4.7.3 4.7.4 4.7.5 4.7.6 4.7.7 4. 7.8

'
:iiii llI! -F
·..···..·..·· ··· ·· ·

··.. ·.. ··
···

··

190
191

Timers~liI!--F· ..··..·..· UART

-192

:iiii:qij-F -F

· ··

·· _-

193 194
195

ADC ~J1Q

Project Information i2\;_JlIi-F

i:E~i"Jitt~

·..··..·

196

4.8
4.9

§¥-~I~
Atmel AVR Studio
4.9.1 4.9.2
4.9.3

207

Wiat.:2J
ir~--1'COFF

208

AJ

AVR Studio

xf"t-· ..·· ···..·..···· ·..··..··..··..·..·..···..·..··


··..·

·..···..··

208
209
209

fA. CodeVisionAVR ~iJJ AVR Studio

n Jf::t1'ttJttfii,lliil:

• XI·
4.9.4 4.9.5 4.9.6 4.9.7

:7l~,

rf1!t'f~Ji!MJii

.,

·· ·····.·----·.·····.············ ··· . . . ..·209


···.. •···••••····•·•·····•• .. •·· ······.. ······.. ···210

-ijtjf_;fUjf1f~Wi.~

it -t!'lOtl t:& ~-ff3101!::.l¥Jm


ltw;fU-{tfi:&tn.1ffl.:tII:~ ---- -----

· ······ ..·210 211 213

4.10

*:i1it/J
~~

·· ·········· ······..····..······ ··..211 ······.········.·.. 212 . .

!fi

·..·········· ..·..·..··..· ·..·

~5.
5.1

4.11

4.12

_LtrL.!t.33
IJlI §:1f~
· ·..···..····· ..··..··

·..···..·..·214

*1l§-W-

5.2 5.3 5.4

'11 s· m~7f:5tDft ~ 1_jij §l 7f /fi.. J:i~ EttJ iV ~


5.4.1 5.4.2 5.4.3 5.4.4 5.4.5 5.4.6 5.4.7

····..·

··214 214 214 214

Jt5(Ilfl'@!
-rit-i-tllft

m:

--.,

---

····..·..·· ···

···..·········.· ······..····· ····· ·· ···· .


--···

··· ··

·····..····· ····· ··214


·216
217

gi!~ut:JE5(mt!i1:
ltft_fO ~~ut: iJ!it lfjt ~ M' f-ll ~:fi1f.~AA;fil7f1itjlfr.IW:: ]'!::&1E~~ilI:jlfrf3!: ···· .. ····· .. ······ .. ····· ····

· ····217
··· ····..····· ····..·..····..····..·..218 ···· .. •.. ··218 218

lJ(tRllftf:lt:

··..·..···..······..·····..·······..··· ·····..·.. ···218

5.5 5.6

1_jij ~ If tit rl:fi!.~ tti

:1J"19~:rm §: ~-1'-~at~~~fl.
5.6.1 5.6.2 5.6.3 5.6.4 5.6.5 5.6.6 5.6.7 5.6.8 5.6.9 5.6.1 0 5.6.11 5.6.12

··· · ·..···.··219 .
···219 ·· ······.·····.·····.·..···..····· ···..·..·· ... ···219 ·· ··· ······· ..··..······224 --~ ---

ftJ.~IlfI.IW:: ..···..·····..····· ··..····· · ···· ···..······ ;tXrm-f:It: i~:mJrtt:(£~it1iillll¥J~

1il:jI-~ifatrli_fftf!itit
3fy~iI!i:i!i':l¥.I.itftf-1Q:it ---

·..·············· ····· ..·235 ..···· ..····


···· ·· ···· ·· ····..······..···237

3!: ~ ~jil¥J~# l!l!~utJEX!lfr~ -.-

liit --

· · ··· ········..··239 .. .. .. ····· ··· ····.· . · ·..·· ····.····· ·241 . ..


·244
·250

'¥:~~:'i:i¥J~{tj:~it it:1r__fl] ~~ ill:Ii~ ~lftj: M' f-ll


~

····.····· ······.··· ··..··..·..··..··..·····243 . .

tJE~ n1t oW f;t ftj::7llit I!lr E9: -------* 7H~ JI


----1irr9~fi'_

~~~~;fil$.:{1=-7fttM-fJl:

··..·· ·258

~m;JmlA:wrf-ll···· ..···..·..·· ···..·····..·····..·····..····..······ ····..····..·..···..······..······..···

··

·284 ·288

5. 7 5.8 5.9

#~~
~3]

*./J"~
J::.;f11.~::g

······· ..···.···.···.·················.· ..··· . . . .

··. ·· ··· ···· ······ ..··288 . · .. .. · ······ ·


288

5.1 0

289

·XII·

iiXA:tC c fAiIi~l=j
~i?Ei~~;tjCodeVisionAVR AVR ~m*IJfi~~

Atmel AVR

11f:1~ A

··

··········· .. ···· .. ··· .. ········ .. ··.. ···290

~*
I!f:t*"
~~ ~~

1Jfo1~ B
C D E F G H

'Q STK500 Ail

··..·369
··········································381 ··..····'.···..··384 .

~~*1fm
ASCII AVR

TheCableAVR

MegAVR-DEV

3f£tli·······················································..····.··..····.··· ···394 ..··· ..··· . . ..

*~:"i:
m4r~:i[.'itl

·..···.····.··..···.···.····.···.·······397 .. ....
·········· ..···.···.··..··· .··401 ..···· . . .
·..· · ········ ···409 ..

~t~ ~t~

ffB#~3]~~

:JE S/..., #tii£;fniJUJ~~~:m:~ru ~1;t~~m, ~&i3:.tJ:b~ruEt'~1tm~:fJlm~ • jJ 1§.# IJ, eg fl-m~l'i!i ~ ~~5t.'ffi. % I!Jj lO¥-r1 • ~:fj:~BJ:H T~!iFf!ij • im:i3tmtlll;iE.r4~~t.fllS:~ til ~ •

~~*.~~~, ~~~.~~~~~:
*-

• iffii c m~q:r1tmegPJTfflE.~!f{J~* • JM#4fJ l'tf jQ mlJ ifr iD X1~Jf mt fi1" ~ I¥J 11: • -BHt'E1*ff~ii~ g.r1~n~mtliiH:Ul¥Jffi~, • If:m fl;rft " ~JIL ~;j1;JlO)tffl~M1t:kl • m !it Ft''.I1tt~MJl C 1ft §~J:f: ;fC7?c PX if

f!r~~. *
~~

l;.1,jcmt*f5Eif?J-

1.2

511:

*.frm7WC~~$m~*A~.n~B~~~0~TMeg~*~m.'E1~~~ CodeVisionA VR C iif"*eg -tffl7t(f] C ~! Ii j§jf JA~*~:!."Et'l 7fMl. f't.E~3i;.(f] ~;:J '.Q @~. ~~m~~~.~~.~~~eg~~*~~~~~ *=~frm~m~m~M.V~~~~3~~eg:
0

• F 1lJI~.1ON.;.

• mJ$a<J¥i.i\!n$Jlli(l/O). i.rJj~PJIZU]H1 *f!1!m.12*~BI¥J#rr~Q 1/0


• ~.101t.mtlll, ~~.~1T.*ill.

~m~.•m~~~~~~~~~E~~.~~*~.*.o

.ffl-~~frm~~m(f]~M.~~m#"flm,~~:fJl~ffl~m,

• C~~ifr~~~~~~J~tt~~m~*%~~.~C~~

~~~m~cm~~

re~@~$~~*, ~~.~m~~~W.i\~C~~w~~.m+

.~cm~#~~~~t*m.M.~ffl#~m~:6~n*~41~m~flO*~~~
•• ~-~m~~fl. IDfl

·2 •

*Aj\ c ~l~ ~ Atmel

AVR

• ~W~~-.~~.J ~~.M~m~~~~ ti-t- c ifrllf~rf.~:J;,;ff-1'"mJ&, ~~mainOj$I~o


J¥f-t:i-qttt rr ~ ft.Q.r2. fA:ff A'1 $!1:3it i1: mJ&
Q

mainOfElltiJ!~~iA7J H:li1E.1:;~ ff91f9J.. E ~Ja i;l]m~J¥I¥J~#tm~fflEt:L~ ~tJ #f~ ~'rH(£·F,majn{)MlRg*m:p egm1:r]. Jk JI:b*iDI¥JftffltR &~19Jtmft~om![:fJA ¥rj:£- -+~IJ&IY~l~Jf~ff ~

~w~~~.

ill~.~.IIc~~~~.~~~-~~
maint)

00.n

majnOffillc~ C i~8mJ¥8'!1~lltfj. £~ ti w; fBJ tilfpJ Jt] ~" ~ if P.R ~ PI ~ £jG ~


I -t-~.

-1' ffi fII:

-~BM.OO.A~c~km~~F:
void mainO
{

while (1)

/1 do forever ..

~_t~JF wI IJ.ftt*±I!!J1t1ftliQil-f!liEh, ::[email protected]~/G~~DmMYfl''f :] ~H 18- 13-i¥J~ft¥t. JC~


#include<stdio.h>
void mairu)

&~~WffW~OO~3o~~~~~~~ff~~,
0

B. ~~~~filli~oo~a~~~.

E§ 1J~mJ¥{£;@trl11

#R~UH

printf("HELLO WORLD");/"'the while(1)


lido forever ..

classic C test program .. *1

~~J-:~f¥::m~*
fF-§*Jt:Jb!1m.£fo
PC

~'HELLO WORLD"

~iliirJtlFt1UIitHfJPl:trJ:., ib:MlliNtr:Q:ffnrtJ~~-~

*H~no~*~.~~.~mff~~~J ~m~~o~~ffl&~~~T~m~~~.A~.~~fte~Z~ff9~*~m:
m:ff~ft*!Jt,
--_§_~J¥t*l.tJ.

~~.~~.~-A~M~*. ~~.~.~~B

.A~@fflm

~ret¥1M;tty:fS'~1'P~#t~ ffii~Aj:"t.~

~.&~.*~#t, ~ft~~~ff~~*~~, ~~.-t-.A~.~~.~Jf.AW£~1m* (i3:1'-x~~ll*ffi:+~~J¥!.f! i¥J~1'-{tr.1i), #,ti~_tOO19lJT~ i¥J whilet l), ~f¥DJ ~~ Illl.iIT~.
m1~~~f~T-nJfiii~ld~M1fJL$ftt While ~#J~~mHff~ "l19='frrtJ t17~lEff.futM1~ T -1"~ mf!ll:~W~if:~(preprocessor compiler)m~IYJ~{9rl #include ~W stdio.h Xftf1t7iJ*f~FfA''1-$5to printf()~I~EBjrHW~tM~, I1J 1j.t£~~J¥!fJ1!:m, £ b.I9 1;] ~P1!fil¥Jf£5t..1fL-T stdio.h cp xftf:qr tU:J.J5 [8~Ti l.}l. ~J!t.m~HH1CMt~JiXt£-~i1i£ .citI¥J{9rl T~ ~-!l-~1I:lf#~gl¥Jj[;ffi, 1-1:
0
Q

~~~~*

.o

Jt:* 5(~*

·3 •

{}

:ttn; ~ {}iII f:tfi!iJ!PlIf1:?iJ?{j.


Mt~

(f] 7fMl

~ta ~, J&Jfl-f"m7ft#J---

9~miij fF -:tJ -1-IU~*

"text"
II~/· ... */

ttWQ£~*m~~OO~Me~WM~W~~~~tt+%.~, ~Wft~£~X~A~~ ~£.WOO~~~~~~OO~o*~~~iliOOttwm*MW~~~~~.-ff~~ftottW~


~~~r±W~J'fr.JftfflJi!4tr.-Ji. -5(1*)~~ -'7-*4r_[(*f) ~4tI~,m-5 ffl-TriWa<J7fM1 ~w:mi - gi@lf~~HI-Jl ~(/*). Wt21,~ ~WOO)(*(~P1t£$tr)(*). [~U:iAJIj£ ] -5-~tI("'f)o X1-Tlltr
0
Q

~MW~$~OO~Wfi~*ffl.

W~~ •• ~~-fr~~~~ffl~~~m+.

mHI9~J¥rm*. mainOPl!t&~m 1 E~J%Jft7}Wr.r~{9~T M -T ~~tI % J'fr.t ti if ~ 7t ta oog. 'B]iJi iE J1~-fj' ~)[


Q

tro

{9~rI 1WW~ff 4' main() PEl ~ 9 fll¥J

~ <If X;f!lll ~ 1'I;B B9i*A, ftff J EY. ~ it -®ia rtj;J{!!l!U 1I1£ • ~if!~r~5t.~~~t&:g. EI3~£fMr:M~m~-j!*I¥J~-m:lO/~~~.
fli,aX: • *$iRr4!R%:k/J\~

***:

lO/~r~IJ~

•~mnm~~~.*~.~~~~.Rmm~~•oo*~, m • ~~32~~~D
bY: _~~:~j/J \ 'G'

~~

• ~~.W~~ti~~A~~.OO*~ .• A. *HM~~ffl*~m~DrOO~iliT~~*M~~
auto break
bit case

~~~*m~o~~*m*$~~~~*~.
Long

defined
do

float for funcused goto if


inline int

static struct switch typedef


Union

while

Register return short signed


sizeof

double eeprom else


enum

char const continue default

unsigned
void

extern flash

sfrb sfrw

interrupt

volatile

• ~~ c iH11r £-~rp § EtOfai\;~iR~. ~~I='-~~ ~1-5'31 Em~ "~S" ffl. 'E1j! ~#t(fB] IWJ) ~ mu ~:ro: 1OJf 17 (@J1f1-ff!l/9R;:I!tdf1tf)
0

j~;;bt,t~.afo ~S

.4.

*A.~c fiIilW~

Atrnel AVR

Btl5t.~~#.fl$ff*it;~*/J'\, tp ~*tT # OOffl A


0 0

~w~~~~.~~.m~~OO~.M9a~ft~,
g£:I:~ru_

t:1fJ:tE;firFf¥~~ttJ w.-lr-#JB~~fit'

~.~m.a~~m. ~~.~~~
I

~ffJ1fijH!i¥Ji1~

1.4.1

unsigned char Peabody; int dogs, cats; long int total_dogs_and_cats;

~.~~.#ooa.~~w~~~~U.tp,.~ ••D~~~.~~.mm$&#MM, ili~~.~~~~~OO.~~o ~~, ~~M~~~~~ •.~~mM~.~*~®~mo.


1~2?>IJ

I±! T ~1i~m&Jtj;:/J\e

~
bit

~
1

*'Nf.iL)
0, 1 ~ 128~127
O~255

-m

III

char unsigned char signed char int short int unsigned int sigt!ed int
long int

8 8
8

- 128~127 - 32 768~32 767 - 32 768~32


O~65 535

16 16
16 16

767

- 32 768~32

767 147483647

32
32

- 2 147 483 684~2 O~4 294 967 295 - 2 147 483 648~2

unsigned long int signed long int float double

32

147483647

32
32

± 1.175e - 38~ +3.402e38 ± 1.I75e - 38-- + 3.402e38

~~$OOB~WM~,~.~~.~~m$~~~~~Q~.oo*m~~5t.:tEm~tpBtl ~~~tta~.m.FOO~~$~m~.~~~m~o
1.

FciM~.

~.~.~:tE~~OO.~~~.~~OO~M.~~,

Am.m~:tEft*~d~~~.M.

oo~~oo~o~~~.~n.~~OOM.~~, ~•••~®~.OOWm.R~~m~~~~
~ I*J
0

,$j fflFjl:;l:]E Il~


0

tt--t

-ar bt TI: --t iEf1~ 1tffl, 1~~ ~ fffi

ij !~ {ttl '9.! • 1;51 jg

til ~~.g, #f ~

f& ~ ff'1·~·$?t 2.

Jlli~.~
jg
0

•5•

~~3£.
I¥J.:ff#iB ~ rBJ. PJtlt~J¥ ~ mff B1Jll~ijJ rtl] (ep W ffl i ~iit~)o ~fiU~:S:ij~~{ffiiJW!J~1~BJz, # ll~1*M'~~~ii~J-m:, PJ,-&!;litMiE~1lT w,1tm ru 'tt~ 'F. ~ }~j:~. {£ maim) fB JQ: 7f M1tJ:t.1r iT 7 ~ ~ 01 jg *) Jtt tj W it 1ft ffi ~ W It1~
0

~ffij5t'.(~9Hffi~S:)~
~~§t

m ~if.~?t~

A ~~~~~~~~tJ:t.iT,

rIm i¥J1~~*ffl ~~7J,5E.B1JfFm:l!£:


unsigned char globey; void function_z (void) { unsigned tween=12; globey=47; main_loc=12; } void rnainO unsigned char main _loe ; globey=34; tween=12; int tween;

mT~~~**~~m£~c
Jla global variable

Iithis is a function called from mainO


/I a local variable

IIOK because tween is local IIOK because globey is global

Irrhis line will generate an error


i/because main_loc is local to main

II a variable local to maint)

IIOK because globey is a global


Ilwill cause an error-tween J Ito function is local

z
.

while{l)

lido forever

j~~r.u5E:;I:o 1.4.3

~-~~.~, ~*~$~.$~~~.~~ .• ~M.R1tffl~$~., ~.


~.
{f[ -=r _R *Hflii':ft{ROM) tp. /1"'~;f.&

fffi~~ffl

~~ *!iJ 8<'J '~--$?t


U'iiE1f:
x=3+y;

~ftm~. m.ff~~., ~~~~E~~~&~o~.TI:m$~~T.~~.~~§~


I

?tlfc!fIJilJ~ l~lUJL.:fffit~(RAM)9=J

1tJ'iEt

• 6·

~AA. c f;;~

l'j Atrnel AVR

1B:rt :

~, .*3~~ ~~~~~.B.~~H~$~~~o*.~~~~~~~m~~~R$ •.
printf("hello world");
lIThe text hello world is placed in program memory

//and never changes. x=B' Irrhe letter <B' is permanently set in


I/program memory.

H'11l

*
1.

ff!; ~ff.m

L;lili1f* M ¥ JGRX -m- :iI: I¥J pr IY3:


const char c=57;

const

-*ffi ~ 7tt.

jf

JlxE5l..:;tt~rulO*/j\.

IT: m~;f.ffi*r-1~m7E

3tf¥1m~raJ~,

re 5t.~11 ~ -;It:l: t$. 3t.ff fit -tE~J¥ 1't rAJ JE . 1:1} it ~~~
l3:~ffWJf~·~f.f~~~J
RAM :Y:rHJo t'{fi~~. px:W1-r-" ~A:

IIJ

rfIj /f

~ fi.-fit tE

RAM

'f B'~ ISIl! PI ff


I

i!~1Tf;.Jjf~

tiffl~*.m~~ttm~£~*~$~%~~OO. MW~~~~~A~PI~tta~~~K
• 7t.:lW~a<J+j~HI1tl*P;:(1,lu 1234) • §tr~7g Ob 1¥J...:::itt$'l*P;:ctln OhiOlOO)

• Htr~~

Ox

Ef'~+/\J£iliU~J:tc~n Oxff)

a<J)I..~mtl%i\(1m 0777) ill:ff -Jfu1!t1IJ m PJ !J. £ t1f ±lit. 7E5J...~ it a<J'fiJl AA IJ\~ ffl $i : X • xR~)HIll'ffl.jij" U~ Fnfi U(ftn lOOOOU)
"~1J~Jg 0

• *llm*:l:ET~~J§ii 99L) L(1zrJ

• ~~~M-m-.nr ~~)BtI
2.

• }[;M=~*~~11tit~

tJ:ff ~~

uLctm

99UL)

F(iln 1.234F)

• frq~1it.1~Hmm$51~M~*.

{9IJ~U'a'9Jt'A'

~f.t~~.

'¥M=~1f.j.jij"WI!PfffEPf11(lt.1m O~9, A""'-Z). iB ~J~U!JC~1TEp j*I¥J~1-t(m!~1T H r.t~ @*M~*1f1~~%=)~ PJ1JE[J¥1-1~m-:l:PJflgffi$~1 %~I~*({9I1W1'a')o &~;fIFom-t- JI.. jlHM~+ /\i£iliU-m:. jf R=I $iJl ~ iJl®=*. -tB f1J?,{?r~~1'-fro=~-;t.:

• 7·

$. BEL
Back~e
TAB

ftf
'\a'

7F

J;j".liitrtl+*i!~{i.
'\x07'
'\x08' '\x09' '\xOa'

'\b'
'\t'

LF(~Jj.f.f)

'\n'
'\v' '\C '\t'

VT rr
CR

'\XOb'
'\xOc' '\xOd'

&#4h(\)fOi(! '11 ~-cr¥rq;$: ~il!EB ·~u Ef1&_~tI*1R 7t, I;J. ~tiD~fi~* ~ 16' -1' ~ 19JJfm, '\" ~ ··1'.'JI-,%f~. '\\'R:~1'"&~.:j.;tr~ BEL R:r-'1'-!lfilI¥(~N, ~1Bli+.m~YffiL~~jjfflVJ1{3*
a

!&1M~bttBF~o
1.4.4

~*~Q~..\t

aC.~~~~,m~tt£+*~.~~C~g.~T~*~M~~X~M, ~~w~m ~.m~.xOO~?nX~~~ff.ROOmm*~M.~Q ~*~m~m~* §W*eoomm~~~.OO~~ mM~-~~~~: •. ••


int num_val;
Iideclare an enumeration

IIdeclare an integer variable

enurn {zero_val , one_val, two_val. three ~val};


num_ val=two , val; II the same as :num_va1=2;

f 1it.m 0, one_val JtJ!iIt f :iE itJ b'J.5!i 11u ~ ~ fJJ ~itt , 'W~~Jl :

~ f

zero_val ~.

-m.lt I ;

two_val~.

T 'M'.

iOC.dt~ ffE

enum {start= 10 •next 1 ,next2 ,end _ v al }

#f startMm1u

10.

mmHI9fti~~=¥iOC{xJ.1O

1" nextl -jg 11. next2 -jg 12, next31u 13. ~;ijtffli;.

W~W~*~~~~~~~§tt~~~f'

~xa~#~~~~~.~~.

~~~Xft~m-1'"~*$«~~-1'"~;$:*.~.:
both_leds_on};

~m~~*~m~Q

enum { red_led~on = 1 ,green_led_on. #define leds PORTA

PORTA=Oxl; leds=red_led_on;

flmeans turn the red LED on


IImeans the same thing

igiU#define leds PORTA fttiDwfiajJ§j'itl ijt iiij leds

1M ffl

PORTA

*~tt

leds, 7i:t#define

• 8·

~A.A c tii@~

AtmelAVR

13: -:fft'2:1fL2l.5}

f' r itffl ~~*~ZM~H~7~*T~!lt~~J¥~£$~~~£~3.o


0

!¥J11L:;kJ 2, both_leds_on l¥:Jili~ 30 BlJJ8~t£fj!_J¥tt}ij*f21Mti~~7'6=~&~(LED), ~~Ifj~W: I a1. tI:tJjf; *Oul±\ta 2 a1, ~;:T%~~ ;ttg5(tETf~U¥q::.8<J1j1;ji leds=red_led_on btPORTA=OX I 9!Mtlo #define £ f1Ji ~ ~ fj ff m4'- f1Ji!lt~nff~ ~~J::::f ~ ~ *t.t: ~J - $ 7}, {I::I £ E 1fJI~J m+ H:: c
1¥.!«L7'.1
I. green_led_on

~-~*~ ;f:Y:$i1tl! red_led_on

;f¥-

i~ T

1.£ ~J tl~~

fYi: ~ ~~ff ~!:J ~ ~f. g ~f¥ ~ ~ 7} 7f 8'.)- --1'- ~ fi, ttiL!lt;FJl[ ~ff:(£ a

1.4.5

ff1it~~
auto" static ~ register. Auto(automatic)~lAA1-A.I¥J~~~,

~:l:RTFf!f:I:*t 3 f'ft#Mf~~:

mbl.

~ IIi ¥

auto

PJ gl,~'

815-

1. ~~~.

*~~~mo~~~~ma1,
auto int value _I;

~~~m!¥J~M~.t£~~~OOa1*~illfi~~~,
B~~~}ijagH~~~~~~WY,

~bI.~~~ffl~~~ff~*.~~~

.~. ~ji8<Jm~~~d~~o

@~~~8<J~.~~E~~T:

~~~~~~~~~~Am

2.

wt ~
7}DCc
~

D~~.
int value_I; f.ij $~•

/Ithis is the common, default form

-~.M~fflmm~Itt, ~~~.8<JmH::.~!¥J#Hm~~~a
3_

f1§ ~

m JE 5(t){J f£r • it}ij -WX;( -1' f§t~ tt Jt 1m I¥Jm ~ iJ] ~1),iEl tEit ftij fffiji ft ~ fA] ~ m-- &:: WImitt, 't lit fJ] Ml~ ~ • ill!~ ;UHi-Hlt. * 3f:l: 1~j~ EfJ ffi. ~ lEIill: • -ft E
.R. ~
I ~

~it}ij. R}ij~~M~~tt~I¥J.~o
register char value_3;

$HM~$~.~~~~:I:~M. ~~~.mM~, #R~~~~D~~Z~t£~: n~.q::.~w~~~OO~}ij~~mM*~W*~~.,~.~~~~~.m.8<Jm~.~~~o ~~OOSff •• ~~, ~*ff=~~ •• ~.~~~#~~~~.$~~

.~H~.
static int vaJue_2;

a.

~.~I¥J*.m~J

a1$~ llei ~51 iijIJ~:I: *- "J\ Et'~~~~ 8<J~~o~~~.~tltE.~~~~J::M~~m5~~*~~~


,fjpJ; lQ ~

~~$t~ft

ilftE~*~rrM

fiij

lIift IWF" ~ bI.

~~,

••

1f~~I¥.!~~~

••

m~:

int x;

II II II

a signed, 16-bit ,integer ( - 3278 to 32767)

char y;
x=12;

a signed ,8-bit character( - l28 to 127)


x is a integer,(but its value will fit into a character)

y=fcharjx+S x=(int)y;

II x is converted to a character and then 3 is added, and the value is then placed into y Ily is extended up to an integer, and assigned
to x

~~m~~A~OO~t*~fi.*~.~, ~m~ •• B~M~ ••. ~m$M~~, ~


ft: l¥1t1£fiffJ&. m
int z;

+~t~~~. Jl: ~
Ildeclare z

~1.1I1~ En:Ritj:t:

int x=lSO; char y"'63~ z=(y* I O)+x;

II declare and initialize x Iideclare and initialize y

Et~*'J'\. :ff1~Hi(y*1O)~-1-*:7if(8 til}*#;: )Eft:. 1.&tE:tttt_t 1¥1~*:mm ttl 1iiiifl[~ I¥.J t~- -1'-* 11EJ(;1l 255. JK~~~J~.1ffi z.Fc 1 I¥J~* 118(Ox76). rm::f~IE~Enm: 630(Ox276)o :tE;j(_m:(f<j~ -1-lffi"fj, ~w:RiJ..j9~ W:B1 16 118 ;miif~ Jll~)HW. f/,\J§fIf_f1l x ;fiI1Jn liJi"i z :mJJt1l:k 268. :iA~F.m~!
y

§~~:R9.t~~A;fi:i11~:iE.IM", '8;m~~

jElJ,

@§ffl.~~~~.*~~#~~*ff~~,
r-{{int)y"1O)+x;

~*~~nl¥JlJ*_~~~.

~~~~4~~~wByE~~gW~~~j916~~~~~om~16U*~g.~~~ ['j<J{j[~~~ 6300 1J[]_t~~1l X m¥IJ 780. flE ¥-H!iClll~ 7800 z

¥iI~IYJ-*tlTl¥1mpw~: ~ D!lJ¥j£1T1t.~~
a

~~, ~~~~*~~~.*m~~~, .~M*~.*ma


'(1f~fUJIJ1,

c i!~I¥.J3{m-tt1lH';o ~a~¥*~*~otE_tID~M~*,

1~.*fti,.

B:m~f~ftz,.

tj,~:Rm~fFttl 1¥11N~tt~f§ff91~:tlt
'*~~fft:1JtfdUJE~1Iff.*1T~~,

~*~.x~6W~lE63.~~~£~.W.~~.~
~tr~~~~"
0

.A~.~~.~~ I¥JfttTijij n ~~OO

•• ~.~~#~lig
a

~~, '8mOOW~.A.ttl.fF~~M~~~

{4 *~~iK.Ltb~li1Jt! B1, iK~~~~;t #{tffl sfrb W sfrw ~~:R1ftr~*t1t4iJ1'-#tfY; n~~ 'fiB 1/0 12:1i-?tnc;f,-F~aftffJ¥.f~ 1.611~ii~iKJlli$~, ~mrOO~WJtffl lPf~fttT_n 1¥Jm~: T
#include <90s8535.h>

*~.C~~ m7~*ff~~~fl~~~~~~~.~®~W.~~tt--W~~% ••
liregister definition header file for an Atmel A T90s8535

• 10 ~

IftAA c ~~ "'J Armel


unsigned char z;
void main(void) DDRB=Oxff; while(l) { z=PINA; PORTB=z+l; }

AVR

Iideclare z

Ilset all bits of port B for output

liread the binary value on the port A pins(i.e.

input from port A)

Ilwrite the binary value read from porta plus I to port B

ff~fU~ tJ A ~~lli~ D B (fl'11~f~~:I$]JJ A~, IiSi t.Ji1Jl:!;.fi~~;QHm~!t:Jtf4 90s853S.h ~~JE5L;fffi~(fl1J.it*~m;~g DDRx *HftJlJ7IU;1cJI;:~ tl x(rn ~:£Iftt]!;:Etr.J A, B ~)B1I1JJ~,@{l[JlJ Ttiii ttL ~m:frHH, JYT 1f 110 ~tl~v..JJ4UlJA. ~JIPt, DDRx *{f:tl(flfofi~lli:m~" m~. ~J¥93.ftH1JPI1f~~~{f[fflT tftrl:B:~e~~ DDRx ~ {YIJ~O:
8 {fl~m
Q 0

J:{9IJ~JD7~1=J*trjrtilDf¥J1Jrto I¥JJI#ij u , ffiJ7Cr.J~*r.J~tr~.iEtlf4t~

PIR)L:J;JXr.J-,%fBM*'J\~.,

l3ljJ9WO~-"---}

8 fl[

DDRB = Oxc3

Iiset the upper 2 and lower 2 bits of port B for output

JJ •
:. 0

T~ftfJt5tm.~~fi~Ji!EnAAl_roIJ ~~--r~ib:Af' 1Pt. 1!:'1JjU~JlJfJt5t~~Jj!ti/j·F.f~~m-~*o


0

f1(false) • tE:fJt1fB1:&JJ*. 1f ffl


(true )"

~~~~-~mffi.

~~*~*.~Rn~Umr.Ja~~*o illPI W. ~Ji «L :@.1if ffl ~ FuJ 8!l1¥J

*~ B:ji iR

aJj ~ il;ft

~~~Z~*~~. ~*~
ttl. fi Il}jfi ~t~(fl)g
~it T ~-t-iEJJf{f

- gfl1l!ij T~:I:,

:I:~~~:rt ~ tE
dog==35;
val=dog;

~1lJftfflMffl"::E~1"f{=)*M~l1tff~." M~::stt(flffl":~t.J1lt., *~-9J, :lE~A~~ftJi(tjiJH~)1jm:'r1I¥J~*~ A~!¥JJ1ii;~j£JJturF:

dog=dog+Ox35 ; va1=(2"'(dog+OI72))+6;

y=(m*x)+b

-Qi. 5tit-.1N~ P3(fl*i6~

B~~~~fi~~.*~*.~~~~ .• M.~ff~:.-noM~~~€.,
0

1:.f9rJ4'

y=(m*x)+b.

m~ x

5ti1Hli!. ?&Jff:f1fffi*~ga*fI}11nJ: b,

~M~I¥J

.§.M*~~~y.~M~*t~.~*.H~ft*~o*~~.~ft5tT.~~~~~m

• 11 •

1It~i6~:
y=m+x+b. [t:ij y=(m*x)+b:

M~~~m~~~~~~tt~*14~M«~g~~~*7AmEEl.*~.~n

R::-~ EEl
0

*
/ %

.~~*~g.~~~~g.m~~~€.~,~%.~lli.~~*#~
TILi~Jff.T
~

.7.*g.~~~m~.~*, ~~~~~~$~~~~.~,

§M~~.m~~.~
t'F ~
(char" int

r-r tJ;t. IT ~;:}j ~ ~!lJijj ~ T {~- g& Er'1 ti 11:: Ja !fum.f.f ~~i~ t.\ ~ f{~ Jet-I long ~M)®1tffl *- 1-5 ~J!««5tti}m~5~tH 7 tLig.r.L
N ~.
0

ft.

t;r:

"5

CI

Jf1!.&

« » &
A

lilt
;f:itt !:;r

ff~ E1

-e.~tt~.~l¥JfI:it~r


: lfR B~jE.~ T ~"Hi f{t&:{fl lfR & , liP ~ 0 ~ 7!J 1. M. I ~ 7i1 0 ffl f1:; ii: f$~. N m ¥.J ~ 11:: -e.=j~HM $li:# T- (~. iB:~~~ f{l¥J tL:i: ~ ~ Jm.ii;;fi 11:: Jill¥J" ~ -*.mJi:l¥Jo ~E.t£~ I'~" EE11t\;ffl~I.:*o li:#I w:;ffi~TX1tiitrO'@~ 2 ;;fi.g.~m*~.11::f{~~=*~~~.~f$~~~.~~~~R::.M~~~~~~ *mJi:~~~#lw~3T~~2.~;;fi#~.~T~f.f~~.~x~%~.~~~fl A ~ Ja] tE 1ff.f~II! 4' I¥J fq % t'l:( ii:it1 H<J ~~::g-:li ~1f~ w:)~-GjH~l'~ %:Jt JEft if % ~~.~f{~SM~~~~~~~~ff~#D~X~%~.~#". ~R::M~Jil.AO l§€.r.t~~t-~11::f{EmUl~~1 at. ~t6l!*~ 1 ~!il~.n={Efijj~Nl:1tliI¥J{f[/F[RJHf("-,~-t~ o. ~-~1g I). gal!;t:tJ 1 liJtJii. fq ~ Wi 1- W! t'F f{~ 1Sr J=J. ~ ~ -1'"~ 1. ;ft ji ~mt JIiJ I

• 12 •

IttA.it

c tII1if!ll1;i

Armel AVR

~ 1~6 tit til J -lltifIJ To

fEl i9:5E~~¥:r1

y=OxC9o

~
x

-y

~
x"=Ox36

x=y«3

x=Ox48 x=Oxoc
x=Ox09 x=OxC8 x=OxD9

x-=y»4
x=y&Ox3F X9'''1 ~IOxIO

#include <90s8535.h> unsigned char z; void main(void) { DDRB=Oxff; while(1) { z=PTNA&Ox6; POR TD=PORTB,Ox60; PORTB=PORTB&Oxfe;

I/register definition Ildeclare z

header file for an Atmcl A T90s8535

liset all bits of port B for output

Ilread the binary value on the

Ilport A pins ANDed with 00000110


Ilwrite the binary value from port B IIORed with 01100000 IIANDed with 01100000 back to port B back to port R Ilwrite tile binary value from port B

Ilplus I to port B

~ft~ 0), 1Ifi • a<Jf3'L!l!IJ [u] 1 Htfj fJ~f1:(x faj I HHf -fJ!if1:~ x), J£:~. FJf1fI¥1fl[(:ijfd~~{fl ~ 11 Ef{J r p r~i] t'l~ M,)~1H~Ji ~ 0 T e W3 t'l ijij tJ ~ iM jiJ !j,1& ~:#. fT ~ J:J Er<J - -1'-!ll ~ 1'-V'l • f.§ /F 4? ~ Ilij i11'iffiI D B9Jt1m{fL, 1 (X til !_m PORTB i¥J ft ~tr it m EXi~jf:r1* fti?s 1'" tjfij r=l Ef{J ~ Ul ~ MJ. rm /f~~ ~ MId B9;1t 1mtL n 2 7xH:lf~ PORTB ~Jftli!Jrr_~7J-; T~n{DJiIlitm -1'- o 1111f ~iE.*~fqIij D (J{J-{![~{a; ~

~m~IE~l¥Jm~tt*o*~m.oo~~~$~:mo~~m.l¥Jmmff~.~~~om~~

.l;00 I¥Ji§lJ TriV.K

7 JJf i!i:(masking)fll

{}IfqIijt1 fi!ilJjJ(bitwise port control)

J# i!i::&Jtl~IHjffiIE-=iit

<>

• 13 •

:i2$lf::€~~ ~ *~iEJnrt1~'IH!=Jtili~~,113 1:: i¥J~~.z.:h F * j.:(True). J*~ J.;i&(False): Jt fJTlf~~~illi*7J'. ~Ul*7F i!~~~ftll ~ ffl Kr;!iMmilJ t:p. '31!¥!¥~F¥!tl.1TihL;m m ffff 1N FA
0 0

1. ~$I~.~

*l~mMtt~mM7FT~.~.No

t-,l(AND} ~(OR)

&&

[email protected]~m~.~~mm*, ~:h~$lf:~~~~A~~X*~~.W.g~~§~~ .~~.~.~~.~~*~M~*~j.:. ~~~*~.o~~~W.$R.~-~:h •. ~ .d.~~M*.~.; R~~m~.~.~~~~, ~~**:h.~ ~~7FffiT~m~~~


IK~~:

.11t: x=S.
(x&&y) (x&y)

y=2;

(xIIY)
(xly)

lQ OlOb :i1trrn:1fJ. ~.{t~~a* ~~:1;J{' ffil1J~-t-~f'F~t:p i¥JfT.M --1'-:m~~F~"


IOlb

~*~j.:,

~*~fl.ffiltJm~

~:hWft-~tt~~~~F~i¥Jo

o,
111b(~~-"¥:)o

2.

*~~.f.1

#r*:hJt. ~j;J~A IOlb

o lOb it11TnctL!!.'(~{t:f;9.:±I~* .• ~~&~M~¥~.ff*Ul,

~.~.w~m~~~tto~~~.~.m~-~
~~~~~~j.:,

&~~~.o~~~. ~*~.N.tt~Mm~*~~~*~~lli-~Jt~

~m

~EB~~"

"~~i¥J~~~*f~lli~.~~~?" "~ili~.W~~f~~+~lli~.%.~?"
1-8

JYfJJ;':h*~:@.~o ~{9IJTo l-9

t=
«:

<=

AT

>
>=

• 14·

fin: x=3,

y=5o

(x=y}
(x!=y) (x:<y) (x-c=y) (x--y) (x-=y)

Jt
~

..
~

1N '1M

1.6.3

iS~.~"

~ 11&lE.r.f~n~
~ff]~

C*~~.~~aSffff~*m~.~~. ~~mm~•.~~~.~~m~~~~%'=,~.
~~%'=~~ *~jt1HlE.r.r
fr

Mi1i

c itf~Et-JltM,

f.11f c

*1§~ff~1f1iiiaffija)Hiifr

*Jntii~.~

·~JH:;iJHG:ili~%'=l§JiiZ'11AJU

1. 1S±I~.f.1

~m~~N~.&~m~. ftm$~d~~~~~~, ~~
x=x+1

++x;

Ilpre-increment

operation

x++;

lipost-increment

operation

ili~~~e<J.

~*ZA~tr*~~.
{9Ij:Wl r
i=l ; k=2*i++; i=I, k=2·++i;

iHno 10 ++x if::Atr1JDfilidt. WITx++~J§:b[l~fF: mif::mu~OO*~AA~EOO*~*ilitrU~~.

;li~IIh:., ~2:i1~WEn~lij

l/at completion,

k=2 and i"'2

Ilat completion

k'"'4 and i=2

ft:1~1 ~t.(£
2. ~

r, i Jt~

;J:IHW, N;UQ

1~

tEm2 ;f;1A't5L -F.

i *110 1

¥}fnffi*" 2

l1iitiE.f.I

~.~.R~~*~.n~M

.• tl-~.l.~. ~n~~~fn€~.~:

• 15·
j-; Ilj~j~ Ilj=j-l I

+i;

3. 1!*MfiiE.f";1

Jr. f.f

~.*~.~~~~g.~*~~~~~M~~~~~. ~m$~~LffR~~~~~§~~
0

~~~~m.~~~~~~~~fi~~~~~*~.~~~#E*~Bm~~~.~(=
~J:ruJ ~
a+=3; b - =2; c·=5; d/=a; lIa~a+3
IIb=b - 2

IIc=c:t.5
IId=d/a

al=3; b&=2;
c"=5;

Ila=a OR3 Ilb=b AND 2

Ilc=c exclusively OR-ed with 5


Ilwrite the current value on PORTe IIANDed

PROTC&=3;

with 3 back to port C. Forcing

flail of the bits except the lower 2 to 0 !Iand leaving the tower 2 bits unaffected

il1it xt T fJ] ~lHiJ¥ffUI~ ~:# ~ iR. 1:: E:j 1.7 ~£t(J~$~~iiJlf*~J!1.m~1f


if (expression _A) expressionjB; else
expression , C;

*#.~~~~~.~~~.~#m~~.~~~~~~m~H~~T~~n~~ m

*~.. k§:1g '2 ft-J:tmli. ~ ~


~ JlJl*~~1IT~i~iiJ:

!t{(ltf

*~it

~tHR·1tt it:A ~

expression_A ? expressionBiexpression

C:

Jj,_t_ifW-~%.it, W~tt7tit.~~~~~ expression __ : :ruJ*~*it:Jt1J •• A ~Jt:A expressionB, =a!ll.HtJ.tT~~A expressionC, ~~~~. *#~~~~~d~~~mJt:
(x<5) ? y-l .y=O;

!J!O~1T

!!ifx is less than 5, thcn y=I .else y=O

~A;;t

c ~~£j

Atmel AVR

1.6.5

~.f.1ij[;:$t~

~ili~.m~*~~OO&#M~~. ~~~tt~@re~.o ~~~~M, m~~~M~*~~


~ 1-1 0

~-~*~~~$~.~~~. ~.n~re~g.~7.~.~.~~*fi*~~~~a
5~ ill 7 ~t.b. ftC M.;tr:~;;fi P1 M.;;fi ~lC)a1~

~wm~.~#~&~.~&~~.~~tt~.~~~oo~m~-~~.nA~ffi~~tt~g~
fn ~1t~W
11l~J¥
Q

!lI::W {XFF~ ~ 7ttll

(Grouping)EiJG

.X:lf*( association)
t&

~!W
O·[]-> ! ~ - (type) ·/%

-- Ji;~~r.r
.. [j~jfr.r j

~*~grt

l(lR~~J.U
2


>=-

f:1
- sizeof

ill te

M1.i.¥.ki
M1i¥.ti

"

* & ++ -

3
4

fttLiEfl-rt

w.*:@~n
Lt~~"rt


<

M1i.~t; JAft~;t1
» <=
!=
>

5
6 7

M1r.~'#

*ffi.:ih:llfit {ltg.fit DlJ£~r.1"

Mtr..¥ki
Mli.3I;b·

-&
A

8 9 10
II 12 13 14(:fN: 1t£~

M.1i.?f.ki
}Air "!f:.{1·

tL@JH7
.i!$j~jfr.t

~.J6~:n.
*:1tf: @.~ :ro:
~{j[ili.~

I && II
1:

M.1L~b· Mli..J£ki
M1r..?f.1i

JAfi;PJr.
+ = - = /= *= %= « = »~ &=
1=

Mki~ti

~~.t:1¥.t7± 1.9 ~11l 1.10 ·-PY·rirm, '@1i5:ii;.$:iE·:Jfrf. :tm BlE.1-1(.), lim~ ([1). lf~-rBj~;E.W(-». ~ffJ.m*mlJ~;f$iJ~1\IFrJi1Hl1{~,m, 1Y~~U~~7L~P.1~«J7t* iE§ m fir tt .i5 jJ ~ 1OrBJti} ~:.: ~ ~ • ~p Itt pg~(*) g Jf.1'B= 11l1(vJfuhl(&)€. f..f T lID~ JF'WIJ:f:ij- m WJ fg; £ ttJ :ttIHlI!.M tJ- .L P3 !fJ.: y=3+2*4; Ilwouid yield y= 11 because the * is higher
:RIP (J<] ...
0

IJin precedence

and would be done before the +.

Ilwould yield y=20 because the Ilhighest priority and force the y=4»2*3; y=2*J»4; Ilwould yield y=4»6 Ilwouid yield y=6»4

0 are

the

+ to be done first.

because the

* is higher

Ilin priority than the shift right operation. due to the precedence

1£ ~ r.ff)t 7t i'l ~1a1Mm9!~Ei<J {ii: ffl


ti~fI:*BTI1lTft~tt ~

£j ~

w. ~ ~

;flH!J.: :flu

ffiftffiMm(J<]~~~~o$*~M~~~~~~~~*~,

*If~~, lllUftffl

of the

* operator.

~~~*~~~.fi.

tiS -'5

*liffi

f!?J& ~ it.

~R~.~

• 17·

iftiUffl*~#llIffiI¥JMi*1t\1T~ tB I¥Jfij~tifi;z. while , do/while ~ for iaiUifL*f2ifjtlm~JREt-J:m:~ switch/case *iUffl-Tfti't-ffl.Jf!.-I¥J~JItJTt1. -#ffl<atJi ~ W¥sf(~1J~$~rFw.m'1l ~W~ -t- -arti~ !¥J:m~~Z -

~iM*iUffl *~itIJ;fiffl¥J~trmt~ ft~

if/else

<>

1.7.1

while

while

ii] 1¥J~rt.Wlr:
{

mIJf i~iU 1~!f.;Wi'; f_Al..fr_ tB

ia i:ff.i fi!¥J 11~ ~J-1 TI ~


0

._~*1¥1f2 mu 7t ~ Z.-

~ while ift

w hile( expressi on) statemenrl ;

while( expre ssi on) statement;

statements;
}

tr while 1I"lf ~ f(.JiaiU while iailj m.fff-1(.~1f" Po~ .rflJf" ft.ff-*t!-iiJ.


0 -~

~ ;fi~ l¥11t\lf j£ A

while

Wllt!¥J eg JJi $fJ1.


ffi)C

1¥1 .~ fmlf" ~~~.

X1 iii A* {J[. t.l~

f' while iitiUJ6 rID :tt:m~( (}) ~ f(~

*~*iis it:fJ

;f{( ~F~).

.ooIJ fA

!l!1j::;r;:1ifi~m~. ~ uffflf"

tnt~lHl~i~

while

~l¥J~**iUo~~ff~.~~_M,~~~~~~~mI~OOm.,N~X1*~~illff*m .. Pu*{J[::hJ •• !l!V~~mI~, 1:1JmlJjE~~rt~fmff. itrim.~~tr1t\1TiliE&t£ mllfzJl§fl¥JiEi while


iD ~
~J~Q: #include <stdio.h>

void main(void)
{

char c=O;

c;

printf("Start while(c<100) {

of program

\nil);

lIif c less than


,(int)c);
IJprint

I 00 then ...

printf("c=%d\n"

c's value each time through the loop


c is finished sit here ...

c++;
} printf("End while(l) of program\n");

Ilincrement

flindicate that the program

Ifsince 1 is always TRUE,thenjust

• 18 •

tP:AA

C ~@l::j Atmel AVR

5t. c 1t?t-mtzft1t~ o. if rf1l±1¥:r-J Start of programo ~mt*t.:fJ while mI:£f, c 111»' 0 ~ 99, ~iJ;:1Jo 1, :1tiTfp c t8-fil. 3 C fR:I:t1JJn¥U 1Pf. EBr/FW'J\r too. fmIftili!fL toO H EIJ~ rq: ffi End of program ~ J1t tE while(l )iB-1:iJ _tliw X ~~ lit ~ffl'
~i§IJ
a

+.

Me I!I~ 1 J!ffii~*ffit8~J<5:~. #,fiJi::---r1ir.(l ,~,J!~~~~, ~J1t~~~.),~~~~ •. m~M~t~MIf~~~m+,m~~~~~~-~~.,m~ ---1r:ii! 1T'F 13: ~ 1=Eit1'- #11 LfJ. whi leel)jj II ~ ±t!!t*t.1TM;£F W ll:~J¥ I¥J t*t.1f. ]A it ~:II:if. r 7£ W ::t:t*t.t7 ~ f!iYf: ~~l' # tE I¥J fi: ~ ~ liillJtittiUtE while mJIfF*3 printfOffil2:~1Efrif~~. c ~~~~~~ i!-2:V~£.3li!~.
while(1)iftiOiY-Jffffl+%lJ_§
o

~~~OO#WA~C*~.~:if.~~mm~.R~~.~~~~~.~
while

tm;q:::iL l:) m -* W ffl::# ff ~ n .l; f~ IYJ 1i!:E ~ OJ

void main(void) whilc(PlNA&Ox02)

Ilhangs here waiting while the second bit of port A is high

while(1)

I/since I is al ways TRUE, then just sit here ...

tEJS:1'-iJlJ rep,
~~"{j.J.),

while

fm:£flBiffl;f(Wffl=lIffij n A 1¥1-ffrr:~o x-t~~.:;r-t*{g~

PINA & Ox02, ;(-E

~Ra~]A_DA~ili~.m~~*~.~~,~~~mm~~To~~-~~IIJt.~M.
&: IHt,ijO ~

m UfiJfr-1[t*t.trWi~,

HifJ~-~.tL~-fil~J.I O~ rrf§OO, ~f(]¥.fin1t±t!!9ium, it~;ttJ ~ H::;r:"* ~ Er'..r, fA it~ while I.RiJJ i¥J j£ ~ fiihi ~

1.7.2

do/while f/Nq\

mPF!:§ while 11SIf~~1it~1gJ.~IRt.iUfE-:r do/while ~t:ij~5ttt.trml;q::Fn*~ltfTo HPmltf: P>J (f.Ji~HD3§:1>ttl1T-ZX, ~)§ ;S}~JtElTJ!~~fH~m;f1~ ffiJtE while ~~Jq:., Ji::tE~i1\1tJ.1TmlfI¥J fEj~l1U5t*{f*UWiQ do/while I¥J:1BARor:
do/while
0

do statement I ; statement2; }while( express ion);

do statement; while( expression)

~th.fffU
l!=Illl1do/while

fl;J3£Hf9.*~I1t. M~i5j_\3j<{Jl. 1.1p~*m~.:t.JJJ;(~F~), liJ¥Vft:li~ 1iPf t8ImmL 4ij:iJ;:ttl.1Ti~~~rtJ~I¥..r*~I1t. ~~X1~~~j:tmtr~j'.IJWT S1.~~


do/while
0

itA}.J 1(, ~~~ffm1iIf; fli'!£F ~rtJ at, WI F Ji)f~:


#include <stdio.h>

E!1W m~fI:lff~mlIf-!BM!zJ§

fr,rm4t- M'~~OO rffffl I¥J{1fJ


0

do/while

• 19·

void main(void) {

char
c=O;

c;

printfl:"Start do

of program \n");

printf("c=o/od\n" ,(int)c);

Ilprint c's value each time through the loop


//increment c lIif c is less than 100 repeat the operation Ilindicate that the program is finished IIsince 1 is always TRUE,then just sit here ...

c++;
}while(c<IOO); printft'End while(1) } ofprogram\n");

t£ is:~J tp, ~:I: c lBtm ~-ft ~

o.

:Jr EfJ t!P¥ Ff


Q

'* Start of program ,

~ € ttl. i do/while f

1m if,

cMO~~ •• ~~I.*n$~*o~m~l00~,~~~~l00.~~.Jf~.~n~¥~ $ End of program. 1<S€;mJ¥5t.Nl~#i

1.7.3

for

8$

AmmreT.~.~~~m~~~~.~~~~ff~~~~.~FmR~~~.~~~~ -ft~ !tIJ~~~it~4k{tt~,@B9Z9Jtf ~ for 1i~~1:i]~fa.itmIT;


for(expr 1;expr2;expr3) { statementl statement2; ; for( expr I;expr2 ;expr3 ) statement.

exprl RtEHtA for

.~B1tt1.tr~iX; Amfl(£ T.

exprl

~.-m:~1:i]. nrtlfflT~~1t*

1* expr2

..

expr2

~Jt, ~tr f'1~~@]flJ ~M:m.;q:,

m *-tkJEJ!:a~~trilt" expr3 -tlt~.-m: iit~, m *nJj,@ *ft exprz , ~;m~I¥J$b.tri!A for fI~~J]I!i$~, tb.ff exprl , t'!.lm:f>t4kf4 expr2 :i!rr3jt-m:, tmJ!t6* J!4k1tf:rlifi~~~.
for.~~

~ttl.ifJtl1ii~t6f4J~~$I1t. ~tr expr3, for qm;q:I¥JJ]I!$,pj:-iX*,J~ exprZ. Yu*~ expr2 ~1i11l. Jj!~~fffllt. :amo~~ : ttl.fflt€~iB-~. For 1iJfa<Jtaf4Jffl hile fI~~7J"mIr: w
~ift~-·- ;mJ¥~mtE.lf~
(I

expr l; while( expr2) {

*A~ C iAiiWl:j Atmel AVR


statement 1 ; statement2;

expr3; }

rrm~-~f~ff:
#lnclllde <stdio.h> void main(void) char c=O; printf{"Start of program m"); llif c less than I 00 then ... for( c=O;c< I OO;c++) c;

printf("c=%d\n" printf{"End while(l)

,(int)c);

Ilprint c's value each time through the loop Ilc++ is executed before the loop returns to the top sit here ... lIindicate that the program is finished

of program\n");

Iisince 1 is always TRUE.thenjust

tt1t;tr£P~:N* Start of program. ~r*5t:l;: c wMiitjg o. 1Atf for 1i.f1-. c b\ 0 mlX;t!Dr/l ~:tIflJ 99. #fE~!¥Jffl1T6f1 tIl7tL ~ c .mbllflJ 100. ::f'fI}/J'\f 100 at, ~:ii for im.f1-o :JT6f1*,M:fi End of program. :m.J¥l!.:ii while(l}iH1:i]XItll~~o

:tE*{fIl~.

1.7.4

if/else ~1JJ

if/else

*~~m~ftfmtv

EfJ*m~ W:*~~:!'J.\1j~1X

1. if lifO
ifiH~%j\;~Or:
if(expression) { statement I; statement2; } if{ expression) statement;

1mW:if j§'Ef.J=«it~;:Jg.(~F.),

!M1Atr~J§ Ef.JitttvgX;ifiij~; ~1l:~~*;I;}~.

_ma,m~~Fo

• 21 •

(t(JiTt1ij~iS1=U ~
2. if/else

l!-1:il

if(expression) statement I ; statement2;

if( expression) statement 1; else s1atement2;

else { statement3 ; statement4; }

else -itiiJ riiJ~J¥mtm1J1l7*f~B9~m:, tlPft ~~~fI{:G:~I'tI{Jl1;1NB1;t4~tr

'==i else

miU;ffi

~B9~iUd.~*a~*fI{~~~M1;.~~.~~~~~o -~cl~miU~~~-~ff*iU~Z~~o

._-#?t ~ l¥Jf;;tijfj!tiPjWt~fJ1I&(
if(exprl) statement I; else if ( expr2) statement2; else if (exprJ) statement3 ; else if(expr4) statement4;

if/else iS~,

~J&~:f¥~:

i3:~ if/else miUJ¥:Y~7!.~:f.J~tkfJ-*m~ :tm*~ 1 1--~:i;5:rt exprl 7'-3., !i!IJf.b.1TffliU statementl , ;1t-~mHtAg.o :tm* expr2 jJ.{Jl • .!I!tl~rrmiU statement2, 1t;}t$7t,m~. 1&:Jlt ~ =HE, .un expr 1. exprz, expr3 ttl 1; 1N 9!IJ statemente , ~lf
0

riiiJ;l!-~ if/else ~f'FEr''1f1rJ-T:


#include <stdio.h> void main(void) { char c; prinlf("Start of program \n");

.Ai\; c ~~!:j
foqc=O;c<lOO;c++)
{ if(c<:33) printt{"O<c<3 3 PO); else if((c>32)&&(c<66)) printf("33<c<66"); else printf("66<c<

Atmel AVR

Ilwhile c less than 100 then __ .

Iluse iflelse

to show the range of

Ilnumbers that c is in.

100");
I/print c's value each time Iithrough the loop

printf{"c=%d\n",(int)c);

printf("End while(l)

of programxn");

/Iindicate that the program

is finished

Iisince 1 is always TRUE, then just sit here ...

tt~o

:t£*lJIJ ~. 1ti5t1J fIJ l±lx**r.1 $: Start of program , 11€w c tE for tfj;Ef~it RWlfflJ"trtiit'j;] o, ?&€1'h.1Tfor ~;Ef. 1t for -Mltgafl;Jt*Jc JA 0 -#(ti::if1Jn I EIf~ 99. #1«l.x1TflJlt;{l. ~ c Ul'J'-J- 33!J1, 1TfiJX*¥r.f$ O<c<33; ~ c 1R:tE 34 ~ 65 z.rBJ!f1. 1T~;t*~~:tl 33<c<66; :PJl;!;f! c {i~:tE.l:~ffi:mJ ~ 1T~X*~~$: 66<C<100~ ~ c ij:f;!Jj]jUIOO Irt, for mllftlt51L 1J£pj[**r.f$ End of program, ~1¥;i1tAX~~M'
I

~ m,m*~~ i$dfLB''1~~
#include <90s8535.h> #define testport void main(void) {

~M§~M~~fl;J~~tt*,

m~~.-~~1¥. m~~.~.~*A.D~.-Q*1T
0

llregister

definition

file for an Atmel AT90s8S35

PORTA

unsigned char cnt,bit_ mask;

bit mask= 1·

Ilvariables

Iistart with lowest bit


/Ifor loop to test 8 bits below test port bits and print result

for( cnt=O; cnt<8 ;cnt+-l-) { lithe instructions if(test_prot else printf("Bit %d low .\n".(int)cnt);

&

bit_ mask) %d is high_\n".(int)cnt);

printf("Bit

• 23 •

bitjnask <<. =1;

II shift bit to be tested

while(l )

II since 1 is always TRUE, then just sit here ....

*19IJitffl7 8 ~#iPF!¥J for 1t§PF. lij;-{rr~l&ti):~ W:W bit_mask !¥J«I.M. fftra. ,Eij 1 ~m~.7~~~~m~*oox~mo,Eij~m~~. ~~m.~Mm~.-m~~~~-m~ {ffij;-~mlPFM Ia]. if/else MJ:ii~,Eij ~-m I¥J if iffI~~ {£ if ~:ii* aq *{4~1t] J!-1'*n~
0 ~. 0

~~~~~~.

~~~~~ffl~w~g.m~~~~!¥J~o

3. ~#~;l.t:it

f4 £ it: J!:13 - # %.it ~ if/else it ~ fHufF 7]\ :

itU i!1lJ,1lT W. fiij it~Wi.:#l ~ ~ li~ EJ<J B>.t rEi] 93;

if/else

if (expression_A)
statement 1;

else
statemenrZ;

expression_A

?statementl

:statement2;

tE 1»._tp;§#%~9=J
~ !J!1j~tT
statement2
0

filHtX>tj£.~~:a:;

expression_A

*m.

Prl*«I~ ••

fJtfl"

steternent l ,

-~~~~.
(x<5)?y=J

*#~it:~~~~~~m~:
: y=0: II if x is less than 5,then y=l,else
y=D

1.7.5 switch/case ~1:ij


switch/case

fff1lJ,Eij T;f:fl!@i£~J:\';~«I*~;jf~tr-*m1t]BJ<~mitt1t]

m~~~:

switch( expression) case const I: statement I; statement2; case const2; statement3; statement4; case constx; statementfi;

• 24 •
statemente; default: statement7; statement8;

*AA. c !IJ~~

Atmel AVR

~itA

expression

ff.J{i~m-.(constl

~ const.Z , ., .constx)jft:fftt~. break

1b.:fT~itAEt9m.lll[!;; ~
*iU$:t£~-t-~iU*Etq* #f:MJJ8!ftt!'
0

E~~.~EOOm~o*.~~~m.~~nmQ~oo~~~~~~.~~~m~*~$~~ fT. lifU switch ~1%Jit*~ im1ltM:#::f~£mfFui¥J1ifF. m{:),.


~, PJI:2l.~Jt:tt1.1TEj$.ll1reEnm~m-t=I].
default

mil] ~

i:ij" ilt ~,

mR:: £~~-1£&1f

#fti/Hl1.~mtt£

switch

~ Ic ~.tJ1~~1b.tr~J*~

#include <stdio.h> #include <90s8535.h> Ilregister definition header file for an Atmel AT90s8535

void main(void)
{ unsigned char c; while(l) { c=PINA & Oxf; switch(c) case '0'; Ilread the lower nibble of port A

case 't ':


case '2': case '3':

Ilyou can have mutiple casess I/for a set of statements.

printf("c is a number less than 4 \0");


break; case '5': break; default: printf("c is 4 or 5 is >5 \n"); } } Ilbreak to skip out of the loop II of just one is ok,

printfi" c is a 5 m");

M:~~")tm;~ilifittl
2 pJ; 3. }j~ ~

A .l; (j{J1H, miili~~{fL

X 4=:=r 1'1 ~
c is a 5;

c is a number less than

:tul:!f!:~~J! 4 #.f ~H±i frJ ~ llUifrrtf:l15! tr _t; WI *- 1<f jg


.#~f(E lrn{fL~'m-. tt~,
5).

tI:1X:;$:?lff*
*:::[:11*
7f~Mj[~

jzp*f:~.f'~~iti3:JI:I:;(£ QX;jcT 4.
.§.:tt1.tTT:@:¥3B'.; case
iB-t=I].

JJ!~1Jt:ff default

*"

0.. 1~ 5
I

!ftW

it}il].

fiNt±:Ix
~€

c is 4 or is >50 -

~J¥~:i5@}fU

while ~:tt:tf'JJJi!tfII,

·25·

1.7.6

break .. continue ~ goto


continue fll goto iitiUm

i~1:iJ
while, do/while 5fJt switch

break,

*~t& for,

m1i]t¥JtJ:t.1J'

1. break break

i~1iJ
e

ittiUJ::fBI~:JA for, while .. do/while ~ switch ii-t]q:rilltl:l tIlJ5J!j.3:Jl:hi~iU.~tEJt:ftE.* iD ~ W , Jj~~ break ijfiU ~ 7tJA E ~ lWEB*iO:I}( q:r~ ill ~ ~W~ft~#c~mtr~~~m.ili&4,cm~~~JAO.m~IOO.~~.m~~~~ OQ t'£~ while :f}iIT-~, c ~mJ~ 101, r&RitA1T breakiil:V. :iHlli~m while:f}ilf, ~~tA1T9r Ji!: while fJelf tE7H~ while :flIT- I.{-I, c?Bi:19:j;J 0, X:~ §J ¥~rJ-J fJilf ~~#Jj(;@& JHMIf ~
0 0

*"

#include

<stdio.h>

void maintvoid) { int c; while(l) { while(l) { if(c> 100)

break;
++c; printf("c=o/od\n" ,e); } c=O; } }

Ilthis will take us out of this while


Ilbolck ,clearing c ...

Jlclear c and then things will begin again. Ilprinting the values O~ lOO,O~ 100,etc ..

2. continue i8-i:3J

continue it ilf ~F¥.!IT tf!lit:i1t 11" -".. :(.x while, do/while !!X; for *iiJ T fJe;Ef. continue {E ~ iftiU .1l::fIlfijf1U ~~tT1JW ~ break If 8.~M ~l~jjlJtE ~ continue i~i[JIm MJ].!$7fMifJe ~, ji'jj break ~iV1G~:iHllittIf<>
#include <stdio.h>

void main(void)
{ int c; while( I) {

·26·
c=O;

*A7t c f1i1i!§

Atmel AVR

while(l)
{ if(c> 100)

continue; ++c; printft"c=%d\n" .c);


} }

Ilthis will cause the printing to stop when c>lOO,because Ilcontinue

the

will cause the rest of this loop to be skipped!!

Ilno code after the continue will be executed

~U17 i!tt~*iU.fll
3. goto ~1:i]

~*~~,cm~B~W*,
printf()it:t1iJ
0

.~100o~~,fi~~.~~,~~~~m~~8~R~~

.f~ ~ ftJ 1-t~fi fatE T 00.fll)§ 00. mit ~ijrr ~


I

~o*~m~*.~~~~~~ffI.~~*«~~~~~, #~ff~*~.~~~~~

A~

*- f;Jt Lj=IIlT iJ~ f-t!W.fll1¥fii:U" JJ, t~:tff~


identifier: statement:

~~

m Ij,~

goto

iB~!¥-J lW

goto identifier; identifier: statement

~1!f

goto identifier;

#include <stdio.h>
void main(void) {

int c,d; while(1)


{

start again:
c=O;

d= - I; while( I)
(

if(d=~)
goto start_ again; II(stuck? bail out !)

d=c; if(c> I00)


continue;

Iithis wi\! reinitiate this while loop

++c; printf'('c:%d\n",c); } }

lid will be checked to see if c is stuck


/!because c won't change(>IOO!!)

tE WrJ ~. c lDd 1JJ~ {t 1.:1 ::f ~ I¥J Ui if (d==c )itH=i] ID!~~ 3l::2: i¥.J iR • ,R ~ ~ ttl~ • ~ .I; t@: ttl.fi-itiiiJ d=c, tmJl c +T~T 100. fJ;r.,-fr c j,i:Lj 1 l¥Jit!fF. WtrtB c 'IA. 1!fn3£1t while 1fj~Q c lDd egiR~][ffi& I llflJ C 'lAAT IOOe § C ~T 101 aj. continue i~HtI~;rE"I~++c lDprint£() i~ii]. :tm:!il! if(d:;;;;=c)~., pij'IAttl~~1f goto i~1ij. ffF¥WttW¥~ start_again t;F~., !a:!il!1.:I-j§
0

R-j§~t.H~ilic~.,

MO~l00~

M1:ti~1JIJIji~:AM:St
*W*~~~~§l¥Jm-M~.

.~~~~~~*~.~-~~.~*[email protected].

*1.:IM*~~~§.,A~~~~~: • ttim~~~*ot~JfjijT"VL~jHr~demI¥Hf" (w:lttt: ~OZ1BT:?tJJemfi-ff*fF, :ltW- itffiiiJ\.tI D) • 1!irtP f0 Jf Jt§ I'm jij f'Z 1::~1'-~ ~ T ~:lz: ::::ytl % 00 • p;J :Yt 1T jij t~lJ'm~:tE "l$ #J "

m*~~ml.m~~~~o~~~T-~~.i¥.J~~:

#00%: fj!!jUI!l.(Bar),~F(Bell), • m~~{1tEt9*M1J:t\fltm~*Jl!!i!~: • 3 i'-[Pj-;pp~Et9it-T 5


• -:V~~tiJlj.~7J"
4

mf.l:*

rr.(Lemon)~"f.1t(Cherry)

• -1'-":M~~~ 10 • 3 ~";tj~1ft~!k~ • ;tt 1t!t* iffi !JlU ~ 1ft !J tR!ftI ~ q

*~ ~*~ ifi

fi**~-~~~fi~. ~A~-~~tt~M~QX~.m~~illf.l:~*~#~m~~~ m~*~mfflMI¥J.*~fi~~tti~lD~*~lli-~~~mf.l:. itjij~m~A~ili~f.l:~~f


1f*rt.~JI"tB~~
II II"Slot Machine" II _.- The Mini-Exercise
0

JJ'{§IJft~t:p7J"1[7

while,

do/while , for q,alflj._& if/else

£i!$Jifr~a<J1tm.

IlPhase 1....
#include <9058515 .h> #define xtal 4000000L #detine baud 9600 #include <stdio.h> I· processor specific infonnation*1

I*quartz crystal frequency


/"'Baud rate"'/

[Hz]./

,. this gets the printf'() definition 1/ Columns in the slot machine.

*1

II Global Variable Declarations .. char first.second.third;

iitA~ c ~f]1~
char seed; count; delay;

Atmel AVR

II Number to form a seed for random #s. II General purpose counter.


/I A variable

char
int

for delay time.

void main(void)
{ PORTA DORA DORB
= = =

OxFF;

// Initialize the 1/0 ports // port A all inputs II port B.I is an output (light)

OxOO:

OxOO;

DDRB =Ox02;

/1
11 II

Initialize the UART control register

R.X & TX enabled, 8 data bits UCR=OxI8;


initialize the UART's baud rate

UBRR""xtallI 6lbaud-l ;
while(l) { II do forever.. II Wait for a button and II let the counter roll over and

while(PINA.O)
seed++;

/1 over to form a seed.


first
= second

= third

seed; II preload the columns II mix up the numbers II while waiting for button release.

do
{ first third
1\""

seed» seed»3;

I;

II Exclusive

DRing in the moving seed

second== seed»2;
A=

II can really stir up the numbers. II keep rolling over the seed pattern

seed++; } while(PINA.O

= 0); II while the button

is pressed

for( count=Ojcount-cf

;count++)

Ilflash light when done ..

for( delay=O;deiay< IOOOO;delay++) Iljust count up and wait


PORTB.l =0; Iitum the LED on. fOr( delay==O;delay< 1OOOO;delay++) PDRTB.I=l; }

Iltum the LED

off..

first &=3; second &=3~ third &=3.

!/limit number to values from 0 to 3

• 29·
Ilshow values ..

printfl" ->%d. %d,%d, <-\n" .first ,second,third);


Iidetennine if«(first=3) printf{"Paid e1seif«(first=3) printf("Paid elseiflffirst=ssecond) printf("Paid else printf{"Paid } out:ZERO\n"); //Loser ... out:JACKPOT! 1\n"); a payout ..

&& (second=3) && (third=3»

IIThree "Cherries" 3»
IIOne "Cherry"

II (second=vS) JI (third
out:One Dime\n");

&& (second =s=thirdj) out.One Nicklexn");

IlThree ofa kind

1.8

~4t.
mB~m~*OO-#M.,~~~~~~$*~fflo~~m~~~.~X~~~~dMfi~
~$

~x~~~#. ~~~~mX~@fflfi~dX~~~~~fflo~#~~~~~ ~ ALIlJT 7f ~ (f] ffi B-ar IJ. 1!:ffl ~ m~ u:


0

&~{tffl .. ~.OO-~~~~~.£~~~~~OO~.,

ffi~ffl**-~Ww~~~~~*~~~~*~$*o~#~~~~~~~~~~~~.

~~~.~§OO&~7.fiM2~ff*.~
.• M~~tt,

.~~*~~~*~.~m~.~.~&~~.~ •• ,~~~~§m;g~ft.~~
{13~!§{JlRff~~o

M.~~~&~ffW.B~.~~~ff-~~~~ff§; &~~.~.B. ~~~~~X


-JJ±,lJIJ~ilDr
=

sleept) ;

Iithis function perform a I second delay Ilwith no parameters, in


Of

out

printf{"this

is a parameter

%d ",x); IlprintfwiH print its parameters

c=getchan);
Ilgetehar will return a value from the standard input

type function _ name(type ( statementl statement2; ;

param 1,type param2, ... )

lI*Ait
staternentx; }

tii~~

Atmel AVR

-1" -t f

PEl ¥j m % rtJ~:~rub[] fEr -t, f(IYJ ~


M; fF :tJ

fI}

- /hfEI ~ fA.tf I't:J

11p ¥*iE. f.i 00 ffi -5 (tt~ ~ ~:iE.~)[tij UiO if ~t~flA:ib

fEr.oo~m~.tim~~.oo~~~~,~~m,*Mmdn~~,&~:tJ~.whl~mo void ~~£-#fi~l¥J~m, Jij·f-maA~~9JG.~@Jtil:tJ~::k+(zero size) flJ~l¥Jlt1A~~A1~ Jiff t:), ~-1'"A :~Hr.J I! f( Il)HJfF : PF
0

unsigned char getchar(void) { while(UCSRA return UDR;

& Ox80)""""O) Ilwait for a character to arrive


Ilretum its unsigned char value to the caller

X f.i -5 f 111H ~.~c.~ ••m~~PEI~a~~~.M~~~m~m, ~~~W~.-~W~~~~f(Q


B:
f§1J 4=t,
getchart)
I

PEl l' ffli ~ ff fOJ ~ ti tr fA.17 ~ ~

*Ii1~

@J -

getchar()

1.8.1

Mi~~Jl i?i§~Hltq

~w~ffl~.~~.-ff,~~~~~f(~~~~~~~~~m~m~~~~M~~~ ~ ~~~: -~.M~~~~~~. ~-~~~ti~ml¥J~Jij~


~w.r¥-fH1Dr I¥Jm ~:
Iideclamtion

*~~f(~.~.-~*M~~~o~~~~~~~ffl&~.~~~m~m
of global variable would be first

•.

&~._

int varl,var2,var3;

Iideclaration

of functions would come next x.int y)

int functionl(jnt }

void function2(void) {

Ilmain would be built last


void main(void) {

var3=functionl (varl,var2); functionzt);


}

w~~w~~~~m~~oom~*~oo~~~.a
~JM~D

M.~rum~~~~.~M.~~~~~~~ili~BOO~~~M*.
,-

~&T~m~T.~

l:.iJlJ PI~ tm Jt:i£m~ : r"jj IT


int var I, varz, var3; Ildeclarat.ion of global variable

void main(void)
{

IImain

Ilthese functions are not yet known to the compiler

var3=function 1(var 1.var2);


functionzt); }

Ildeclaration offunctions here now generate a II"Function Redefined Error" int function 1(int x,int y)
{

void function2(void)
{ }

A~~m. T. ~l'Pmm/jtC~~~~lf:~£ttlflh~m ~ •• #~.Jt~~.ma.Mw~~~~~~~ft~D


J]Uffl:
lithe prototype of a function tells the compiler what to expect int functionl (int.int); void function2(void)j int vacl,var2.var3; void main(void)
{

,@_o

~.~~~5tmT~~~~~~~

tAiliW~iitfl mainOmB

'tJiJijffl i¥J

Ildeclaration of global variable


llmain

var3=functlon 1(Vat I.var2); functionzr);


} lithe declaration of functions here is perfectly OK,since lithe format of the function are presented in the prototypes int functionl(int x.int y) { }

• 32·
void function2(void) {

{b;Aj\

c iillll=;i

Atmel AVR

W!2J. R~fF Lj-r ftffl:iZ Jlli &: ;tt 9:1filename.h ~ i:'!--;g- 7 [:$ ~ ~ ()<J ~ 19. ~ ~J X 1tt- Htr jjf 00 -T!--f it fflT printf() 8EJ ~, ~ 1JIJ 1*~ • -*:!Jr.tf jJJJ!l1:: F IW, {S B 001*~ a fk x 14 stdio.h t:p :is: 1$ PI1~~~ffft! PI~ a ~ff!¥J Jf !J..1:!1 115 ~
include<fitename.ht=ijl-til.
0 j 0

~C , ~ ff mt:¥J. tlH!f:LiE.~ ff c .tlfU ifft:N!{1t T if $ $~~.J1tli


0

~~, ~~=a*.~.~~fi~~~.m~~.OOm4.n~~~~~00~.~.~~

~I.
0

X#.~~HM.~~~~~Q.~$~~~m~At:p~fiT~~a
1.8.2
fi§~~

@Jill

~*~.£~T~~~~~~**Me*~~@m.~~o~~¥re~rn ffl~~~~@m*m~~.~ili~~o ~*~4~ww~ru~~, wm~~~.~~~m. a~@]m~]~ru tr:Jm~ void


~*$M~T,
int z; l/globaJ variable

void sun( int x.int y) z=x+y; }

lIz is modified

by the function .sumt)

~.

return

~$t£~.IflEr'~*~o

:tmJJ!~ return

$::fr void i'8~ IF

f7~!m:

void sum(int x, int y)


return ;

Iithis would return nothing ..


Iland this would be skipped

z=x+y;

~ ~UE:r-t EJI.J mWT ~~ float. 3f Jj"


float ( cube

@iiJ#J1t~fFtr:J~tr*tl:H~~~ffl~ff, return ffliiJFo~$*~4l;tA.rr 1l11:~ffi.I¥J~~~~ void. return *frJtE.~f_~~fFilBl±l*PJI~i3.@]f~iMIm~ ~9~


return
Q

return Et<J:;fi ~ return


(float v )

i!1, !j a PEl. pH1J3 y::r m~ ~ thJiB1. float $:~~7 :It~__t ~

~ ~ mA:&XIT:It ~ .L ~ -F Hff IY~ It~ ~

return

return (v*v*v);

Iithis return a type float value

ffi~~~~m~~~~~*~~~-.*,
void main(void) {

~:tmft~W()<J.m~ff~o

float a; b=3.14159; a=cube(b); printf("a=%f,b=%f\n", while(J) } a, b); Ilput PI into b

Ilpass b to the cubed function ,and


Ilassigrt its return value to a Ilprint the result Iidone

~t

a ~M{j[j;J fBI!!: cube(b)l¥Jjg@lie:~


a=31.006198,b=3.14159

#:i ~PPr:

1.8.3 JiV=l~f&

m~~.~.m~~.*M~~~o~d.~~~~~h~C~~~*~-OOD§~m~~.~ .• Wm~B~~.~.~~n~~n2~$, ~~m.~~hl~-~.m~~~m ~~~@o.*~~.~ffl .•~.milifl~~*reo~~~~.mm~mA. ~~~noow fflmmff~~~*re~hl, OO~~~~~~o

-~.~~~~~~~~*~*o~~:
:

ff ~ ..t ~ 3R ~ 1 li~ 7F
#include <stdio.h> int fact(int n) { jf{n=O) return 1; else

n!=n*(n-l)*(n-2) ..*2*1

n!=n*(n-l)!'

J3fT12J,Ui{7J\~!I~(t:rfifftmr:

Ilifn is zero .return a one, Ildefmition of factorial

by

return(n *fact(n - I»; } void main(void) int n; n=fact(5)~

Ilotherwise

,call myself with n - 1 until n=O, then

I/return n*tbe result to the call to myself

Ilcompute

5! .recursively

WAA

C tlij;f!i:loj Atmel AVR

primf("5 !=o/..d\n" .n):

Ilprint the result Iidone.

while(J)

ttiJ.IJfDt*~ -tx, n I¥-J-mmt~ 1 n ~T 0 at. PR~l':P}iffijffl*~. rm J!~fjj]ffi 1 ~ff!J1f51 ~~1'-~@}Eru ~**~m~t.!tJti.Ei@1, JA ffij ~ fJ:~:i!& @] t~maim) tb. rr I¥J ffl. tE maim) fll ~ ~p En j&. 1m~ ~ iJliJ rr
~ ~!~

fact01Bt~~

n if.!Iffl Bj.

'Bi,ij)ffl{j;: *!it n - 1
a

t=p ~~a~~g.~~.n~~ •• ~.t=pw~~*~sma~~~~~-~+*mft~M M~.~Q.tx~.~ffl.*~, .3••~%re~~I¥J~$~.~~@ffi~~@~hl®~ffl


a

Mfi4='

ffl

JljEru~. ~ a-wrwtl4='1115:
(passed parameter) address
Total

int n return

2 bytes
2 bytes

return value

2 bytes
6 bytes, per recursion .minimum

~*~*5!, ~~~~.30*~M~.M~OO*~*m~.~~.ff~E~.m~~~ mff%Ea~ff~~.~~M+%~M. ~~Eru.~~.~~~Wa~.Erua~.~~a~~ mfi~~~.~** .• ft~~.~~~N.~B~o


J! 1 ji 1J'9~IjllI3:
B fl%

~~._~.m.
0

X>jTi'~Jt § i9)~ :&l:m.Er~Jf1t. ¥fiti·rtf'ij1'-~I~, ~ .±mrlf7tfM:~ IJ\BtIf"\W#C*{!~~£ ~ -ar~tEo ~7C$*~it.*ftIII1iA$%~~AiIJfElflq:.~!)!, ~m. T mainOffi~*{t;tt~Eb 'ri switch/case i~1:U~ffl-f+t$.1m7F
0

II
II

"Slot Machine"-The Mini-Exercise

1/
1/ Phase 2..

#include <90s8515.h> #define xta14000000L #define baud 9600 #joclude <stdio.h>

/* processor

specific information *1

/* quartz crystal frequency [Hz} +/

If< Baud rate • /


1+ this gets the printf() definition "';

/1 Global Variable Declarations .. char char first.second.third; seed; count;

1/ Columns in the slot machine.

tt Number

to

form a seed for random #s.

char
int

1/ General purpose counter.


1/ A variable for delay time.

delay;

#define JACKPOT #define DIME #define NICKEL #define ZERO void flash { for(count { for(delay PORTB.l
=

I#. this defines give different

payouts */

2
I

/'" names in order to make the code more

*1

1* readable to humans ·1

o
n)
/I flash the lights a number

Iightsfchar

of times

0; count «: n; count++)

/I flash light when done ..

0; delay < 100000; delay++)


1/ just count up and wait II turn the LED on ..

= 0;

for( delay= PORTB.l } } int get_payout(void) {

0; delay < 10000; delay++)


=

1;

II tum the LED off..

if((first = 3) && (second = 3) && (third = 3» return JACKPOT; else if(first else if(first else return ZERO; 1/ otherwise - you lose .. return DIME;
=

II if all "cherries" ..
= 3»)

= 3) U (second = 3) II (third

II if any are "cherries" .. /I if three are alike. of any kind ..

second) && (second = third»

return NICKEL;

void main(void) { int i; PORTA = OxFF; II declare a local variable for temporary II Initialize the 1/0 ports II port A all inputs use ..

DDRA=OxOOj DDRB
=

OxOO;

II port B.l is an output (light)

DDRB =Ox02; II 1/ /I Initialize the UART control register RX & TX enabled. 8 data bits UCR~x18; initialize the UART's baud rate UBRR=xtaIl16!baud-l;

~A:tt c ~~q
whlle(1) { while(PINA.O) seed++; 1/ do forever ..

Atmel AVR

II Wait for a button and /1 let the counter roll over and II over to form a seed.

flrst = second = third do first third

seed; // preload the columns

1/ mix up the numbers


II while waiting for button release.

"= seed»
A=

I;

1/ Exclusive ORing in the moving seed


II can really stir up the numbers.

second== seed»2; seedcc-J; seed++; } while(PINA.O flash _lights(S)~ first third &= 3;

II keep rolling over the seed pattern = 0); 1/ while the button is pressed
/1 flash the lights 5 limes .. II limit number to values from 0 to 3

second &=- 3;

&>= 3; JI show the values ..

prin t f(" - > 0/1><1. o/o.d , o/od < - \0", firest, second, thi rd); II determine a payout ..

i = get_payout();
switch(i) { case ZERO: printt{"Paid break; out: ZERO\n"); II now print the payout results

case

NICKEL: printft'Pald out: One Nicklein");

break;
case DIME: printf("Paid break; JACKPOT: printf("Paid break; out: JACKPOT!!\n"); out: One Dirne\n");

Case

} }

1~tt ~ ~Jl1'£ c :fD q:.:ff tf i: rz a9 @EJH. IN 'B iiJ it.itl±.J¥:tkif J!~!fX it I¥J5I!:flf 3ti~ I¥J J.J ~~o~*~~~~~~~%~~Mm#~~m*~n~~~~~~~~~, ~~$#M#~ M*~.m~m~~~B~~ili~m~*.~.~*m,
1.9.1

**

*'

mfl'

m~.~.~g~*n*o
0

3t iff 1m: j$ f!Jj :;Yg 11r# 5t : _m


char int *p;

m#J~d~11t5t., ~:I:, ~~.:tmxtj!.I¥J~:bl:q-ff~ij!J[.a9~. IIlJ~ftr1=*, m iluf§]tJ(\Ii


1/ p is a pointer to a character II fp is a pointer to an integer

*fp;

mtt•• ~3M~.OOm.~hlo~~-~Arul¥J~~ ~Ba9#.ft*re~~.M~~16Q~ m~~.gl¥J~~~M~, m~~nOOm#&:;Yg16~ 1¥J{l, ~tf~r1=*~ Rlf 8-fiL -g~~7mtt. ~~~~mttmm~~~:I:~:bl:. ~~~~.~m*~~.$~m~:bl: ~~~~~Wo.~hlill.N~)U.~.~hl.~~:bl:~~.N#~m#.~mtt~mo~. 'J I ffl ~.r1=(*)~13m~f~ rtJ ftJ~:bl:e\J#IJ:(Zo: ~ ':f!f
0

~~~~~~*~~.~oo~~,

char .p ; char a, b; p=&a ;

IIp is a pointer to a character II a and b are characters II P is now pointing to a

~*~~,p~~~~.ae\J~hl,m~~pm~ao~T~~m~5t.pmm~I¥J~.~ ffll'aJf!ili.f.1{"')c ~:tkrrlf1, rB]tJ(]g.t.rl&~ji p a<Jm(--t-~~).ffl*Ifttff.ft!=f! e\J~-tQWom.~*~.~~~~~~~, ~~lli.~A~Q~~mo~~. ~~~a9~~~. *p


~~~'E!*T
b=.p;

p ~tr9~hl~~J~,

t?\)§)1iUl~5e:l: b,

lib equals the contents pointed to by p

lNlit. J:mfWjlJlJ l¥J1~~F~f6J~~!a~:


{§!l:t111 ;
char *p;

b=.a,

fEiJ.:lE;M':N llT~titmiE.N~:tli11, lPJfi:ili

lip is a pointer to a charater Iia and b are charaters

char a.b;
p=&aj

"'p=b;

lithe location pointed to by p .is assigned the value ofb

W*A~

c iJij;ffi!:~

Atmel AVR

ft~ff~.~ffap~~~~_M~~~.b~m.~~~~ffi~~~*a=b~~~~M i!®~f'FIM. &3t¥-i;:r1M: b ~M-mt.J p mf~~ a<J{Il(~ ~); p _MtllPJ a l¥J:l1hhl.. i!f=f1fM
Fiji!kAEjl~ £ EtJw=~:
b=p ; p=a ; /I b will be assigned a value of p, an address. not what p points to.

1/ P will assigned the value of a, not its address.

~OOEtJ~.M-m*~~~~~~~~,
~ 'J I/tQ §
faj

ffir!.

*I¥J~ .I¥J¥~ ~.. mit~ftUJ DJ**~j>~/J\JC.;i&~H~HEBf!~


~
OxlOlO.
unsigned char >l:out_port out_port=OxlOlO; ·out_port=Oxaa;

~E:~rvr~~1f E8IE 1ft I fF


e

~~£.m• • ~M*o~*~~h~
0

ili~D 'j\JI)- B*~ {frJ W,

taft:iE# ijj fr:ij ¥ ~ r.f-J EtJ r ~ 12: -i- ( bt tltl~ A~ ill ~ £-7
{frEtJ*ff~ D~ili{f[_TH~:B~
ildedare out_port Ilassign out_port as a pointer

!l!tijlJ" ~.iA:urij:ll'31 mJg_N*ViIIlJ *jjij n.

D) rmfJt

*1ft!

•• tt~.
TIM • if]1f --1'- 8

with the address value address a value

Ilnow assign out_port's

tE*Hft~~ EBout_port m rOJ ~JttB:tJl:_M-mjg !¥Jf1i -MI~~ A HfiI:H:l1hhl OxlOlO 'flo


I

OxAAo fijf¥i£

PJm~t.J:

iffiirM¥k*out_port

./F~':tIJt§m~L~p~
int "'pi; int "'*p2;

~C*~M~~.~1fm~mff~m#~~~~,MT~#~~~~
a

'WlJWl:

•• ~*~~~;R

Ilpl is a pointer to an integer IIp2 is a pointer .to a pointer to an integer IIp3 is a pointer .to a pointer, lito an integer to a pointer

int

>I:"p3;

int iJ; pl=&i; p2=&pi; p3:&p2; Ilpl is assigned that address ofi IIp2 is now pointing to the pointer to i IIp3 is pointing to the pointer that is pointingtoi Iitherefor, Ilj is assigned the value pointed to by the value Ilpointed to by the value pointed to by p3

j=i;

Ilanyquestion??

~fmft~a~~~hl1f~.
:l1h~JE.1M
I

~* Hm

~~~.tlm~l¥Jm+*.~,.~~i&~#

••

~mff

iA IE UJrJ i¥J ttkhl~~T~:S:

I¥J~~!!JG1li ~~ !YJ'Atfl3f6it1Til·:W:l¥J" 'WlHm:

int *ptr;

long *lptl";

ptr=ptr+l;

I/move the pointer to the next integer 11(2 bytes away)

lptr=lptr+I ;

IImove the pointer to the next long integer 11(4 bytes away)

10 lptr =* B<J ~:::f [riJ ~


ptr

~ § ±I-1'-{trif.
0

~~jl:: ptr $QJ 2


o

¥11.

ffij Iptr;f$i9J 4

~11. ;is:~~tJ't:;ifJ_,g

§ ~~

f'F [PJ f.T- ~m lit

ptr+ +;

Ilmove the pointer to the next integer location 11(2 bytes away)

-lptr;

Ilmove the pointer


1/(-4 bytes)

to

the preceding long integer location

~~~~~«~~~c ~*~.M~e.~.~&~-~~.~. •. A~~~~«~~*~


iflJ:Wl, "FOOBtl *.~(~:J!1W § e ±llllFc §ilii~)~m;-m1\¥! ~~-$#~.~.~.fi~OO~~. ~~~~}!~~ffi~~«~m~~~ •• ~M.~. Jfu~.ViB<J~it~JAft'¥;f5*lIl~
char c;
char *p; c="'p++;

ttl j; fa] fj€ iJ Im.@ .rf(*)l0 ltJjfu iLI:;E~ f{j( &)~}! ftiE. Vi. A fl:ll i@i tt Jt 1JLE ffltE~ ~

Ilassign c the value pointed to by Ilincrement the address p

p .and then

c=*++p;

Jlincrement the address p ,then assign c the


IlvaJue pointed to by p

II increment the value pointed to by p .then assign it to c .leaving the


II value of p untouched

c=(.p)++;

IJassign c the value pointed to by p .and then //increment the value pointed to by p leaving lIthe value ofp untouched

mtt jij" ,Eij :~jHr1E ~ ~ .:f4f~~~tJmttffl;:ial~ru~,


void swap2(int

jg @] ~

ffi .m~ § ftJtl return rl iM itH=a ~ _R W~:lS ]-1'-il. Yo @ !J!IJft.lflt!l!!~§]llftjJQEtJUlg Jy~"FOO~ swap20fli~:
0

~* ~

• a, int *b)

{
int temp;

temp=sb; *b=.a;

IJplace value pointed to by b into temp IImove the value pointed by a into location

ilXAA:
/1 pointed to by b

c f:i~_bj

Atmel AVR

ta=temp;
}

Iinow set the value of location a to the Ilvalue of temp

*~~~~.~~a~b~ili.
int v l , v2 ;
swap2(&vl, &v2) ;

~m~.~.Mm~~.~m#. ~~:
IJpass [he addresses of v l and v2

ffJ1mr:

~ swap20 PJ§ 1& 1t m J f{(ii I¥J ft!!:I1t, 1l ~ 3Z~ jtJt VI f:O V2 i¥J U1 {~;ii ft 6'1:l:1~!l 1It if m, tE *jf- ffF.~ ~ Ea It(1m scanf()) qJ it!. ~ is: ff: H: stdio. h q..l JE 5( ~ scanfl) ~ ~ it ilf JA fi'F iff. ~ AI~~ I2J.mA{tErJ1rj:titL&~ t-*~, Tf~:tETffttH.:p t~JEBtlt'lll ~ M scanfO~ ~~$l.~~
~r~
0

int x,

.z;

scanfl."%d %d %d".&x ,&y ,&z );

~~~w~km~.m.3.d~~Ao
1.9.2

;JS:- scanf()~ •

.#J JA;f*fl!ftWA'f!~!&

~+i!ffltJ~~til.

#~A~.

x , y;fP z Ip

scanfO

flm
0 ~

~~~-n, R~MffiBm~Bm~.~+.:
int digits] I0]; char str[20] ;

fi m}! J3 -- ~rtr{Rl it iJ ~flL~ tiL ~ ill ~ 12Jfi ~A ~tu(..J1f fF fi Ii~ 1; ~ ~


Iithis declares an array of 10 integers
If this declares an array of 20 characters

~1l d!lj ~ F

1ft.g£~

str[O],

str(l

J.

str[2], .. , ... str[l9J.

5E>(F' H_ij -fiT ldl'ELti51JJtmft l£~Jtltm.:p. M~1tiil~$:H:f~1UF~-ff:~:H.!K~r:p, .(E maim) fEI~~iTij'UM ruiJjU~~T-Et'.f~m t:p jft.~f!lJJ~~IDJ, JtU1iiT~ffTf~ft.r::p, l1 iH~' RAM ~ #~r8J iffl RAM Tffltj~:t£~11$tl~~~"~tt~/j,. - ---t-AtrHt~WM1f-t~m4?tm-F JiJT7i\:
0 Q

int array[Sj= {12, 15. 27, 56, 94};

lItHt

array[O]=12.

C @ -¥3-:f: ~

~~~~~~~~ffi~~~~oM~

1itJ&f£L R''.!:i11!Jf- ~ ~ , !.In *=~ 5i iliilliil T It~1l YJf j,ft


Ilan array of characters

arrayj l j=Io , array[2]=27.

array(3]=56 .. array[4];;;;94.,
I

#1it1:H.#.f~ 1;1. /f~IJJffi iWfl Btl1i

char digits] l O]={O.I.2,3,4,S,6,7.8,9); numb=digits[12];

IJthis read outside the array

·41 •

?J&!1lff.1m?tff-1m~"P i¥Ji!:ttff.fitl.!¥Jl; ~
'1f ~:fff~i(!j[;, j'8.J§it1{'{ft i:J INil 6 _t ~}1(~ 13 _t1¥J1t~~ tE~-t-ff.11tif!7CI¥J~1@~Jtre1tI~
~~~~-f$. &:,~j!IJ'Jt.'~

digits[5]ti*~1£9!l_I~tfiltA~~llm

Itllltm 2 iT ~ t\,~~~itjl:Jm~it;1{'{,*
numb.

.1t~~

{13~lli:tJt~a:eg~*~

'31 3ilj. IE:!zUJtftB

nz:.

-t-~

T ±mhl~'1I(!!giLI:'1l fflff-1mi(!ft).

~m.m~m~M*~~m~~:

~m~ff-

••

~*~7~~~~l¥Jg.,
:(£~.ff,j* ~,
C

WmffR*~ W~
~W~ §i9Jftffl~!¥J

~Mn. d~$.~~~~f,j$M~o
~~~WOO*~~I.
char str[ 12];

A¥f.1 *I::Jm.l1:J1j1~1~-§m-fJJt**ill:HJEB)Ht..r ~~ft~~ffl~.~n.


l/variab1e string

.~m~~m~~~f~~m. X$~~~~~~o*%$~.~~X~f~~m,
e

Eh~ff!nF" aA~JE3!.~~T-f('1ff-fii~ijj I'D] IR~C:!t':.)o

~e~~~~$~~.~~.~,

p~~~m*~~~

printf("Hello World!"): const cstr[16J ="Constant String";

Ilcons1ant string in program memory Ilconstan1 string in program memory

cstrt9:Jt~~ 16-i'-ui, 1;tiI~¥M**~lf 15-t-f~' ~-1'-1i1~~rq:<> flm;gj§B\I~ i1lnJijlmff{iiJ~~~J.m.jC~ ~~f=f.1lJ Rffl - ---t-;g ~*'1~m -1'-flffil¥J:m 1 -1" ~. &~ ~i3 rat. Itgililit ~f(m I¥Jm -1' 7G~ f(~±m:l1L ~rTIff~ ~ ~: ~ *i91J~.
<>
0

char stog[20];
char *p;

p=stng;

II P is pointing to stng[O]

p=&stng[O]; lip is pointing to stng[O]

~~$m*.¥~M.~fn~~~*fi~Jm, Mm~~~~$fi&6 •• A~~~ (Liquid crystal display. LCD) IP.t mt fm ~;iS: ¥F ~ rOO t8 i91J ~ i1t~ ~ 1:[ ft 'j IflHi ~ riJJ ~ ~ Iffl i¥J PI 1i T ~ tt.. ~ j! 1'-f7~T q. . itffl $~It putcharO*~ -1'-* tI Ii. ~ ~tlf~jjJ tm ~ - 1'- qf tr tffij tll¥J fiF ~tHIDJtH 19:1i-:

#include <stdio.h> const char s [15]"'" {" This is a test"} :


char i; char *p;

WAA c ~fiq
void main(void) { for(i=O;i<15ji++) putchar(s[i]); p=s; for(i=O;i<15;i++) putchart "p++ ) while(1) }

Atmel AVR

Ilprint each character of the array Ilby using i as an index Ilpoint to string as a whole Ilpoint each character of the array Iland move to the next element Ilby incrementing the pointer p

Wffl¥J~-iflI5t1tffl for milf*.-t-tlE:fDtrtB~ml¥J_1-*:f{f ..ttm for 1mlfit~fttFj;J~51 *f&!~ ~ ra!l EfJ 4ij --1'* 1'f , lJ -if: 'Bur b'J, lit~ ~ 3ltl putcharO PEl~ till:5t1t ffl mtt *i)] Iii] ~ ~gj[.~. P=S: (OJ *~m~-1-5C~I¥JHfi.7G" ~f3 for milf1tmmtt(_-CX!StJll)

*~fjU~ttmit., !J4jtf$~~ # 1.9.3

iJ_mn*i1i

putcharr),

~tt~m

~-. --ji, 1m~9XJt:~ti !1t!: ~ l¥J tit tlf ~IJJliF Il]1m 1;:

Cm~~~~~~me~$m~m~~€,

m~jt~j;J~ml¥J~m.$m~mm~~~= ffi1~EfJHfi;t3HOO'.iCJ~,~ Ei3Ff!ij 1¥J.~:ii11¥J~5IJiffi]1fl f

int two_d[51[IO];

two_d[O][O] , two_d[O][I], two_d[l][O] two_d[3}[OJ. ,two_d[1][1] two_d[3}(1), two dC2]{Ol , two_d[2][I] two_d[4J[OJ ,two_d[4J(IJ

two_d[O][2]. • two_d[1J[2J. , two_d[2J[2] two_d(3]{2]. ,

two_d[O][9] two_d[I][9J.

two_d{2][9], 1wo_d[3][9] two_d[4][9] ,

,two~dI4][2] ,

int matrix[3][4]=={O, 1,

2,3,

4.5,

6, 7,

8, 9, io, 11 };

~~I~i1IM-T~~jg:~~

:ct~~~~1t*(look.lJ_p.table,

LUT)-t-7t-~ffl

PJflJ" :

19IJ ft ff] m_ JE: ~ -1- Et! i.Hi IR, ~ if :ttf"f H1, ~ ~ -rr ~ - J~ llQ ~~!il 1=1 11~.. it ~ffi. ilJ m 1t!r~*;f.!. #H-=lWi~~3t~~fil¥J9;lt-j ASCII o!l(;ffJ=#J:tE*~J ~-(f115E~J~etkeycodeO g Mmmfi~m, #~.row~c~I¥J~, ~m~~~mIo~ff~"#M.tF~ft~~.~~

~*:rr

-=

#include <stdio.h>

#define #define

TRUE ) ; FALSE 0; "'row ,char ·col) routine gets the key press and

char getkeycode(char

lithe getkeycode

llreturn TRUE if a key is pressed ,FALSE if not 1*look up table for ASCII values.' constchar keys[4][3]={ 'I', '2', '3', '4'. '5', '6', '7', '8', '9'. void main(void) { char row, col; char i; while(1) ( i=getkeycode(&row,&col); Ilwhile forever .... '.' , '0' '#'}'

..

IffRUE,ifthere if(i=TRUE)

was a key pressed

Iland row and col contain which key Iionly print the key value that is pressed putchar(keys[ } } row] [colj);

tzii it :!iJ ~

~-#£~~£~=m~mm~~*~$~mo~~~~T-~*~m, ~~$~m.w
.P.iJ

H<r§t ~ ~ ~

char day ~of_the_ week[7J[1 O]={ "Sunday". "Monday", "Tuesday". "Wednesday". "Thursday". "Friday", "Saturday"} ;

~¥Rm~~2~~nG~MM~~~~#M.~*.mM~o~~m.~~~E~.~~W ~~~tr~*~$, ~~m~~.tr~ili~#*mOO¥n$~ 7'J TVifr:IJ jg(!gx:1ttr ~ ~ ifJ ~fn*!l*I¥J¥~$.1ff§ printf()m~


~ 1 ~*~~lli.il;

~~~~m~, ~~*A~~~~*~o~~~~~¥~$*~~~,~~~~.~~
D

printf()&§fiWf~*~$

fftA~ c ~N~ Atrnel AVR


printf("%s",&day_ofthe_

m~~m=.~.~¥M$.

¥n

* tr.J :g ~:fBi

week[3]{O]); J/prints Wednesday

iA ~ ~ ~ ~1 q I

m I -1' ~ r:r Eru& ilL f M~. ~~Eruftm~~n*mH~M:


Ilprints Wednesday

jj9

7J ~JI! R If.m ,~.-ft;t orl;J.lf :~JIB ~ f '51

printf("%s",day_of _the_week[3J);

m#¥1 1'8J*? 1m -1" ~:bo ~:r:JJ ~~:t£ -r·'8 ff] I=lT ~ J5Y.ffl~· fB ~ 1:em tli rPJ i*J ~ Fr~t~tt-it if Pi!~ JA 1t 13(; ~ ~ it ta "P iT ~ m" m rilJ m Ii ~ tIJ #tE. it.if fB ~ ~ ~ 1m 15i\: fti ~ i*;tt 1m Pn If( ~ ~ i!PTL;lfB{m*12JJitM~Et'1tAffmt. 1ra~-iE.1i$AJ •• El tii&" 1-t.:~a F lID~ -1- JA m 1& t~n 1f. if-J imI m PilIi I¥J #11 ~ it ffl scanfi) m 1& JA t$ m tfu A ~ M- ~~~ A Ul i$. Ul ~ ¥IJfN: It. J;l. if ~1*~ :2.l::ff. 1~ 6 ffi:!J.) P,J ~p 51! tfu A Ul~ if Er''1, JiW fune _ num ber it ~ffi li tEHt~t_H Er''1Wt ~ 10 :ff fune _number ~ '31~ fr'~lf(mfi~]!litm:~fp. fp ~fli it]:it8. @J ~ru ~ void I¥HiR l1!: Et''1 -1-t~# 1 ,8 TiA-I W ~IJ Et''1 ffi 1&tiT- ;1lUf~.it: J : A

~*

type function_name (type paraml ,type paramz, .. ,) statement I;


statementz;

:t£:iS:;pH. R T. fp ~ ilL Il:t£ ~ ~ n1 JI i1~ no ~


0

it~m~

-gJt~~~,

~JPJif1jli~

~:1!*iE.1:70a 'E! -if i1j ~ E11!tl!fJ~

ft~.~

M-ij-(llPjij~JE:.11=)~ W~il$~;f$iRr.7g~ 1m faj 1!€ iE:W M • tJ, ~ ~ 1ti# fp t~~ I¥J PElIi I¥J is:. ~ f'tij !1(! I¥J 15i\:itl:1riIJ m:
0 ~

(·fp)O;

Ilexecute the function pointed to by fp

#include <stdio.h> void do_start_task(void)


{

printf("start seJected\n"); } void do_stop_task(void) { printf("stop selcctedvn");

void do~up_task(void)

{ printf("up selectedin");

J
void do_down_task(void) { printf("down } void do _lefl_ task(void) selected\n");

t
printf(" left selectedvn"); ) void do _right_ tasktvoid) { printf("right selected\n");

void (*stack_list[6])(void)={ do _ start _ task; do_stop_task; do_up_task; do _ clown _ task; do_left_task; do _right_task; }; void main(void) {

II*an array of pointers to functions */

int rune_number;
void('" fp )(void); while(l) { printf("\n

I*fp is a pointer

to a function'v

select a function l-6:");


_ number); (fune _ nurnber<7») - I];
to fp./

scanf{"%d".&func

if((func _number>O)&&

{
fp=task_Hst[func_numher (*fp)O;

1* assign the function address


} }

I*and call the selected function=z

·46·

*Aft:; c f;;~~

Atmel AVR

am~~n$.~~m~.¥. &~mmm~~.~~.*1 w.~~~m.~o


II II "Slot Machine" -- The Mini-Exercise Phase 3 ..

tE 11:11S.it: § i;I] tit m tit i¥J Jf ~

• 1 ilq\f§ljJjli §; C $#

~J i!~ ~ lift'

, l;IG 1[) ftffl ~

*~ ~ * *~t5:rm ~
$
~j1i

!¥J 9~ ~

1/
II

#include <90s8515.h> #define xtaI4000000L #define baud 9600 #include <stdio.h>

1* processor specific information 1*


quartz crystal "'/ the printf() frequency

.1
[Hz] */

1+ Saud rate

1*

this gets

definition

*/

/1 Global Variable Declarations ..


char char char first.second.third; seed; count; delay; 3 2 1 0 BELL, LEMON,

II Columns in the slot machine. II Number


10 form a seed for random

#s

1/ General purpose counter. II A variable for delay time.

int

#define JACKPOT #define DIME #define NICKEL #define ZERO enum {BAR,

1* this defines give different payouts *1 1* names in order to make the code more *1
/* readable to humans s/

CHERRY};

II the values for each kind

char

kind{4] [7] "'BAR", "BELL", "LEMON", "CHERRY"

II

for kinds of names.,

}; void flash_lights(char ( fortcount (


=

n) 0;

II

flash the lights a number of times count++) II flash light when done ..

count « n;

for(delay

= 0;

delay < 10000; II

delay++) just count up and wait delay++)

POR TB.l = 0; for(delay = 0; PORTS. I "" I; }

II tum the LED on .. delay < 10000;

II tum the LEO of I..

·47·
} int get_payout { if(flrst (void)

= CHERRY)

&&

(second ~

CHERRY)

&&

third ~

CHERRY»

return JACKPOT; else if(first else if(first else return ZERO;


= CHERRY)

II if all

"cherries" ..

II
&&

(second

= CHERRY

~I(third = CHERRY»

return DIME; return NICKEL;

II

if any are

"cherries" .. of any kind ..

== second)

(second == third»)

II if three are alike, II otherwise - you lose ..

void main(void) (

int i;

/1 declare a local variable for 1/ temporary use .. II


Initialize the 110 ports (light)

PORT A = OxFF;

DDRA=OxOO; DDRB = OxOO; DORB = Ox02;


II Initialize the UART control register 8 data bits baud rate

II port A all inputs II port B.I is an output

II RX & TX enabled, UCR=OxI8;

II initiaJize the UARTs


UBRR=xta1l16Ibaud·l; while(l) ( while(PINA.O) seed+s-;

II do forever .. II Wait for a button and


II let the counter

roll over and


."

1/ over to form a seed.


first = second = third do { first third
1\.,.

= seed;

II preload the columns /I mix up the numbers /I while waiting for button release.

seed»l; seed»3;

II Exclusive

ORing in the moving seed

second== seed»2;
I\=.

1/ can really stir up the numbers.


1/ keep rolling over the seed pattern

seed++; } whHe(PINA.O flash lights(5); first &= 3;

= 0);
II

II while the button is pressed

flash the lights 5 times ..

1/

limit number to values from 0 to 3

second &=3;

• 48·
third &= 3;

~A:t\;

c tj¥-E!:i

Atmel AVR

II show the values., // 1/ printf(".> "/0$, %s. %s <-\n" • kindjfirst], kind[second],

BY NAME!!

simply change the %d to %5 the 2D array kind[] to printf{)

1/ and pass the pointer to the string from

kindjthirdj);

II determine a payout.. i = gerpayouuj; switchti) ( case

/1 now print the payout results

ZERO;
prlntff'Paid out: ZERO\n");

break;

case
break; case break; case break; } }

NICKEL:
printfl"Paid out: One Nickehn");

DIME:
printf("Paid out: One Dime\n");

JACKPOT:

printff 'Paid out:

JACKPOT!!\n

lt

);

~~~m-m~~~~.~~m~~~~ffl,~~~Affl~~W~~~~~~#*~.*~~a W~~.~~~*~&~~~,~~~ffl~~~~*~T~~**~~~~~~~~Jij~~
f_!{I, ~ ~~
0

~;MJ~:ttm1*Jij*#-1"tJFlr(eJG~f$rfx.t~i1:ill:tr7t~IL

ffif:*-J~ c iit~~ff9='

<>

••

M~»

:@~ffi~!.f)~mX1~~#E::f:t:m
1.10.1 ~~

ta;MJ ~ ~ Jij~ 1¥1fiX 1Q tt iA:*-J ~ ~ ~~

atJ~rt~

i¥J J;i fte~:I:)" 11 ~ff 9=', x>J~;g l& 1Jt T - ¥Jr(11

~ ft;J ~

JA -1- ~ $1-~t-€~il-1'- itt - ~ ~

IY-J 1111;; ~

t5~ 9='IY-J ~t lac:!9 fiX 9':! i!l* 1]11;; it


II

i9=iffi ii- -t-:g 'J I ~ lJJ. ~ -g~ ffl --t-~fiJFlyt~Ur:

Jfu ~

m.

~*fr~~~OO~~.~~~~~Mo
struct structure _ tag_ name { type member_1; type member _ 2; type member_x; };

*~ ~ fit ft

Mfj. J.J ~ JKll~Ji~*It Mi MJ l:j ~ m ~ 5t


o

IRj ,

struct structure _ tag_ name { type rnember jl ; type member_2; type memberjx; }structure _ var _name;

1f;fj_ff~, _- _fi~tt]I¥J,mf&JE)(~ T 1:.JG, structure_tag_name fPAr-1'"01t:~J1£jj!r-r, *f5a_ijiK1'P~?tHr>J!af'LJo rooEt-J7J'~JF~ varl ~ var2 Wj-t-~f~' %~ var3 YiIfaft1?tiJH.o
struct struct_tag_name varl.var2,var3[5];

~&.:

~~ •• ~§*~fit~~~~~,

fj!ffl ~JJ!!i~jt1-1(.)ijJ IPJ!'aMJ


structure _ VaT _ name.member_l

~.*~X~~,

§MX~M~,m~~.~m#&m~M~~m~om #*~re#M.~~,R~~~~~M~~.~~*~~OOo
Pi I¥J~!T.t.

BtE!a~~nX.1J.l:g~tt~*:

structure _ var _ name.member_x

struct DATE int day; int year; }

int month;

Iideclare a template for a Iidate structure

Iideclare a structure variable and initialize struct DATE date _ of_ birth=] 2.21.1961};

it

date _ of _ birth.month=2; date _ of _ birth.day=21; date _ of_ birth.year= 1961;

tmr:

~~~~~-fit~.~~~.~~~f~~~$~M~
struct LOCA nON int x; int y; I/this is the location coordinates x and y

•• &~m~o~~-fiMft1~~

~;
struct P ART {

~AA
char part_name[20}; long int sku;

t1ii~!:i

Atmel AVR

II a string for the part name Iia bin;

sku number for the part

struct LOCACTION
}widget;

Ilits location in the warehouse

x_location=widget.bin.x; y~Iocation=widget.bin.y;

II the x coordinate of the bin of the widget


lithe y coordinate of the bin of the widget

widget.bin.x=l wldget.bin.y=Zs;

O;

lithe x coordinate lithe y coordinate

of the bin of the widget of the bin of the widget

struct PART newIocationtlnt


{ struct PART temp;

x, int y)

temp.part name=" ";


temp.sku=O; temp.bin.x=x; temp.bin.y=y; return temp; }

//initialized

the name to NULL


y

Iland zero the sku number

/lset the location to the passed x an

Iland then return the structure to the caller

'E~:iS@]~;Ml

ft! sku,

I¥JtfOO.7GP1b'Hl-m:ti5'~ltjf~:ii~m~" part _ name .fE JS 1'-t5;MJ is I!!i Zru-m 5! T ~ 1:mi I¥J~ ~ jij"j1( Jffi: tnrf rr9 % It.-m::
PART.
bin.x ~ bin.x
<>
I

1t'fl1¥J

.E&

widget=new _location( 10, 10);

t6*~. part_name
1.10.2

l'Il sku ~:fBO~5!. widget.bin.x Rl widget.bin.y

e<J-m:tiJ;;

100

~~ltm

struct PART { char part_name[20]; long int sku; struct LOCACTION } widget(lOO]; bin; /I a string for the part name Ila sku number for the part

Ilits location in the warehouse

i11lill ~ 9:l [J{J 7Ji'tdnf!.tl! - # [J{J. pt- [J{J ::f rR"J z.~ tE T ~ ~ ~:II -1'-~ JE widget B"n¥ fiIi $. Jt, J:lf ~ ffffl ~fl..iT 'F 00 I¥J:S IJij :
x_'ocation=widget( 12J. bin.x;

*~e<J~ :s I

Ilsl JIt • ~ Vi (Ii]

• 51 • II the x coordinate y _Iocati on =widget [12].bin.y; of the bin of the widget 12 of the bin of the widget 12

lithe y coordinate

widget] 12J.part _ name;

l!the name of widget 12

widget [12] .part_name[O]; lithe first character in the name of widget 12

m~~~m~.~~~~~~~.~trm~~o
struct DATE int month; int day; int year; } struct DATE birthdates[3]={ {

~~~~~W~~~n~£;

~~~~m~~~Fom*g%reW~~mg~*, ~~~~

2,21,1961, 8.8, 1974, 7, 11, 1997 };

1.10.3

:Jiia1!6~tf(Jm*
Jt n ~ z.0

~nd:tffl ~ ftJ I¥J tilt f¥iJ sI m ~~ •• *.m~~~l¥Jm#W~~#.~~M~om~M~OOMltm~~~~:


fl"1J.J 11~ {j!ffl1t I¥J 15 *1ll2$ ~ ftJ I¥J pJ(; ~.. ~~ ~
struct sttucture_tag_name *structure_ var_name;

~-~.m#~~~~-1'~~~ffOO~~,~B~~~~~~~.F~~~T~~.~*~o 1;1. r~i!3ffl*FaAM~~. widget ~m~t;!f;fq~.oom# thiswidget,


I'fl:ft!tbl:JJiitI~mtt this_widget,
struct LOCATION int x; lnt y; Iithis is the location coordinates x and y {

jg.1<f(·F~a] structvar name ~-1'ti~ structurejag jrame ~J!gat4JB9m#

~JtftB~
widget

fiJ§-n-4tf

};
struet PART { char part_name[20]; long int sku; struct LOCACTION }; bin; II a string for the part name Iia sku number for the part Ilits location in the warehouse

struct PART, ·this_widget;


Iideclare a structure and a pointer to a structure

this_ widget=&widget; Ilassign the pointer the address of a structure

this_ widge1->sku=1234;

(*this_ widgetr.sku=

1234;

G§ AI this_widget

& widget

li'~mft, !dJ_t~7F fi<Jj:Jilj;:pflMUl1J1t~~-&tJmfi<J widget jl-I¥-J this_


0

iKn:

M~£~~~, ~~~~~.~~M~~.W~ttX.£~~~*~~M~,
* (this_ widget.partname[Oj). rm:iZ~
~j_tAl widget

~*~~~ ••

!¥J±tB:l:j_L

M~~~~m~~~M~fi<Jmft,
~a~AI~m.

-ffffiIRJfi<J*/J\~ iilrlmlnJffilflJ~~I¥-JM#;), ta~"6JtJ.J£n IAlJlt.


struct LIST _ITEM {

~~~~.~B%OO, .~ •• &#~~~fi<J~~~~~m#m~*~.
u

~~~~~m~~-~~~M~~m~oM~~bft~

WA

§slffll¥-J"~ ~*~'WlJtmr:

char ·string;

Iia text string

int position; Ilits position in a list struct LIST_ITEM "next jtem; II a pointer to another }item.il~m2; item.next_item=&item2; Ilassign the pointer with the address of the item2 /lstructure of the same type

item.next_ item-s-position

AI next_item mJfiJ!¥J
item2.position

position

~#JJ:£m

~1f)--F

A~m~ -F. § s ~ M~ffl Tii1*lO~~!4~ff:"~Ii~f¥~ ffl


1.10.4

:!:tFJHt

~ffl.~~~®~~~M~~~o~ffl~~~~-ff~TM~:

·53·

union union_tag_name type member _I; type member_2; type member_x; };

union union _tag_name type member _1; type member_2; type member }union_ var_name; x;

• *~!i!!

~ffl~~~~.*~gm~TH~B~~ren~~~ffl~~~~~~L~$~m~~-~
Efo} 1f fit U:
union SOME_TYPES{ char character; int integer; long int long_one;

} my space;

~*17lJ tp , myspace ;treEn.*H1itft~.~T*Jt:~~. jfi! *~~3t.E8Mm.1>J:


my_space.long_ one=Ox 12345678L;

long_one(4 *i'i)~A/J'\

till

, my _ space.character=Ox my _ space.integer=Ox

12;

1234.

~m~~~.m~i'i~~~~OO~~~~~*W$~.~~fi~m.
0

H~~~~~m~

~ffl.~-#~~HMft".~g»~.~n~~~m~~~m*M~AOO •• M~~M*. 1Ii!i~-/J\$~ tEmrm~{9IJT!fl, ~J!,j~m (r{JN~IOCtk:T m~ffl ~.t'i!i~~lO~~flUif:fttm{ilJ S1:~ATJt:~*lt(8 i!L)OO~ ... flUifft7f JIlJ. H~I¥1.1Ii!i{g::fI¥iJ"1cmPT11m¥l'l7X~3Cfli!!. l PT11~*7X~3C~ .•• ~ ••
o

ifffl~m~Q

~~T~.~~~~A~O~ft~-~16~OO~!floml#~~~m3~~~~.m2#~~
#include <delay.h> #include <90s85IS.h> void main(void) { unsigned whHe(I) { port _ w=PINA; port_w «""8; port_w ~=PINB; int port _ w;

~o*wm*~~~~u~~mm~.fi~~•• ~m~~~~~~l'l~_~.M-#m~ft~~~-#m~m.~~~~~c~~OOl¥1m

II register definition header file for a Atmel AT90S8535

Ilget port A into the 16 bit value


IIshift it up ... IInow combine in port B

*AA c ~~1:i
printf(" 1 6-bits=%d04x\n" }

Atmel AVR

.port , w); I/put the combined value out to lithe standard output

~ffl~ffl~~~~n~m~~~~#~~~~
#include #include <delay.h> <90s8515.h>

liregister definition header file for an Atmel AT90s8535

void main(void)
Iideclare the two types of data in lIa union to occupy the same space ... union ( unsigned char port_b[2];

unsigned int port , w;


}value; while{l) ( value.portb[O]=PINA; vaJue.portb[O]=PINB;

Ilget port A II get port B

lithe union eliminates the need II to combine the data manually printfi" 16-bits=%d04x\n" .port, w);
} Ilput the combined value out to

1.10.5
j(

typedef @.f.1

c *i§3tr.fB~rt1fi~M~
0

fJllt;!Jl:

~.~Q

lithe standard output

typedefiE.~

ft.ilf.14~~~~pr~

1u ~:l:~~fl~~jaJ

typedefunsigned

char byte;

typedef unsigned int word;

f_W.tE~IJ~

byte ~ word

ETlj,ffl

*pr 1fflY;A-j;J::1u unsigned


int

char

~~1O

unsigned int

~M~~.

byte varl;
word var2;

Iithis is the same as an unsigned char


Ilthis is the same as an unsigned

typedef struct { char name120];

char age;
'int home_room~number;

}student;

·55· student student Bob; Sally;


t

Iithese allocate memory in the form of II a structure of type student

1£ tfiD ifff! J;!~ff~ #define miD~ It-t ffl T 3mrl;t 5l.t H~*ffi. m~fflTF~~~ru~~~~m~~ili~~J;!~n~~f¥o
1.10.6

*~1~:f;tl. ~ m f¥ ZlJ
tT

typedef

EBtfiD W

ft:

ill: ,j:HtI.1a

1¥~32
tfM'~,

fa]~ ill Ilt. 3m~ ~ft:ffl R fF~:;1t § GI¥J5t:l:'3-IJfl

{fL~1fl:~ -.Lf:bttiji*ft bit ~m. EE 3t* ~W.


0 0

§ i9J7Hfc

1JIl:tm

bit running;

lithe compiler allocate a single bit of IIstorage for this flag .. lithe only two possibilities for value are 1 and 0

running=l ; running ...O; if(running)

lIthe value can be tested as well as assigned

~N~~~~,

~QN~~~~~~ffi~~.

{fL~£~eT£*~£-~~*~o@~~m~~~A~ttijif.~x~~
~~B~F~M~~r:
struct structure _ tag_ name {

struct structure _ tag_ name { unsigned int bit_l: I;


unsigned int bit_2:1;

unsigned int bit _1 :1; unsigned int bit_2:1; unsigned int bit_l5: 1; }struct_ Val' _ name;

unsigned intbit_15:1; };

~::k h\ 1 itl unsigned IJ\ -~~~.~-~.~89~~~~~~.H~ ••


g

mJEUl~I¥J1Jit~, ~ I¥J ifL 89::k fiX _M~ +~


struct{

1r5t~llin)G_M~(7!.~ ~

unsigned int ~~),

Ri&~1'- 'a'45-(:)~t&-m:m$ int 5J~JIM '1' ffl) 89 (16 it. i~-1'-~ f1;J ~ I¥J

~.~g
*

~~

unsigned tnt unsigned int unsigned int }machine;

running: 1; stoped: I; counter: 4;

machine.stopped=I
machine.running=O;

If these are single bits __o only I and 0 are allowed s

machine.counter++;

Ilthis is 4-bit value,so 0-15 are allowed

• 56 ~

i(XA;tt: C ~.fMlj

AtmeJ AVR

typedef struct bit~O:J; bitL}; bit_2:1; bit_3:1; bit_ 4:1; bit_5:1; hit_6:1; bit_7:I~

[bits;

t# -F

Nd~~~*)~ i3:ftilff¥}¥ J!l'~.-ff:


#define PORTB("(bits

£(.J# de fine

ffl

*tE Ox38 ff~ lit Jt;tJFn: ff.~ ~i¥.J ~ ~(#define:fBE


mtE~1'W)¥Jflili1
:t:)Ox38)

fm&lJmW)¥ §fF X:$: fj PORTB *ijjfr:TIt¥~fiI¥J1f1i'lit5G Ox38. Wtfl't:

IIPORTB is the contents of address Ox38 , a bitfield "bits"

/lhltfields can be used in assignments PORTB.bit~3=1 PORTB. bit_ 5=0; Ilbitfields can be used in conditional if(PORTB.bit_211 PORTB.bit_l) PORTB.bit_ 4=1; expressions ;

1.10.7

sizeof

m.f.1
f=1::-1-5Jfl

~5i.:~-ft1f$:h sizeof E8-j[;ig;WW~ j61':iE#:fq~-1-iJjH'IPf*f~IL 1mX1j!.~~~f§*JJ*B9n.1lo sizeof m.nB9J&.A~r:


sizeof(type_name) sizeof( object) Jltype_name could be keyword int, char, long.etc

c iif

Ilobject could be a variable ,array, structure

,or union variable name

int value,x: long int array[2][3J; struct record { char name[24];

int

id _number;

}students] toO];

x=sizeoffint);

x=sizeoflvalue); x=sizeofflong);
x=sizeofl array); x=sizeof(record); x=sizeof(students);

IIthis would set x=z.since an int is 2 bytes IIthis would set x=2,slnce value ia an int IIthis would set x=4,since an int is 4bytes II x= x=sizeofrlongj+array
/lx=24+2 for the character _ width" array _length=4 string plus the integer characters
If 3'" 2=24

Ilx=lOO Elements*(24
Ilx= I 00·(24+2)=2600!!

+sizeoftintj)

.~~.~M~~.¥*~.~~.*~~M~~~~~~~ •• ~~~~~~~~~. ~~~~Mff~-~~~~ff.B~, OO~~~~~~&~*~$~A~~~m~~~~~~~~~~~B~o~~~m3#~~~*~~,fflf~~~~~~.~~~~~~~ffi~~~


1.11.1
AVR

JJ1iiHlHm:~~rf*~ ~1J'jlb1~f4::rf11f.ft.~~Cf.([rm-1tlD~~~~.)It-t.

iI&'*5i:~~~f1Jollrl.

m.~Il~-=:
l1jU~jfjIJBitffl tli.ftJ~it. Harvard
*~~(SRAM) ... ~~(FLASH)~ CodeVisionAVR EEPROM

~M~
I;:..( itffl

1tffl*ff~~hl3:!rB]o ~f'P~.ftJ:#f~~ 2 .~Wim1t-mo ~~~ft..~~. ~ft~~a~~~~~~~~~~MBo


0

lD;t:tftBtiUwBgl;!l
0

~itl¥J,*iA~ §i9J*re(~{j!mfj~B:Jl~r1~t'@'T)J!tE SRAM rr:i£tr~ flash wt const ::X:tI~IlT FLASH ~~~(~r¥:£ra])~ Mft&~ff EEPROM
e

1IttPJ

t:p ~3f::ii:, ~1t ffl~.¥ eeprom ~:l:pia)j!;J,JE~ flash.ffi eepron ~tt~ 1¥J~1l:§i,R;+:5t:l:~o , flash :tI11* const dee~m.~lli.,~&W.~~gl;~~~M~:5tre~~m~~.~~~~M.~.t:po~~
~m!FIlAilflLit~
flash eJ?; eeprom

m_~.*!;}:t£1mJll:t'lf SRAM
~m~pi~~m.~~~ PfVtff It-t:r-,,~ ~ e ~
flash int flash char char_constant='a'; flash long long jntconstantl flash

~.*ZE.l2;_~7J'\~~:I:~r;lm
~
0

FLASH ~ EEPROM

~~:I:,
g

~~1t~~pia)j

FLASH ~ EEPROM

9=rl¥Jmftlt-f1it~ fflJU

••

~fm~#.~~USffi~a~~~~~

••

**.!

ff~

integer _ constant=1234+5; =99L;

flash long long_int_constant2=OxlOOOOOOO;

int

integer_array

I []={ 1.2,3 };

Iffhe first two elements will be I and 2 ,the rest will be O. flash int integer ~ array2[] = {1,2};

·58·
flash int multidim_array[2,31={

ittA~

c iJiiW!.] Atmel

AVR

{1 ,2,3 },{4,5,6}};

flash char string_ constant 1[]=,,"This is a string constant";

const char string_constant2[J="This is also a string constant"; flash


struct {

int a;

char b[3],c[3]; }sf=={ {OxOOOa}. {Oxb 1,Oxb2,Oxb3}. {OKC 1,Oxc2,Oxc3} };

EEPROM

~rHJ~~~~Xgqff1ift.5t:l:I3:~"
cycly _count;

~:l:JlJJmU:ffi'j1f!.l¥Jpi~rml[,T EEPROM ffmt

3'f-! :
eeprom int

/Iallocate an integer space in EEPROM


Ilallocate a bytes area in EEPROM

eeprom char ee_string[20]; eeprom struct { char a; int b; char c[15];

[se:

(FLASH) 71< A (EEPROM) ff ~ ft. f8: ~?± • 1O nASH~~&§~~~Bm~tt*~~o~~ft~*M.~?±~~~~o


is:~ 7XR

lIallocate

\ 8 byte structure "se" in EEPROM

A it; ¥ €1t 9J ~

iq: $~ l.E £j{J m jjs:

~~**~~WW*ft~~,

?±~~.~~~

•.

m~~~SMM~~Q

M*-~~N$~fflMr~~~W~~nA~OO~
char mystring[30]=" This string is placed in SRAM";

mIJi$<¥~$~:tE

SRAM t.p SRAM ~


Q

5tDC30 ~¥il ~ ~~

FLASH

f¥1i-H~<> FLASH ~Jttw~~m1f~2;ijR1j!1tiUiU SRAM

This string is placed in SRAM ~.

t!8rJILl:U'l

:#-iixiB~H1J!ijjlb]

mystring,

ff~-Hr::p :

~1¥mii;::tE ~ 1Jt 30 ¥ 11i¥J SRAM

rp5E1To fuHl~r¥tE:@rrAIHS]jf&WX>t~f~ $:iiHffit&, 9l1J~ rBJ 1; T [fj .il: SMM ~ IQ]fJ ¥~ fll. r.J- $ EJ llifill 1& J1i J!ij:ff- ~:tE FLASH f

*"

flash char mystring[30]="

This string is placed in SRAM";

~ ftm EEPROM W: itit{if: f1it. 1JIUW i3:f'tl4tl I:9!.tTI # 1t~ Jl §:ff 1f .Ei~ ~ 1it t:& 5t ffJ fi m~~#1i?±~g~~P~~T~~~~J!*~~~BmB~,~~~~~trR1~~~,it~*
a

**, ~~~B+E~.~M~~~*oMTfi.~~ill~B~m~~_a
I

Et! 11t, Ii:m{7H'1:~ X~ ~ @'~fl~ E(.J o¥7j(R ttBJ w.a~W:-FIR~I¥:JIi:~ EEPROM ift~ ---r~irrM. fI~1f;ilHfEl!3$:i&~Y&z. WIlTJ?J 1At:f~11iIf~JM:*~xB i3:lW.~TEPROM ~fitft.*~ l¥J~i!:&JtE@.{t~~tt ~ :tE*$*t. E m ~ i3:1-"fffit~g •• ~fmj£tr 10000 iX~~fto Mi~t1Ttt*I¥J1tJl, ~~fFgqli: § 1f~Wi
EEPROM
c
0

12i~f$1v ~~~ X £j{J. is:1il!tlf ?:t§~~t~3Wi

*~~

~~O

~I[m.~.

*!R~~I:: ~fl

1.11.2

mtt
~~~~.~
I

~~~fiM~,

••

EEPROM~

ffM:HeIR~, m~ll,.f-_lJlJtt*5t1£~~?t SRAM I=f :tE:i3:#111N5fF0-E ~ ~ ~tfi!~ char ~ int ~ m. iJl ffll¥:rff.1m:He~ru.~'1l!ttNii!~~, tJ.1f~t.tiIiw:He~~~:i:pX;iEiiJi (f<J ft~*~~~~*~~~o~~*fl~h~e~romm*W~~~. M~:
FLASH
Q

~.~m#~~~.~n~~~~~g~.m#~m

I*pointer to a string that is located in SRAM +1 char "'ptr_to_ram="This string is placed in SRAM";

I+pointer to a string that is located in FLASH "'I char ·p(r~to_flash="This string is placed in FLASH";

I*pointer to a string that is located in EEPROM *1 char *ptr_to_eeprom="This string is placed in EEPROM";

111tB'! I¥JSRAMIR~ 'eL ffi-1'-l$ jg '6!~:B X #(Register ftiJJ:He OO~O.~~:He~x~*m~~, ~AI~~d«.ff~"~o~ ~~--1-wUL1-'6!ff:B, 1fffl.ff.1m~ register ~11Ji1'irQ
A VR
register int abc:

File}1¥JIR~ i!1- g tir -E! 15


0

••

*Wt.tili*B.~.0-

!lP'it*- 1fffl register ~tffi:M=, flU il:He1£I:lT Jifi; 1J § z;jr~ 3t.it Ej~ --1-!& 11 1- '6!.ff.~ LfJ

~ it~.0-Ejtl'6!ff:Hel=f. ~iH~i:&o
volatile int abc;

lii~1f:ffl

volatile

~11Ji1'ir" ~W*flUif:B~~.iJT~tEMiiilltiBi:5'r

~111t~~~~~~m~~~(~~~~£-#W~~~M~~~~,AmM~T:tE~~~ffl ~ff~'itm). SRAM *l¥Jmffl~ff~-m-1fm volatile ~tffino :i3:#, 4ijiXtE111tB'!iIJIJ 3tt1l~MJ§Jg@J¥UlE-;t~~~. SRAMt:pl¥.l~iifl~o m&fl0-~~'6!~~~~~~.~~?tSAAMI¥J~~g~~~~~.~~~o~afl0-~ ~~~3~I¥.lR$~.~M?tSAAM~.I¥J.mtt •• ~~~~~0-~OO~~~.
sfrb ~O sfrw

~.~.tE

~D~:9~~ii11-fflT~~:HeXftt~ ~nt. ~flm~ffl*~i}ffAij~:He SRAM {flll .. I/O GDd.~.x#t:p~-~*m.11-~~~~~o sfrb ~ sfrw ~~*-aiJf~il3 IN 1OOUT n:t.tiIim~J!m*-ttffllJ AVR mtB'!ftiJJ31¥J '6!~ I/O
VO
0

fi~"
I"'Define the Special Function sfrb PINA""Ox19; Registers(SFR)*' 118-bi1 access to the SFR input port A

*AA
sfrb sfrb TeNT I L=Ox2c; TCNTl H=Ox2d;

C :!'#9~l..j

Atmel AVR

118-bit access to the lower part of timer;

118-bit access to the upper part of timer;


1116-bit access to the timenunslgned

sfrw

TCNTI =Ox2c;

int)

void main(void) unsigned char a;

a=PINA; TCNT! =Oxll11;


while(1)

IIRead PORTA input pins

IlWrite to TCNTIL

& TCNTlH

registers

jjjill#Hf[~ j!J~ Ilft nll 7£ 1/0 *:ff:Z I¥J-7 Z~. nr lj,ft ilf X11/0 ~:ff#:iJ11Tw:-~ AgVi 11:1] ~ f!I j~xt*:ff~ff'~lli:~~-ijj IIll 1/0 :&imiiftffl CBI, SBL SBIC 1DSBIS j§m,**~f_w.l¥J. P,JT t;J, tr 1tffl sfrb ~ ~ lf1, fffl: ±fg hll'fl m: rn! ~m:tEOxoo,.._, Ox1F .L rBJ 1£ it ffl sfrw F ~ 8>f. 11 #i1k hl1!Hlii 1£ OxOO--- Ox 1E Z rBJ fJlJ P.n :

n=~*

sfrb sfrb sfrb

PORTA=Oxlb; DDRA=Ox 18; PINA=Ox 19;

void main(void)
DDRAO=l; DDRA.l""O;

Iiset bit 0 of Port A as output Iiset bit 1 of Port A as input 11se1 bit 0 of Port A to a 1

PORTAO=I;
while(1 )

if(P]NAl)
PORTA. 0=0;

Iltest bit I input of Port A

imlit.

sfrb

~n sfrw ~.~-fiL-r~If:-:Jf~$7T1fffl#include

ffi~~m,*Ag~X149='

:A~11f

~~. ~~~X#~~~~~~~~~*~o~tt~~x#~~~~~~I¥JOO$~. ;!t ftE lli.-r( l£W )kmffl ~ff 9=J 1J!fflAg)~~:ttl ~ '-P Em 1f ffl ~:ffH ~
!::l. ~:& ·-t-Eil~il:#~{lt:I¥JA~~~Jt14(90s8515.h):
III/O registers definitions sfrb ACSR=8; sfrb UBRR:.9; sfrb UCR=Oxa; for the AT90S8515

~H~~

~J.
sfrb USR~Oxb; sfrb UDR=Oxc; sfrb SPCR=Oxd; sfrb SPSR=()xe; sfrb SPDR=Qxf; sfrb PIND=Ox 10; sfrb DDRD=Ox 11 ; sfrb PORTD=Ox12; sfrb PINC""Ox13; sfrb DDRC=Ox14; sfrb PORTC=OxI5; sfrb PINB=Ox16; sfrb DDRB=OxI7; sfrb PORTB=Ox18 sfrb PINA=0x19; sfrb DDRA=Qxla; sfrb PORTA=Oxlb; sfrb EECR=Oxlc; sfrb EEDR=Oxldj sfrb EEARL=Ox 1e; sfrb EEARH=Oxlf; sfrb EEAR=Ox1e; sfrb WDTCR =Ox2] ; sfrb ICRIL=Ox24; sfrb ICRIH=0x25; sfrb ICRl=0x24; sfrb OCRIBL=Ox28~ sfrb OCRIBH=Ox29; sfrb OCRIB~x28; sfrb OCR 1AL=Ox2a; sfrb OCR} AH"'Ox2b; sfrb OCRIA=Ox2a; sfrb TCNTlL=Ox2c; sfrb TCNTIH=Ox2d; sfrb TCNT1 =Ox2c~ sfrb TCCRIB=Ox2e; sfrb TCCRIA=Qx2f; sfrb TCNTO=Ox32; sfrb TCNRO=Qx3 3;

.AAC$1f~it-

• 61 •

1116 bit access

1116 bit access

1116 bit access

1116 bit access

1116 bit access

• 62·
sfrb MCUSR=Ox34; sfrb MCUCR=Ox35;
sfrb TIFR=Ox38; sfrb TIMSK=0x39; sfrb GIFR=Ox3a; sfrb FIMSK=Ox3b; sfrb SPL=Ox3d; sfrb SPH=Ox3e;

~Ai\

C #;lii@ 5j Atmel AVR

sfrb SREG=Ox3f;

IIInterrupt

vectors definitions

#define EXT _INTO 2

#define EXT_INTI

#define TIM I CAPT 4 #define TIMI_COMPA 5

#define TMl_ COMPB 6


#define TIM1_OVF 7

#define T1MO~OVF 8 #define SPI STC 9 #define UART_RXC #define UART_DRE 10 11

#define UART TXC 12


#define UART_COMP 13

1.12

~at~*

~m~~~it1t~~meg~h.~~~.~~~.o 1.12.1

~atfiJ¥~it~R1t~:t&~iAjJH:R5kffij#~eg:9.tJm, R~tr:t£~~T Linux ~ UNIX ~ili 1t*!JtJJ{Jj(~rHn.:H_t.<> ~~Y...t.#~~-ruHtt, tEtll$~~r. *Aj\~~£$~1ffl~1:A.tr~It-t!ltJ£o -~m.OOfi~m.~.~ff<>g&~G~@~:t£~~#~~. m~ffl~a~It-t~£.~ *ma~~nm*m~ft~~~n, ~:t£*E~R1~~~ttm.~~~1:A.ff~~~~~nog &~ f!J.I ftJ¥1i R~ Xi?'~$~d it(:Qu ~j WiA.)1£. e~ :±I~ m. fJ itt fF W~ $... :9/- EEltit 4 - eg A VR l' 'QCtlJ I:U Il(.3 DY ~i:-flfi 1-R1 fi. j:£ ~ t1- Xi f@J A. ~ it fF f±l1fnJ m.
Q

1l!m IfJ Ii
c ~

~ Wi~ 83Ji;J $~;~$~£1lfij [~~~J¥~1t 9=r eg~1r;.. mt~~WG1T 1fT IW-..t. IT£. ~1-M! ~ #?~~~~~ffl~~**~~~~tE~Wiffi~~rr~~W1:A.trm, ~~~ffi~~~~~~~
iJ!I~(interrupt service routine. ISR)o ~
_§.

ISR

1f.6X; r ~"im..JT7fM:i, ~~M ~ 1JTl¥J{![=r:tAIT

aAVR~,

~~~~~.~3W@~~.~ ••

~~~*~-~*§~hl~~~~a~~~hl~~~~.o

~*~~. *~~f~~~.@~~.
EfJijI¥J~~~ift~~"
0

~ £!firF~~ ~IYJrtJ:ltlU:E rtJ. MPT ~Jj!:i:tm=Bm~ RETI(RETum from Interrupt):lS.@]3itliE 1iH~*:{)t~g RET! f!J4-#Ft~~.#~1~Jf fit:ffYiJ "1f~~, #:mfi~itltft~12:~.QX;~B({ IYJr

~ ~m~.L.m -~m~
AVR
0

~~.L.

tlU~1t~:H~1fit$~Jij~tp~Mo

AVR Ji!!J;;:.

rOO3T7UllilYJ

!f.f~1YJ

~-~~~tto~~~~~*.~ffi~*~ft*~~~*$,
JlInterrupt vectors definitions #define EXT _lNTO 2 #define EXT_INTI 3 #define EXT INT2 4 #define EXT INT3 5 #define EXT #define EXT lNT4 6 INTS 7

i3:~~5Ci&:fr. ATMegal28 ffJ=*Jcitt Mega128.h .:p

*R~M~~~~~3~~~

#define EXT INT6 8 #define EXT !NT7 9 #define TIM2_COMP 10 #define TIM2 OVF 11 12

#define TIMI_CAPT

#define 11Ml COtvlPA 13 #define TIMI COMPB 14

#define TIM I OVF I 5 #define TIMO COtvIP 16 #define TIMO OVF 17 #define SPI STC 18 #define USARTO_RXC 19

#define USARTO ~ DRE 20 #define USARTO_TXC #define ADC_INT 22 #define EE RDY 23 #defme ANA _ COMP 24 #define TIMI COMPC 25 21

#definc TIM3 _ CAPT 26 #define TIM3_COMPA 27

#define TIM3 _ COMPB 28 #define TIM3 COMPC 29

#define TlM3 _ OVF 30

·64·
#define USARTI_RXC #define USARTl_DRE #define USARTl_TXC #define TWI 34 #define SPM~RDY 35 31 32 33

$tAA c ~W ~

Atmel AVR

it 1'- ,,11~ -E11! ~ 1'0&rp Itfi~ I'f.J ~::f:;ffi ~ It\ EfJ ~ ~ rtl~~ '31 ~ ISR. I'lTkiJI:tt:f:tJilfHW::f: interrupt *~mJ&~~~tfPr.f;*:itt1TF~
interrupt [EXT _IINTOJ void external jntntvoid) f·CaHed automatically on external interrupt 0*/

,u

Jl~~ t:p Ifi it ~

*'~~

ffl Ef'.l~

interrupt [EXT_OVFJ void timerO_overflow(void) I·Called automatically on TIMERO overflow=/

*ilf interrupt itiR 1£~ t] 1€. ~ ijI~ Lf1!tfi~ i¥J 1tiJ:il1tliL ~ .~::f: M[ 1]7ftm, f812;f ~ m I 1'- jtJ • R: :j::B_ rtI• • JiJT ttfl J¥ 1Jl1if W. -ffffl ~ 3; ~j; Lf11tfi FiJ ii ~ [2] 7f 1.tti (fJ JA - a tpWT~q'jIJfJJMt«:, :# il3tfif~,$J 9-IWT, [SR ~Qfw.t£1ffuJIJot1~fA1T ISR ~fIE;JSfi51 1f11tJ fl. {t!11 f:£ti;#:..t.:f:ff-{E "iJfilffl#" • t:: ,~,R:lJfJE SA 7;; void ~~ ~-rfR")t=f1¥J mt ffiI, /Ffft:; !OJ ISR f$ J~HfiiJ ~IL f a.A~~~tp. ~ItfiR •• ~~~m~~fAfiQt£~m$*~.4~~&~, ~~~~ .i"iU1£ mainO~~~~~ while(I)tJB~o t£iK®tti51 'f. mainO~!i&JU~:x.N.!1lJ:j1HTwMt«:. ffij
0
0 0

*.~.~~Itfi~~~~ff~$~.~G*o
11110 register definitions #include <90s8515 .h> for the AT90S85 I 5

Ilquartz crystal frequency [Hz]


#define xtal 400000 /ILED blink frequency[Hz] #define Blink IrrllvtERI Rate 2 interrupt service routine

overflow

Iloccurs every 0.5 seconds


interrupt { Ifpreset again [TIMl_ OVF] void timerl_overflow(void)

TIMERI

TCNTl=OxlOOOO PORTB_O"= I; } void main(void)

- (XTALIl0241B1ink_Rate); Iitoggle the LSB of PORTH

Iiset the 110 ports and initialize TltvlERl DDRB=OxO 1; TCCRIA=O; TCCR1B=5; TIFR=O; TIMSK=Ox80; GlMSK=O; #asm("sei"); fiPORTB.O pin is an output IfflMERl IffIMERl is disconnected clock isxtaV1024 IfpresetTIMERl interrupt flags interrupt from pin OCI, no PWM

TCNT1=OxIOOOO - (XTALlI0241BIink_Rate); Ilclear any TIMER! lienable TlMERl

overflow

flaIl other interrupt sources are disabled f/global enable interrupts

while(l)
lithe rest is done by the"timerl_overflow"function }

;;$:1JJJ ~.

mainO~~ffl

BJ*-f1)'ft
while(l)~

LED
0

~t$-r

~ i9.f~H1.a<J~: ~_ LED -ll:t£r-Yt!f-Bt. mainO-ll~tEAJlH"'lf

*:mMi{t~t1f.. TIMER1 m l±l ~ wr ~ t:p ltJj i!&~ :!i:ft

timerl_overflow

1.12.2

~~*Jl
t.J IYI J.t~~FF~:i:~r.t!fiilr Jit1tFWtft~mo A, i!

m~~~~.~~m*m~.W~~M~fi~~~.~~-~~.Qu~m&~~~m-~£ *~m~*a~~~~~~~. ®x~~~~~. ~~~~~~~:t£~~~~m~~~~rer


lit tT:l! DO ~ ita~ ft i9J _fF
g

:a:11ttt~fft:p,

4:R~mr£Jit:tE

switch/case

110*,

*OO.~~ ••~.*~c~~~-.m~~mp~~.:mp~m,~~, •••


m.~$~.i:&~~
1-1

U~mft~fi~OO*~~._n#€ft*8.~~~ •• ~~~~~ •• m&~cru~ ~.m~cru~*.*mBt~~~~~~_~Q*.~~ ••mMA.~fi*fii!~~~ff.


00

*~~

m~1;] f:! ifitl~

~J 1:i!iT Jj~J~U!l

.AJt c ~;fi

~ Atmel AVR

u 0-----.
::>

.......
I

0-

1:1::_

~¥'.

!ffi
U)

Z
"'I
-I..cl
a:~

... ::

I W

-111 0«:

Q~,

2~

a:~

..... ~

..;f

·67·

#includt: <90t>8515.h:> #include #define #define # defi nc #define #define #define #define #define #define char time_left; int current_ state; char flash_toggle; II This enumeration <delay.h> EW _RED _LITE PORTB.O EW -YEL LITEPORTB.I EW_GRN_LITE NS _ RED
_ LITE

I· definitions

to actual outputs

./

I'" used to control the lights

*/

PORTB.2 PORTB.3 PORTS.S PINA.O PINA.l

NS _ YEL _ LITE PQR TBA NS_GRN_LlTE PED_XING_EW PED_XING~NS

;* pedestrian crossing push button "'I


I'" pedestrian crossing push button */
1* switch input for 4- Way Stop'" I

FOUR _ WAY _STOP PINA.3

II time in seconds spent in each state

1/ current state of the lights


II toggle used for FLASHER state

creates a simple way to add states to the machine generate an integer value for each name making the code easier to maintain. EW_WARNING, NS_MOVING, NS_WARNING. FLASHER};

1/ by name.

Enumerations

II automatically,

enum {EW_MOVING,

1/ The actual state machine is here ..


void Do _ Statesfvold) { switch( current_ state) { case EW MOVING: EW _GRN_LITE NS_GRN_LITE NS_RED _LITE EW_RED_LITE EW _ YEL_LITE NS_ YEL_LITE

II east-west has the green!!

= I;

= 0;
=

I;

II north-south

has the redl!

0;

= 0;
= 0;

if(PED _XING_EW

II FOUR_
10)

WAY _STOP)

II pedestrian wishes to cross.or a 4-way stop is required


if(time_Jeft> time_left = 10; II shorten the time

} if(time_left != 0) II count down the time

*AA c ~~ ~ Atmel

AVR

--ti me _Ieft; return; } time_left = 5;


/I return to main

1/ time expired.

so ..

1/ give 5 seconds to WARNING


II time expired. move

current state = EW _ WARNING;


break; case EW WARNING: EW_GRN_LITE= NS_GRN_LITE NS_RED_LlTE EW _RED_LITE EW _ VEL_LITE NS_YEL_LITE
= =

II to the next state

0;
OJ I; II north-south has the red .. If and east-west has the yellow

= 0;
I; = 0;
='

itltime_left
{ return;

!= 0)

II count down the time

--time left:

.
II return to main /I time expired. /I so ..

if(FOUR_ WAY _STOP) current_state


=

if 4-way requested then start


II the flasher

FLASHER;

else
/I otherwise ..

time_left = 30; /I give 30 seconds to MOVING current_state = NS_MOVING;


} break; II II

time expired, to the next state

move

case

NS MOVING:
EW_GRN_LITE::; NS_GRN_LITE= NS_RED_LITE EW_RED_LITE EW_YEL_LITE 0; 1;
= 0;

If north-south has the green! r II east-west has the redl!

1;

= 0;

NS _YEL_LITE = 0;
if(PED_XlNG_NS { /I

II FOUR_WAY_STOP)

if a pedestrian wishes to cross.or a 4-way stop is required ..


=

if{time left> 10)


time_left } 10; II shorten the time II count down the time

itl.time left

1= 0)

=time Jeft;
return;

II

return to main

II time expired. time_Jeft


break; case NS_ WARNING: EW_GRN_LITE NS_GRN_LlTE NS RED_LITE EW~RED_LITE EW _ YEL_LITE NS_YEL_LITE

so .. time expired, move

= 5;
=

II give 5 seconds to WARNING


NS _WARNING;

current_ state

II

1/ to the next state = 0;

= O. = OJ
= = =

II north-south

has the yellow ..

1; 0; II and east-west has the red..

1;

if(time_left
{

!= 0)

II count down the time

-time_left;
return; II return to main

}
iftFOUR_ WAY _STOP) current_state else

II time expired,

so ..

/I if 4-way requested then start


II the flasher II otherwise ..

= FLASHER;

time_left

current
} break; case FLASHER:

= 30; state = E W _ MOVING;

II give 30 seconds to MOVING

II time expired.
II to the next state

move

EW _ GRN_LITE NS_GRN_LITE EW_ YEL _ LITE NS_YEL_LITE flash_toggle if(flash_togglc { ~

= 0; = 0;
= 0; = 0;
1; &. 1)

II all yellow and


/I green lites off

II toggle LSB ..

NS_RED~LIlE= EW_RED_LITE

1;

II blink red lights

= 0;

} else {
NS_RED_LITE EW_RED_LITE

= 0;
=
I;

II alternately

} if(!FOUR_ WAY_STOP) current_state break;

II

if no longer a 4-way stop

= EW _ WARNJNG;
II then return to normal

default: current_state break; } = NS_ WARNING; /I set any unknown state to a good onel!

void maine void) DDRB""OxFF; DDRA=OxOO; II portb all out II porta all in

current jstate ~ NS_ WARNING; while(l) ( delay ms(1000);

II initialize to a good starting I! state (as safe as poss ib le)

III second delay.,

this time could


it knows to do next

II be used for other needed processes

Do Statest):

II call the state machine, II where it is and what

*{JlJrp,
delay_msOffi~ffl

~~tTt1i!m

trl¥J*~~~~~*~~~*H15I¥Jrp~~ff~Mo ik~m Do_States()'tit.fP~ffl-~tx. mE tE:lS@J!J majn()ffi~z mr.Rtttrr1~j>l¥Jm~ 1!ftij 3ft current state J1Irt Do_StatesO;fH~t~~fF¥1rto PED_XING_EW" PED_XING_NS ~ FOUR_ WAY _STOP 4i1trAJ1Iii~~atfBJ~;pJBXm.ftI¥J~12(91:M"IW*)*.F 1:iE~fJL5mr:I¥JN~ 33: 1'-7J\{Jl1 t/EJl:9 iU IE'm I¥J f¥ $tJ I¥J):}J ~~ ~ gs1J rOJ ~ trtr at, 1¥J ~t.1f~ ~ ~ iT 1£ tt-tQ

~~*fO~n,

*~itIJIt1 fa], ~t¥-EJ ~1JIJ:rtaJ $0 tE~~~1:m a:p, .i!-t-H1 j'fi}Wm *~1t~3C~

PORTB

?IVmZ9Jl¥J~t1JI~JfO*gs1J~l¥JiI~.:trmainO~1¥J tt~:
0

1J~~.~ffH~n~.M.tr~~tt"~58~.trW~.~.-ffA.VM~
PED_XING_EW

!lJ!; PED_XING_NS

FOUR_WAY_STOP 7f~,

:{z[1;~1T1f jjfI-z.m1fe<Jtr#f~~ilJT lSffr~3E.R~1t1::tE.iJ~~It-t, tl.fi


10 ~"
0

~4R#f-@fftEi:&~:I5"

~mrJjl1TBtIaJ1>-T

•. ~~

1iE5tj!~~c

ffl~~fO*~*M~"am~ •• ", ~Mtt •• Bo~$I¥J.~~~~~M~~W.


Il€lA~#i*~~~3l':{{'I¥J'~m:
0

mpnoo,~~"~a.*,~.AD,,m.~

•••

~~~ff~t¥-~~1i!ffl~~m~~

W~~

·71 •

m,~~~~ffl~~~mT~~#~3~~~+~flmo ~ it: r\:fO r.r ({y, m I/O it ft)~~ T tk:fT.#: fif1:: 1UjIflJ wrjz _*14 ~ ~mfl ]jj: 1fu*it~ In iE:JJ:M-iElfiJt2:*IJ~~, Pu while, do/while, for 1mIf .. switch/case 1I1 if/else *~-~itm, *]f; PX; fB ~ • rli~ ~ff ~ :ttvff mt *¥~frffiT~m~wrlUtt~m~~~~~~~m~.~1fu.~fflT~~Pu~re~~tktr ~~~ftM~. ~*X~~ftt ~~~M~c.~~§~m.~~~~~~~o
0
0

~*~~~~~TC~~~~*~~o~_~~.~~~~~&~~m~~~~. ~~~

*~Jl7f~ill1f

c ift~fi~a9~~mo

JE.

1.14
1.

t$ J3
>!'~.1U1It_(l.4 11). ffH~W, r~*BIJJliEiJB~pr~(1.41t): A. ';t- x, iIl:!9 789 B. ~. fred. l[i{ii1ffilE 3---456 c. ~. sensor_out, tt~71' - 10..... -+45 D. 1-f 10 1'-]1j~~~.~tlL ~1''7G~a9l[i{ii1mml:!9 fE E. F. G.

*2.

23---345

-1'-ti.1iJ Press
tlitt arry ... ptr,

m~ ffi 3---567
t::::()x23;

here to end

B<1¥:M-$1itt

a9ft*mpx;~ft~

3. MroofttfjiYJ*1f:31({il(1.611)
A. unsigned char t;

itJij~*~~.:m

uno... dos , tres Ift=?

l¥JiK7tjjHi:J.J 22, 21 ..

23

B. unsigned int t; D. char d; d=456;

t=Ox78/34; Ilt=? Ilx=? Ild=? Ilz=?

C. unsigned char x; x=678;

E. enum {sta.rt= 11.off ,on .gone}; Ilgone=?

F. x==Oxc2; y=Ox2; z=xoy;


G. e=Oxffed; e=e»4; H. e=27;e+=3; I. f=Ox9017;11f=? J. x=12;y=x+2; I/y=? *4. x=? e=?

Ile=?

~t£*{tf:i!1i]~~tF,
A. (x=Ox45) B. (xly)

*,JttfirOOl¥Jti~ltiE£1N. ;!t~

x=Ox45;

y=Oxc6(1.6lj):

c. (x>y)

KtA5l:
D. (y - Ox06=Oxc)

c .f1~ Atmel

AVR

5. ~rOO~J¥~&f&jgtr
unsigned int value=O;

Jfi 1¥J3t':.* il(l. 7 13):

unsigned char loop _count; for(loop _ count= 123;Ioop _ count< 133;loop _count++) value+=10; //value=?? 6. ~fiJ¥~&f&JErr

FcI¥J5tilbJ{iil(l. 713):

unsigned char cntr=10; unsigned int value=lO; do

{ value+r;
}whiJe (cntr-c l 0); j/value=?? 7. xtli~~~aiEtr Cntr=??

€1¥J5t:i:)j(iil(1. 7 13)

unsigned char cntr=tO; unsigned int value=IO; while(cntr<10)


{

value++; } IIvaluc=?? Cntr=?? 8. 9.

ft5:Efl:

unsigned char nwn_array[]={ 1.2.3.4.5.6~7.8.9,10.11~12}; (1.9 )j( num_array[3]=? unsigned int*ptr_array; (1.913) ;

11)

'lNJj;;fl:

unsigned int num _array[]= {I 0.20.30,40.50,60.70} unsigned int x.y; ptr _ array=nwn _array;

x=·ptr_array++;
y=*ptr _array++; x=?? y=?? ptr_array=??

10. fAjj~-a

c m1§ff\:~. ;n~-t--if~fl~L
7J\ ~

##f 21¥J
IfffjJt H

21-26

iXlil1i9:iUij{f,dllq:r

lWiiHWfl·I¥] >J ~~

Ill¥] ~ ~!ll $'1hM ~tE

q:r1lJ W- ti; flJ •

1.15
l.

J:.~~ ~
fj;fJit, ~wlflIUiJ\~-"i'":mJ¥, :tE~X£.aH9:~.L1T~f-~$
0

C Rules and Assembler

Drools!!

fMi;E} liP. 7J\")3: 1'-~1Hiij ~ 4 tX., a<J:m ffffl while 1ilti1tfi:l,:~, 1T~l3:1"W:* 3 {X., ffffl do/while 1ilf*1TfpUl1'-~~ 5 {Xo
for

~ ~liJ¥ , itffl

i9:it-lilf. it.~rru(J<J~~., iQJ~ Jit~ *M.~~ A .. B~ C(.UL1iJ~~.J~'.)9='ittfj.:#f~ ::'=tJ$~IJ~X£~tfj 19:~ .L c :m~iiJ ~J.lI¥.J.:Xft -tAl 50 ~ftJit, 50 ~_R 25 ~ft~OlltlldR :*I¥Jn& tE~*iEtifBit.~.(J<J;lI!r1Jitm iEtifB<J5e.:XJJ'lfl~~~~ T)" a 1t b'J:Wrf1JA1T~ ~ *(x, y. z ~ qq ffil1.l~~f£lIl): n&a<JW~x._R*.y.ft •• z~_R •• ~~1J.ft WJ¥JEtr~*, 1T~tJt*Ja.wJt" 3.~~.2~OO~~.~::'=31'-.A~~~M-~.lliftW~~,~ft~1T~*~$The
2.

*,

input exceeds the program range"

4.
5.

19:it-~J¥M7F 2 a<J 0--212 It.o 2 m.m~ 1-1112:it-li~. ftffl switch/case iEH:Dj]£~M~tl


• 1-11 switch/case

ilf£:Vit1f41PHfij~Yifijr:::J B~
tI::I

t1ti:s:j

ttl
OxOO OxOI
Ox02

).,

ObOOOOOOOO ObOOOOllll
Obll110000

*"

Ox03
Ox04

ObOlOlOIOI OblOlOlOlO Obi 100001 1


ObOOllllOO

OxOS
Ox06 OX07 Other

OblOOOOOOI Oblll11111

€'Jit--1""liJ¥*tifB}E*~.a<Jfff*mU'o 7. €lJJl~1'-f¥.J¥. ~li J¥tJtHf$:tE f£ m~ ;jf1t~ JE I¥.J F ~ -1'- 'E!~ ,qt 9i month, day ~#.J {j~L lflyeM~~~f£mc~~mW~~Al-~Rli~~B~B~~~a~~8~~~8$:tEf£m9=' ~~~~H~lli*. ~~~~~~~-~I.,f¥.J¥~~~f£m*tr;jf1t*~~~~~S, ~~ 1TCn-*~,~ H/J,j:: S ffi,m.,g± lIi:tEf£m~ a<J1!lJ1'"1t51 8.• ~.7ma<Jft~, itmmft*~.~~.ma 9. M ~~t-~J¥. 1tffl ~fl¥.IA PEl~ getchar()*~JtR ~1'-fr.t, 1I.\Jff*)3: 1'-f~~{t1l.r~]I
6.
I 0

m~ 1.10 l'l~li~~ffla<J~~.

It:A.it

c ~~~

Atmel AVR

-T tl#J n A i¥J 11:1£ 1f Itt3t

1m -* [ll; ~

if:ffl printfO

m~ fT r;p ~~ ,m jtl tj ilJIfHB ti~

:tIo-* Jiiij D A

* ~~Itfi~~~~~~~~~m~~~.*rr.~~~~o
EBiill*-Tm!&i~&'~¥r.Jl¥Jiill,
10.

OOU:#J OxOlllJiH±:!3jtliffiHJH, 1!f!JW~ Ox80 ~1±I~tl~D 'B1~ tp Itfi :& 1:B1. ffJH JHflHii! 14 tp Wi f.f - m ,~1J EjJ ~Hg.m tDtr I±I t5t ~ "

B~

'~Jat rr $1;t 1:

Atmel Rise It
2.1

II

~*~#
~a*$~~~, e&••~~~~3~
• J.E~ ~:m~ RIse ~¥-l1M~B'lfIH*,. #tf-iJt'8Et~n~·#t~rEij~*H-B
• ~~~jM~Btl~iIl7tftC~~-Jm § • :(£ i~f*=' ffl ~ WI e 9 if: • tfiIj ~ c .f~J¥ !J. ~ ~ na if! sq I/O {f:* • Bffl AVR tI¥-l1t~~I¥J±'~7rW:*f&¥~~-Tii1t-&9* • fffifjO £i e lMF¥*7E~ trim ffi • OOii AVR 1CiiHa§W~. #mll5E;tt~~

~ f~!R

2.2

511; t>tr il&~ JJX;J}j:tt&mffl Atmel


Jfij ~
0

~ ~ U! 141Jf jJ{: ~Jl iJ~~ ~Fiit:fF!fJM~~i~~-t-F

RIse mt~~~~*MtJc Ill]IiiBtl:rJi! ~ffirm15. *llm;-m jc § 7 r}j:tt&ill!ffll¥1 ~trm;~: 19: t~~, 3;t~~1ft 7 M~.mii ~j!#;m iR • mt IRJ fiB 11'] i
A VR

$ffl j~ 19: N i{?; ~ Wi ,blli ~ liFo, t$ -g- tl .L J1JfW sq;m m,

!J.@~*~7.#M~~~~~~.m~, ~~~~~~~H-M~o

*.~~:m~T*RB&9~#~~:#~M~~~~o

••• WE~MT~~H-#t~~oo.

rlfj9=i.

~47.~+M~~~*B~~~.#sq~.*w:~n~,~ffimOO~~~m~*~9=', ••
~:(£-~~~~~m~~fi*&9~., ~~&~iliT~~ffl~~.~*llM~~*~&.Tk m. ~~~~~.~~~#~~~~Wmffi~~o

Ult~tHm* m~~ 1It ffl ~

~ Jt;Effl

••

iUJlii § Ertl!Jl: ~ ~
e

afiM*~~~~
mill

*
0

ftC

lli-f ~

Atmel

mu~ ibtl3 Ertl~


0

# 71-19: , -§ffi 7 mr(J<J Mega


Atmel

ilf~

31tl $14 J! @ijt 4- ~ Btl$ftt-:mHf ~ 71'Btl7r19:~~~~;fljffl9rnt*1r


~

lit ~

Atmel
~

~J}jfj~

-*~PJTmf~EfJt1t~fM~J! tf5t BifJ£ RIse W:1ir


I

Atmel AVR
0

RIse W~iMBo

*ltfrm;1t#*~;fiJe
eomputing(.mfijjitfH8'-+~
I

RIse {"Ve: educed R

Instruction Set

-ItXAA

c tiI~l:;J

Atmel AVR

~1'fijf*~ml f~+~) -{:gWtJ! iJt, M~ ~ 4'irIj,f~ffl r[ Et~m~~(Machine-level)m+. rmft DJ f1f M ~ 11J!iJt ~ Jl:b fi mlI Ef.rm z.Pfi~ fJ$m ~ fi:l!J:l, )~dNAI~~ ftr+ I¥J .~ fir ~ f{ § J!tk + l:.1. ~ m :l':d> I 1Lt$m~ DJ ~tE-~5tt:I..t~I?tf!f J*JM ~ Ji;J:£. :m*1tffl IPS{Millions of Instructions per M Second. m:tp Pfi~1T Ef 7J *m~f{)*OOtit.itJ:l, E8 -1'fl BMHz 1J>ff!f~* B<J VR 5tt:I..tft. A PT~tE-l!P#pgth1T TIJJ*ft:bA<Jm~. if?~J!#f:i5: 8 1'- MIPS l¥J~ltc 8
0

Ui

~~, ~~~~~.~~~o.~~.~~Ift~~#~MWm.1'~Uft.~~ ~oo~~~M.~7.~M~~OO~ml¥J~.m*.D ~~~~J:£~-~M~~, T ill---tvm~. fm:h!*J'I/O W4'irEt-Jw;, iet ~mfF.i~Er1lffl'iijmIt5~mntfr~j>o


{jt{tii!14~ft~.,*~tEm~it,tm~.~m1!tI1JfI!WitA<Jm~.

•• ~Pfi

~.Jt~~

M~¥-~~~~Am@m.~m~~~~~w#m*A<J*~e~-~m~~~#~~*fift
1-tB'~!lt3!l!~~. ~ff. M~~;#W;A~- -~ 1/0 t1jL$'~~ AND~ OR, XOR M#;ltft!rm+$:!&~i$<ft#ij
~~P(EXtf;lt~1IM.#~)~flr)i;{f[Bt.

.~WBDM:m

1t~it~ ii~~~~W 1/0 .• ~~.,M*~.~#fioo_~ #.~~-mB<J~~m~oa~~ ••


~ B9TIlo

Jiffl

U~oo~~w~rep~A~M~ttB<Jm~~:h~~~;
PORTA
=

PORTA

IOx4;

I/set bit2 high

MMUifi~m~~m.~*.~~~.~~fi~~.~.m~=#~M.W~.~~~A<Ja
f-t~;

~~~~mmB~ItE~fi~OO~~~Bt.
tJ;111Hl~;Jj~~:
IN

~*.~~.®.~M.m~~OO~~Et-J~~

R30.0x1B R26, R30


R30, LOW(4) R30, R26 RxlB,RJO

MOV
LOl OR

OUT ~~$~ft:
SBI

'E71':hW71'~CPU(Centra1 Processing Unit), fffifl~® I/O~ CPU W?tJ1;:ftffJlj~~a<JW?tJ rm1Tfi#~~ I/O $71' JEm;ltm ~OO, .~&ffl~*AM7Moam~~~*.moo~~W~71'mM~.-~$~~~M~a
AVR

rm.~~.~R. -~m~~Uia~CM~~D~~.~ •• ~~.~.Jtffi~~~m~~~.~5ffi. W~~ .R~;ltI~g~~~~.M.~.~~~~#~m+* •• ~fr.Jt®~&~~a


,g.OOi5BIL Atmel
19:1iEt'1~#;J~~-t-.~*tl.I¥J~;MJ
D

~OO~M~~.

~*.~~ ••ffl5~m+~re¥-Q~.~~~~.

OXlB,2

~ 2.

AtmelRise ~ftfi

• 77·

~ ~ iW?t;IE;t 1ti1¥.r, ~ ~ 11J Iii]I¥.r ~ ~ jilt


Code

Memory)f{l~m1¥fitft.(DataMemory)~1lIJ. -rul~1I1fl EEPROM, ~{gni aEf1~Do ~~~1¥M.~~m~~.31¥.r-ff~:fi*~~M~.~~3~#fit3~~~.~, ~ffi~W:~$IJ3 ~Ji$llffl ~


0

*~t'.!

ff~"CPU 1m:fi '!iUL: ¥.i g I FLASH ~pq1¥fit a(FLASH


EEPROM

1¥#I ••

2.4.1

FLASH

f'!i~fffilH

FLASH~~ff~3ifi7GJ!-1-tt!&:IaI::.9 OxOOOO ~ FLASH(fAJ1¥}t¥Mft.. ffl'tlf{J~:l:EHA l 1*1¥.r Atmel AVR.~*IJ3-tk:Jj!o FLASHf\:pq#~a~~F!lr !ktt(non-volatile)1¥~~. ~p~ Et!i1j 1ft iJJ ttn tI. Ja'. ~1¥t£ _m 00 ~ ~Jmill~ ~ m !k fff ~ 1;lBtffl * 1¥fit rlT ttl.tr f\:~'lU Ii;l :.9l! ~ ~~~~~~W~E~~~*1¥t£#fit3~o~~1¥~a~~$~fffit.7G~16N~,.~# filifi7GIlT~~iW-1' 16{ft*l¥.im-B~m.o 002-1 ~t±I T FLASH~pq#fita.7UI¥.r#M3~M ~
0

*:1;:.

f+- t;II;TtM~*JH~m_

fifilHl¥JJ](ijI,

$.

..__ ffMl-r ox 0000

M.nAmf\:~1¥Mfi£m~ttl.ffft~~~.~~~. m~~~m~~ffS~*M FLASH~S. &;'~lljfij;1tim~:1Jt!#;iSo 1N.LIt. JAS~ 11l1¥.r1bLt*if. 1:::R~-1'.R w:e<J?+fiI alfflB" .~J:., 't-tE.R~m*fffit1tT~trff.pq~~.o 'ii.~*#jIJ FLASH e<Jlf1fll. ruT FLASH 11:pq1¥fitft.ifi~*lll¥.it&~. ~ §l;IJi1k~~px.~(int)A"l\~5P.:ii"
2.4.2

1l.1'-¥filH
0

AVR1rl:Jma~WrM1¥Mft.-A2:-e!.ni -1'fft![lR:ll:I¥J~/13#~1K 3 1'ijifflIi~*ffft.. 1l.~ 64 l' I/O ~#a, z..J§'Jii.t~P3$ SRAM" ~fflI~1¥MB-A2:fflT#~~~mrrtt~~~~$~:I:~~im~If1t'.!I¥.r~~.
Atmel

.{L\:;I¥.iIRJ:i£~ 32 ~~~~

·78·

t$!:AA c f;t!fIi Ej Atmel

AVR

!:1ffl T' -fffit± ~ 5t:. 64 -t- 1/0 ~ * H 9W filHi=:1#1tts! 1: 110 ~~.fJl:91- ~ I¥J 1& ITOP3 $ SRAM $J H tl JilIJ i'F jg illffll¥J 5f:!f fffir ~ rEi) • lil] B1 tk ~!l.tW 3 ~ ft~
0 0
g

~~fffit~Enflj(MI!I:WlOO2-2

/Fo

.,..._ SRAM QIJ J9j:ljj •

$.lI/; 'T

Af$rJViH2:t1J~~W.

I+-SRAM :7fflb'T
~;;§ 'iIf#~(SREG) ,~;Jt 64

1'-'llf.fHifFJJ

--

Ox60

uo ~Jk
Ox51(I10

+lfl'M1f.itilUII.tII: g<f.ff-H~ Ox3F)

110 d~lJ(Jttu ;t;{t jJ.J -Sf'di


ltff#31(R3I)

f4~

I/O

.til: Ox20(I10'llH!fa~J Oxl)

7f!ltif"it 11iHf fill' ft.tta


OxOlF

iiimii1fa~!:RT

321' 8 f.ljmJF.lIfF:~#~

~.ff-" ~ffH

I{RI) O(RO)

14---

1. fW#H

mmI~.*~~mY.m~M3.~~32-t-¥n.~~*ffft.~*.H~WI¥J-fffit. j[; - ff, 9. :l!iE:f. 00 ff \r 8'j9X ~ ~J fiij 1¥1 itJt:~ !'L B fn~ Ii'f Jij T -ff iItI ftij $1e., ~ If1ffl T * {¥ M~~~~, ~~fflT-ffMm~*~B~ffl~-fffir~¥~~m~oMffij~ZI ~~ft~~rr~ ~B1~fflg~32~I~~ff3.~®~*ft~~ffl-.~C.~.*~~, ITO~~~~~M *1Qiij~~. ~~~frh~fflK:~*~· ..
2.I/O.~H

~ff3!i!ll ~~ T ~ffl~ff.uE!fiEtJ.iWi 64 -t-~l1l¥Jf{~-fffit.~r8](~~OO 2-2)., ;Jt~ 89 4ij: -ff3 m~ fJt M~ ~ $~ i*J $I¥J 1/0 :Sr~ ~ f11M ** ~ ~~ 1m ~ ff~ I¥J r~ ~ff l1l ff. ~ l1J (jJ l21.*~~ffl I/O .ff~*~ jgil£:!ijjiJ. EnI/O :SrHi ~J:Jt r::J. {Em 1 jJtJ:l, ftfrH:.~1&~i1>fllffl 110~.ffH89~FF PORTA l1J DDRAo :tEJ~~JlliiJlFF~ ~ [/0 if ~~ £* TiE ---i'#tr ~ D ~ A~~IOC~1mEtJ :« ~ 2-1 m~:t;.fJl #tr$ffil tl ;ffl~1If(:e::p I¥J1 1 -t- I/O ~-ffg~~~
I/O
g 0

-t-.

*
I/O DDRA

2 - 1 ~ milO If1'=fB
SRAMi1t±.lt i.jt Port A Port B P}I

IfffH~

1/0

.1'=fB i'IUIt

OxlA

Ox3A
Ox37
Ox3B

~ffmJE rJiJ if1f:a I¥1ltii~ rtu ltif:M 1¥.l~ If!~ 1'¥=Pir ~A~JM41

DDRB
PORTA PINB

Oxl7 OxlB
Ox16

Port A
PortH

Ox36 Ox5F

SREG

OxlF

~Jl~~;;ii~1'¥:ft

tij-t ]/0 *:ff:ftW~-1'-;g*~ ~oo*:ff.;g*~~~~~~W~OO~M.


~£J§ SRAM

JiBi1I::~-1' SARM *fuitl:., i!~~FF 9!l:lM.1f\'~ffl C m1'-~~~.m*fuitl:~#~.¥OO. ~~~~m :It!!.f1J:#LXt¥J. mF~fjl-b1~~tnJEl:§ JIO JiBhlffi*OOotm*¥7M:ff3k AVR m+~
I/O
0

-1'

BX;~l[~iI~OOi$fHlPsJ~. #~2.111L ffiMr c ifr~~J¥ In mi1!i, I/O *-ffe!¥.J~ *ffl~*ttJtght1W1.ftll! {Eb~, c 1ft 1f~if. Jt~~~ ~~j;lliRJS @~ *E8* )( ~& 't::lDJiB:W:!¥.J ~~*~ 00 4ij1'-Wf¥1W¥-§.1f --t-#include ~~#, ~~~#~~*.~x~~~*~~~Z~ftOO~hlo ~~#~~w~ml.~B~
0

W~JiB~fl~~7QrWOOC*~~f¥~~T~W~~Z..~m7*:ff:ft~*ID~~~Do
# include <90s8535.h:> maint) { DDRA=OxFF; II all bits of Port A are output

DDRB ""OXOO
while (1)
{ PORTA = PINB; }

/1 all bits are inut

Ilread port B and write 10 A

~ooOOWJ¥lUmPINB ]/0 *ff:e*~A'3I~ PORT B aeJ~j~. ffl PORTA ;tfff:.*1E~* ~ttlJiJ Port A Ef!J~W.9:r" c m1§~*emmWFFm 1 trB1#include ~X1't·~ I¥JJER f£.ffl ~f¥. :ft;g**$ltffMfi'iOO;tfffti*fuitl:. *iJIJtp~ 90s853S.h ~flfo ,~Ef.J~*, c ~§~ife5¥tJffl I/O *-ff~fFj;JV1ttQ*tl3Pim I/O e!#ii~lJ<J~D<> ""fWi4 4~I!Jj~tQ$lJ~~4ij-t 110 7r~ !;J.~~'"tff];fft~1¥J 1/0 It:ffel¥Jfflrt;;~J}Jfllie
3. 5 RAM

SAAM:ff•• ~~~ffl*ff:.W~~m~:ff$~.we9:rOO~.,*m~-ff.~~3n~D SRAM f¥.fitH~~~OO 2-3 m~" :W1l!J2-3 PJflJ' • .ff SRAM 9:r~&fl~jjlJErJ-ffM::lRrEijyJ(;Pi:ff7ttko ~m~f«h\ $H~:ff •• W~~.!¥.J~~~~M:ff •• m$*M-fffit~o&.~~.m-fffit~~.~~ *ljffl:ff.eQ

SRAM

Erl/t

I&:A~

c t;;~~

Atmel AVR

*- f JU*~IU~*~fi~!r!

1;1ift~ (iij ~J J]j #iii.

lfO-It

I-+-

ffM'i-f-

ox 0000

:rufF:

~WHm.~~~.B~~*~~~~~.~ili@M~~#.OO~~M*~~~.MOO~ ~ ~111¥J fi~ ~f-_t. CodeVision A VR C i!13~~3gl;mW31'-:lt~: ffi; €1f:lt~(System Stack) ~~~ft~(data stack)" *€1f:lt~M. SRAM jg~l¥Jmi$Jffla, T#1itffOO~~:il&@J:Itk~~~ m ~3~~~mA; ••• ~.ft~m~~~~w,~~~~~~.~I¥JI*~~~mffi~I*, m~ff •• H.m, ~~~~OO~$~.o S~Mfi.~.~m~~~~w~~, fS~§~~ffl~~~~~~~~~Te.~~~~ *~~.HffM®fflmlli*l¥Jff •• 2~ ••• ~~~~fflWili~.ti*~~~-~.B~ LIFO(Last in First out. J§":llBt 1±I)8<JI*~it;I'F.*'TI¥Jlt~(:tm t 1IiJ-*ifl¥JiEt)ftl:lim:~~; mi _m iJIT mt:ff I¥J (fa ~ ~ ~ T _til .t;008<1 Jj~*~) EJ l-!1.M..~5!'tl1±l •• ~ ~~mft(Stack Pointer, SP)t~ rtJ ~ mr~ ffl 00 ff 1itiftLM~ 37 ~~mftA~m~~-1'-W~mT~ •• ~.~I¥J~.ift~o~8<JI~~it;I*~~: (1) ~lf*'TOO.~~$j~~1i.~ Ifit. ~lm3~;fUm SP *t.i:llif¥.Ji'{fi.t¥J#1it3:1t!!JIl:" i¥I1:Wl ~ ~ ~ ~ WJ I¥Jp ~ W ErJ 1i;lE 0x200, !itO~ ~ g.~ ht19: SRAM ~ I¥J x200 ~.ff S O Jmhl: 9=J (2) i3:Bf~ SP ~(1i£d>jtl Ox1FF. ~1f1~3!!fi:JfMi~tr~ ~-t-m~o iti3:frI!1J:a.:~, SP .~~ .ff$.~-~~m~~~~~.~~ff.HM~. it~€I¥J.~~. ~lm3~~~.R.€-~l:lim:~~~~~mo~~.I~It'FM~
0 ~

*"

~~WBtRf~t!KIfimp;;* JA.~9=J5!'tll±lli~lf1. SP 8<Jil:1tJZElPM1m 1. Bit~ SP l*J11 SP ,~~m~ r-- ilJm -1'Tf¥.ht:lt~fi.I¥J~.3:1t!!JIJ:(2if~), M:lfU~Jlift;fJ[] it 1 Z€, ~~~~~ffispm~~~~L-~mT#~lt~~~M.~o ~~~Hm~~~SP~8<JJmhlMft~~*ili ••••• ~~Q~~.~M.~~8<J •• c~~~1'-N~~M, XMhl~*WitsP9=J, ~~m~~~~-~EJm~~Mift~~m~ .~l¥Jtl:tF"J"~~OO-4 WT7J'o 2
(I)

~ 2"

Armel Rise ~nB JA:Iltl<F_ f±\~~,

• 81 •

tJJ!Ih*#-,

~_~:

~~~~

....---:---:------.
>...J..U!~~

S~

~~...__._.joxJSF ~~...__._.jOX25E
~~.........joxlSB

.Jt;;W:
O>:25D OX25C

~-~;

~~SP
~

~~M
.
til

SP: ~X25C

OX25¥ {)XlSE

~
.
:1'-'<1'

ox2SD
Ox25C

~~---.-.lOx25A

.
.
,
"

...,.......'"""'-----i~ ere,
......... L •• _. ... __ ...

: anilabk

i ore.
,
7 _ .... "

i'lx2SA

QXJ5B

.t

..
,

. .~.r' '...__
SRAM

L~'

.tI!!:bI: sp,
~X1SB ~
newm.ta .

OxJSF Ox15E
Ox25C OX25B OXlSA

OX25D

~able :..~~"I,W"I."

etc.

it.: m1fU;q

available

iJ(J.!l1 j[;~ ~J: • .ff~;q ••

, jjij::r;t!i'1EJmt!~II!t~attHE ~lII::I:. 1
2-4

:'tE;!l!;,t

mrtIIJ:liKJ'dl" iff tIfJ~ ••

m~~~~~B~~~.~. m~~~.~~~ftW~A~~~~o
IIJ

;lfUi if!ft:

~itii~~ffla<J~lfit. SRAMtBitffl~ff:fijfjt •• ~i1~}y\ SUM ~Maa<JJ(EtlIHf~. rEJ J:l!IfItJ .~~f;!t~aaq SUM ::k'J'R:lfml¥J. ml?J~~ffl ~M~~f!trI1i:ltfX-1'~l!If£l3~::;trtJ r. ~~~m/F~#Z9JJlJ::;t~~, l?J1ro"l1:~fn~B::m:~lUTvt, :I&~~F'M'.m:~~"mm:~T ~
0

~•• ~ili*.~~§.~m~~~OO~M.ft~~.~.

~~~~~~~¥.~~~.~~~mffi*~~,~~~SUM~~m~~Ma~~m§ 5t::k, )}.. RAMI¥J $ lffiil !trr M f$; ~. ~ILIA ~fX ~ 3iIi tfHIt. ~ 7t itii~ S JY!

5flJ ffll¥J ff:fJl. ~

~~ .•• ~~.m:~T~m,fi~~ft~~~~~ffi~I¥JM*a

2.4.3 EEPROMfffilB ff:.3 q:r a<J EEPROMJR~~-tk r:tJ J?1 il/~4~~ *=ii~fJl£flij ~ jg~ -e;'M'lJim *ff$J!5 jJ,t~fi1i~lj.JS:lFfm~*=~~.f;!*~a¥&~)Em ~J!:m. EEPROMI¥Jlt!tIl:lIl:~ OxOQQ, _*a<J
a

~hlm~.*fmffl~A_~~.Ba

~~oo~mAA~m~~E~ROM~~R~*.~~~~~.~~~~~Sff:~~mBml¥Jo
Jft
2 .lJ'iJlJIJi ~: A 5~

1i~ EEPROM ffiit.~~il~a<J, ill.tll&ffl'8*ff$:-Ri~~:II:<> 1A:~ffif/g EEPROMa<J ~Dffl-~.8~~~.-~*~.~~~~~<>*:II:.m~~ffM.*~. jt:S:~fl::kW1t~~f~!ll::@:a~~gL JR1~, ~. 1 ~B~tllJtl:i1T, EEPROMRfm~~lf~

~.~$*~.

~~~~~~m*~-~~.~.2~~lli~l¥J.z.

~R:~~m-w*<>~~~~~§

tt'*AA

fiij1_ I:'?j Atmel AVR

~*~fY. ,-~. *~~1'- 2 *~*.IPfI¥J~1m. ~}§~~m_t~jtl PC trt9=Jntrr7tfJT ?±:*~~Et.*I¥Ji9:itrr. ~:ftHgjctt~~M<ttP*ilH~'} !~~ rpm(Revolutions Per Minute. ~]}fJtl~.). ~m».. -1-S2~@~9=J~te~~tftff#Z)iJ$dJJ:Et-J1iI11*~fi~*ic3R~ i;iJ *tir ~ rpm. 11~ -~ ~!Sit 1£ f{ {fir ,1: 00I¥J RTD( ~ ~_[:a: lt ~~~ ~)~ ~ ill} iG JK iJI$IY~m\lt ~
*.ftREma~lf1f-Em.m.
0

~~ru.~.*~.~*.*~, ~~.~~: fX ~ Em ::gtR~ -1- £ T Atmel Mega 163 ~ rltlJ ~ 89li 1m:*.~ m 3t ~1~ fiit ftRM iJ! . W ~~~~~~*ffl**.-~158~mfi~.~*.~.m~om~~.
f@J
a

[Q]

::g.~~. ~ ~ -t- ~

rr B<J}j~-

~$ftR~M~MAa~M~~*~.ili~m~$~~.

W H: f.&; IlJ I¥J

{§~Mf~ ;$:~

W H: 1'-!if ~

,m,

!tI:h ~M: nt. fJi ~ J:-. T ;;;1$


0

~~*MOO~~~R##.

m%
Q

tI!~!IJ89 ~~~~;EJtpX;~. ffEJiJTffif~1~~Jff,ft£!AQEJ-1'-~~-t:*~~m*1E'BffJ:i!J!J PC ;fn~ ~ f~ I¥J tt ~ ff J!~ ~ - r ~t;F='~ R: sjti~~~ I¥J ~ i1j -t: 7EJJJG l3:~ I fF R 5t;. ;& ffJ 51:: tI! Mt· - "F ~~~~ N 1¥~fi fit:/G~~f!W??J.!}IJJiJTfl~.mfD5ll:W~~. JiJTJ!:ffntIY1~1mmt H::(_f}i§ *# ~. ~!Wi -ml¥J.m 89 ,g.1"t1 , JiJTB' JiJTJ;~ ~ jj!i tll Em U 1Wi •• 'F :

*~

~*.
I

JE~*

~t.~

X 60 .tV/]} X 1 mlty

= 120 (~_[)

Jiff tJ. f&.H2E16' ~m ~


10 000.
~;ijt~ rpm

~ tJ1'-tim m r!:i ffl89f

~1fiifO~~ 1 rpm.

mm:!9 1000---2 000. Wi.iJi'~ffHiJrji~ lOPe

M ::gtR~ 1I!tit ~ ij f if EJ<J rpm illm j;J 2 000 ~ rm'3If1~J~Ji'm:~~ 100° F--250° r, ~tR~* rpm
I

11.~

~~.#-m~m~,.~M~1'-~m.m.a.~~~mA~~.m(~~.~~M:h~
l'f'l1. ,g.jt 41'-flJ): 1¥M3:: fa] :kJ :
120

ffjj:h71¥fil~Lt~1m. ~1Rf~-1'-fi1A/J\89~{M"

mw.j'~,;tt:rm.1¥J

m X 5 f11 I~ll=600

*11
40

~~~o*~~~~7m*.~M~ •
12:~~Jitl~~~ft~ ~

~Jf~~~89ii.fX)" lfl#J Megal63 ~~B~4=5, ~nt~. mU~OO~M ••• .,

finh~.*. ~.~.~~~~Em~.*~~*~a-.~*.
~l~JJ1J,~~P:i!iM~:ff
1 024

~Tli1mff~U9r.

f~Ha!ffli.*~ 25 ~f'l1~*i5t:l#.~fO*~

~ft.~~o.~~~~~~,
~

¥~89tim*li(Ml.t; mm.~89~$~ ••

f11£r-}
••

SRAM

~~~lf-r*t

•• , ~M~1'-*~.~ffl~mfi~ •• J:.

~~~*

~~&£

~.ftMl.~B~W~M7, ~~~~~~~~#M~~~o*~I¥J§~.~m~~~ ~1J1!tL(~·-~~~~~J ~$1)~tmfllJIfF~ ~ ~ Wi. ~;g JiR5(' gr:iJ!: ~ ~~w.~ru:t!:~If{Jfj~¥At. #iJl~B~~lJ-l".pWi H[i~~ff (Inerrupts Service Routine, ISP). ~BIl ~ fi~ £;f1j@: I¥J 9=J .& ~ at .~tr ~1.I: • 9=J ft 1ffl~ ~ ItIT

*"

m: 2.

AtmelRise!1J:J!lB

·83·

~ •• ili~~~&~~~.~.ftm~~~~~.¥~.#£~~ •• *~m~~~~*ili. it~ aq ~ m*u ffl tp ~ JUUi* ~ rHl(tF fj W. :!Ii] ifHg -it ~ ~ m) , 9l ~ tE~ ~ tw (5L I±\ & iJll=f Ifj I 1lT a-1 t+1Il ~~m~~~ •• ~w~~m.~W~ffi~~m.~fio ~~tF~mm~~~~fftm.a.~~n~A.*Gw.~~.~~m+aq~hffi~. A~ n jJff!tR:: it.aq":tul:1l ~ ~ W ft~~-R!I R::i!f ~~ 1ft~~~ ft fn • m-1-., -#f~ Jl111m ft~ 1:: I¥J li~#mRr. ~m.*~w.~~-~~~ .• ~~fi~~~w#~~~m.m~~mJl~~ *B~.ml¥J.D~.~~.~*~R::~~~1-~*tp~.l~ff:!li], ~~.~~~~*nI wtp. -I~.m~.~~~1-~~~.o tplfjaq~m. f~~ftM~W~~¥~*1::.~ft # m~••• n •• *.-R!I.~tFM$I±\*o ~Ifj&~m.&, ~m.*~.~~~~OO~~I*~c.1-*~~~-~m~~~#. fi~hl~~.~~~.~.~tp~m*~~~~~hl~ffi~~*m.+~~ffi@aqtp~~.tpD ~*~~~~.~~li%~7~mtt~~traqm+m. ~~~~~tp~ffl*~aq~~~.D~
€. fi~th.trffi@l~ff"2J~m+, WIi*fIJ tplt'Tijll*fi~f\~~#7ftra~tr '£~ ~. M! @] ~:IlI:(1A;ff ji; ~ ~ FPth.tr 'F-aq fI!!:IlI:)~ ~ # Jtl ~!1f. tx~ W ~ ~ nil aq.J§ ~ i!iU R::ETI n=f;ij + i!~m+!if.l:iS §J :itB:IlI:(retum address)JA R iif Bm f *!1f.~txtF~l±\, ~~~~~tp~~~~H~~~th.tro

*wr *~~ *"

*m ~

~AVR~~~W.*.m~tplt'Tntt~~~~-~~om~.~ftWGM*~~n~ff ~~traqtplt'T~~,-~tF~W~~ntt~~~~~~o@Jl~~.~n~~~~~~~~ ~ ~.~~.~~~-~~~.~,~~~~~fi.~tp~e~~~,~1-~Ifj.~~:!Ii]tt~~Q ~~~tp~~I3't1t~~, mJ~t2:+~f;ij%jti].~tp wr~1ft*fJ;1.rr ~ W!BI ~~-ti1i~~ tF~~.~dA~~.~~.n~~~~


2.5.1 I:J:IIJj

2.5.1

IJ'iln

~~:YlWfifffiJEo tFlt'T~.~~~~~~I*,d~~~~~ma~M~~Ifj.-~~#~M~:.-~. ~~~I~~tplfjaqm~~~~ .=~~mm~~~~M~aqtF~nffD ±:1$ ~ ~ B<J J#-1Fii~:m: ~ 9J ~ ~ ffl JlJ ~ ~ 1M~ #fiaq iMill: 11tI:JiJ 1" jjlm Iff mfi( General ~ tp Interrupt Mask. GIMSK)~~fi~ 00 2-5 m7l'
a

*- 2-2 ~ili~~tEAT90S8535 **i!Ht9~lfjil*mr tFlfj~ltJtM:ltn~jf~fIH~iA 6 +.

OxOOOO OxOOOI OxOOO2 OxOOO3


OxOO04

..

i:I!! .tit

icl ••
1 2

iff
jttlq:.1f1j

it

it 7~aull{!L.
INTO ~ IjIfI i¥Jljj!ft fff -if
INTI

aI,I

_tlt!_. Watchdog

1i~

7H$q:.*T
_7Hlll ~ If1T
2

0
I

3 4
5

i;l1j$tfJil#fff~

~itll1ftl:t~

2 -iJit~ftll1l3
2

2~itnt.~1±l

~itll1a-tI_l±I

·84·

iP:AAJjfi_!::jj C
Bit 7 Bit 6 Bit 5 Bil4

Atmel AVR

Bil)

Bil2

Bill

8itO

~.ffl*5i~5'Hffi9JltJfl¥.JgINTO tL~5i*;r$9JWi O(-1'"~H~f!~JJ.!f4w~. tiil iEffl Ti"i#iB:!/gNTO t¥JSIJJ4J). I ~fffiil INTI {fr!i!IJ5l:~;'H~U9JWi 1., :tm~m1"tltH!LmW:jfT. ].l!1j ~~~1'"9J~~m~N~~~~~-~~ffl~ ~fl~~a<JJJ.! ~ 1±I!JI!.7£ R1. fJlJ:Wl -5 JE1ll ~ 14ffi [NT 1 mffif GIMS K 9JEt:J INT 1 {fr1frll!. ~I¥.J ir :Wl*~~JE 1. ~--ttpltJf~*tttR:&~ {a:N;:tm~ INTI ifrfitI1g O. ~i~:Ula<Jil.~I¥J1i *~.~O. W~~~~~*~~~.~£om~:!/gT~.~tp~~ff~I~. ~~.~9J~
GIMSK
~~o

5l: l{J ItJT t¥J -= Z:f7 YJt~ 19: il tL -T ~ fI.s* ~ ~ ffft-(Status t.f m Register. REG) r.p I¥J 1::fi:ij tp~ x S ~11L i&-;!v~1znr%J&t¥J;
# asm(ltsei")

mifiHtL

~-fl~~gm~#c~g~*~~trH~~tpWix~~I¥.J~nD~-tr~~~ffl7~~ m+#~m,m~.~~a~m.A~~*~m+sm.~~~m~m+l¥J~ffl:N;fiI~~$ltJf 5l:~~~ 10 *~£~R1.~~M**~~~®~~~~ltJftl~.ff.s9J~$~m.m*ff~.~~#, m~a<JM~~1ll~~~Wix*Qi1ffi~.~~.g:tm*.€t¥JM~~~.1t¥J~. ~m9J~. *~~mm.&"~~.s~7£~ltJf~.~~~~~§l¥.JftW~~~rOO~~M~ •• ~T*. $~ 0 a<J.ffl1!:


0

#inclode <90s8S35.h>
interrupt [EXT_INTO) { PORTA = PORTA "-Oxl; } void main(void) void ext_intO_isr(void)

litoggle the LSB of port A

DDRA=OxOl; GIMSK"" MCUCR= Ox40; Ox02;

liport A LSB set for output Iiset the INTO bit to enable external interrupt 0
Ilset the ISCOl to activate on a falling edge

#asm("sei") while (I)

lIenable the gJobaJ interrupt enabJe bit

lido nothing - the interrupt does it all }

~iJ~~J¥*tlm :9~tfflcplfi *~u.:iitt?± Port A 1: 0 nrl¥J LED n- 4ij~it~:tE INTO ~IHtPI¥J 0 #(mlBpl~·*Bt.art A 1: 0 1fL1¥! LED $t~~It9* ~ 002-6 f8'ili 7 ~~J¥;ffiS(flH!{ifiittlll~ P
0

RESET
)(TAI.2

~AOC7)PA1

(AD06)PA:I!
(ADC4)1111.4

XTAl.l

(ADC3)""3 (A.C1C2)1'A2
(ADC1)PA1

AREF

Ava;; .<iBND

IADCO)PAO (MI$0)P98
(SCK)P87

c~

vee
eND

~~

IAW1)Pm (Tl)PB1 {TD)P80

(AINOJPB2

{'ro5C2)PC1 {rOSC2)PC6

PC4

F'C5

PC3 PC2 1'(:1 !'CO (ICP)PDII (OCIAjPOli


(OC18)P04

[0C2Jf'07

(lHTllPD3 (INTIl)P02 ("n(D)POI


IRXtI)POO

~p

00 2-6 ~1fif'1J¥tiJ!#i!~r¥I

*t!t:i!1'"~~B1.
(fH~f~B~cpJ6f[ilJ.Q

1t3l.~~~~m I tr~#include j!~lfI¥J 4 rr~J¥14~_9rWqtltJi

iftiiJ, fflT~R AT90S8535 tlt~ft~:H 0 I¥!cpltJim*~J¥(lSR)o ISR ~~I¥J~ 1

1T~w.~mf

interrupt

lfM1. :igR:i:7~tMi*BJtm3fl¥Jm~~-1'-cpJ6fm*~J¥"

fAfil3:iitl
RET 2e

m~~*.

~~mAt.mm.#:H(fl~3~~~~~~~*~~~.I(?±~~~*~~~.e~WffiA iltt. ~ :~nr-t'}if ttHi2 rfl.fi:W 1 5l(lili B113: 1'- ill~ &:. ;{OH~J, RETI -1'"

*). ~

m +~JfL ffij;;r: ~!;l

[EXT_INTO] ~

rn #include Y. {if JE R I¥J :it H W:l~ • I


••
Q

{t~ 2, ~

jg:9r$cp J6f0 I¥J JfiJ.tM %~

tM~:H~ffl:ig~~A~.*~.~Ifi~ 1OJt.fUHltJl ~ ~F J!ij -fF

*~ISR~~OO.~~hl,,:ig~~~~~I¥J~.$
Q

~ ISR

Nff(flm

3 tT~-~fl~l¥J1Atr~iU Port A

4ij~~~th.fTlff,
e

14m OR

If{JMfEHfL

*iIT"*fMlu.: Port A
GIMSK

:tE mainOfEJ~l¥J~ 1 tr.

(flM1I£tfllttW:IlfF:1J*8U ~lirfl-fTftli~~W:. ill

*~m~~,~:tE~~cp~~~m~lBt*.~J6fOft.~#I~oN~-ff~W~ ~1tIJ'fif~-B(Control Register. MCllCR)i:fJ ~CPJ6f~J1ii!l.(Interrupt Sense Bit).

{j!;~$r:p~ 0 Fmltt

••

MCU

You might also like