Académique Documents
Professionnel Documents
Culture Documents
the 8051/8052
CSCI
255:
Introduc/on
to
Embedded
Systems
•
Keith
Vertanen
•
Copyright
©
2011
Course
outcomes
(what
you'll
learn)
ü Number
systems
ü binary,
octal,
decimal,
hexadecimal
• How
computers
represent
numbers
ü integers
(posi=ve/nega=ve)
– floa=ng-‐point
ü Boolean
algebra,
bit
manipula=ons
ü Combina=onal
and
sequen=al
circuits
ü How
these
things
build
up
to
a
computer
• Embedded
programming
– In
assembly
language
and
C
– Subrou=nes,
=mers,
interrupts
2
Today
• 8051/8052
microcontroller
overview
• Assembly
language
programming
– in
general
– for
8051
microcontroller
3
8051/8052
microcontroller
• 8051/8052
– Microcontroller
with
instruc=on
set
compa=ble
with
the
MCS-‐51
standard
• Developed
by
Intel
in
1980
• Compa=ble
chips
made
by
a
variety
of
other
companies
– Harvard
architecture
• Code
and
data
stored
separately
– 8052
is
enhanced
version
of
8051
• 256
bytes
of
RAM
instead
of
128
bytes
• Adds
a
third
=mer
4
8051
microarchitecture
5
8051
educa=onal
board
6
Port
0
7
Port
2
9
Compiling:
1-‐step,
desktop
#include <stdio.h>
Source
code:
int main(int argc, char** argv)
Plain
text
file
created
in
a
high-‐ {
level
programming
language
printf("Hello world!\n");
return 0;
}
HelloWorld.c
Machine
language:
Actual
binary
run
by
a
par=cular
processor,
not
human
readable/writeable
HelloWorld.exe
10
Compiling:
2-‐step,
desktop
#include <stdio.h>
HelloWorld.c
_main:
Assembly
language:
pushl
movl
%ebp
%esp, %ebp
Low-‐level,
but
s=ll
human
subl $8, %esp
…
readable/writeable
HelloWorld.s
% gcc HelloWorld.s -o HelloWorld
Machine
language:
Actual
binary
run
by
a
par=cular
processor,
not
human
readable/writeable
HelloWorld.exe
11
Assembly
language
C
Keil IDE
Keil
IDE
Hex
file
MRT loader
12
Assembly
language
• Assembly
language
– Very
processor
specific
• Different
instruc=on
sets
• Different
forms
of
addressing
– Requires
understanding
of
underlying
hardware
– Stored
in
a
human
"readable"
text
file:
HandleFreqOutput:
CLR A
MOV TL0,A
MOV TH0,A ;clear the counter
MOV Overflow,A
MOV GateTime,#20
MOV TL2, # Low (-49999) ;V0.2 new ReloadValue -49999
MOV TH2, # High(-49999)
SETB T2I0
SETB TR0 ;restart both Timers
CLR bReady
13
Assembly
syntax
• Each
line
of
assembly
code
can
have:
– Label
• Programmer
chosen
symbol
naming
this
instruc=on's
address
in
memory.
– Instruc=on
• Instruc=on
to
execute
along
with
any
parameter(s)
– Comment
• Text
so
you
can
remember
what
the
heck
this
does
14
Assembly
syntax
MyLabel: MOV A, #25h ; This is a sample comment
Labels
are
case-‐ Instruc=ons,
register
sensi=ve,
MYLABEL
names,
etc.
are
not
not
the
same
place
in
case-‐sensi=ve
memory
15
Number
bases
• Literal
values
– Fix
numeric
used
by
your
code
– Start
with
#
– Decimal:
normal
– Hex:
trailing
H
MOV A, #30 ; decimal
– Binary:
trailing
B
MOV A, #11110b ; binary
MOV A, #1Eh ; hex
– Octal:
trailing
Q
MOV A, #36q ; octal
16
Basic
registers
• R0
–
R7
– Bank
of
8
general
purpose
8-‐bit
registers
• A
– Accumulator
(8-‐bit)
– Half
of
255
opcodes
use
A
• B
– Used
by
mul=ply
(MUL)
and
division
(DIV)
(8-‐bit)
– Also
can
be
used
as
general
purpose
register
• PC
– Program
counter
(16-‐bit)
– Not
accessible,
changed
via
jump
instruc=ons
17
Basic
registers
• DPTR
– Data
pointer
(16-‐bit)
– Only
user-‐accessible
16-‐bit
register
– Used
to
access
external
memory
• 16-‐bits
allows
addressing
of
64K
of
memory
– Ojen
used
by
programmers
for
two-‐byte
variable
• SP
– Stack
pointer
(8-‐bit)
– Indicates
next
value
in
RAM
to
remove
from
stack
– Ini=alized
to
07h
18
8052
memory
architecture
Port
0-‐3,
serial
port,
=mers,
interrupts
AT89S8253
12KB
flash
19
8052
memory
addresses
20
8052
SFR
memory
21
Port
0
• P0
– General
purpose
input/output
port
– Lives
at
memory
address
80h
– Bit
addressable
• P0.0,
P0.1,
…,
P0.7
• Wri=ng
1
to
bit
brings
I/O
high
• Wri=ng
0
brings
I/O
low
• Bring
pin
high
before
reading
– Also
hooked
up
to
8
onboard
LEDs
• Set
bit
to
1
to
turn
off
LED
(instruc=on
SETB)
• Set
bit
to
0
to
turn
on
LED
(instruc=on
CLR)
22
Simple
LED
START:
SETB P0.0 ; Turn LED D0 off
CLR P0.1 ; Turn LED D1 on
SETB P0.2 ; Turn LED D2 off
CLR P0.3 ; Turn LED D3 on
SETB P0.4 ; Turn LED D4 off
CLR P0.5 ; Turn LED D5 on
SETB P0.6 ; Turn LED D6 off
CLR P0.7 ; Turn LED D7 on
23
Simple
LED
-‐
loading
all
bits
START: START:
SETB P0.0 ; Turn LED D0 off MOV P0, #01010101b
CLR P0.1 ; Turn LED D1 on
SETB P0.2 ; Turn LED D2 off SJMP START ; Loop forever
CLR P0.3 ; Turn LED D3 on END
SETB P0.4 ; Turn LED D4 off binary
literal
CLR P0.5 ; Turn LED D5 on
SETB P0.6 ; Turn LED D6 off START:
CLR P0.7 ; Turn LED D7 on MOV P0, #55h
SJMP START ; Loop forever SJMP START ; Loop forever
END END
hex
literal
START: START:
MOV P0, #85 MOV P0, #125q
hex
literal
START: START:
MOV 80h, #85 MOV 80h, #125q
26
Sepng,
clearing,
flipping
bits
• SETB
– Sets
a
bit
to
1
– Specify
SFR
like
P0.1
– Bit
addressable
memory
• 20h
–
7Fh
• CLR
– Sets
a
bit
to
0
• CPL
– Flips
a
bit
27
Moving
a
bit
• MOV
– Only
available
to/from
carry
bit
MOV C, P0.0 ; Move value of P0.0 to the carry bit
MOV 25h, C ; Copy result to user bit 25h
28
Register
movement
• MOV
– 57
of
the
255
opcodes
– MOV
des=na=on
register,
source
value
• Des=na=on
always
a
register
or
memory
address
• Source
value
may
be
register,
memory
address,
or
literal
• Use
#
to
indicate
literal
29
Increment
and
decrement
• INC,
DEC
– Add/subtract
one
from
a
register
or
memory
address
– Incremen=ng
255
results
in
0
– Decremen=ng
0
results
in
255
– Overflow,
underflows
don’t
set
any
flags
(such
as
the
carry
bit)
30
Program
loops
• DJNZ
– Decrement
then
jump
if
resul=ng
value
not
zero
– Most
common
way
to
do
a
loop
execu=ng
a
specific
number
of
=mes
(i.e.
for-‐loop)
31
Goto
considered
necessary
• LJMP,
SJMP,
AJMP
– Change
execu=on
to
given
loca=on
– Normally
a
label
in
your
program
• LJMP,
can
jump
anywhere
(3
bytes)
• SJMP,
can
jump
within
128
bytes
before
or
127
byte
ajer
current
loca=on
(2
bytes)
• AJMP,
can
jump
within
same
2K
block
(2
bytes)
– Keil
lets
us
just
use
JMP
and
it
will
use
the
most
memory
efficient
version
32
Summary
• Overview
of
8052
– Memory
architecture
– Important
registers
– Import
special
func=on
registers
(SFRs)
• Assembly
language
introduc=on
– Sepng,
clearing,
moving
bits
– Moving
8-‐bit
values
– Incremen=ng,
decremen=ng
– Crea=ng
a
basic
loop
33