Sunteți pe pagina 1din 42

MICROCONTROLLER- AT 89C51 MICROCONTROLLER:

The main controlling unit of the ro o!e" !#!tem i! the microcontroller$ The main feature! of microcontroller articularl# Atmel 89C51 i! "i!cu!!e" here$ A microcontroller con!i!t! of a o%erful C&' tightl# cou le" %ith memor# (RAM)ROM or E&ROM*)+ariou! I,O feature! !uch a! !erial ort!) arallel ort! )timer,counter!) interru t controller )"ata re-ui!ition interface ) Analog to "igital con+erter(A.C*)"igital to 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 ) "e en"ing on the nee" an" area of a lication for %hich it i! "e!igne") the on chi feature! re!ent in it ma# or ma# not inclu"e$ An# microcom uter !#!tem! re-uire! memor# to !tore a !e-uence of in!truction! ma0ing u a rogram ) arallel ort or !erial ort for communicating %ith an e1ternal !#!tem timer,counter for control ur o!e li0e generating time "ela#$
VCC U2 P1_0 P1_1 P1_2 P1_3 P1_4 P1_5 P1_6 P1_7 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 P3.0/RXD P3.1/TXD P3.2/ NTO P3.3/ NT1 P3.4/TO P3.5/T1 P3.6/!R P3.7/RD

C4

0.1uF R2 P0.0/AD0 P0.1/AD1 P0.2/AD2 P0.3/AD3 P0.4/AD4 P0.5/AD5 P0.6/AD6 P0.7/AD7 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 P0_0 P0_1 P0_2 P0_3 P0_4 P0_5 P0_6 P0_7 2 3 4 5 6 7 8 9 1 20 VCC

VCC

40

GND

S P%9%1&

VCC P3_5 P3_6 P3_7 S!0

EA/V PP ALE/PROG PSEN P2.7/A15 P2.6/A14 P2.5/A13 P2.4/A12 P2.3/A11 P2.2/A10 P2.1/A 9 P2.0/A 8

RST

10uF

C5

R3 8K2 11.0592"#$ X1 33PF

18 19 C7 33PF

RST XTAL2 XTAL1 AT89S51

P2_3 P2_2 P2_1 P2_0

C6

CHIP DESCRIPTION
It inclu"e! the chi "etail! of microcontroller 89c51)A,. con+erter) Multi le1er an" I,O E1 an"er an" Li-ui" Cr#!tal .i! la#$

VCC-2u

l# +oltage

GND-3roun" Port 0 &ort 4 i! an 8-/it o en "rain /i-"irectional I,O ort$ A! an out ut ort) each in can !in0 eight TTL in ut!$ 5hen 16! are %ritten to ort 4 in!) the in! can /e u!e" a! high im e"ance! in ut!$ &ort 4 ma# /e configure" to /e the multi le1e" lo% or"er a""re!!,"ata /u! "uring acce!!e! to e1ternal rogram an" "ata memor#$ In thi! mo"e ort 4 ha! ull u !$ &ort 4

al!o recei+e! the co"e /#te! "uring 7la!h rogramming an" out ut! the co"e /#te! "uring rogram +erification$ Port 1 &ort 1 i! an 8-/it /i-"irectional I,O ort %ith internal ull-u !$ The ort 1 out ut /uffer! can !in0,!ource four TTL in ut!$ 5hen 16! are %ritten to ort 1 in! the# are ulle" high /# the internal ull-u ! an" can /e u!e" a! in ut! $A! in ut!) ort 1 in! that are e1ternall# /eing ulle" lo% %ill !ource current /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 8 i! an 8-/it /i-"irectional I,O ort %ith internal ull u 6!$ The ort 8 out ut /uffer! can !in0 ,!ource four TTL in ut!$ 5hen 16! are %ritten to ort 8 in! the# are ulle" high /# the internal ull u ! an" can /e u!e" a! in ut!$ A! in ut!) ort 8 in! that are e1ternall# /eing ulle" lo% the !ource current /ecau!e of internal ull u !$ &ort 8 emit! the higher or"er a""re!! /#te "uring fetche! from e1ternal rogram memor# an" "uring acce!! to e1ternal "ata memor# that u!e 19-/it a""re!!e!$ In thi! a lication) it u!e! !trong internal ull u ! %hen emitting 16!$.uring acce!!e! to e1ternal "ata memor# that u!e 8-/it a""re!!e!$ &ort 8 al!o recei+e! the high or"er a""re!! /it! an" !ome control !ignal! "uring 7la!h rogramming an" +erification$ Port 3 &ort : i! an 8-/it /i-"irectional I,O ort %ith internal ull u !$ The ort : out ut /uffer! can !in0,!ource four TTL in ut!$ 5hen 16! are %ritten to ort : in! the# are ulle" high /# the internal ull u ! an" can /e u!e" a! in ut!$ A! in ut!) ort : in! that are e1ternall# /eing ulle" lo% %ill !ource current /ecau!e of ull u !$&ort : al!o !er+e! the function! 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$

Pin Alternate Functions &:$4 R;. <2erial in ut ort= &:$1 T;.<2erial out ut ort= &:$8 INT4 <e1ternal interru t 4= &:$: INT1 <e1ternal interru t 1= &:$> T4 <timer 4 e1ternal in ut= &:$5 T1 <timer 1 e1ternal in ut= &:$9 5R <e1ternal "ata memor# %rite !tro/e= &:$? R. <e1ternal "ata memor# rea" !tro/e= RST Re!et in ut: A high on thi! in for t%o machine c#cle! %hile the o!cillator i! running re!et! the "e+ice$ A E!PROG A""re!! Latch Ena/le out ut ul!e for latching the lo% /#te of the a""re!! "uring acce!!e! to e1ternal memor#$ Thi! in i! al!o the rogram ul!e in ut <&RO3= "uring 7la!h &rogramming$ In normal o eration ALE i! emitte" at a con!tant rate of 1,9 the o!cillator# fre-uenc#) an" ma# /e u!e" for e1ternal timing or cloc0ing ur o!e!$ One ALE &ul!e i! !0i e" "uring each acce!! to e1ternal "ata memor#$ If "e!ire") ALE o eration can /e "i!a/le" /# !etting /it 4 of 27R location 8E@$5ith the /it !et) ALE i! acti+e onl# "uring a MOA; or MOAC in!truction$ Other%i!e) the in i! %ea0l# ulle" high$ 2etting the ALE-"i!a/le /it ha! no effect if the microcontroller i! in e1ternal e1ecution mo"e$ PSEN &rogram 2tore Ena/le i! the rea" !tro/e to e1ternal rogram memor#$ 5hen the AT 89C51 i! e1ecuting co"e from e1ternal rogram memor#) &2EN i! acti+ate" t%ice each machine c#cle) e1ce t that t%o &2EN acti+ation! are !0i e" "uring each acce!! to e1ternal "ata memor#$

EA!VPP E1ternal Acce!! Ena/le: EA mu!t /e !tra e" to 3N. in or"er to ena/le the "e+ice to fetch co"e from e1ternal rogram memor# location! !tarting at 4444@ to 7777@$EA !houl" /e !tra e" to ACC for internal rogram e1ecution!$ Thi! in al!o recei+e! the 18 +olt rogramming ena/le +oltage <A&&= "uring 7la!h &rogramming) for ort! that re-uire 18 +olt Acc$

OSCILLATOR INPUTS (XTAL1, XTAL2)

The 8458 i! t# icall# "ri+en /# a cr#!tal connecte" to in! 18 <;TAL8= an" 19 <;TAL1=$ Common cr#!tal fre-uencie! are 11$4598MhB a! %ell a! 18MhB) although man# ne%er "eri+ati+e! are ca a/le of acce ting fre-uencie! a! high a! >4MhB$ 5hile a cr#!tal i! the normal cloc0 !ource) thi! i!n6t nece!!aril# the ca!e$ A TTL cloc0 !ource ma# al!o /e attache" to ;TAL1 an" ;TAL8 to ro+i"e the microcontroller6! cloc0$ "TA 1 In ut to the in+erting o!cillator am lifier an" in ut to the internal cloc0 o erating circuit$ "TA 2 Out ut from the in+erting o!cillator am lifier %ith the ! ecifie" /au" rate$

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 gra hic$ The# are: 1= On-Chi Memor#) 8= E1ternal Co"e Memor#) an" := E1ternal RAM$

