3 Introduction to Micro Controller

  • Upload
    mygodsp

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

  • 8/12/2019 3 Introduction to Micro Controller

    1/42

    MICROCONTROLLER- AT 89C51

    MICROCONTROLLER:

    The main controlling unit of the roo!e" !#!tem i! the microcontroller$ The mainfeature! of microcontroller articularl# Atmel 89C51 i! "i!cu!!e" here$A microcontroller con!i!t! of a o%erful C&' tightl# coule" %ith memor# (RAM)ROMor E&ROM*)+ariou! I,O feature! !uch a! !erial ort!) arallel ort! )timer,counter!)interrut controller )"ata reui!ition interface ) Analog to "igital con+erter(A.C*)"igitalto analog con+erter) e+er#thing integrate" into a !ingle !ilicon chi$It "oe! not mean that an# microcontroller !houl" ha+e all the a/o+e !ai" feature! on a

    !ingle chi) "een"ing on the nee" an" area of alication for %hich it i! "e!igne") the onchi feature! re!ent in it ma# or ma# not inclu"e$

    An# microcomuter !#!tem! reuire! memor# to !tore a !euence of in!truction! ma0ingu a rogram ) arallel ort or !erial ort for communicating %ith an eternal !#!temtimer,counter for control uro!e li0e generating time "ela#$

    VCC

    VCC

    VCC

    P2_0P2_1

    P2_2P2_3

    P1_7P1_6

    P1_1P1_0

    P1_3

    P1_2

    P1_5P1_4

    P3_7

    P3_5P3_6

    P0_1P0_2

    P0_0

    P0_3P0_4

    P0_7

    P0_5P0_6

    R38K2

    C5

    10uF

    U2

    AT89S51

    91819

    20

    293031

    40

    123

    45678

    2122232425262728

    1011121314151617

    393837

    3635343332

    RSTXTAL2XTAL1

    GND

    PSENALE/PROG

    EA/VPP

    VCC

    P1.0P1.1P1.2P1.3P1.4P1.5P1.6P1.7

    P2.0/A8P2.1/A9

    P2.2/A10P2.3/A11P2.4/A12P2.5/A13P2.6/A14P2.7/A15

    P3.0/RXDP3.1/TXDP3.2/NTOP3.3/NT1P3.4/TOP3.5/T1P3.6/!RP3.7/RD

    P0.0/AD0P0.1/AD1P0.2/AD2P0.3/AD3P0.4/AD4P0.5/AD5P0.6/AD6P0.7/AD7

    X111.0592"#$

    C733PF

    C6

    33PF

    C

    R2

    SP 91&

    1234

    56789

    C4 0.1uF

    S!0

    RST

  • 8/12/2019 3 Introduction to Micro Controller

    2/42

    CHIP DESCRIPTION

    It inclu"e! the chi "etail! of microcontroller 89c51)A,. con+erter) Multileeran" I,O Ean"er an" Liui" Cr#!tal .i!la#$

    VCC-2ul# +oltage

    GND-3roun"

    Port 0

    &ort 4 i! an 8-/it oen "rain /i-"irectional I,O ort$ A! an outut ort) each in can !in0eight TTL inut!$ hen 16! are %ritten to ort 4 in!) the in! can /e u!e" a! highime"ance! inut!$

    &ort 4 ma# /e configure" to /e the multilee" lo% or"er a""re!!,"ata /u! "uringacce!!e! to eternal rogram an" "ata memor#$ In thi! mo"e ort 4 ha! ull u!$ &ort 4

  • 8/12/2019 3 Introduction to Micro Controller

    3/42

    al!o recei+e! the co"e /#te! "uring 7la!h rogramming an" outut! the co"e /#te! "uringrogram +erification$

    Port 1

    &ort 1 i! an 8-/it /i-"irectional I,O ort %ith internal ull-u!$ The ort 1 outut /uffer!can !in0,!ource four TTL inut!$

    hen 16! are %ritten to ort 1 in! the# are ulle" high /# the internal ull-u! an" can/e u!e" a! inut! $A! inut!) ort 1 in! that are eternall# /eing ulle" lo% %ill !ourcecurrent /ecau!e of the internal ull u!$&ort 1 al!o recei+e! the lo%-or"er a""re!! /#te!"uring 7la!h rogramming an" +erification$

    Port 2

    &ort i! an 8-/it /i-"irectional I,O ort %ith internal ull u6!$ The ort outut /uffer!

    can !in0 ,!ource four TTL inut!$

    hen 16! are %ritten to ort in! the# are ulle" high /# the internal ull u! an" can/e u!e" a! inut!$ A! inut!) ort in! that are eternall# /eing ulle" lo% the !ourcecurrent /ecau!e of internal ull u!$

    &ort emit! the higher or"er a""re!! /#te "uring fetche! from eternal rogram memor#an" "uring acce!! to eternal "ata memor# that u!e 1-/it a""re!!e!$ In thi! alication) itu!e! !trong internal ull u! %hen emitting 16!$.uring acce!!e! to eternal "ata memor#that u!e 8-/it a""re!!e!$

    &ort al!o recei+e! the high or"er a""re!! /it! an" !ome control !ignal! "uring 7la!hrogramming an" +erification$

    Port 3

    &ort i! an 8-/it /i-"irectional I,O ort %ith internal ull u!$ The ort outut /uffer!can !in0,!ource four TTL inut!$ hen 16! are %ritten to ort in! the# are ulle" high/# the internal ull u! an" can /e u!e" a! inut!$ A! inut!) ort in! that areeternall# /eing ulle" lo% %ill !ource current /ecau!e of ull u!$&ort al!o !er+e! thefunction! of +ariou! !ecial feature! of the AT89C51 a! li!te" /elo%$ &ort al!o recei+e!!ome control !ignal! for 7la!h rogramming an" rogram +erification$

  • 8/12/2019 3 Introduction to Micro Controller

    4/42

    Pin Alternate Functions

    &$4 R;.

  • 8/12/2019 3 Introduction to Micro Controller

    5/42

    EA!VPP Eternal Acce!! Ena/le: EA mu!t /e !trae" to 3N. in or"er to ena/le the

    "e+ice to fetch co"e from eternal rogram memor# location! !tarting at 4444@ to7777@$EA !houl" /e !trae" to CC for internal rogram eecution!$ Thi! in al!orecei+e! the 1 +olt rogramming ena/le +oltage

  • 8/12/2019 3 Introduction to Micro Controller

    6/42

    T#$es o% &e'or#

    The 8451 ha! three +er# general t#e! of memor#$ To effecti+el# rogram the 8451 it i!nece!!ar# to ha+e a /a!ic un"er!tan"ing of the!e memor# t#e!$

    The memor# t#e! are illu!trate" in the follo%ing grahic$ The# are:

    1= On-Chi Memor#)= Eternal Co"e Memor#) an"= Eternal RAM$

    On(C)i$ &e'or#refer! to an# memor#

  • 8/12/2019 3 Introduction to Micro Controller

    7/42

    Co+e &e'or#

    Co"e memor# i! the memor# that hol"! the actual 8451 rogram that i! to /e run$ Thi!

    memor# i! limite" to >D an" come! in man# !hae! an" !iBe!: Co"e memor# ma# /e

    foun" on-chip) either /urne" into the microcontroller a! ROM or E&ROM$ Co"e ma#

    al!o /e !tore" comletel# off-chipin an eternal ROM or) more commonl#) an eternal

    E&ROM$ 7la!h RAM i! al!o another oular metho" of !toring a rogram$ ariou!

    com/ination! of the!e memor# t#e! ma# al!o /e u!e"--that i! to !a#) it i! o!!i/le to

    ha+e >D of co"e memor# on-chipan" >0 of co"e memor# off-chipin an E&ROM$

    hen the rogram i! !tore" on-chi the >D maimum i! often re"uce" to >0) 80) or 10$

    Thi! +arie! "een"ing on the +er!ion of the chi that i! /eing u!e"$ Each +er!ion offer!

    !ecific caa/ilitie! an" one of the "i!tingui!hing factor! from chi to chi i! ho% much

    ROM,E&ROM !ace the chi ha!$

    @o%e+er) co"e memor# i! mo!t commonl# imlemente" a! off-chi E&ROM$ Thi! i!

    e!eciall# true in lo%-co!t "e+eloment !#!tem! an" in !#!tem! "e+eloe" /# !tu"ent!$

    E*ternal RA&

    A! an o/+iou! oo!ite ofInternal RAM) the 8451 al!o !uort! %hat i! calle"External

    RAM$

    A! the name !ugge!t!) Eternal RAM i! an# ran"om acce!! memor# %hich i! foun" off-

    chip$ 2ince the memor# i! off-chi it i! not a! flei/le in term! of acce!!ing) an" i! al!o

    !lo%er$ 7or eamle) to increment an Internal RAM location /# 1 reuire! onl# 1

    in!truction an" 1 in!truction c#cle$ To increment a 1-/#te +alue !tore" in Eternal RAM

    reuire! > in!truction! an" ? in!truction c#cle!$ In thi! ca!e) eternal memor# i! ? time!

    !lo%er

    hat Eternal RAM lo!e! in !ee" an" flei/ilit# it gain! in uantit#$ hile Internal

    RAM i! limite" to 18 /#te! D$

  • 8/12/2019 3 Introduction to Micro Controller

    8/42

    On(C)i$ &e'or#

    A! mentione" at the /eginning of thi! chater) the 8451 inclu"e! a certain amount of on-

    chi memor#$ On-chi memor# i! reall# one of t%o t#e!: Internal RAM an" 2ecial

    7unction Regi!ter

  • 8/12/2019 3 Introduction to Micro Controller

    9/42

    Git Memor# al!o li+e! an" i! art of internal RAM$ ell tal0 more a/out /it memor#

    +er# !hortl#) /ut for no% Hu!t 0ee in min" that /it memor# actuall# re!i"e! in internal

    RAM) from a""re!!e! 4h through 7h$

    The 84 /#te! remaining of Internal RAM) from a""re!!e! 4h through ?7h) ma# /e u!e"

    /# u!er +aria/le! that nee" to /e acce!!e" freuentl# or at high-!ee"$ Thi! area i! al!o

    utiliBe" /# the microcontroller a! a !torage area for the oerating stack$ Thi! fact !e+erel#

    limit! the 8451! !tac0 !ince) a! illu!trate" in the memor# ma) the area re!er+e" for the

    !tac0 i! onl# 84 /#te!--an" u!uall# it i! le!! !ince thi! 84 /#te! ha! to /e !hare" /et%een

    the !tac0 an" u!er +aria/le!$

    Re,ister -an.s

    The 8451 u!e! 8 FRF regi!ter! %hich are u!e" in man# of it! in!truction!$ The!e FRF

    regi!ter! are num/ere" from 4 through ? ) R5) R) an" R?=$ The!e

    regi!ter! are generall# u!e" to a!!i!t in maniulating +alue! an" mo+ing "ata from one

    memor# location to another$ 7or eamle) to a"" the +alue of R> to the Accumulator) %e

    %oul" eecute the follo%ing in!truction:

    ADD A/RThu! if the Accumulator containe" the +alue ) the

    Accumulator %oul" contain the +alue 9 after thi! in!truction %a! eecute"$

    @o%e+er) a! the memor# ma !ho%!) the FRF Regi!ter R> i! reall# art of Internal RAM$

    2ecificall#) R> i! a""re!! 4>h$ Thi! can /e !ee in the /right green !ection of the memor#

    ma$ Thu! the a/o+e in!truction accomli!he! the !ame thing a! the follo%ing oeration:

    ADD A/0)Thi! in!truction a""! the +alue foun" in Internal RAM a""re!! 4>h to the +alue of the

    Accumulator) lea+ing the re!ult in the Accumulator$ 2ince R> i! reall# Internal RAM

    4>h) the a/o+e in!truction effecti+el# accomli!he" the !ame thing$

  • 8/12/2019 3 Introduction to Micro Controller

    10/42

    Gut %atch out A! the memor# ma !ho%!) the 8451 ha! four "i!tinct regi!ter /an0!$

    hen the 8451 i! fir!t /oote" u) regi!ter /an0 4 %ill no longer /e the !ame a! Internal

    RAM a""re!! 4>h$ 7or eamle) if #our rogram in!truct! the 8451 to u!e regi!ter /an0

    ) FRF regi!ter R> %ill no% /e !#nonomou! %ith Internal RAM a""re!! 1Ch$

    The concet of regi!ter /an0! a""! a great le+el of flei/ilit# to the 8451) e!eciall# %hen

    "ealing %ith interrut!

  • 8/12/2019 3 Introduction to Micro Controller

    11/42

    SET- 0)

    SET- 0)

    SET- 0)

    SET- 04)

    A! illu!trate" a/o+e) /it memor# i!nt reall# a ne% t#e of memor#$ It! reall# Hu!t a !u/!et

    of Internal RAM$ Gut !ince the 8451 ro+i"e! !ecial in!truction! to acce!! the!e 1

    /#te! of memor# on a /it /# /it /a!i! it i! u!eful to thin0 of it a! a !earate t#e of

    memor#$ @o%e+er) al%a#! 0ee in min" that it i! Hu!t a !u/!et of Internal RAM--an" that

    oeration! erforme" on Internal RAM can change the +alue! of the /it +aria/le!$

    Git +aria/le! 44h through ?7h are for u!er-"efine" function! in their rogram!$ @o%e+er)

    /it +aria/le! 84h an" a/o+e are actuall# u!e" to acce!! certain 27R! on a /it-/#-/it /a!i!$7or eamle) if outut line! &4$4 through &4$? are all clear

  • 8/12/2019 3 Introduction to Micro Controller

    12/42

    S$ecial Function Re,ister 6SFR7 &e'or#

    2ecial 7unction Regi!ter!

  • 8/12/2019 3 Introduction to Micro Controller

    13/42

    SFRs

    :)at Are SFRs;

    The 8451 i! a flei/le microcontroller %ith a relati+el# large num/er of mo"e! ofoeration!$ Jour rogram ma# in!ect an",or change the oerating mo"e of the 8451 /#

    maniulating the +alue! of the 8451! 2ecial 7unction Regi!ter!

  • 8/12/2019 3 Introduction to Micro Controller

    14/42

    range

  • 8/12/2019 3 Introduction to Micro Controller

    15/42

  • 8/12/2019 3 Introduction to Micro Controller

    16/42

    T0!TH0 6Ti'er 0 o>!Hi,)/ A++resses 5A)!5C)7=The!e t%o 27R!) ta0en together)

    rere!ent timer 4$ Their eact /eha+ior "een"! on ho% the timer i! configure" in the

    TMO. 27R ho%e+er) the!e timer! al%a#! count u$ hat i! configura/le i! ho% an"

    %hen the# increment in +alue$

    T1!TH1 6Ti'er 1 o>!Hi,)/ A++resses 5-)!5D)7=The!e t%o 27R!) ta0en together)

    rere!ent timer 1$ Their eact /eha+ior "een"! on ho% the timer i! configure" in the

    TMO. 27R ho%e+er) the!e timer! al%a#! count u$ hat i! configura/le i! ho% an"

    %hen the# increment in +alue$

    P1 6Port 1/ A++ress 90)/ -it(A++ressa

  • 8/12/2019 3 Introduction to Micro Controller

    17/42

    !ecific interrut!) %here a! the highe!t /it i! u!e" to ena/le or "i!a/le ALL interrut!$

    Thu!) if the high /it of IE i! 4 all interrut! are "i!a/le" regar"le!! of %hether an

    in"i+i"ual interrut i! ena/le" /# !etting a lo%er /it$

    P3 6Port 3/ A++ress -0)/ -it(A++ressa

  • 8/12/2019 3 Introduction to Micro Controller

    18/42

    Ot)er SFRs

    The chart a/o+e i! a !ummar# of all the 27R! that ei!t in a !tan"ar" 8451$ All "eri+ati+e

    microcontroller! of the 8451 mu!t !uort the!e /a!ic 27R! in or"er to maintain

    comati/ilit# %ith the un"erl#ing M2C251 !tan"ar"$

    A common ractice %hen !emicon"uctor firm! %i!h to "e+elo a ne% 8451 "eri+ati+e i!

    to a"" a""itional 27R! to !uort ne% function! that ei!t in the ne% chi$

    7or eamle) the .alla! 2emicon"uctor .284C4 i! u%ar"! comati/le %ith the 8451$

    Thi! mean! that an# rogram that run! on a !tan"ar" 8451 !houl" run %ithout

    mo"ification on the .284C4$ Thi! mean! that all the 27R! "efine" a/o+e al!o al# to

    the .alla! comonent$

    @o%e+er) !ince the .284C4 ro+i"e! man# ne% feature! that the !tan"ar" 8451 "oe!

    not) there mu!t /e !ome %a# to control an" configure the!e ne% feature!$ Thi! i!

    accomli!he" /# a""ing a""itional 27R! to tho!e li!te" here$ 7or eamle) !ince the

    .284C4 !uort! t%o !erial ort!

  • 8/12/2019 3 Introduction to Micro Controller

    19/42

    -asic Re,isters

    T)e Accu'ulator

    If #ou+e %or0e" %ith an# other a!!em/l# language! #ou %ill /e familiar %ith the concetof anAccumulatorregi!ter$

    The Accumulator) a! it! name !ugge!t!) i! u!e" a! a general regi!ter to accumulate the

    re!ult! of a large num/er of in!truction!$ It can hol" an 8-/it

  • 8/12/2019 3 Introduction to Micro Controller

    20/42

    The FRF regi!ter! are al!o u!e" to temoraril# !tore +alue!$ 7or eamle) let! !a# #ou

    %ant to a"" the +alue! in R1 an" R together an" then !u/tract the +alue! of R an" R>$

    One %a# to "o thi! %oul" /e:

    &OV A/R3Mo+e the +alue of R into the accumulator

    ADD A/RA"" the +alue of R>

    &OV R/A2tore the re!ulting +alue temoraril# in R5

    &OV A/R1Mo+e the +alue of R1 into the accumulator

    ADD A/R2A"" the +alue of R

    S8-- A/R2u/tract the +alue of R5 =

    A! #ou can !ee) %e u!e" R5 to temoraril# hol" the !um of R an" R>$ Of cour!e) thi!

    i!nt the mo!t efficient %a# to calculate

  • 8/12/2019 3 Introduction to Micro Controller

    21/42

    hile .&TR i! mo!t often u!e" to oint to "ata in eternal memor#) man# rogrammer!

    often ta0e a"+antge of the fact that it! the onl# true 1-/it regi!ter a+aila/le$ It i! often

    u!e" to !tore -/#te +alue! %hich ha+e nothing to "o %ith memor# location!$

    T)e Pro,ra' Counter 6PC7

    The &rogram Counter 4h$ On the other han") if #ou eecute LM&

    >4h #ou+e effecti+el# accomli!he" the !ame thing$

    It i! al!o intere!ting to note that %hile #ou ma# change the +alue of &C

  • 8/12/2019 3 Introduction to Micro Controller

    22/42

    Thi! or"er of oeration i! imortant$ hen the 8451 i! initialiBe" 2& %ill /e initialiBe" to

    4?h$ If #ou imme"iatel# u!h a +alue onto the !tac0) the +alue %ill /e !tore" in Internal

    RAM a""re!! 48h$ Thi! ma0e! !en!e ta0ing into account %hat %a! mentione" t%o

    aragrah! a/o+e: 7ir!t the 8451 %ill increment the +alue of 2&

  • 8/12/2019 3 Introduction to Micro Controller

    23/42

    Imme"iate a""re!!ing i! +er# fa!t !ince the +alue to /e loa"e" i! inclu"e" in the

    in!truction$ @o%e+er) !ince the +alue to /e loa"e" i! fie" at comile-time it i! not +er#

    flei/le$

    Direct A++ressin,

    .irect a""re!!ing i! !o-name" /ecau!e the +alue to /e !tore" in memor# i! o/taine" /#

    "irectl# retrie+ing it from another memor# location$ 7or eamle:

    &OV A/30)

    Thi! in!truction %ill rea" the "ata out of Internal RAM a""re!! 4

  • 8/12/2019 3 Introduction to Micro Controller

    24/42

    In"irect a""re!!ing i! a +er# o%erful a""re!!ing mo"e %hich in man# ca!e! ro+i"e! an

    ecetional le+el of flei/ilit#$ In"irect a""re!!ing i! al!o the onl# %a# to acce!! the etra

    18 /#te! of Internal RAM foun" on an 845$

    In"irect a""re!!ing aear! a! follo%!:

    &OV A/@R0

    Thi! in!truction cau!e! the 8451 to anal#Be the +alue of the R4 regi!ter$ The 8451 %ill

    then loa" the accumulator %ith the +alue from Internal RAM %hich i! foun" at the

    a""re!! in"icate" /# R4$

    7or eamle) let! !a# R4 hol"! the +alue >4h an" Internal RAM a""re!! >4h hol"! the

    +alue ?h$ hen the a/o+e in!truction i! eecute" the 8451 %ill chec0 the +alue of R4$

    2ince R4 hol"! >4h the 8451 %ill get the +alue out of Internal RAM a""re!! >4h

  • 8/12/2019 3 Introduction to Micro Controller

    25/42

    Eternal Memor# i! acce!!e" u!ing a !uite of in!truction! %hich u!e %hat I call FEternal

    .irectF a""re!!ing$ I call it thi! /ecau!e it aear! to /e "irect a""re!!ing) /ut it i! u!e" to

    acce!! eternal memor# rather than internal memor#$

    There are onl# t%o comman"! that u!e Eternal .irect a""re!!ing mo"e:

    &OV" A/@DPTR

    &OV" @DPTR/A

    A! #ou can !ee) /oth comman"! utiliBe .&TR$ In the!e in!truction!) .&TR mu!t fir!t /e

    loa"e" %ith the a""re!! of eternal memor# that #ou %i!h to rea" or %rite$ Once .&TR

    hol"! the correct eternal memor# a""re!!) the fir!t comman" %ill mo+e the content! of

    that eternal memor# a""re!! into the Accumulator$ The !econ" comman" %ill "o the

    oo!ite: it %ill allo% #ou to %rite the +alue of the Accumulator to the eternal memor#

    a""re!! ointe" to /# .&TR$

    E*ternal In+irect

    Eternal memor# can al!o /e acce!!e" u!ing a form of in"irect a""re!!ing %hich I call

    Eternal In"irect a""re!!ing$ Thi! form of a""re!!ing i! u!uall# onl# u!e" in relati+el#

    !mall roHect! that ha+e a +er# !mall amount of eternal RAM$ An eamle of thi!

    a""re!!ing mo"e i!:

    &OV" @R0/A

    Once again) the +alue of R4 i! fir!t rea" an" the +alue of the Accumulator i! %ritten to

    that a""re!! in Eternal RAM$ 2ince the +alue of PR4 can onl# /e 44h through 77h the

    roHect %oul" effecti+el# /e limite" to 5 /#te! of Eternal RAM$ There are relati+el#

    !imle har"%are,!oft%are tric0! that can /e imlemente" to acce!! more than 5 /#te!

    of memor# u!ing Eternal In"irect a""re!!ing ho%e+er) it i! u!uall# ea!ier to u!e

    Eternal .irect a""re!!ing if #our roHect ha! more than 5 /#te! of Eternal RAM$

    Pro,ra' Flo>

  • 8/12/2019 3 Introduction to Micro Controller

    26/42

    hen an 8451 i! fir!t initialiBe") it re!et! the &C to 4444h$ The 8451 then /egin! to

    eecute in!truction! !euentiall# in memor# unle!! a rogram in!truction cau!e! the &C

    to /e other%i!e altere"$ There are +ariou! in!truction! that can mo"if# the +alue of the

    &C !ecificall#) con"itional /ranching in!truction!) "irect Hum! an" call!) an" Freturn!F

    from !u/routine!$ A""itionall#) interrut!) %hen ena/le") can cau!e the rogram flo% to

    "e+iate from it! other%i!e !euential !cheme$

    Con+itional -ranc)in,

    The 8451 contain! a !uite of in!truction! %hich) a! a grou) are referre" to a! Fcon"itional

    /ranchingF in!truction!$ The!e in!truction! cau!e rogram eecution to follo% a non-

    !euential ath if a certain con"ition i! true$

    Ta0e) for eamle) the G in!truction$ Thi! in!truction mean! Fum if Git 2et$F An

    eamle of the G in!truction might /e:

    - )/HEO

    NOP

    HEO= BBBB

    In thi! ca!e) the 8451 %ill anal#Be the content! of /it >5h$ If the /it i! !et rogram

    eecution %ill Hum imme"iatel# to the la/el @ELLO) !0iing the NO& in!truction$ If

    the /it i! not !et the con"itional /ranch fail! an" rogram eecution continue!) a! u!ual)

    %ith the NO& in!truction %hich follo%!$

    Con"itional /ranching i! reall# the fun"amental /uil"ing /loc0 of rogram logic !ince all

    F"eci!ion!F are accomli!he" /# u!ing con"itional /ranching$ Con"itional /ranching can

    /e thought of a! the FI7$$$T@ENF !tructure in 8451 a!!em/l# language$

    An imortant note %orth mentioning a/out con"itional /ranching i! that the rogram ma#

    onl# /ranch to in!truction! locate" %ithim 18 /#te! rior to or 1? /#te! follo%ing the

    a""re!! %hich follo%! the con"itional /ranch in!truction$ Thi! mean! that in the a/o+e

  • 8/12/2019 3 Introduction to Micro Controller

    27/42

    eamle the la/el @ELLO mu!t /e %ithin K,- 18 /#te! of the memor# a""re!! %hich

    contain! the con"itional /ranching in!truction$

    Direct u'$s

    hile con"itional /ranching i! etremel# imortant) it i! often nece!!ar# to ma0e a "irect

    /ranch to a gi+en memor# location %ithout /a!ing it on a gi+en logical "eci!ion$ Thi! i!

    eui+alent to !a#ing F3otoF in GA2IC$ In thi! ca!e #ou %ant the rogram flo% to

    continue at a gi+en memor# a""re!! %ithout con!i"ering an# con"ition!$

    Thi! i! accomli!he" in the 8451 u!ing F.irect um an" CallF in!truction!$ A! illu!trate"

    in the la!t aragrah) thi! !uite of in!truction! cau!e! rogram flo% to change

    uncon"itionall#$

    Con!i"er The Eamle:

    The LM& in!truction in thi! eamle mean! FLong um$F hen the 8451 eecute! thi!

    in!truction the &C i! loa"e" %ith the a""re!! of NEQA..RE22 an" rogram eecution

    continue! !euentiall# from there$

    The o/+iou! "ifference /et%een the .irect um an" Call in!truction! an" the con"itional

    /ranching i! that %ith .irect um! an" Call! rogram flo% al%a#! change!$ ith

    con"itional /ranching rogram flo% onl# change! if a certain con"ition i! true$

    It i! %orth mentioning that) a!i"e from LM&) there are t%o other in!truction! %hich

    cau!e a "irect Hum to occur: the 2M& an" AM& comman"!$ 7unctionall#) the!e t%o

    comman"! erform the eact !ame function a! the LM& comman"--that i! to !a#) the#

    al%a#! cau!e rogram flo% to continue at the a""re!! in"icate" /# the comman"$

    @o%e+er) 2M& an" AM& "iffer in the follo%ing %a#!:

    The 2M& comman") li0e the con"itional /ranching in!truction!) can onl# Hum

    to an a""re!! %ithin K,- 18 /#te! of the 2M& comman"$

    The AM& comman" can onl# Hum to an a""re!! that i! in the !ame 0 /loc0 of

    memor# a! the AM& comman"$ That i! to !a#) if the AM& comman" i! at co"e

    &P NE:ADDRESS

    B

    B

    B

    NE:ADDRESS= BBBB

  • 8/12/2019 3 Introduction to Micro Controller

    28/42

  • 8/12/2019 3 Introduction to Micro Controller

    29/42

    hen 8451 eecute! an LCALL in!truction it imme"iatel# u!he! the current &rogram

    Counter onto the !tac0 an" then continue! eecuting co"e at the a""re!! in"icate" /# the

    LCALL in!truction$

    Returns %ro' Routines

    Another !tructure that can cau!e rogram flo% to change i! the FReturn from 2u/routineF

    in!truction) 0no%n a! RET in 8451 A!!em/l# Language$

    The RET in!truction) %hen eecute") return! to the a""re!! follo%ing the in!truction that

    calle" the gi+en !u/routine$ More accuratel#) it return! to the a""re!! that i! !tore" on the

    !tac0$

    The RET comman" i! "irect in the !en!e that it al%a#! change! rogram flo% %ithout

    /a!ing it on a con"ition) /ut i! +aria/le in the !en!e that %here rogram flo% continue!

    can /e "ifferent each time the RET in!truction i! eecute" "een"ing on from %here the

    !u/routine %a! calle" originall#$

    NTERRUPTS

    The 8451 ha! fi+e interrut !ource!: one from the !erial ort %hen a tran!mi!!ion or

    recetion oeration i! eecute" t%o from the timer! %hen o+erflo% occur! an" t%o come

    from the t%o inut in! INT4) INT1$ Each interrut ma# /e in"een"entl# ena/le" or

    "i!a/le" to allo% olling on !ame !ource! an" each ma# /e cla!!ifie" a! high or lo%

    riorit#$

    A high riorit# !ource can o+erri"e a lo% riorit# !er+ice routine$ The!e otion! are

    !electe" /# interrut ena/le an" riorit# control regi!ter!) IE an" I&$

    hen an interrut i! acti+ate") then the rogram flo% comlete! the eecution of the

    current in!truction an" Hum! to a articular rogram location %here it fin"! the interrut

  • 8/12/2019 3 Introduction to Micro Controller

    30/42

    !er+ice routine$ After fini!hing the interrut !er+ice routine) the rogram flo%! return to

    /ac0 to the original lace$

    The &rogram Memor# a""re!!) 444@ i! allotte" to the fir!t interrut an" net !e+en

    /#te! can /e u!e" to "o an# ta!0 a!!ociate" %ith that interrut$

    Interru$t Source Serice routine startin, a++ress

    Eternal 4 444@

    Timer,Counter 4 444G@

    Eternal 1 441@

    Timer,counter 1 441G@

    2erial ort 44@

    Instruction Set/ Ti'in,/ an+ o>(eel In%o

    In or"er to un"er!tan"--an" /etter ma0e u!e of--the 8451) it i! nece!!ar# to un"er!tan"

    !ome un"erl#ing information concerning timing$

    The 8451 oerate! /a!e" on an eternal cr#!tal$ Thi! i! an electrical "e+ice %hich) %hen

    energ# i! alie") emit! ul!e! at a fie" freuenc#$ One can fin" cr#!tal! of +irtuall# an#

    freuenc# "een"ing on the alication reuirement!$ hen u!ing an 8451) the mo!t

    common cr#!tal freuencie! are 1 megahertB an" 11$459 megahertB--%ith 11$459 /eing

    much more common$ h# %oul" an#one ic0 !uch an o""-/all freuenc# There! a real

    rea!on for it--it ha! to "o %ith generating /au" rate! an" %ell tal0 more a/out it in the

    2erial Communication chater$ 7or the remain"er of thi! "i!cu!!ion %ell a!!ume that

    %ere u!ing an 11$459MhB cr#!tal$

    Microcontroller!

  • 8/12/2019 3 Introduction to Micro Controller

    31/42

    A c#cle i!) in realit#) 1 ul!e! of the cr#!tal$ That i! to !a#) if an in!truction ta0e! one

    machine c#cle to eecute) it %ill ta0e 1 ul!e! of the cr#!tal to eecute$ 2ince %e 0no%

    the cr#!tal i! ul!ing 11)459)444 time! er !econ" an" that one machine c#cle i! 1

    ul!e!) %e can calculate ho% man# in!truction c#cle! the 8451 can eecute er !econ":

    11)459)444 , 1 91)58

    Thi! mean! that the 8451 can eecute 91)58 !ingle-c#cle in!truction! er !econ"$ 2ince

    a large num/er of 8451 in!truction! are !ingle-c#cle in!truction! it i! often con!i"ere"

    that the 8451 can eecute roughl# 1 million in!truction! er !econ") although in realit# it

    i! le!!--an") "een"ing on the in!truction! /eing u!e") an e!timate of a/out 44)444

    in!truction! er !econ" i! more reali!tic$

    7or eamle) if #ou are u!ing eclu!i+el# -c#cle in!truction! #ou %oul" fin" that the

    8451 %oul" eecute >4)?91 in!truction! er !econ"$ The 8451 al!o ha! t%o reall# !lo%

    in!truction! that reuire a full > c#cle! to eecute--if #ou %ere to eecute nothing /ut

    tho!e in!truction! #ou" fin" erformance to /e a/out 4)95 in!truction! er !econ"$

    It i! again imortant to emha!iBe that not all in!truction! eecute in the !ame amount of

    time$ The fa!te!t in!truction! reuire one machine c#cle

  • 8/12/2019 3 Introduction to Micro Controller

    32/42

    2ince all the in!truction! reuire "ifferent amount! of time to eecute a +er# o/+iou!

    ue!tion come! to min": @o% can one 0ee trac0 of time in a time-critical alication if

    %e ha+e no reference to time in the out!i"e %orl"

    Luc0il#) the 8451 inclu"e! timer! %hich allo% u! to time e+ent! %ith high reci!ion--

    %hich i! the toic of the net chater$

    Ti'ers

    The 8451 come! euie" %ith t%o timer!) /oth of %hich ma# /e controlle") !et) rea")

    an" configure" in"i+i"uall#$ The 8451 timer! ha+e three general function!: 1= Deeing

    time an",or calculating the amount of time /et%een e+ent!) = Counting the e+ent!

    them!el+e!) or = 3enerating /au" rate! for the !erial ort$

    The three timer u!e! are "i!tinct !o %e %ill tal0 a/out each of them !earatel#$ The fir!t

    t%o u!e! %ill /e "i!cu!!e" in thi! chater %hile the u!e of timer! for /au" rate generation

    %ill /e "i!cu!!e" in the chater relating to !erial ort!$

    Ho> +oes a ti'er count;

    @o% "oe! a timer count The an!%er to thi! ue!tion i! +er# !imle: A timer al%a#!

    count! u$ It "oe!nt matter %hether the timer i! /eing u!e" a! a timer) a counter) or a /au"

    rate generator: A timer i! al%a#! incremente" /# the microcontroller$

    8SING TI&ERS TO &EAS8RE TI&E

    O/+iou!l#) one of the rimar# u!e! of timer! i! to mea!ure time$ e %ill "i!cu!! thi! u!e

    of timer! fir!t an" %ill !u/!euentl# "i!cu!! the u!e of timer! to count e+ent!$ hen a

    timer i! u!e" to mea!ure time it i! al!o calle" an Finter+al timerF !ince it i! mea!uring the

    time of the inter+al /et%een t%o e+ent!$

    Ho> lon, +oes a ti'er ta.e to count;

  • 8/12/2019 3 Introduction to Micro Controller

    33/42

  • 8/12/2019 3 Introduction to Micro Controller

    34/42

    effecti+el# eecute #our e+ent once er !econ") accurate to %ithin thou!an"th! of a

    !econ"$

    Thu!) %e no% ha+e a !#!tem %ith %hich to mea!ure time$ All %e nee" to re+ie% i! ho%

    to control the timer! an" initialiBe them to ro+i"e u! %ith the information %e nee"$

    Ti'er SFRs

    A! mentione" /efore) the 8451 ha! t%o timer! %hich each function e!!entiall# the !ame

    %a#$ One timer i! TIMER4 an" the other i! TIMER1$ The t%o timer! !hare t%o 27R!

  • 8/12/2019 3 Introduction to Micro Controller

    35/42

  • 8/12/2019 3 Introduction to Micro Controller

    36/42

    INT1

    3ATE4

    hen thi! /it i! !et the timer %ill onl# run %hen

    INT4

    A! #ou can !ee in the a/o+e chart) four /it! 8$ 3enerall# the 1-/it timer mo"e i!

    not u!e" in ne% "e+eloment$

    hen the timer i! in 1-/it mo"e) TL %ill count from 4 to 1$ hen TL i! incremente"

    from 1) it %ill Fre!etF to 4 an" increment T@$ Thu!) effecti+el#) onl# 1 /it! of the t%o

    timer /#te! are /eing u!e": /it! 4-> of TL an" /it! 4-? of T@$ Thi! al!o mean!) in

  • 8/12/2019 3 Introduction to Micro Controller

    37/42

    e!!ence) the timer can onl# contain 819 +alue!$ If #ou !et a 1-/it timer to 4) it %ill

    o+erflo% /ac0 to Bero 819 machine c#cle! later$

    Again) there i! +er# little rea!on to u!e thi! mo"e an" it i! onl# mentione" !o #ou %ont /e

    !urri!e" if #ou e+er en" u anal#Bing archaeic co"e %hich ha! /een a!!e" "o%n

    through the generation!

  • 8/12/2019 3 Introduction to Micro Controller

    38/42

  • 8/12/2019 3 Introduction to Micro Controller

    39/42

    T)e TCON SFR

    7inall#) there! one more 27R that control! the t%o timer! an" ro+i"e! +alua/le

    information a/out them$ The TCON 27R ha! the follo%ing !tructure:

    TCON of the 8 /it!$ That! /ecau!e the other > /it! of

    the 27R "ont ha+e an#thing to "o %ith timer!--the# ha+e to "o %ith Interrut! an" the#

    %ill /e "i!cu!!e" in the chater that a""re!!e! interrut!$

    A ne% iece of information in thi! chart i! the column F/it a""re!!$F Thi! i! /ecau!e thi!

    27R i! F/it-a""re!!a/le$F hat "oe! thi! mean It mean! if #ou %ant to !et the /it T71--

    %hich i! the highe!t /it of TCON--#ou coul" eecute the comman":

    MO TCON) 84h

    $$$ Or) !ince the 27R i! /it-a""re!!a/le) #ou coul" Hu!t eecute the comman":

    2ETG T71

    Thi! ha! the /enefit of !etting the high /it of TCON %ithout changing the +alue of an# of

    the other /it! of the 27R$ '!uall# %hen #ou !tart or !to a timer #ou "ont %ant to mo"if#

    the other +alue! in TCON) !o #ou ta0e a"+antage of the fact that the 27R i! /it-

    a""re!!a/le$

    Initialiin, a Ti'er

  • 8/12/2019 3 Introduction to Micro Controller

    40/42

  • 8/12/2019 3 Introduction to Micro Controller

    41/42

    then rea" the high /#te of the timer a! 15$ In thi! ca!e) %hat actuall# haene" %a! that

    the timer +alue %a! 1>,55 ) lo% /#te 55= /ut #ou rea" 15,55$ h#

    Gecau!e #ou rea" the lo% /#te a! 55$ Gut %hen #ou eecute" the net in!truction a

    !mall amount of time a!!e"--/ut enough for the timer to increment again at %hich time

    the +alue rolle" o+er from 1>,55 to 15,4$ Gut in the roce!! #ou+e rea" the timer a!

    /eing 15,55$ O/+iou!l# there! a ro/lem there$

    The !olution It! not too tric0#) reall#$ Jou rea" the high /#te of the timer) then rea" the

    lo% /#te) then rea" the high /#te again$ If the high /#te rea" the !econ" time i! not the

    !ame a! the high /#te rea" the fir!t time #ou reeat the c#cle$ In co"e) thi! %oul" aear

    a!:

    RE&EAT: MO A)T@4

    MO R4)TL4

    CNE A)T@4)RE&EAT

    $$$

    In thi! ca!e) %e loa" the accumulator %ith the high /#te of Timer 4$ e then loa" R4

    %ith the lo% /#te of Timer 4$ 7inall#) %e chec0 to !ee if the high /#te %e rea" out of

    Timer 4--%hich i! no% !tore" in the Accumulator--i! the !ame a! the current Timer 4

    high /#te$ If it i!nt it mean! %e+e Hu!t Frolle" o+erF an" mu!t rerea" the timer! +alue--

    %hich %e "o /# going /ac0 to RE&EAT$ hen the loo eit! %e %ill ha+e the lo% /#te

    of the timer in R4 an" the high /#te in the Accumulator$

    Another much !imler alternati+e i! to !iml# turn off the timer run /it

  • 8/12/2019 3 Introduction to Micro Controller

    42/42

    In the a/o+e co"e the fir!t t%o line! initialiBe the Timer 4 !tarting +alue to 19)>5?$ The

    net t%o in!truction! configure timer 4 an" turn it on$ 7inall#) the la!t in!truction N-

    TF0/) rea"! Fum) if T74 i! not !et) /ac0 to thi! !ame in!truction$F The FSF oeran"

    mean!) in mo!t a!!em/ler!) the a""re!! of the current in!truction$ Thu! a! long a! the

    timer ha! not o+erflo%e" an" the T74 /it ha! not /een !et the rogram %ill 0ee

    eecuting thi! !ame in!truction$ After 1,4th of a !econ" timer 4 %ill o+erflo%) !et the

    T74 /it) an" rogram eecution %ill then /rea0 out of the loo$