8051 - Basics
8051 - Basics
between
Microprocessor
and
Microcontroller
CRO CONTROLLER MICRO PROCESSER
s a single chip • It is a cpu
nsists Memory, • Memory, I/O Ports to be
ports connected externally.
CPU
CPU MEMORY
MEMORY
ever
Small size
Low cost
Low power
• What
is
a
Microcontroller?
•
A
single
chip
computer
or
A
CPU
with
all
the
peripherals
like
RAM,
ROM,
I/O
Ports,
Timers
,
ADCs
etc...
on
the
same
chip.
For
ex:
Motorola’s
6811,
Intel’s
8051,
Zilog’s
Z8
and
PIC
16X
etc.
4-‐bit
microcontroller
!
8-‐bit
microcontroller
!
16-‐bit
microcontroller
!
The
salient
features
of
8051
Microcontroller
:-‐
i.
4
KB
on
chip
program
memory
(ROM
or
EPROM)).
ii.
128
bytes
on
chip
data
memory(RAM).
iii.
8-‐bit
data
bus
iv.
16-‐bit
address
bus
v.
32
general
purpose
registers
each
of
8
bits
vi.
Two
-‐16
bit
Zmers/counters
T0
and
T1
vii.
Five
Interrupts
(3
internal
and
2
external).
ix.
Four
Parallel
ports
each
of
8-‐bits
(PORT0,
PORT1,PORT2,PORT3)
with
a
total
of
32
I/O
lines.
x.
One
16-‐bit
program
counter
and
One
16-‐bit
DPTR
(
data
pointer)
xi.
One
8-‐bit
stack
pointer
xii.
One
Microsecond
instrucZon
cycle
with
12
MHz
Crystal.
xiii.
One
full
duplex
serial
communicaZon
port.
A
Single
chip
microcontroller
Inside
Architecture
of
8051
External
interrupts
On-‐chip
Timer/Counter
CPU
Bus
Serial
4 I/O
Ports
OSC
Control
Port
10
PIN
Diagram
of
8051
Microcontroller
!
• The
8051
microcontroller
is
available
as
a
40
pin
DIP
chip.
•
It
works
at
+5
volts
DC.
• Among
the
40
pins
,
a
total
of
32
pins
are
allo_ed
for
the
four
parallel
ports
P0,P1,P2
and
P3
i.e
each
port
occupies
8-‐pins
.
• The
remaining
pins
are
VCC,
GND,
XTAL1,
XTAL2,
RST,
EA
,PSEN.
XTAL1,XTAL2:
These
two
pins
are
connected
to
Quartz
crystal
oscillator
which
runs
the
on-‐chip
oscillator.
The
quartz
crystal
oscillator
is
connected
to
the
two
pins
along
with
a
capacitor
of
30pF
as
shown
in
the
circuit.
If
we
use
a
source
other
than
the
crystal
oscillator,
it
will
be
connected
to
XTAL1
and
XTAL2
is
lef
unconnected.
!
• RST:
The
RESET
pin
is
an
input
pin
and
it
is
an
acZve
high
pin.
When
a
high
pulse
is
applied
to
this
pin
the
microcontroller
will
reset
and
terminate
all
acZviZes.
Upon
reset
all
the
registers
except
PC
will
reset
to
0000
Value
and
PC
register
will
reset
to
0007
value.
Register Reset Value
PC 0000
ACC 0000
B 0000
PSW 0000
SP 0007
DPTR 0000
Accessing
external
code
memory
Accessing External
Data Memory
Figure
2-11
Interface
to 1K
RAM
External code memory
WR
RD
PSEN
OE
ALE
74LS373
CS
G
P0.0
A0
D
P0.7
A7
D0
D7
EA
P2.0
A8
P2.7
A15
8051
ROM
External data memory
WR
WR
RD
RD
PSEN
ALE
74LS373
CS
G
P0.0
A0
D
P0.7
A7
D0
D7
EA
P2.0
A8
P2.7
A15
8051
RAM
8051 Internal Block Diagram
• A
and
B
Registers
:
The
A
and
B
registers
are
special
funcZon
registers
which
hold
the
results
of
many
arithmeZc
and
logical
operaZons
of
8051.The
A
register
is
also
called
the
Accumulator
and
as
it’s
name
suggests,
is
used
as
a
general
register
to
accumulate
the
results
of
a
large
number
of
instrucZons.
By
default
it
is
used
for
all
mathemaZcal
operaZons
and
also
data
transfer
operaZons
between
CPU
and
any
external
memory.
•
• The
B
register
is
mainly
used
for
mulZplicaZon
and
division
operaZons
along
with
A
register.
•
MUL
AB
:
DIV
AB.
• It
has
no
other
funcZon
other
than
as
a
locaZon
where
data
may
be
stored.
• The
R
registers:
The
"R"
registers
are
a
set
of
eight
registers
that
are
named
R0,
R1,
etc.
up
to
and
including
R7.
These
registers
are
used
as
auxillary
registers
in
many
operaZons.
The
"R"
registers
are
also
used
to
temporarily
store
values.
• Program
Counter(PC)
:
8051
has
a
16-‐bit
program
counter
.The
program
counter
always
points
to
the
address
of
the
next
instrucZon
to
be
executed.
Afer
execuZon
of
one
instrucZon
the
program
counter
is
incremented
to
point
to
the
address
of
the
next
instrucZon
to
be
executed.It
is
the
contents
of
the
PC
that
are
placed
on
the
address
bus
to
find
and
fetch
the
desired
instrucZon.Since
the
PC
is
16-‐bit
width
,8051
can
access
program
addresses
from
0000H
to
FFFFH
,a
total
of
6kB
of
code.
Data
Pointer
Register(DPTR)
• It
is
a
16-‐bit
register
which
is
the
only
user-‐
accessible.
DPTR,
as
the
name
suggests,
is
used
to
point
to
data.
It
is
used
by
a
number
of
commands
which
allow
the
8051
to
access
external
memory.
When
the
8051
accesses
external
memory
it
will
access
external
memory
at
the
address
indicated
by
DPTR.
This
DPTR
can
also
be
used
as
two
8-‐registers
DPH
and
DPL.
Registers
A
B
R0
DPTR DPH DPL
R1
R2 PC PC
R3
R6
R7
q DATA registers
q CONTROL registers
v Timers
v Serial ports
v Interrupt system
v Analog to Digital converter
v Digital to Analog converter Addresses 80h – FFh
v Etc.
Direct Addressing used to
access SPRs
Bit Addressable RAM
Summary
of the 8051
on-chip
data
memory
(RAM)
Example
• SETB
25H
;sets
the
bit
25H(becomes
1)
Bit
25H
is
actually
bit
5
of
internal
RAM
locaZon
24H
•
CLR
25H
;clears
the
bit
25H(becomes
0)
• SETB
P2.3
Bit Addressable RAM
Summary
of the 8051
on-chip
data
memory
(Special
Function
Registers)
Register Banks
!
PARALLEL
I
/O
PORTS
• The
8051
microcontroller
has
four
parallel
I/O
ports
,
each
of
8-‐bits
.So,
it
provides
the
user
32
I/O
lines
for
connecZng
the
microcontroller
to
the
peripherals.
The
four
ports
are
P0
(Port
0),
P1(Port1)
,P2(Port
2)
and
P3
(Port3).
Upon
reset
all
the
ports
are
output
ports.
In
order
to
make
them
input,
all
the
ports
must
be
set
i.e
a
high
bit
must
be
sent
to
all
the
port
pins.
This
is
normally
done
by
the
instrucZon
“SETB”.
• Ex:
MOV
A,#0FFH
;
A
=
FF
•
MOV
P0,A
;
make
P0
an
input
port
Inside
Architecture
of
8051
External
interrupts
On-‐chip
Timer/Counter
CPU
Bus
Serial
4 I/O
Ports
OSC
Control
Port
D0
D7
EA
P2.0
A8
P2.7
A15
8051
ROM
External data memory
WR
WR
RD
RD
PSEN
ALE
74LS373
CS
G
P0.0
A0
D
P0.7
A7
D0
D7
EA
P2.0
A8
P2.7
A15
8051
RAM
Port
1
• Port
1
occupies
a
total
of
8
pins
(pins
1
through
8).
It
has
no
dual
applicaZon
and
acts
only
as
input
or
output
port.
In
contrast
to
port
0,
this
port
does
not
need
any
pull-‐up
resistors
since
pull-‐up
resistors
connected
internally.
Upon
reset,
Port
1
is
configured
as
an
output
port.
To
configure
it
as
an
input
port
,
port
bits
must
be
set
i.e
a
high
bit
must
be
sent
to
all
the
port
pins.
This
is
normally
done
by
the
instrucZon
“SETB”.
For
Ex
:
•
MOV
A,
#0FFH
;
A=FF
HEX
•
MOV
P1,A
;
make
P1
an
input
port
by
wriZng
1’s
to
all
of
its
pins
Hardware Structure of I/O Pin
TB1
Read
pin
Writing “1” to Output Pin P1.X
TB1
Read
pin
Writing “0” to Output Pin P1.X
TB1
Read
pin
Reading “High” at Input Pin
1
1
P1.X
pin
Internal
CPU
bus
D
Q
P1.X
0
M1
Write
to
latch
Clk
Q
TB1
Read
pin
3.
Read
pin=1
Read
latch=0
Write
to
latch=1
Reading “Low” at Input Pin
TB1
Read
pin
3.
Read
pin=1
Read
latch=0
Write
to
latch=1
8051
IC
Port
2
• Port
2
is
also
an
eight
bit
parallel
port.
(pins
21-‐
28).
It
can
be
used
as
input
or
output
port.
As
this
port
is
provided
with
internal
pull-‐up
resistors
it
does
not
need
any
external
pull-‐up
resistors.
Upon
reset,
Port
2
is
configured
as
an
output
port.
If
the
port
is
to
be
used
as
input
port,
all
the
port
bits
must
be
made
high
by
sending
FF
to
the
port.
For
ex,
• MOV
A,
#0FFH
;
A=FF
hex
•
MOV
P2,
A
;
make
P2
an
input
port
by
wriZng
all
1’s
to
it
Dual
role
of
port
2
• Port2
lines
are
also
associated
with
the
higher
order
address
lines
A8-‐A15.
In
systems
based
on
the
8751,
8951,
and
DS5000,
Port2
is
used
as
simple
I/O
port..
But,
in
8031-‐based
systems,
port
2
is
used
along
with
P0
to
provide
the
16-‐bit
address
for
the
external
memory.
Since
an
8031
is
capable
of
accessing
64K
bytes
of
external
memory,
it
needs
a
path
for
the
16
bits
of
the
address.
While
P0
provides
the
lower
8
bits
via
A0-‐A7,
it
is
the
job
of
P2
to
provide
bits
A8-‐A15
of
the
address.
In
other
words,
when
8031
is
connected
to
external
memory,
Port
2
is
used
for
the
upper
8
bits
of
the
16
bit
address,
and
it
cannot
be
used
for
I/O
operaZons.
PORT
3
• Port3
is
also
an
8-‐bit
parallel
port
with
dual
funcZon.(
pins
10
to
17).
The
port
pins
can
be
used
for
I/O
operaZons
as
well
as
for
control
operaZons.
The
details
of
these
addiZonal
operaZons
are
given
below
in
the
table.
Port
3
also
do
not
need
any
external
pull-‐up
resistors
as
they
are
provided
internally
similar
to
the
case
of
Port2
&
Port
1.
Upon
reset
port
3
is
configured
as
an
output
port
.
If
the
port
is
to
be
used
as
input
port,
all
the
port
bits
must
be
made
high
by
sending
FF
to
the
port.
For
ex,
• MOV
A,
#0FFH
;
A=
FF
hex
•
MOV
P3,
A
;
make
P3
an
input
port
by
wriZng
all
1’s
to
it