On(C)i$ &e'or# refer! to an# memor# <Co"e) RAM) or other= that h#!icall# e1i!t! on the microcontroller it!elf$ On-chi memor# can /e of !e+eral t# e!) /ut %eCll get into that !hortl#$ E*ternal Co+e &e'or# i! co"e <or rogram= memor# that re!i"e! off-chi $ Thi! i! often in the form of an e1ternal E&ROM$ E*ternal RA& i! RAM memor# that re!i"e! off-chi $ Thi! i! often in the form of !tan"ar" !tatic RAM or fla!h RAM$

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 9>D an" come! in man# !ha e! 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" com letel# off-chip in an e1ternal ROM or) more commonl#) an e1ternal E&ROM$ 7la!h RAM i! al!o another o ular metho" of !toring a rogram$ Aariou! 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-chip an" 9>0 of co"e memor# off-chip in an E&ROM$ 5hen the rogram i! !tore" on-chi the 9>D ma1imum i! often re"uce" to >0) 80) or 190$ Thi! +arie! "e en"ing on the +er!ion of the chi that i! /eing u!e"$ Each +er!ion offer! ! ecific ca a/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# im lemente" a! off-chi E&ROM$ Thi! i! e! eciall# true in lo%-co!t "e+elo ment !#!tem! an" in !#!tem! "e+elo e" /# !tu"ent!$ E*ternal RA& A! an o/+iou! o RAM$ A! the name !ugge!t!) E1ternal RAM i! an# ran"om acce!! memor# %hich i! foun" offchip$ 2ince the memor# i! off-chi it i! not a! fle1i/le in term! of acce!!ing) an" i! al!o !lo%er$ 7or e1am le) to increment an Internal RAM location /# 1 re-uire! onl# 1 in!truction an" 1 in!truction c#cle$ To increment a 1-/#te +alue !tore" in E1ternal RAM re-uire! > in!truction! an" ? in!truction c#cle!$ In thi! ca!e) e1ternal memor# i! ? time! !lo%erE 5hat E1ternal RAM lo!e! in ! ee" an" fle1i/ilit# it gain! in -uantit#$ 5hile Internal RAM i! limite" to 188 /#te! <859 /#te! %ith an 8458=) the 8451 !u u to 9>D$ ort! E1ternal RAM o!ite of Internal RAM) the 8451 al!o !u ort! %hat i! calle" External

On(C)i$ &e'or# A! mentione" at the /eginning of thi! cha ter) the 8451 inclu"e! a certain amount of onchi memor#$ On-chi memor# i! reall# one of t%o t# e!: Internal RAM an" 2 ecial 7unction Regi!ter <27R= memor#$ The la#out of the 8451C! internal memor# i! re!ente" in the follo%ing memor# ma :

A! i! illu!trate" in thi! ma ) the 8451 ha! a /an0 of 188 /#te! of Internal RAM$ Thi! Internal RAM i! foun" on-chip on the 8451 !o it i! the fa!te!t RAM a+aila/le) an" it i! al!o the mo!t fle1i/le in term! of rea"ing) %riting) an" mo"if#ing it! content!$ Internal RAM i! +olatile) !o %hen the 8451 i! re!et thi! memor# i! cleare"$ The 188 /#te! of internal ram i! !u/"i+i"e" a! !ho%n on the memor# ma $ The fir!t 8 /#te! <44h - 4?h= are Fregi!ter /an0 4F$ G# mani ulating certain 27R!) a rogram ma# choo!e to u!e regi!ter /an0! 1) 8) or :$ The!e alternati+e regi!ter /an0! are locate" in internal RAM in a""re!!e! 48h through 17h$ 5eCll "i!cu!! Fregi!ter /an0!F more in a later cha ter$ 7or no% it i! !ufficient to 0no% that the# Fli+eF an" are art of internal RAM$

Git Memor# al!o li+e! an" i! art of internal RAM$ 5eCll 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! 84h through 87h$ 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" fre-uentl# or at high-! ee"$ Thi! area i! al!o utiliBe" /# the microcontroller a! a !torage area for the o erating 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 ? <R4) R1) R8) R:) R>) R5) R9) an" R?=$ The!e regi!ter! are generall# u!e" to a!!i!t in mani ulating +alue! an" mo+ing "ata from one memor# location to another$ 7or e1am le) to a"" the +alue of R> to the Accumulator) %e %oul" e1ecute the follo%ing in!truction: ADD A/R0 Thu! if the Accumulator <A= containe" the +alue 9 an" R> containe" the +alue :) the Accumulator %oul" contain the +alue 9 after thi! in!truction %a! e1ecute"$ @o%e+er) a! the memor# ma !ho%!) the FRF Regi!ter R> i! reall# art of Internal RAM$ 2 ecificall#) 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 accom li!he! the !ame thing a! the follo%ing o eration: ADD A/00) 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# accom li!he" the !ame thing$

Gut %atch outE A! the memor# ma !ho%!) the 8451 ha! four "i!tinct regi!ter /an0!$ 5hen the 8451 i! fir!t /oote" u ) regi!ter /an0 4 <a""re!!e! 44h through 4?h= i! u!e" /# "efault$ @o%e+er) #our rogram ma# in!truct the 8451 to u!e one of the alternate regi!ter /an0!I i$e$) regi!ter /an0! 1) 8) or :$ In thi! ca!e) R> %ill no longer /e the !ame a! Internal RAM a""re!! 4>h$ 7or e1am le) 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 conce t of regi!ter /an0! a""! a great le+el of fle1i/ilit# to the 8451) e! eciall# %hen "ealing %ith interru t! <%eCll tal0 a/out interru t! later=$ @o%e+er) al%a#! remem/er that the regi!ter /an0! reall# re!i"e in the fir!t :8 /#te! of Internal RAM$ -it &e'or# The 8451) /eing a communication!-oriente" microcontroller) gi+e! the u!er the a/ilit# to acce!! a num/er of bit variables$ The!e +aria/le! ma# /e either 1 or 4$ There are 188 /it +aria/le! a+aila/le to the u!er) num/er" 44h through ?7h$ The u!er ma# ma0e u!e of the!e +aria/le! %ith comman"! !uch a! 2ETG an" CLR$ 7or e1am le) to !et /it num/er 8> <he1= to 1 #ou %oul" e1ecute the in!truction: SET- 20) It i! im ortant to note that Git Memor# i! reall# a art of Internal RAM$ In fact) the 188 /it +aria/le! occu # the 19 /#te! of Internal RAM from 84h through 87h$ Thu!) if #ou %rite the +alue 77h to Internal RAM a""re!! 84h #ou+e effecti+el# !et /it! 44h through 4?h$ That i! to !a# that: &OV 20)/10FF) i! e-ui+alent to: SET- 00) SET- 01) SET- 02) SET- 03)

SET- 00) SET- 02) SET- 03) 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 19 /#te! of memor# on a /it /# /it /a!i! it i! u!eful to thin0 of it a! a !e arate 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 o eration! 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 e1am le) if out ut line! &4$4 through &4$? are all clear <4= an" #ou %ant to turn on the &4$4 out ut line #ou ma# either e1ecute: &OV P0/101) or #ou ma# e1ecute: SET- 50) Goth the!e in!truction! accom li!h the !ame thing$ @o%e+er) u!ing the 2ETG comman" %ill turn on the &4$4 line %ithout effecting the !tatu! of an# of the other &4 out ut line!$ The MOA comman" effecti+el# turn! off all the other out ut line! %hich) in !ome ca!e!) ma# not /e acce ta/le$

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


2 ecial 7unction Regi!ter! <27R!= are area! of memor# that control ! ecific functionalit# of the 8451 roce!!or$ 7or e1am le) four 27R! ermit acce!! to the 8451! :8 in ut,out ut line!$ Another 27R allo%! a rogram to rea" or %rite to the 8451! !erial ort$ Other 27R! allo% the u!er to !et the !erial /au" rate) control an" acce!! timer!) an" configure the 8451! interru t !#!tem$ 5hen rogramming) 27R! ha+e the illu!ion of /eing Internal Memor#$ 7or e1am le) if #ou %ant to %rite the +alue F1F to Internal RAM location 54 he1 #ou %oul" e1ecute the in!truction: &OV 20)/101) 2imilarl#) if #ou %ant to %rite the +alue F1F to the 8451! !erial ort #ou %oul" %rite thi! +alue to the S-8F 27R) %hich ha! an 27R a""re!! of 99 @e1$ Thu!) to %rite the +alue F1F to the !erial ort #ou %oul" e1ecute the in!truction: &OV 99)/101) A! #ou can !ee) it a ear! that the 27R i! art of Internal Memor#$ Thi! i! not the ca!e$ 5hen u!ing thi! metho" of memor# acce!! <it! calle" "irect a""re!!=) an# in!truction that ha! an a""re!! of 44h through ?7h refer! to an Internal RAM memor# a""re!!I an# in!truction %ith an a""re!! of 84h through 77h refer! to an 27R control regi!ter$

SFRs
:)at Are SFRs; The 8451 i! a fle1i/le microcontroller %ith a relati+el# large num/er of mo"e! of o eration!$ Jour rogram ma# in! ect an",or change the o erating mo"e of the 8451 /# mani ulating the +alue! of the 8451C! 2 ecial 7unction Regi!ter! <27R!=$ 27R! are acce!!e" a! if the# %ere normal Internal RAM$ The onl# "ifference i! that Internal RAM i! from a""re!! 44h through ?7h %herea! 27R regi!ter! e1i!t in the a""re!! range of 84h through 77h$ Each 27R ha! an a""re!! <84h through 77h= an" a name$ The follo%ing chart ro+i"e! a gra hical re!entation of the 8451C! 27R!) their name!) an" their a""re!!$

A! #ou can !ee) although the a""re!! range of 84h through 77h offer 188 o!!i/le a""re!!e!) there are onl# 81 27R! in a !tan"ar" 8451$ All other a""re!!e! in the 27R

