Professional Documents
Culture Documents
Teks Dasar Fisika Radiasi
Teks Dasar Fisika Radiasi
(C) Copyright 1997, 1998, 1999 by Vault Information Services. All Rights Reserved.
http:\\www.8052.com
Contents
8051 TUTORIAL:
INTRODUCTION ...............................................................................................................................1
8051 TUTORIAL: TYPES OF
MEMORY ........................................................................................................................2
8051 TUTORIAL:
SFRS ..............................................................................................................................................5
8051 TUTORIAL: BASIC
REGISTERS ...........................................................................................................................9
8051 TUTORIAL: ADDRESSING
MODES ....................................................................................................................11
8051 TUTORIAL: PROGRAM
FLOW...........................................................................................................................13
8051 TUTORIAL: INSTRUCTION SET, TIMING, AND LOW-LEVEL
INFO .....................................................................15
8051 TUTORIAL: TIM
ERS .........................................................................................................................................16
8051 TUTORIAL: SERIAL
COMMUNICATION.............................................................................................................22
8051 TUTORIAL:
INTERRUPTS..................................................................................................................................25
1
8051 Tutorial: Types of Memory
The 8051 has three very general types of necessary to have a basic
understanding of these
memory. To effectively program the 8051 it is memory types.
Code Memory
Code memory is the memory that holds This varies depending on the version of
the chip
the actual 8051 program that is to be run. This that is being used. Each version offers
specific
memory is limited to 64K and comes in many capabilities and one of the distinguis hing
factors
shapes and sizes: Code memory may be found from chip to chip is how muc h
ROM/EPROM
on-chip, either burned into the microcontroller as space the chip has.
ROM or EPROM. Code may also be stored However, code memory is most commonly
completely off-chip in an external ROM or, more implemented as off-chip EPROM. This
is
commonly, an external EPROM. Flash RAM is especially true in low-cost development
systems
also another popular method of storing a program. and in systems developed by students.
Programming
Tip: Since code
memory is
Various combinations of these memory types may
restricted to 64K, 8051
programs are limited to
64K.
also be used--that is to say, it is possible to have
Some assemblers and
compilers offer ways to get
4K of code memory on-chip and 64k of code
around this limit when used
with specially wired
memory off-chip in an EPROM.
hardware. However,
without such special
compilers and
When the program is stored on-chip the
hardware, programs are
limited to 64K.
64K maximum is often reduced to 4k, 8k, or 16k.
External RAM
2
On-Chip Memory.
As mentioned at the beginning of this (SFR) memory. The layout of the 8051's
internal
chapter, the 8051 includes a c ertain amount of on- memory is presented in the following
memory
As is illustrated in this map, the 8051 has a Bit Memory also lives and is part of
internal
bank of 128 bytes of Internal RAM. This Internal RAM
RAM. We'll talk more
about bit memory very s
hortly,
is found on-chip on the 8051 so it is the fastest RAM but for now just keep in mind that bit
memory ac tually
available, and it is also the most flexible in terms of resides in internal RAM, from
addresses 20h through
reading, writing, and modifying it’s contents. Internal 2Fh.
RAM is volatile, so when the 8051 is reset this The 80 bytes remaining of Internal RAM,
from
memory is cleared. addresses 30h through 7Fh, may be used by user
The 128 bytes of internal ram is subdivided variables that need to be accessed
frequently or at
as shown on the memory map. The first 8 bytes (00h high-speed. This area is also utilized
by the
- 07h) are "register bank 0". By manipulating certain microcontroller as a storage area for
the operating
SFRs, a program may choose to use register banks stack. This fact severely limits the
8051’s stack sinc e,
1, 2, or 3. These alternative register bank s are as illustrated in the memory map, the area
reserved
located in internal RAM in addresses 08h through for the stack is only 80 bytes--and
usually it is less
1Fh. since this 80 bytes has to be shared between the
We'll disc uss "register banks" more in a later stack and user variables.
chapter. For now it is sufficient to know that they "live"
and are part of internal RAM.
Register Banks
The 8051 us es 8 "R" registers which are the above instruction acc omplishes
the same thing
used in many of its instructions. These "R" as the following operation:
regis ters are numbered from 0 through 7 (R0, R1,
ADD A,04h
R2, R3, R4, R5, R6, and R7). These registers are
generally used to assist in manipulating values
This instruction
adds the value
found in
and moving data from one memory location to
Internal RAM addres s 04h
to the value of the
another. For example, to add the value of R4 to
Accumulator, leaving the
result in the Accumulator.
the Accumulator, we would exec ute the following
Since R4 is really Internal
RAM 04h, the above
instruction:
instruction effectively ac
complis hed the same
thing.
ADD A,R4
But watch out! As
the memory map
However, as the memory map shows, the shows, the 8051 has four distinct
register banks.
"R" Register R4 is really part of Internal RAM. When the 8051 is first booted up, register
bank 0
Specifically, R4 is address 04h. This can be see in (addresses 00h through 07h) is used by
default.
the bright green section of the memory map. Thus However, your program may instruct
the 8051 to
3
use one of the alternate register banks; i.e., interrupts later). However, always remember
that
regis ter banks 1, 2, or 3. In this case, R4 will no the register banks really reside in the firs
t 32 bytes
longer be the same as Internal RAM address 04h. of Internal RAM.
Programming
Tip: If you only
use the first
For example, if your program instructs the 8051 to
register bank (i.e. bank 0),
you may use Internal RAM
use register bank 3, "R" register R4 will now be
locations 08h through 1Fh
for your own use. But if you
synonomous with Internal RAM address 1Ch.
plan to use register banks
1, 2, or 3, be very careful
The concept of register banks adds a
about using addresses
below 20h as you may end
up
great level of flexibility to the 8051, especially
overwriting the value of
your "R" registers!
when dealing with interrupts (we'll talk about
Bit Memory
The 8051, being a c ommunications- Bit variables 00h through 7Fh are for user-
oriented microcontroller, gives the us er the ability defined functions in their programs.
However, bit
to access a number of bit variables. These variables 80h and above are actually used to
variables may be either 1 or 0. access c ertain SFRs on a bit-by-bit basis. For
There are 128 bit variables available to the example, if output lines P0.0 through
P0.7 are all
user, numberd 00h through 7Fh. The user may clear (0) and you want to turn on the P0.0
output
make use of these variables with commands such line you may either execute:
as SETB and CLR.
MOV P0,#01h ||
SETB 80h
It is important to note that Bit Memory is
really a part of Internal RAM. In fact, the 128 bit
Both these
instructions
accomplish the
variables occupy the 16 bytes of Internal RAM
same thing. However, using
the SETB c ommand
from 20h through 2Fh. Thus, if you write the value
will turn on the P0.0 line
without effec ting the
FFh to Internal RAM address 20h you’ve
status of any of the other P0
output lines. The
effectively set bits 00h through 07h.
MOV command effectively
turns off all the other
But since the 8051 provides special
output lines which, in some
cases, may not be
instructions to access these 16 bytes of memory
acceptable.
on a bit by bit basis it is useful to think of it as a
Programming
Tip: By default,
the 8051
separate type of memory. However, always keep
initializes the Stack Pointer
(SP) to 08h when the
in mind that it is just a subset of Internal RAM--and
microcontroller is booted.
This means that the stack
will
that operations performed on Internal RAM can
start at address 08h and
expand upwards. If you will
be
change the values of the bit variables.
using the alternate register
banks (banks 1, 2 or 3) you
Programming Tip: If your program does not
must initialize the stack
pointer to an address above
the
use bit variables, you may use Internal RAM locations
highest register bank you
will be using, otherwise the
20h through 2Fh for your own use. But if you plan to use
stack will overwrite your
alternate register banks.
bit variables, be very careful about using addresses
Similarly, if you will be
using bit variables it is
usually a
from 20h through 2Fh as you may end up overwriting
good idea to initialize the
stack pointer to some value
the value of your bits!
greater than 2Fh to
guarantee that your bit
variables are
protected from the stack.
Special Function Registers (SFRs) are 99 Hex. Thus, to write the value "1" to
the s erial
areas of memory that control specific functionality port you would ex ecute the
instruction:
of the 8051 processor. For example, four SFRs
MOV 99h,#01h
permit access to the 8051’s 32 input/output lines.
Another SFR allows a program to read or write to
As you can see, it
appears that the
SFR is
the 8051’s serial port. Other SFRs allow the user
part of Internal Memory.
This is not the case.
to set the serial baud rate, control and access
When using this method of
memory access (it’s
timers, and configure the 8051’s interrupt s ystem.
called direct address), any
instruction that has an
When programming, SFRs have the
address of 00h through 7Fh
refers to an Internal
illusion of being Internal Memory. For example, if
RAM memory address; any
instruction with an
you want to write the value "1" to Internal RAM
address of 80h through FFh
refers to an SFR
location 50 hex you would execute the instruction:
control regis ter.
Programming
Tip: SFRs are
used to control
MOV 50h,#01h
the way the 8051 functions.
Each SFR has a specific
purpose and format which
will be discussed later. Not
all
Similarly, if you want to write the value "1"
addresses above 80h are
assigned to SFRs. However,
to the 8051’s serial port you would write this value
this area may NOT be used
as additional RAM memory
to the SBUF SFR, which has an SFR address of
even if a given address has
not been assigned to an
SFR.
4
8051 Tutorial: SFRs
What Are SFRs?
The 8051 is a flexible microcontroller with RAM is from address 00h through
7Fh whereas
a relatively large number of modes of operations. SFR registers exist in the address range
of 80h
Your program may inspect and/or change the through FFh.
operating mode of the 8051 by manipulating the Each SFR has an address (80h through
values of the 8051's Special Func tion Registers FFh) and a name. The following chart
provides a
(SFRs). graphical presentation of the 8051's SFRs, their
SFRs are accessed as if they were normal names, and their address.
SFR Types