8051 Tutorial: Introduction

You might also like

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 13

8051 Tutorial

(C) Copyright 1997, 1998, 1999 by Vault Information Services !ll "ights "eserve#
http$%%&&&8'()com
Contents
8'(1 *+*,"I!-$
I.*",/+C*I,. 1
8'(1 *+*,"I!-$ *012S ,3
424,"0 )
8'(1 *+*,"I!-$
S3"S (
8'(1 *+*,"I!-$ 5!SIC
"26IS*2"S 9
8'(1 *+*,"I!-$ !//"2SSI.6
4,/2S 11
8'(1 *+*,"I!-$ 1",6"!4
3-,718
8'(1 *+*,"I!-$ I.S*"+C*I,. S2*, *I4I.6, !./ -,79-2V2-
I.3, 1(
8'(1 *+*,"I!-$ *I4
2"S 1:
8'(1 *+*,"I!-$ S2"I!-
C,44+.IC!*I,.))
8'(1 *+*,"I!-$
I.*2""+1*S)(
8051 Tutorial: Introduction
/espite it;s relatively ol# age, the 8'(1 is programming *he appen#ices are a useful
one of the mos t popular microcontrollers in use reference tool that &ill assist both the novice
to#ay 4any #erivative microcontrollers have since programmer as &ell as the e<perience#
been #evelope# that are base# on99an# professional #eveloper
compatible &ith99the 8'(1 *hus, the ability to *his #ocument assumes the follo&ing$
program an 8'(1 is an important s=ill for anyone
> ! general =no&le#ge of programming
&ho plans to #ev elop pro#ucts that &ill ta=e
> !n un#erstan#ing of #ecimal, he<
i#ecimal, an#
a#vantage of microcontrollers
binary number systems
4any &eb pages, boo=s, an# tools are
> ! general =no&le#ge of har#&are
available for the 8'(1 #eveloper
*hat is to say, no =no&le#ge of the
8'(1
I hope the information containe# in this
is assume#99ho&ever, it is as sume# you;ve
#one
#ocument?&eb page &ill assist you in mastering
some amount of programming before, have a
8'(1 programming 7hile it is not my intention that
basic un#erstan#ing of har#&are, an# a firm
grasp
this #ocument replac e a har#copy boo= purchase#
on the three numbering s ystems mentione#
above
at your local boo= s tore, it is entirely poss ible that
*he concept of converting a number from
this may be the case It is li=ely that this #ocument
#eciminal to he<i#ecimal an#?or to binary is
not
contains everything you &ill nee# to learn 8'(1
&ithin the scope of this #ocument99an# if
you can;t
assembly language programming ,f course, this
#o those types of conversions there are
probably
#ocument is free an# you get &hat you pay for so
some concepts that &ill not be completely
if, after rea#ing this #ocument, you still are los t you
un#erstan#able
may fin# it necessary to buy a boo=
*his #ocument attempts to a##ress the nee#
of
*his #ocument is both a tutorial an# a
the typical programmer 3or e<ample, there
are
reference tool *he various chapters of the
certain features that are nifty an# in some
cases
#ocument &ill e<plain the 8'(1 step by step *he
very useful99but 9(@ of the programmers
&ill
chapters are targete# at people &ho are
never use these features
attempting to learn 8'(1 assembly language
1
8051 Tutorial: Types of Memory
*he 8'(1 has three very general types of necessary to have a basic
un#erstan#ing of these
memory *o effectively program the 8'(1 it is memory types
On- Chip Memory refers to any
memory External Code
Memory is c ode or
(Co#e, "!4, or other) that physically e<ists on the program) memory that resi#es off9
chip *his is
microcontroller itself ,n9chip memory can be of often in the form of an e<ternal
21",4
several types, but &eAll get into that shortly 2<ternal "!4 is "!4 memory that
resi#es off9chip *his is
often in the form of
stan#ar# static "!4 or
flash "!4
Code Memory
Co#e memory is the memory that hol#s *his varies #epen#ing on the version of
the chip
the actual 8'(1 program that is to be run *his that is being use# 2ach version offers
specific
memory is limite# to :BC an# comes in many capabilities an# one of the #istinguis hing
factors
shapes an# siDes$ Co#e memory may be foun# from chip to chip is ho& muc h
",4?21",4
on-chip, either burned into the microcontroller as space the chip has.
",4 or 21",4 Co#e may also be store# Eo&ever, co#e memory is most commonly
completely off9chip in an e<ternal ",4 or, more implemente# as off9chip 21",4 *his
is
commonly, an e<ternal 21",4 3lash "!4 is especially true in lo&9cost #evelopment
systems
also another popular metho# of storing a program an# in systems #evelope# by stu#ents
Programming
Tip: Since code
memory is
Various combinations of these memory types may
restricted to 64K, 8051
programs are limited to
64K.
also be use#99that is to say, it is possible to have
Some assemblers and
compilers offer was to get
BC of co#e memory on9chip an# :B= of co#e
around this limit when used
with speciall wired
memory off9chip in an 21",4
hardware. !owe"er,
without such special
compilers and
7hen the program is store# on9chip the
hardware, programs are
limited to 64K.
:BC ma<imum is often re#uce# to B=, 8=, or 1:=
External !"M
!s an obvious opposite of Internal "!4, 7hat 2<ternal "!4 loses in spee#
an#
the 8'(1 also supports &hat is calle# 2<ternal fle<ibility it gains in Fuantity 7hile
Internal "!4 is
#$%. $s the name suggests, &'ternal #$% is an limited to 1(8 btes the 8051 supports
&'ternal
ran#om access memory &hich is foun# off9chip "!4 up to :BC
Programming
Tip: The 8051
may only
Since the memory is off9chip it is not as fle<ible in
address 64) of #$%. *o
e'pand #$% beond this
limit
terms of accessing, an# is also slo&er 3or
re+uires programming and
hardware tric)s. ,ou ma
e<ample, to increment an Internal "!4 location by
ha"e to do this -b hand-
since man compilers and
1 reFuires only 1 instruction an# 1 instruction
assemblers, while
pro"iding support for
programs in
cycle *o increment a 19byte value store# in
e'cess of 64), do not
support more than 64) of
#$%.
2< ternal "!4 reFuires B instruc tions an# 7
*his is rather strange since
it has been m e'perience
instruction cyc les In this case, e<ternal memory is
that programs can usuall
fit in 64) but often #$% is
7 times slo&erG
what is lac)ing. *hus if ou
need more than 64) of
#$%,
chec) to see if our
compiler supports it-- but if
it
doesn.t, be prepared to do
it b hand.
)
On-Chip Memory#
!s mentione# at the beginning of this (S3") memory *he layout of the 8'(1As
internal
chapter, the 8'(1 inclu#es a c ertain amount of on9 memory is presente# in the follo&ing
memory
chip memory ,n9chip
memory is really
one of t&o map$
!s is illustrate# in this map, the 8'(1 has a 5it 4emory also lives an# is part of
internal
ban= of 1)8 bytes of Internal "!4 *his Internal "!4
"!4 7eAll tal= more
about bit memory very s
hortly,
is foun# on9chip on the 8'(1 so it is the fastest "!4 but for no& Hust =eep in min# that bit
memory ac tually
available, an# it is also the most fle<ible in terms of resi#es in internal "!4, from
a##resses )'h through
rea#ing, &riting, an# mo#ifying it;s contents Internal )3h
"!4 is volatile, so &hen the 8'(1 is reset this *he 8' bytes remaining of Internal "!4,
from
memory is cleare# a##resses 8'h through 73h, may be use# by user
*he 1)8 bytes of internal ram is sub#ivi#e# variables that nee# to be accesse#
freFuently or at
as sho&n on the memory map *he first 8 bytes (''h high9spee# *his area is also utiliDe#
by the
9 '7h) are Iregister ban= 'I 5y manipulating certain microcontroller as a storage area for
the operating
S3"s, a program may choose to use register ban=s stac= *his fact severely limits the
8'(1;s stac= sinc e,
1, ), or 8 *hese alternative register ban= s are as illustrate# in the memory map, the area
reserve#
locate# in internal "!4 in a##resses '8h through for the stac= is only 8' bytes99an#
usually it is less
13h since this 8' bytes has to be share# bet&een the
7eAll #isc uss Iregister ban=sI more in a later stac= an# user variables
chapter 3or no& it is sufficient to =no& that they IliveI
an# are part of internal "!4
!e$ister %an&s
*he 8'(1 us es 8 I"I registers &hich are the above instruction acc omplishes
the same thing
use# in many of its instructions *hese I"I as the follo&ing operation$
regis ters are numbere# from ' through 7 ("', "1,
!// !,'Bh
"), "8, "B, "(, ":, an# "7) *hese registers are
generally use# to assist in manipulating values
*his instruction
a##s the value
foun# in
an# moving #ata from one memory location to
Internal "!4 a##res s 'Bh
to the value of the
another 3or e<ample, to a## the value of "B to
!ccumulator, leaving the
result in the !ccumulator
the !ccumulator, &e &oul# e<ec ute the follo&ing
Since "B is really Internal
"!4 'Bh, the above
instruction$
instruction effectively ac
complis he# the same
thing
!// !,"B
5ut &atch outG !s
the memory map
Eo&ever, as the memory map sho&s, the sho&s, the 8'(1 has four #istinct
register ban=s
I"I "egister "B is really part of Internal "!4 7hen the 8'(1 is first boote# up, register
ban= '
Specifically, "B is a##ress 'Bh *his can be see in (a##resses ''h through '7h) is use# by
#efault
the bright green section of the memory map *hus Eo&ever, your program may instruct
the 8'(1 to
8
use one of the alternate register ban=sJ ie, interrupts later) Eo&ever, al&ays remember
that
regis ter ban=s 1, ), or 8 In this case, "B &ill no the register ban=s really resi#e in the firs
t 8) bytes
longer be the same as Internal "!4 a##ress 'Bh of Internal "!4
Programming
Tip: If you only
use the first
3or e<ample, if your program instructs the 8'(1 to
register ban) /i.e. ban) 00,
ou ma use 1nternal #$%
use register ban= 8, I"I register "B &ill no& be
locations 08h through 12h
for our own use. 3ut if ou
synonomous &ith Internal "!4 a##ress 1Ch
plan to use register ban)s
1, (, or 4, be "er careful
*he concept of register ban=s a##s a
about using addresses
below (0h as ou ma end
up
great level of fle<ibility to the 8'(1, especially
o"erwriting the "alue of
our -#- registers5
&hen #ealing &ith interrupts (&eAll tal= about
%it Memory
*he 8'(1, being a c ommunications9 5it variables ''h through 73h are for user9
oriente# microcontroller, gives the us er the ability #efine# functions in their programs
Eo&ever, bit
to access a number of bit variables *hese variables 8'h an# above are actually use# to
variables may be either 1 or ' access c ertain S3"s on a bit9by9bit basis 3or
*here are 1)8 bit variables available to the e<ample, if output lines 1'' through
1'7 are all
user, number# ''h through 73h *he user may clear (') an# you &ant to turn on the 1''
output
ma=e use of these variables &ith comman#s such line you may either e<ecute$
as S2*5 an# C-"
4,V 1',K'1h LL
S2*5 8'h
It is important to note that 5it 4emory is
really a part of Internal "!4 In fact, the 1)8 bit
5oth these
instructions
accomplish the
variables occupy the 1: bytes of Internal "!4
same thing Eo&ever, using
the S2*5 c omman#
from )'h through )3h *hus, if you &rite the value
&ill turn on the 1'' line
&ithout effec ting the
33h to Internal "!4 a##ress )'h you;ve
status of any of the other 1'
output lines *he
effectively set bits ''h through '7h
4,V comman# effectively
turns off all the other
5ut since the 8'(1 provi#es special
output lines &hich, in some
cases, may not be
instructions to access these 1: bytes of memory
acceptable
on a bit by bit basis it is useful to thin= of it as a
Programming
Tip: By default,
the 8051
separate type of memory Eo&ever, al&ays =eep
initiali6es the Stac) 7ointer
/S70 to 08h when the
in min# that it is Hust a subset of Internal "!499an#
microcontroller is booted.
*his means that the stac)
will
that operations performe# on Internal "!4 can
start at address 08h and
e'pand upwards. 1f ou will
be
change the values of the bit variables
using the alternate register
ban)s /ban)s 1, ( or 40 ou
Programming Tip: If your program does not
must initiali6e the stac)
pointer to an address abo"e
the
use bit "ariables, ou ma use 1nternal #$% locations
highest register ban) ou
will be using, otherwise the
(0h through (2h for our own use. 3ut if ou plan to use
stac) will o"erwrite our
alternate register ban)s.
bit "ariables, be "er careful about using addresses
Similarl, if ou will be
using bit "ariables it is
usuall a
from (0h through (2h as ou ma end up o"erwriting
good idea to initiali6e the
stac) pointer to some "alue
the "alue of our bits5
greater than (2h to
guarantee that our bit
"ariables are
protected from the stac).
'pecial (unction !e$ister '(!) Memory
Special 3unction "egisters (S3"s) are 99 Ee< *hus, to &rite the value I1I to
the s erial
areas of memory that control specific functionality port you &oul# e< ecute the
instruction$
of the 8'(1 processor 3or e<ample, four S3"s
4,V 99h,K'1h
permit access to the 8'(1;s 8) input?output lines
!nother S3" allo&s a program to rea# or &rite to
!s you can see, it
appears that the
S3" is
the 8'(1;s serial port ,ther S3"s allo& the user
part of Internal 4emory
*his is not the case
to set the serial bau# rate, control an# access
7hen using this metho# of
memory access (it;s
timers, an# configure the 8'(1;s interrupt s ystem
calle# #irect a##ress), any
instruction that has an
7hen programming, S3"s have the
a##ress of ''h through 73h
refers to an Internal
illusion of being Internal 4emory 3or e<ample, if
"!4 memory a##ressJ any
instruction &ith an
you &ant to &rite the value I1I to Internal "!4
a##ress of 8'h through 33h
refers to an S3"
location (' he< you &oul# e<ecute the instruction$
control regis ter
Programming
Tip: SFs are
used to control
4,V ('h,K'1h
the wa the 8051 functions.
&ach S2# has a specific
purpose and format which
will be discussed later. 8ot
all
Similarly, if you &ant to &rite the value I1I
addresses abo"e 80h are
assigned to S2#s. !owe"er,
to the 8'(1;s serial port you &oul# &rite this value
this area ma 89* be used
as additional #$% memor
to the S5+3 S3", &hich has an S3" a##ress of
e"en if a gi"en address has
not been assigned to an
S2#.
B
8051 Tutorial: '(!s
*hat "re '(!s+
*he 8'(1 is a fle<ible microcontroller &ith "!4 is from a##ress ''h through
73h &hereas
a relatively large number of mo#es of operations S3" registers e<ist in the a##ress range
of 8'h
0our program may inspect an#?or change the through 33h
operating mo#e of the 8'(1 by manipulating the 2ach S3" has an a##ress (8'h through
values of the 8'(1As Special 3unc tion "egisters 33h) an# a name *he follo&ing chart
provi#es a
(S3"s) graphical presentation of the 8'(1As S3"s, their
S3"s are accesse# as if they &ere normal names, an# their a##ress
Internal "!4 *he only #ifferenc e is that Internal
Programming
Tip: It is
recommended
that you
!s you can see, although the a##ress
not read or write to S2#
addresses that ha"e not
been
range of 8'h through 33h offer 1)8 possible
assigned to an S2#. :oing
so ma pro"o)e undefined
a##resses, there are only )1 S3"s in a stan#ar#
beha"ior and ma cause
our program to be
8'(1 !ll other a##resses in the S3" range (8'h
incompatible with other
8051-deri"ati"es that use
the
through 33h) are consi#ere# invali# 7riting to or
gi"en S2# for some other
purpose.
rea#ing from these registers may pro#uce
un#efine# values or behavior
'(! Types
!s mentione# in the chart itself, the S3"s be thought of as au<illary S3"s in
the sense that
that have a blue bac=groun# are S3"s relate# to they #onAt #irectly configure the 8'(1 but
obviously
the I?, ports *he 8'(1 has four I?, ports of 8 bits, the 8'(1 cannot operate &ithout them
3or
for a total of 8) I?, lines 7hether a giv en I?, line e<ample, once the serial port has been
configure#
is high or lo& an# the value rea# from the line are using SC,., the program may rea# or
&rite to the
controlle# by the S3"s in green serial port using the S5+3 register
Programming
Tip: The SFs
!hose names
*he S3"s &ith yello& bac=grouns are
appear in red in the chart
abo"e are S2#s that ma
be
S3"s &hich in some &ay control the operation or
accessed "ia bit operations
/i.e., using the S&*3 and
the configuration of some aspect of the 8'(1 3or
"# instructions$% The
other SFs cannot &e
accessed
e<ample, *C,. controls the timers, SC,.
using bit operations. $s
ou can see, all S2#s that
controls the serial port
whose addresses are
di"isible b 8 can be
accessed
*he remaining S3"s, &ith green
with bit operations.
bac=groun#s, are Iother S3"sI *hese S3"s can
(

You might also like