range <84h through 77h= are con!i"ere" in+ali"$ 5riting to or rea"ing from the!e regi!ter! ma# ro"uce un"efine" +alue! or /eha+ior$

SFR T#$es
A! mentione" in the chart it!elf) the 27R! that ha+e a /lue /ac0groun" are 27R! relate" to the I,O ort!$ The 8451 ha! four I,O ort! of 8 /it!) for a total of :8 I,O line!$ 5hether a gi+en I,O line i! high or lo% an" the +alue rea" from the line are controlle" /# the 27R! in green$ The 27R! %ith #ello% /ac0groun"! are 27R! %hich in !ome %a# control the o eration or the configuration of !ome a! ect of the 8451$ 7or e1am le) TCON control! the timer!) 2CON control! the !erial ort$ The remaining 27R!) %ith green /ac0groun"!) are Fother 27R!$F The!e 27R! can /e thought of a! au1iliar# 27R! in the !en!e that the# "onCt "irectl# configure the 8451 /ut o/+iou!l# the 8451 cannot o erate %ithout them$ 7or e1am le) once the !erial ort ha! /een configure" u!ing SCON) the rogram ma# rea" or %rite to the !erial ort u!ing the S-8F regi!ter$

SFR Descri$tions
Thi! !ection %ill en"ea+or to -uic0l# o+er+ie% each of the !tan"ar" 27R! foun" in the a/o+e 27R chart ma $ It i! not the intention of thi! !ection to full# e1 lain the functionalit# of each 27R--thi! information %ill /e co+ere" in !e arate cha ter! of the tutorial$ Thi! !ection i! to Hu!t gi+e #ou a general i"ea of %hat each 27R "oe!$ P0 6Port 0/ A++ress 50)/ -it(A++ressa<le7= Thi! i! in ut,out ut ort 4$ Each /it of thi! 27R corre! on"! to one of the in! on the microcontroller$ 7or e1am le) /it 4 of ort 4 i! in &4$4) /it ? i! in &4$?$ 5riting a +alue of 1 to a /it of thi! 27R %ill !en" a high le+el on the corre! on"ing I,O in %herea! a +alue of 4 %ill /ring it to a lo% le+el$

SP 6Stac. Pointer/ A++ress 51)7= Thi! i! the !tac0 ointer of the microcontroller$ Thi! 27R in"icate! %here the ne1t +alue to /e ta0en from the !tac0 %ill /e rea" from in Internal RAM$ If #ou u!h a +alue onto the !tac0) the +alue %ill /e %ritten to the a""re!! of 2& K 1$ That i! to !a#) if 2& hol"! the +alue 4?h) a &'2@ in!truction %ill u!h the +alue onto the !tac0 at a""re!! 48h$ Thi! 27R i! mo"ifie" /# all in!truction! %hich mo"if# the !tac0) !uch a! &'2@) &O&) LCALL) RET) RETI) an" %hene+er interru t! are ro+o0e" /# the microcontroller$ DP !DPH 6Data Pointer o>!Hi,)/ A++resses 52)!53)7= The 27R! .&L an" .&@ %or0 together to re re!ent a 19-/it +alue calle" the Data Pointer$ The "ata ointer i! u!e" in o eration! regar"ing e1ternal RAM an" !ome in!truction! in+ol+ing co"e memor#$ 2ince it i! an un!igne" t%o-/#te integer +alue) it can re re!ent +alue! from 4444h to 7777h <4 through 95)5:5 "ecimal=$ PCON 6Po>er Control/ A++resses 54)7= The &o%er Control 27R i! u!e" to control the 8451C! o%er control mo"e!$ Certain o eration mo"e! of the 8451 allo% the 8451 to go into a t# e of F!lee F mo"e %hich re-uire! much le!! o%er$ The!e mo"e! of o eration are controlle" through &CON$ A""itionall#) one of the /it! in &CON i! u!e" to "ou/le the effecti+e /au" rate of the 8451C! !erial ort$ TCON 6Ti'er Control/ A++resses 55)/ -it(A++ressa<le7= The Timer Control 27R i! u!e" to configure an" mo"if# the %a# in %hich the 8451C! t%o timer! o erate$ Thi! 27R control! %hether each of the t%o timer! i! running or !to e" an" contain! a flag to in"icate that each timer ha! o+erflo%e"$ A""itionall#) !ome non-timer relate" /it! are locate" in the TCON 27R$ The!e /it! are u!e" to configure the %a# in %hich the e1ternal interru t! are acti+ate" an" al!o contain the e1ternal interru t flag! %hich are !et %hen an e1ternal interru t ha! occure"$ T&OD 6Ti'er &o+e/ A++resses 59)7= The Timer Mo"e 27R i! u!e" to configure the mo"e of o eration of each of the t%o timer!$ '!ing thi! 27R #our rogram ma# configure each timer to /e a 19-/it timer) an 8-/it autoreloa" timer) a 1:-/it timer) or t%o !e arate timer!$ A""itionall#) #ou ma# configure the timer! to onl# count %hen an e1ternal in i! acti+ate" or to count Fe+ent!F that are in"icate" on an e1ternal in$

T 0!TH0 6Ti'er 0 o>!Hi,)/ A++resses 5A)!5C)7= The!e t%o 27R!) ta0en together) re re!ent timer 4$ Their e1act /eha+ior "e en"! on ho% the timer i! configure" in the TMO. 27RI ho%e+er) the!e timer! al%a#! count u $ 5hat i! configura/le i! ho% an" %hen the# increment in +alue$ T 1!TH1 6Ti'er 1 o>!Hi,)/ A++resses 5-)!5D)7= The!e t%o 27R!) ta0en together) re re!ent timer 1$ Their e1act /eha+ior "e en"! on ho% the timer i! configure" in the TMO. 27RI ho%e+er) the!e timer! al%a#! count u $ 5hat i! configura/le i! ho% an" %hen the# increment in +alue$ P1 6Port 1/ A++ress 90)/ -it(A++ressa<le7= Thi! i! in ut,out ut ort 1$ Each /it of thi! 27R corre! on"! to one of the in! on the microcontroller$ 7or e1am le) /it 4 of ort 1 i! in &1$4) /it ? i! in &1$?$ 5riting a +alue of 1 to a /it of thi! 27R %ill !en" a high le+el on the corre! on"ing I,O in %herea! a +alue of 4 %ill /ring it to a lo% le+el$ SCON 6Serial Control/ A++resses 95)/ -it(A++ressa<le7= The 2erial Control 27R i! u!e" to configure the /eha+ior of the 8451C! on-/oar" !erial ort$ Thi! 27R control! the /au" rate of the !erial ort) %hether the !erial ort i! acti+ate" to recei+e "ata) an" al!o contain! flag! that are !et %hen a /#te i! !ucce!!full# !ent or recei+e"$ S-8F 6Serial Control/ A++resses 99)7= The 2erial Guffer 27R i! u!e" to !en" an" recei+e "ata +ia the on-/oar" !erial ort$ An# +alue %ritten to 2G'7 %ill /e !ent out the !erial ortC! T;. in$ Li0e%i!e) an# +alue %hich the 8451 recei+e! +ia the !erial ortC! R;. in %ill /e "eli+ere" to the u!er rogram +ia 2G'7$ In other %or"!) 2G'7 !er+e! a! the out ut ort %hen %ritten to an" a! an in ut ort %hen rea" from$ P2 6Port 2/ A++ress A0)/ -it(A++ressa<le7= Thi! i! in ut,out ut ort 8$ Each /it of thi! 27R corre! on"! to one of the in! on the microcontroller$ 7or e1am le) /it 4 of ort 8 i! in &8$4) /it ? i! in &8$?$ 5riting a +alue of 1 to a /it of thi! 27R %ill !en" a high le+el on the corre! on"ing I,O in %herea! a +alue of 4 %ill /ring it to a lo% le+el$ IE 6Interru$t Ena<le/ A++resses A5)7= The Interru t Ena/le 27R i! u!e" to ena/le an" "i!a/le ! ecific interru t!$ The lo% ? /it! of the 27R are u!e" to ena/le,"i!a/le the

! ecific interru t!) %here a! the highe!t /it i! u!e" to ena/le or "i!a/le ALL interru t!$ Thu!) if the high /it of IE i! 4 all interru t! are "i!a/le" regar"le!! of %hether an in"i+i"ual interru t i! ena/le" /# !etting a lo%er /it$ P3 6Port 3/ A++ress -0)/ -it(A++ressa<le7= Thi! i! in ut,out ut ort :$ Each /it of thi! 27R corre! on"! to one of the in! on the microcontroller$ 7or e1am le) /it 4 of ort : i! in &:$4) /it ? i! in &:$?$ 5riting a +alue of 1 to a /it of thi! 27R %ill !en" a high le+el on the corre! on"ing I,O in %herea! a +alue of 4 %ill /ring it to a lo% le+el$ IP 6Interru$t Priorit#/ A++resses -5)/ -it(A++ressa<le7= The Interru t &riorit# 27R i! u!e" to ! ecif# the relati+e riorit# of each interru t$ On the 8451) an interru t ma# either /e of lo% <4= riorit# or high <1= riorit#$ An interru t ma# onl# interru t interru t! of lo%er riorit#$ 7or e1am le) if %e configure the 8451 !o that all interru t! are of lo% riorit# e1ce t the !erial interru t) the !erial interru t %ill al%a#! /e a/le to interru t the !#!tem) e+en if another interru t i! currentl# e1ecuting$ @o%e+er) if a !erial interru t i! e1ecuting no other interru t %ill /e a/le to interru t the !erial interru t routine !ince the !erial interru t routine ha! the highe!t riorit#$ PS: 6Pro,ra' Status :or+/ A++resses D0)/ -it(A++ressa<le7= The &rogram 2tatu! 5or" i! u!e" to !tore a num/er of im ortant /it! that are !et an" cleare" /# 8451 in!truction!$ The &25 27R contain! the carr# flag) the au1iliar# carr# flag) the o+erflo% flag) an" the arit# flag$ A""itionall#) the &25 regi!ter contain! the regi!ter /an0 !elect flag! %hich are u!e" to !elect %hich of the FRF regi!ter /an0! are currentl# !electe"$ ACC 6Accu'ulator/ A++resses E0)/ -it(A++ressa<le7= The Accumulator i! one of the mo!t-u!e" 27R! on the 8451 !ince it i! in+ol+e" in !o man# in!truction!$ The Accumulator re!i"e! a! an 27R at E4h) %hich mean! the in!truction &OV A/120) i! reall# the !ame a! &OV E0)/120)$ @o%e+er) it i! a goo" i"ea to u!e the fir!t metho" !ince it onl# re-uire! t%o /#te! %herea! the !econ" o tion re-uire! three /#te!$ - 6- Re,ister/ A++resses F0)/ -it(A++ressa<le7= The FGF regi!ter i! u!e" in t%o in!truction!: the multi l# an" "i+i"e o eration!$ The G regi!ter i! al!o commonl# u!e" /# rogrammer! a! an au1iliar# regi!ter to tem oraril# !tore +alue!$

Ot)er SFRs
The chart a/o+e i! a !ummar# of all the 27R! that e1i!t in a !tan"ar" 8451$ All "eri+ati+e microcontroller! of the 8451 mu!t !u ort the!e /a!ic 27R! in or"er to maintain com ati/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 !u ort ne% function! that e1i!t in the ne% chi $

7or e1am le) the .alla! 2emicon"uctor .284C:84 i! u %ar"! com ati/le %ith the 8451$ Thi! mean! that an# rogram that run! on a !tan"ar" 8451 !houl" run %ithout mo"ification on the .284C:84$ Thi! mean! that all the 27R! "efine" a/o+e al!o a l# to the .alla! com onent$ @o%e+er) !ince the .284C:84 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! accom li!he" /# a""ing a""itional 27R! to tho!e li!te" here$ 7or e1am le) !ince the .284C:84 !u ort! t%o !erial ort! <a! o o!e" to Hu!t one on the 8451=) the 27R! 2G'78 an" 2CON8 ha+e /een a""e"$ In a""ition to all the 27R! li!te" a/o+e) the .284C:84 al!o recogniBe! the!e t%o ne% 27R! a! +ali" an" u!e! their +alue! to "etermine the mo"e of o eration of the !econ"ar# !erial ort$ O/+iou!l#) the!e ne% 27R! ha+e /een a!!igne" to 27R a""re!!e! that %ere unu!e" in the original 8451$ In thi! manner) ne% 8451 "eri+ati+e chi ! ma# /e "e+elo e" %hich %ill run e1i!ting 8451 rogram!$

-asic Re,isters
T)e Accu'ulator If #ou+e %or0e" %ith an# other a!!em/l# language! #ou %ill /e familiar %ith the conce t of an Accumulator regi!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 <1-/#te= +alue an" i! the mo!t +er!atile regi!ter the 8451 ha! "ue to the !hear num/er of in!truction! that ma0e u!e of the accumulator$ More than half of the 8451! 855 in!truction! mani ulate or u!e the accumulator in !ome %a#$ 7or e1am le) if #ou %ant to a"" the num/er 14 an" 84) the re!ulting :4 %ill /e !tore" in the Accumulator$ Once #ou ha+e a +alue in the Accumulator #ou ma# continue roce!!ing the +alue or #ou ma# !tore it in another regi!ter or in memor#$ T)e ?R? re,isters The FRF regi!ter! are a !et of eight regi!ter! that are name" R4) R1) etc$ u to an" inclu"ing R?$ The!e regi!ter! are u!e" a! au1illar# regi!ter! in man# o eration!$ To continue %ith the a/o+e e1am le) erha ! #ou are a""ing 14 an" 84$ The original num/er 14 ma# /e !tore" in the Accumulator %herea! the +alue 84 ma# /e !tore" in) !a#) regi!ter R>$ To roce!! the a""ition #ou %oul" e1ecute the comman": ADD A/R0 After e1ecuting thi! in!truction the Accumulator %ill contain the +alue :4$ Jou ma# thin0 of the FRF regi!ter! a! +er# im ortant au1illar#) or Fhel erF) regi!ter!$ The Accumulator alone %oul" not /e +er# u!eful if it %ere not for the!e FRF regi!ter!$

The FRF regi!ter! are al!o u!e" to tem oraril# !tore +alue!$ 7or e1am le) let! !a# #ou %ant to a"" the +alue! in R1 an" R8 together an" then !u/tract the +alue! of R: an" R>$ One %a# to "o thi! %oul" /e: &OV A/R3 IMo+e the +alue of R: into the accumulator ADD A/R0 IA"" the +alue of R> &OV R2/A I2tore the re!ulting +alue tem oraril# in R5 &OV A/R1 IMo+e the +alue of R1 into the accumulator ADD A/R2 IA"" the +alue of R8 S8-- A/R2 I2u/tract the +alue of R5 <%hich no% contain! R: K R>= A! #ou can !ee) %e u!e" R5 to tem oraril# hol" the !um of R: an" R>$ Of cour!e) thi! i!nt the mo!t efficient %a# to calculate <R1KR8= - <R: KR>= /ut it "oe! illu!trate the u!e of the FRF regi!ter! a! a %a# to !tore +alue! tem oraril#$ T)e ?-? Re,ister The FGF regi!ter i! +er# !imilar to the Accumulator in the !en!e that it ma# hol" an 8-/it <1-/#te= +alue$ The FGF regi!ter i! onl# u!e" /# t%o 8451 in!truction!: M'L AG an" .IA AG$ Thu!) if #ou %ant to -uic0l# an" ea!il# multi l# or "i+i"e A /# another num/er) #ou ma# !tore the other num/er in FGF an" ma0e u!e of the!e t%o in!truction!$ A!i"e from the M'L an" .IA in!truction!) the FGF regi!ter i! often u!e" a! #et another tem orar# !torage regi!ter much li0e a ninth FRF regi!ter$ T)e Data Pointer 6DPTR7 The .ata &ointer <.&TR= i! the 8451! onl# u!er-acce!!a/le 19-/it <8-/#te= regi!ter$ The Accumulator) FRF regi!ter!) an" FGF regi!ter are all 1-/#te +alue!$ .&TR) a! the name !ugge!t!) i! u!e" to oint to "ata$ It i! u!e" /# a num/er of comman"! %hich allo% the 8451 to acce!! e1ternal memor#$ 5hen the 8451 acce!!e! e1ternal memor# it %ill acce!! e1ternal memor# at the a""re!! in"icate" /# .&TR$

5hile .&TR i! mo!t often u!e" to oint to "ata in e1ternal memor#) man# rogrammer! often ta0e a"+antge of the fact that it! the onl# true 19-/it regi!ter a+aila/le$ It i! often u!e" to !tore 8-/#te +alue! %hich ha+e nothing to "o %ith memor# location!$ T)e Pro,ra' Counter 6PC7 The &rogram Counter <&C= i! a 8-/#te a""re!! %hich tell! the 8451 %here the ne1t in!truction to e1ecute i! foun" in memor#$ 5hen the 8451 i! initialiBe" &C al%a#! !tart! at 4444h an" i! incremente" each time an in!truction i! e1ecute"$ It i! im ortant to note that &C i!nt al%a#! incremente" /# one$ 2ince !ome in!truction! re-uire 8 or : /#te! the &C %ill /e incremente" /# 8 or : in the!e ca!e!$ The &rogram Counter i! ! ecial in that there i! no %a# to "irectl# mo"if# it! +alue$ That i! to !a#) #ou cant "o !omething li0e &CL8>:4h$ On the other han") if #ou e1ecute LMM& 8>:4h #ou+e effecti+el# accom li!he" the !ame thing$ It i! al!o intere!ting to note that %hile #ou ma# change the +alue of &C </# e1ecuting a Hum in!truction) etc$= there i! no %a# to rea" the +alue of &C$ That i! to !a#) there i! no %a# to a!0 the 8451 F5hat a""re!! are #ou a/out to e1ecuteNF A! it turn! out) thi! i! not com letel# true: There i! one tric0 that ma# /e u!e" to "etermine the current +alue of &C$ Thi! tric0 %ill /e co+ere" in a later cha ter$ T)e Stac. Pointer 6SP7 The 2tac0 &ointer) li0e all regi!ter! e1ce t .&TR an" &C) ma# hol" an 8-/it <1-/#te= +alue$ The 2tac0 &ointer i! u!e" to in"icate %here the ne1t +alue to /e remo+e" from the !tac0 !houl" /e ta0en from$ 5hen #ou u!h a +alue onto the !tac0) the 8451 fir!t increment! the +alue of 2& an" then !tore! the +alue at the re!ulting memor# location$ 5hen #ou o a +alue off the !tac0) the 8451 return! the +alue from the memor# location in"icate" /# 2&) an" then "ecrement! the +alue of 2&$

Thi! or"er of o eration i! im ortant$ 5hen 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 aragra h! a/o+e: 7ir!t the 8451 %ill increment the +alue of 2& <from 4?h to 48h= an" then %ill !tore the u!he" +alue at that memor# a""re!! <48h=$ 2& i! mo"ifie" "irectl# /# the 8451 /# !i1 in!truction!: &'2@) &O&) ACALL) LCALL) RET) an" RETI$ It i! al!o u!e" intrin!icall# %hene+er an interru t i! triggere" <more on interru t! later$ .ont %orr# a/out them for no%E=$

A++ressin, &o+es
An Fa""re!!ing mo"eF refer! to ho% #ou are a""re!!ing a gi+en memor# location$ In !ummar#) the a""re!!ing mo"e! are a! follo%!) %ith an e1am le of each: I''e+iate A++ressin, Direct A++ressin, In+irect A++ressin, E*ternal Direct Co+e In+irect MOA A)O84h MOA A):4h MOA A)PR4 MOA; A)P.&TR MOAC A)PAK.&TR

Each of the!e a""re!!ing mo"e! ro+i"e! im ortant fle1i/ilit#$ I''e+iate A++ressin, Imme"iate a""re!!ing i! !o-name" /ecau!e the +alue to /e !tore" in memor# imme"iatel# follo%! the o eration co"e in memor#$ That i! to !a#) the in!truction it!elf "ictate! %hat +alue %ill /e !tore" in memor#$ 7or e1am le) the in!truction: &OV A/120) Thi! in!truction u!e! Imme"iate A""re!!ing /ecau!e the Accumulator %ill /e loa"e" %ith the +alue that imme"iatel# follo%!I in thi! ca!e 84 <he1i"ecimal=$

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! fi1e" at com ile-time it i! not +er# fle1i/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 e1am le: &OV A/30) Thi! in!truction %ill rea" the "ata out of Internal RAM a""re!! :4 <he1i"ecimal= an" !tore it in the Accumulator$ .irect a""re!!ing i! generall# fa!t !ince) although the +alue to /e loa"e" i!nt inclu"e" in the in!truction) it i! -uic0l# acce!!a/le !ince it i! !tore" in the 8451! Internal RAM$ It i! al!o much more fle1i/le than Imme"iate A""re!!ing !ince the +alue to /e loa"e" i! %hate+er i! foun" at the gi+en a""re!!--%hich ma# /e +aria/le$ Al!o) it i! im ortant to note that %hen u!ing "irect a""re!!ing an# in!truction %hich refer! to an a""re!! /et%een 44h an" ?7h i! referring to Internal Memor#$ An# in!truction %hich refer! to an a""re!! /et%een 84h an" 77h i! referring to the 27R control regi!ter! that control the 8451 microcontroller it!elf$ The o/+iou! -ue!tion that ma# ari!e i!) FIf "irect a""re!!ing an a""re!! from 84h through 77h refer! to 27R!) ho% can I acce!! the u er 188 /#te! of Internal RAM that are a+aila/le on the 8458NF The an!%er i!: Jou cant acce!! them u!ing "irect a""re!!ing$ A! !tate") if #ou "irectl# refer to an a""re!! of 84h through 77h #ou %ill /e referring to an 27R$ @o%e+er) #ou ma# acce!! the 8458! u a""re!!ing mo"e) Fin"irect a""re!!ing$F er 188 /#te! of RAM /# u!ing the ne1t

In+irect A++ressin,

In"irect a""re!!ing i! a +er# o%erful a""re!!ing mo"e %hich in man# ca!e! ro+i"e! an e1ce tional le+el of fle1i/ilit#$ In"irect a""re!!ing i! al!o the onl# %a# to acce!! the e1tra 188 /#te! of Internal RAM foun" on an 8458$ In"irect a""re!!ing a ear! 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 e1am le) let! !a# R4 hol"! the +alue >4h an" Internal RAM a""re!! >4h hol"! the +alue 9?h$ 5hen the a/o+e in!truction i! e1ecute" 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 <%hich hol"! 9?h= an" !tore it in the Accumulator$ Thu!) the Accumulator en"! u hol"ing 9?h$ In"irect a""re!!ing al%a#! refer! to Internal RAMI it ne+er refer! to an 27R$ Thu!) in a rior e1am le %e mentione" that 27R 99h can /e u!e" to %rite a +alue to the !erial ort$ Thu! one ma# thin0 that the follo%ing %oul" /e a +ali" !olution to %rite the +alue 1 to the !erial ort: &OV R0/199) ILoa" the a""re!! of the !erial ort &OV @R0/101) I2en" 41 to the !erial ort -- 5RON3EE Thi! i! not +ali"$ 2ince in"irect a""re!!ing al%a#! refer! to Internal RAM the!e t%o in!truction! %oul" %rite the +alue 41h to Internal RAM a""re!! 99h on an 8458$ On an 8451 the!e t%o in!truction! %oul" ro"uce an un"efine" re!ult !ince the 8451 onl# ha! 188 /#te! of Internal RAM$

E*ternal Direct

E1ternal Memor# i! acce!!e" u!ing a !uite of in!truction! %hich u!e %hat I call FE1ternal .irectF a""re!!ing$ I call it thi! /ecau!e it a ear! to /e "irect a""re!!ing) /ut it i! u!e" to acce!! e1ternal memor# rather than internal memor#$ There are onl# t%o comman"! that u!e E1ternal .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 e1ternal memor# that #ou %i!h to rea" or %rite$ Once .&TR hol"! the correct e1ternal memor# a""re!!) the fir!t comman" %ill mo+e the content! of that e1ternal memor# a""re!! into the Accumulator$ The !econ" comman" %ill "o the o o!ite: it %ill allo% #ou to %rite the +alue of the Accumulator to the e1ternal memor# a""re!! ointe" to /# .&TR$ E*ternal In+irect E1ternal memor# can al!o /e acce!!e" u!ing a form of in"irect a""re!!ing %hich I call E1ternal 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 e1ternal RAM$ An e1am le 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 E1ternal RAM$ 2ince the +alue of PR4 can onl# /e 44h through 77h the roHect %oul" effecti+el# /e limite" to 859 /#te! of E1ternal RAM$ There are relati+el# !im le har"%are,!oft%are tric0! that can /e im lemente" to acce!! more than 859 /#te! of memor# u!ing E1ternal In"irect a""re!!ingI ho%e+er) it i! u!uall# ea!ier to u!e E1ternal .irect a""re!!ing if #our roHect ha! more than 859 /#te! of E1ternal RAM$

Pro,ra' Flo>

5hen an 8451 i! fir!t initialiBe") it re!et! the &C to 4444h$ The 8451 then /egin! to e1ecute in!truction! !e-uentiall# 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 &CI ! ecificall#) con"itional /ranching in!truction!) "irect Hum ! an" call!) an" Freturn!F from !u/routine!$ A""itionall#) interru t!) %hen ena/le") can cau!e the rogram flo% to "e+iate from it! other%i!e !e-uential !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 e1ecution to follo% a non!e-uential ath if a certain con"ition i! true$ Ta0e) for e1am le) the MG in!truction$ Thi! in!truction mean! FMum if Git 2et$F An e1am le of the MG in!truction might /e: A- 02)/HE O NOP HE O= BBBB In thi! ca!e) the 8451 %ill anal#Be the content! of /it >5h$ If the /it i! !et rogram e1ecution %ill Hum imme"iatel# to the la/el @ELLO) !0i ing the NO& in!truction$ If the /it i! not !et the con"itional /ranch fail! an" rogram e1ecution 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 accom li!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 im ortant note %orth mentioning a/out con"itional /ranching i! that the rogram ma# onl# /ranch to in!truction! locate" %ithim 188 /#te! rior to or 18? /#te! follo%ing the a""re!! %hich follo%! the con"itional /ranch in!truction$ Thi! mean! that in the a/o+e

A&P NE:CADDRESS B B B NE:CADDRESS= BBBB e1am le the la/el @ELLO mu!t /e %ithin K,- 188 /#te! of the memor# a""re!! %hich contain! the con"itional /ranching in!truction$ Direct Au'$s 5hile con"itional /ranching i! e1tremel# im ortant) 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! e-ui+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! accom li!he" in the 8451 u!ing F.irect Mum an" CallF in!truction!$ A! illu!trate" in the la!t aragra h) thi! !uite of in!truction! cau!e! rogram flo% to change uncon"itionall#$ Con!i"er The E1am le: The LMM& in!truction in thi! e1am le mean! FLong Mum $F 5hen the 8451 e1ecute! thi! in!truction the &C i! loa"e" %ith the a""re!! of NE5QA..RE22 an" rogram e1ecution continue! !e-uentiall# from there$ The o/+iou! "ifference /et%een the .irect Mum an" Call in!truction! an" the con"itional /ranching i! that %ith .irect Mum ! an" Call! rogram flo% al%a#! change!$ 5ith con"itional /ranching rogram flo% onl# change! if a certain con"ition i! true$ It i! %orth mentioning that) a!i"e from LMM&) there are t%o other in!truction! %hich cau!e a "irect Hum to occur: the 2MM& an" AMM& comman"!$ 7unctionall#) the!e t%o comman"! erform the e1act !ame function a! the LMM& 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) 2MM& an" AMM& "iffer in the follo%ing %a#!:

The 2MM& comman") li0e the con"itional /ranching in!truction!) can onl# Hum to an a""re!! %ithin K,- 188 /#te! of the 2MM& comman"$ The AMM& comman" can onl# Hum to an a""re!! that i! in the !ame 80 /loc0 of memor# a! the AMM& comman"$ That i! to !a#) if the AMM& comman" i! at co"e

memor# location 954h) it can onl# "o a Hum to a""re!!e! 4444h through 4?77h <4 through 84>?) "ecimal=$ Jou ma# /e a!0ing #our!elf) F5h# %oul" I %ant to u!e the 2MM& or AMM& comman" %hich ha+e re!triction! a! to ho% far the# can Hum if the# "o the !ame thing a! the LMM& comman" %hich can Hum an#%here in memor#NF The an!%er i! !im le: The LMM& comman" re-uire! three /#te! of co"e memor# %herea! /oth the 2MM& an" AMM& comman"! re-uire onl# t%o$ Thu!) if #ou are "e+elo ing an a lication that ha! memor# re!triction! #ou can often !a+e -uite a /it of memor# u!ing the 8-/#te AMM&,2MM& in!truction! in!tea" of the :-/#te in!truction$ Recentl#) I %rote a rogram that re-uire" 8144 /#te! of memor# /ut I ha" a memor# re!triction of 80 <84>8 /#te!=$ I "i" a !earch,re lace changing all LMM&! to AMM&! an" the rogram !hrun0 "o%n to 1954 /#te!$ Thu!) %ithout changing an# logic %hat!oe+er in m# rogram I !a+e" 154 /#te! an" %a! a/le to meet m# 84>8 /#te memor# re!triction$ NOTE: 2ome -ualit# a!!em/ler! %ill actuall# "o the a/o+e con+er!ion for #ou automaticall#$ That i!) the#6ll automaticall# change #our LMM&! to 2MM&! %hene+er o!!i/le$ Thi! i! a nift# an" +er# o%erful ca a/ilit# that #ou ma# %ant to loo0 for in an a!!em/ler if #ou lan to "e+elo man# roHect! that ha+e relati+el# tight memor# re!triction!$

Direct Calls Another o eration that %ill /e familiar to !ea!one" rogrammer! i! the LCALL in!truction$ Thi! i! !imilar to a F3o!u/F comman" in Ga!ic$

5hen 8451 e1ecute! an LCALL in!truction it imme"iatel# u!he! the current &rogram Counter onto the !tac0 an" then continue! e1ecuting 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 e1ecute") 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! e1ecute" "e en"ing on from %here the !u/routine %a! calle" originall#$

NTERRUPTS
The 8451 ha! fi+e interru t !ource!: one from the !erial ort %hen a tran!mi!!ion or rece tion o eration i! e1ecute"I t%o from the timer! %hen o+erflo% occur! an" t%o come from the t%o in ut in! INT4) INT1$ Each interru t ma# /e in"e en"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 o tion! are !electe" /# interru t ena/le an" riorit# control regi!ter!) IE an" I&$ 5hen an interru t i! acti+ate") then the rogram flo% com lete! the e1ecution of the current in!truction an" Hum ! to a articular rogram location %here it fin"! the interru t

!er+ice routine$ After fini!hing the interru t !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 interru t an" ne1t !e+en /#te! can /e u!e" to "o an# ta!0 a!!ociate" %ith that interru t$ Interru$t Source SerDice routine startin, a++ress E1ternal 4 444:@ Timer,Counter 4 444G@ E1ternal 1 441:@ Timer,counter 1 441G@ 2erial ort 448:@

Instruction Set/ Ti'in,/ an+ o>( eDel 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 o erate! /a!e" on an e1ternal cr#!tal$ Thi! i! an electrical "e+ice %hich) %hen energ# i! a lie") emit! ul!e! at a fi1e" fre-uenc#$ One can fin" cr#!tal! of +irtuall# an# fre-uenc# "e en"ing on the a lication re-uirement!$ 5hen u!ing an 8451) the mo!t common cr#!tal fre-uencie! are 18 megahertB an" 11$459 megahertB--%ith 11$459 /eing much more common$ 5h# %oul" an#one ic0 !uch an o""-/all fre-uenc#N 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 cha ter$ 7or the remain"er of thi! "i!cu!!ion %ell a!!ume that %ere u!ing an 11$459MhB cr#!tal$ Microcontroller! <an" man# other electrical !#!tem!= u!e cr#!tal! to !#ncrhroniBe o eration!$ The 8451 u!e! the cr#!tal for reci!el# that: to !#nchroniBe it! o eration$ Effecti+el#) the 8451 o erate! u!ing %hat are calle" Fmachine c#cle!$F A !ingle machine c#cle i! the minimum amount of time in %hich a !ingle 8451 in!truction can /e e1ecute"$ although man# in!truction! ta0e multi le c#cle!$

A c#cle i!) in realit#) 18 ul!e! of the cr#!tal$ That i! to !a#) if an in!truction ta0e! one machine c#cle to e1ecute) it %ill ta0e 18 ul!e! of the cr#!tal to e1ecute$ 2ince %e 0no% the cr#!tal i! ul!ing 11)459)444 time! er !econ" an" that one machine c#cle i! 18 ul!e!) %e can calculate ho% man# in!truction c#cle! the 8451 can e1ecute er !econ": 11)459)444 , 18 L 981)58: Thi! mean! that the 8451 can e1ecute 981)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 e1ecute roughl# 1 million in!truction! er !econ") although in realit# it i! le!!--an") "e en"ing on the in!truction! /eing u!e") an e!timate of a/out 944)444 in!truction! er !econ" i! more reali!tic$ 7or e1am le) if #ou are u!ing e1clu!i+el# 8-c#cle in!truction! #ou %oul" fin" that the 8451 %oul" e1ecute >94)?91 in!truction! er !econ"$ The 8451 al!o ha! t%o reall# !lo% in!truction! that re-uire a full > c#cle! to e1ecute--if #ou %ere to e1ecute nothing /ut tho!e in!truction! #ou" fin" erformance to /e a/out 8:4):95 in!truction! er !econ"$ It i! again im ortant to em ha!iBe that not all in!truction! e1ecute in the !ame amount of time$ The fa!te!t in!truction! re-uire one machine c#cle <18 cr#!tal ul!e!=) man# other! re-uire t%o machine c#cle! <8> cr#!tal ul!e!=) an" the t%o +er# !lo% math o eration! re-uire four machine c#cle! <>8 cr#!tal ul!e!=$

NOTE: Man# 8451 "eri+ati+e chi ! change in!truction timing$ 7or e1am le) man# o timiBe" +er!ion! of the 8451 e1ecute in!truction! in > o!cillator c#cle! in!tea" of 18I !uch a chi %oul" /e effecti+el# : time! fa!ter than the 8451 %hen u!e" %ith the !ame 11$459 MhB cr#!tal$

2ince all the in!truction! re-uire "ifferent amount! of time to e1ecute a +er# o/+iou! -ue!tion come! to min": @o% can one 0ee trac0 of time in a time-critical a lication if %e ha+e no reference to time in the out!i"e %orl"N Luc0il#) the 8451 inclu"e! timer! %hich allo% u! to time e+ent! %ith high reci!ion-%hich i! the to ic of the ne1t cha ter$

Ti'ers
The 8451 come! e-ui e" %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= Dee ing time an",or calculating the amount of time /et%een e+ent!) 8= 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 !e aratel#$ The fir!t t%o u!e! %ill /e "i!cu!!e" in thi! cha ter %hile the u!e of timer! for /au" rate generation %ill /e "i!cu!!e" in the cha ter relating to !erial ort!$ Ho> +oes a ti'er count; @o% "oe! a timer countN The an!%er to thi! -ue!tion i! +er# !im le: 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$ 5e %ill "i!cu!! thi! u!e of timer! fir!t an" %ill !u/!e-uentl# "i!cu!! the u!e of timer! to count e+ent!$ 5hen 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;

7ir!t) it! %orth mentioning that %hen a timer i! in inter+al timer mo"e <a! o

o!e" to

e+ent counter mo"e= an" correctl# configure") it %ill increment /# 1 e+er# machine c#cle$ A! #ou %ill recall from the re+iou! cha ter) a !ingle machine c#cle con!i!t! of 18 cr#!tal ul!e!$ Thu! a running timer %ill /e incremente": 11)459)444 , 18 L 981)58: 981)58: time! er !econ"$ 'nli0e in!truction!--!ome of %hich re-uire 1 machine c#cle) other! 8) an" other! >--the timer! are con!i!tent: The# %ill al%a#! /e incremente" once er machine c#cle$ Thu! if a timer ha! counte" from 4 to 54)444 #ou ma# calculate: 54)444 , 981)58: L $45>8 $45>8 !econ"! ha+e a!!e"$ In lain Engli!h) a/out half of a tenth of a !econ") or onet%entieth of a !econ"$ O/+iou!l# it6! not +er# u!eful to 0no% $45>8 !econ"! ha+e a!!e"$ If #ou %ant to e1ecute an e+ent once er !econ" #ou6" ha+e to %ait for the timer to count from 4 to 54)444 18$>5 time!$ @o% can #ou %ait Fhalf of a timeNF Jou can6t$ 2o %e come to another im ortant calculation$ Let6! !a# %e %ant to 0no% ho% man# time! the timer %ill /e incremente" in $45 !econ"!$ 5e can "o !im le multi lication: $45 R 981)58: L >9)4?9$15$ Thi! tell! u! that it %ill ta0e $45 !econ"! <1,84th of a !econ"= to count from 4 to >9)4?9$ Actuall#) it %ill ta0e it $4>99998:? !econ"!--!o %ere off /# $44444419: !econ"!-ho%e+er) that6! clo!e enough for go+ernment %or0$ Con!i"er that if #ou %ere /uil"ing a %atch /a!e" on the 8451 an" ma"e the a/o+e a!!um tion #our %atch %oul" onl# gain a/out one !econ" e+er# 8 month!$ Again) I thin0 that6! accurate enough for mo!t a lication!--I %i!h m# %atch onl# gaine" one !econ" e+er# t%o month!E O/+iou!l#) thi! i! a little more u!eful$ If #ou 0no% it ta0e! 1,84th of a !econ" to count from 4 to >9)4?9 an" #ou %ant to e1ecute !ome e+ent e+er# !econ" #ou !im l# %ait for the timer to count from 4 to >9)4?9 t%ent# time!I then #ou e1ecute #our e+ent) re!et the timer!) an" %ait for the timer to count u another 84 time!$ In thi! manner #ou %ill

effecti+el# e1ecute #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! <TMO. an" TCON= %hich control the timer!) an" each timer al!o ha! t%o 27R! "e"icate" !olel# to it!elf <T@4,TL4 an" T@1,TL1=$ 5e6+e gi+en 27R! name! to ma0e it ea!ier to refer to them) /ut in realit# an 27R ha! a numeric a""re!!$ It i! often u!eful to 0no% the numeric a""re!! that corre! on"! to an 27R name$ The 27R! relating to timer! are: SFR Na'e T@4 TL4 T@1 TL1 TCON TMO. Descri$tion Timer 4 @igh G#te Timer 4 Lo% G#te Timer 1 @igh G#te Timer 1 Lo% G#te Timer Control Timer Mo"e SFR A++ress 8Ch 8Ah 8.h 8Gh 88h 89h

5hen #ou enter the name of an 27R into an a!!em/ler) it internall# con+ert! it to a num/er$ 7or e1am le) the comman": MOA T@4)O85h Mo+e! the +alue 85h into the T@4 27R$ @o%e+er) !ince T@4 i! the !ame a! 27R a""re!! 8Ch thi! comman" i! e-ui+alent to: MOA 8Ch)O85h No%) /ac0 to the timer!$ 7ir!t) let! tal0 a/out Timer 4$

Timer 4 ha! t%o 27R! "e"icate" e1clu!i+el# to it!elf: T@4 an" TL4$ 5ithout ma0ing thing! too com licate" to !tart off %ith) #ou ma# Hu!t thin0 of thi! a! the high an" lo% /#te of the timer$ That i! to !a#) %hen Timer 4 ha! a +alue of 4) /oth T@4 an" TL4 %ill contain 4$ 5hen Timer 4 ha! the +alue 1444) T@4 %ill hol" the high /#te of the +alue <: "ecimal= an" TL4 %ill contain the lo% /#te of the +alue <8:8 "ecimal=$ Re+ie%ing lo%,high /#te notation) recall that #ou mu!t multi l# the high /#te /# 859 an" a"" the lo% /#te to calculate the final +alue$ That i! to !a#: T@4 R 859 K TL4 L 1444 : R 859 K 8:8 L 1444 Timer 1 %or0! the e1act !ame %a#) /ut it! 27R! are T@1 an" TL1$ 2ince there are onl# t%o /#te! "e+ote" to the +alue of each timer it i! a arent that the ma1imum +alue a timer ma# ha+e i! 95)5:5$ If a timer contain! the +alue 95)5:5 an" i! !u/!e-uentl# incremente") it %ill re!et--or overflow--/ac0 to 4$ T)e T&OD SFR Let! fir!t tal0 a/out our fir!t control 27R: TMO. <Timer Mo"e=$ The TMO. 27R i! u!e" to control the mo"e of o eration of /oth timer!$ Each /it of the 27R gi+e! the microcontroller ! ecific information concerning ho% to run a timer$ The high four /it! </it! > through ?= relate to Timer 1 %herea! the lo% four /it! </it! 4 through := erform the e1act !ame function!) /ut for timer 4$

The in"i+i"ual /it! of TMO. ha+e the follo%ing function!: TMO. <89h= 27R -it Na'e ? E*$lanation o% Function Ti'er 1

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

INT1 <&:$:= i! high$ 5hen thi! /it i! clear the timer %ill run regar"le!! of the !tate of INT1$ 5hen thi! /it i! !et the timer %ill count e+ent! on 9 5 > : C,T1 T1M1 T1M4 T1 <&:$5=$ 5hen thi! /it i! clear the timer %ill /e incremente" e+er# machine c#cle$ Timer mo"e /it <!ee /elo%= Timer mo"e /it <!ee /elo%= 5hen thi! /it i! !et the timer %ill onl# run %hen 3ATE4 INT4 <&:$8= i! high$ 5hen thi! /it i! clear the timer 4 %ill run regar"le!! of the !tate of INT4$ 5hen thi! /it i! !et the timer %ill count e+ent! on 8 1 C,T4 T4M1 T4 <&:$>=$ 5hen thi! /it i! clear the timer %ill /e incremente" e+er# machine c#cle$ Timer mo"e /it <!ee /elo%= 4 4 T4M4 Timer mo"e /it <!ee /elo%= 4 A! #ou can !ee in the a/o+e chart) four /it! <t%o for each timer= are u!e" to ! ecif# a mo"e of o eration$ The mo"e! of o eration are: T*&1 4 4 1 1 T*&0 4 1 4 1 Timer Mo"e 4 1 8 : .e!cri tion of Mo"e 1:-/it Timer$ 19-/it Timer 8-/it auto-reloa" 2 lit timer mo"e 4 1 1 1

13(<it Ti'e &o+e 6'o+e 07 Timer mo"e F4F i! a 1:-/it timer$ Thi! i! a relic that %a! 0e t aroun" in the 8451 to maintain com ata/ilit# %ith it! re"ece!or) the 84>8$ 3enerall# the 1:-/it timer mo"e i! not u!e" in ne% "e+elo ment$ 5hen the timer i! in 1:-/it mo"e) TL1 %ill count from 4 to :1$ 5hen TL1 i! incremente" from :1) it %ill Fre!etF to 4 an" increment T@1$ Thu!) effecti+el#) onl# 1: /it! of the t%o timer /#te! are /eing u!e": /it! 4-> of TL1 an" /it! 4-? of T@1$ Thi! al!o mean!) in

e!!ence) the timer can onl# contain 8198 +alue!$ If #ou !et a 1:-/it timer to 4) it %ill o+erflo% /ac0 to Bero 8198 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 !ur ri!e" if #ou e+er en" u anal#Bing archaeic co"e %hich ha! /een a!!e" "o%n through the generation! <a generation in a rogramming !ho i! often on the or"er of a/out : or > month!=$ 13(<it Ti'e &o+e 6'o+e 17 Timer mo"e F1F i! a 19-/it timer$ Thi! i! a +er# commonl# u!e" mo"e$ It function! Hu!t li0e 1:-/it mo"e e1ce t that all 19 /it! are u!e"$ TL1 i! incremente" from 4 to 855$ 5hen TL1 i! incremente" from 855) it re!et! to 4 an" cau!e! T@1 to /e incremente" /# 1$ 2ince thi! i! a full 19-/it timer) the timer ma# contain u to 955:9 "i!tinct +alue!$ If #ou !et a 19-/it timer to 4) it %ill o+erflo% /ac0 to 4 after 95)5:9 machine c#cle!$ 5(<it Ti'e &o+e 6'o+e 27 Timer mo"e F8F i! an 8-/it auto-reloa" mo"e$ 5hat i! that) #ou ma# a!0N 2im le$ 5hen a timer i! in mo"e 8) T@1 hol"! the Freloa" +alueF an" TL1 i! the timer it!elf$ Thu!) TL1 !tart! counting u $ 5hen TL1 reache! 855 an" i! !u/!e-uentl# incremente") in!tea" of re!etting to 4 <a! in the ca!e of mo"e! 4 an" 1=) it %ill /e re!et to the +alue !tore" in T@1$ 7or e1am le) let! !a# T@4 hol"! the +alue 7.h an" TL4 hol"! the +alue 7Eh$ If %e %ere to %atch the +alue! of T@4 an" TL4 for a fe% machine c#cle! thi! i! %hat %e" !ee: &ac)ine C#cle TH0 Value T 0 Value 1 8 : > 5 7.h 7.h 7.h 7.h 7.h 7Eh 77h 7.h 7Eh 77h

9 ?

7.h 7.h

7.h 7Eh

A! #ou can !ee) the +alue of T@4 ne+er change"$ In fact) %hen #ou u!e mo"e 8 #ou almo!t al%a#! !et T@1 to a 0no%n +alue an" TL1 i! the 27R that i! con!tantl# incremente"$ 5hat6! the /enefit of auto-reloa" mo"eN &erha ! #ou %ant the timer to al%a#! ha+e a +alue from 844 to 855$ If #ou u!e mo"e 4 or 1) #ou" ha+e to chec0 in co"e to !ee if the timer ha" o+erflo%e" an") if !o) re!et the timer to 844$ Thi! ta0e! reciou! in!truction! of e1ecution time to chec0 the +alue an",or to reloa" it$ 5hen #ou u!e mo"e 8 the microcontroller ta0e! care of thi! for #ou$ Once #ou+e configure" a timer in mo"e 8 #ou "ont ha+e to %orr# a/out chec0ing to !ee if the timer ha! o+erflo%e" nor "o #ou ha+e to %orr# a/out re!etting the +alue--the microcontroller har"%are %ill "o it all for #ou$ The auto-reloa" mo"e i! +er# commonl# u!e" for e!ta/li!hing a /au" rate %hich %e %ill tal0 more a/out in the 2erial Communication! cha ter$ S$lit Ti'er &o+e 6'o+e 37 Timer mo"e F:F i! a ! lit-timer mo"e$ 5hen Timer 4 i! lace" in mo"e :) it e!!entiall# /ecome! t%o !e arate 8-/it timer!$ That i! to !a#) Timer 4 i! TL4 an" Timer 1 i! T@4$ Goth timer! count from 4 to 855 an" o+erflo% /ac0 to 4$ All the /it! that are relate" to Timer 1 %ill no% /e tie" to T@4$ 5hile Timer 4 i! in ! lit mo"e) the real Timer 1 <i$e$ T@1 an" TL1= can /e ut into mo"e! 4) 1 or 8 normall#--ho%e+er) #ou ma# not !tart or !to the real timer 1 !ince the /it! that "o that are no% lin0e" to T@4$ The real timer 1) in thi! ca!e) %ill /e incremente" e+er# machine c#cle no matter %hat$ The onl# real u!e I can !ee of u!ing ! lit timer mo"e i! if #ou nee" to ha+e t%o !e arate timer! an") a""itionall#) a /au" rate generator$ In !uch ca!e #ou can u!e the real Timer 1 a! a /au" rate generator an" u!e T@4,TL4 a! t%o !e arate timer!$

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 <88h= 27R -it Na'e ? 9 5 > T71 TR1 T74 TR4 -it A++ress 87h 8Eh 8.h 8Ch E*$lanation o% Function Ti'er 1 ODer%lo>$ Thi! /it i! !et /# the microcontroller %hen Timer 1 o+erflo%!$ Ti'er 1 Run$ 5hen thi! /it i! !et Timer 1 i! turne" on$ 5hen thi! /it i! clear Timer 1 i! off$ Ti'er 0 ODer%lo>$ Thi! /it i! !et /# the microcontroller %hen Timer 4 o+erflo%!$ Ti'er 0 Run$ 5hen thi! /it i! !et Timer 4 i! turne" Ti'er 1 1 4

4 on$ 5hen thi! /it i! clear Timer 4 i! off$ A! #ou ma# notice) %e+e onl# "efine" > 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 Interru t! an" the# %ill /e "i!cu!!e" in the cha ter that a""re!!e! interru t!$ 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 5hat "oe! thi! meanN It mean! if #ou %ant to !et the /it T71-%hich i! the highe!t /it of TCON--#ou coul" e1ecute the comman": MOA TCON) O84h $$$ Or) !ince the 27R i! /it-a""re!!a/le) #ou coul" Hu!t e1ecute 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! /ita""re!!a/le$ InitialiEin, a Ti'er

No% that %e+e "i!cu!!e" the timer-relate" 27R! %e are rea"# to %rite co"e that %ill initialiBe the timer an" !tart it running$ A! #oull recall) %e fir!t mu!t "eci"e %hat mo"e %e %ant the timer to /e in$ In thi! ca!e %e %ant a 19-/it timer that run! continuou!l#I that i! to !a#) it i! not "e en"ent on an# e1ternal in!$ 5e mu!t fir!t initialiBe the TMO. 27R$ 2ince %e are %or0ing %ith timer 4 %e %ill /e u!ing the lo%e!t > /it! of TMO.$ The fir!t t%o /it!) 3ATE4 an" C,T4 are /oth 4 !ince %e %ant the timer to /e in"e en"ent of the e1ternal in!$ 19-/it mo"e i! timer mo"e 1 !o %e mu!t clear T4M1 an" !et T4M4$ Effecti+el#) the onl# /it %e %ant to turn on i! /it 4 of TMO.$ Thu! to initialiBe the timer %e e1ecute the in!truction: MOA TMO.)O41h Timer 4 i! no% in 19-/it timer mo"e$ @o%e+er) the timer i! not running$ To !tart the timer running %e mu!t !et the TR4 /it 5e can "o that /# e1ecuting the in!truction: 2ETG TR4 ' on e1ecuting the!e t%o in!truction! timer 4 %ill imme"iatel# /egin counting) /eing incremente" once e+er# machine c#cle <e+er# 18 cr#!tal ul!e!=$ Rea+in, t)e Ti'er There are t%o common %a#! of rea"ing the +alue of a 19-/it timerI %hich #ou u!e "e en"! on #our ! ecific a lication$ Jou ma# either rea" the actual +alue of the timer a! a 19-/it num/er) or #ou ma# !im l# "etect %hen the timer ha! o+erflo%e"$ Rea+in, t)e Dalue o% a Ti'er If #our timer i! in an 8-/it mo"e--that i!) either 8-/it Auto Reloa" mo"e or in ! lit timer mo"e--then rea"ing the +alue of the timer i! !im le$ Jou !im l# rea" the 1-/#te +alue of the timer an" #ou6re "one$ @o%e+er) if #oure "ealing %ith a 1:-/it or 19-/it timer the chore i! a little more com licate"$ Con!i"er %hat %oul" ha en if #ou rea" the lo% /#te of the timer a! 855)

then rea" the high /#te of the timer a! 15$ In thi! ca!e) %hat actuall# ha ene" %a! that the timer +alue %a! 1>,855 <high /#te 1>) lo% /#te 855= /ut #ou rea" 15,855$ 5h#N Gecau!e #ou rea" the lo% /#te a! 855$ Gut %hen #ou e1ecute" the ne1t 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>,855 to 15,4$ Gut in the roce!! #ou+e rea" the timer a! /eing 15,855$ O/+iou!l# there! a ro/lem there$ The !olutionN 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 re eat the c#cle$ In co"e) thi! %oul" a ear a!: RE&EAT: MOA A)T@4 MOA R4)TL4 CMNE A)T@4)RE&EAT $$$ In thi! ca!e) %e loa" the accumulator %ith the high /#te of Timer 4$ 5e 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$ 5hen the loo e1it! %e %ill ha+e the lo% /#te of the timer in R4 an" the high /#te in the Accumulator$ Another much !im ler alternati+e i! to !im l# turn off the timer run /it <i$e$ CLR TR4=) rea" the timer +alue) an" then turn on the timer run /it <i$e$ 2ETG TR4=$ In that ca!e) the timer i!nt running !o no ! ecial tric0! are nece!!ar#$ Of cour!e) thi! im lie! that #our timer %ill /e !to e" for a fe% machine c#cle!$ 5hether or not thi! i! tolera/le "e en"! on #our ! ecific a lication$

AN- TF0/FIIf T74 i! not !et) Hum /ac0 to thi! !ame in!truction

In the a/o+e co"e the fir!t t%o line! initialiBe the Timer 4 !tarting +alue to 19)>5?$ The ne1t t%o in!truction! configure timer 4 an" turn it on$ 7inall#) the la!t in!truction ANTF0/F) rea"! FMum ) if T74 i! not !et) /ac0 to thi! !ame in!truction$F The FSF o eran" 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 e1ecuting thi! !ame in!truction$ After 1,84th of a !econ" timer 4 %ill o+erflo%) !et the T74 /it) an" rogram e1ecution %ill then /rea0 out of the loo $