www.elsolucionario.org
The 8051 Microcontroller and Embedded
Systems
Using Assembly and C
Second Edition
Muhammad Ali Mazidi
Janice Gillispie Mazidi
Rolin D. McKinlay
CONTENTS
Introduction to Computing
The 8051 Microcontrollers
8051 Assembly Language Programming
Branch Instructions
I/O Port Programming
8051 Addressing Modes
Arithmetic & Logic Instructions And Programs
8051 Programming in C
8051 Hardware Connection and Hex File
8051 Timer/Counter Programming in Assembly and C
8051 Serial Port Programming in Assembly and C
Interrupts Programming in Assembly and C
8051 Interfacing to External Memory
8051 Real World Interfacing I: LCD,ADC AND
SENSORS
LCD and Keyboard Interfacing
8051 Interfacing with 8255
INTRODUCTION TO
COMPUTING
The 8051 Microcontroller and Embedded
Systems: Using Assembly and C
Mazidi, Mazidi and McKinlay
Chung-Ping Young
楊中平
Home Automation, Networking, and Entertainment Lab
Dept. of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
www.elsolucionario.org
OUTLINES
HANEL
Numbering and coding systems
Digital primer
Inside the computer
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
2
NUMBERING
AND CODING
SYSTEMS
Decimal and
Binary Number
Systems
HANEL
Human beings use base 10 (decimal)
arithmetic
¾
There are 10 distinct symbols, 0, 1, 2, …,
9
Computers use base 2 (binary) system
¾
¾
There are only 0 and 1
These two binary digits are commonly
referred to as bits
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
3
NUMBERING
AND CODING
SYSTEMS
Converting
from Decimal
to Binary
Divide the decimal number by 2
repeatedly
Keep track of the remainders
Continue this process until the quotient
becomes zero
Write the remainders in reverse order
to obtain the binary number
Ex. Convert 2510 to binary
Quotient
Remainder
25/2 =
12
1
LSB (least significant bit)
12/2 =
6
0
6/2 =
3
0
3/2 =
1
1
1/2 =
0
1
MSB (most significant bit)
Therefore 2510 = 110012
HANEL
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
4
www.elsolucionario.org
NUMBERING
AND CODING
SYSTEMS
Know the weight of each bit in a binary
number
Add them together to get its decimal
equivalent
Converting
Ex. Convert 110012 to decimal
from Binary to
Weight:
24
23
22
Decimal
Digits:
1
1
0
Sum:
16 +
8+
0+
21
20
0
1
0+
1 = 2510
Use the concept of weight to convert a
decimal number to a binary directly
Ex. Convert 3910 to binary
32 + 0 + 0 + 4 + 2 + 1 = 39
Therefore, 3910 = 1001112
HANEL
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
5
NUMBERING
AND CODING
SYSTEMS
Hexadecimal
System
Base 16, the
hexadecimal system,
is used as a
convenient
representation of
binary numbers
¾
ex.
It is much easier to
represent a string of 0s
and 1s such as
100010010110 as its
hexadecimal equivalent of
896H
HANEL
Decimal
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Binary
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Hex
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
6
NUMBERING
AND CODING
SYSTEMS
To represent a binary number as its
equivalent hexadecimal number
¾
Start from the right and group 4 bits at a
time, replacing each 4-bit binary number
with its hex equivalent
Converting
between Binary Ex. Represent binary 100111110101 in hex
1001 1111 0101
and Hex
=
9
F
5
To convert from hex to binary
¾
Each hex digit is replaced with its 4-bit
binary equivalent
Ex. Convert hex 29B to binary
=
HANEL
2
9
B
0010
1001
1011
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
7
www.elsolucionario.org
NUMBERING
AND CODING
SYSTEMS
Converting
from Decimal
to Hex
Convert to binary first and then
convert to hex
Convert directly from decimal to hex
by repeated division, keeping track of
the remainders
Ex. Convert 4510 to hex
32
16
8
4
2
1
1
0
1
1
0
1
32 + 8 + 4 + 1 = 45
4510 = 0010 11012 = 2D16
Ex. Convert 62910 to hex
512 256 128 64 32 16 8 4 2 1
1
0
0
1
1
1
0 1 0 1
62910 = 512+64+32+16+4+1 = 0010 0111 01012 = 27516
HANEL
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
8
NUMBERING
AND CODING
SYSTEMS
Converting
from Hex to
Decimal
Convert from hex to binary and then to
decimal
Convert directly from hex to decimal
by summing the weight of all digits
Ex. 6B216 = 0110 1011 00102
1024 512 256 128 64 32 16 8 4 2 1
1
1
0
1
0
1
1
0
0 1 0
1024 + 512 + 128 + 32 + 16 + 2 = 171410
HANEL
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
9
NUMBERING
AND CODING
SYSTEMS
Addition of Hex
Numbers
Adding the digits together from the
least significant digits
¾
¾
If the result is less than 16, write that digit
as the sum for that position
If it is greater than 16, subtract 16 from it
to get the digit and carry 1 to the next
digit
Ex. Perform hex addition: 23D9 + 94BE
23D9
+ 94BE
B897
HANEL
LSD: 9
1
1
MSD: 2
+
+
+
+
14 = 23
13 + 11 = 25
3+4=8
9=B
23 – 16 = 7 w/ carry
25 – 16 = 9 w/ carry
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
10
www.elsolucionario.org
NUMBERING
AND CODING
SYSTEMS
Subtraction of
Hex Numbers
HANEL
If the second digit is greater than the
first, borrow 16 from the preceding
digit
Ex. Perform hex subtraction: 59F – 2B8
59F
– 2B8
2E7
LSD: 15 – 8 = 7
9 + 16 – 11 = 14 = E16
5–1–2=2
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
11
NUMBERING
AND CODING
SYSTEMS
The ASCII (pronounced “ask-E”) code
assigns binary patterns for
¾
¾
ASCII Code
¾
Numbers 0 to 9
All the letters of English alphabet,
uppercase and lowercase
Many control codes and punctuation
marks
The ASCII system uses 7 bits to
represent each code
Selected ASCII codes
HANEL
Hex
Symbol
Hex
Symbol
41
42
43
44
...
59
5A
A
B
C
D
...
Y
Z
61
62
63
64
...
79
7A
a
b
c
d
…
y
z
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
12
DIGITAL
PRIMER
Binary Logic
Two voltage levels can be represented
as the two digits 0 and 1
Signals in digital electronics have two
distinct voltage levels with built-in
tolerances for variations in the voltage
A valid digital signal should be within
either of the two shaded areas
5
4
3
2
1
0
HANEL
Logic 1
Logic 0
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
13
www.elsolucionario.org
DIGITAL
PRIMER
AND gate
Logic Gates
Computer Science Illuminated, Dale and Lewis
OR gate
Computer Science Illuminated, Dale and Lewis
HANEL
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
14
DIGITAL
PRIMER
Tri-state buffer
Inverter
Logic Gates
(cont’)
Computer Science Illuminated, Dale and Lewis
XOR gate
Computer Science Illuminated, Dale and Lewis
HANEL
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
15
DIGITAL
PRIMER
NAND gate
Logic Gates
(cont’)
Computer Science Illuminated, Dale and Lewis
NOR gate
Computer Science Illuminated, Dale and Lewis
HANEL
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
16
www.elsolucionario.org
DIGITAL
PRIMER
Half adder
Logic Design
Using Gates
Full adder
Digital Design, Mano
HANEL
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
17
DIGITAL
PRIMER
4-bit adder
Logic Design
Using Gates
(cont’)
Digital Design, Mano
HANEL
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
18
DIGITAL
PRIMER
Logic Design
Using Gates
Decoders
¾
Decoders are widely used for address
decoding in computer design
Address Decoders
(cont’)
HANEL
Address decoder for 9 (10012)
Address decoder for 5 (01012)
The output will be 1 if and
only if the input is 10012
The output will be 1 if and
only if the input is 01012
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
19
www.elsolucionario.org
DIGITAL
PRIMER
Flip-flops
¾
Flip-flops are frequently used to store data
Logic Design
Using Gates
(cont’)
Digital Design, Mano
HANEL
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
20
INSIDE THE
COMPUTER
The unit of data size
¾
Important
Terminology
¾
¾
¾
0 or 1
Byte : 8 bits
Nibble : half of a bye, or 4 bits
Word : two bytes, or 16 bits
The terms used to describe amounts of
memory in IBM PCs and compatibles
¾
¾
¾
¾
HANEL
Bit : a binary digit that can have the value
Kilobyte (K): 210 bytes
Megabyte (M) : 220 bytes, over 1 million
Gigabyte (G) : 230 bytes, over 1 billion
Terabyte (T) : 240 bytes, over 1 trillion
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
21
INSIDE THE
COMPUTER
Internal
Organization of
Computers
CPU (Central Processing Unit)
¾
I/O (Input/output) devices
¾
Execute information stored in memory
Provide a means of communicating with
CPU
Memory
¾
RAM (Random Access Memory) –
temporary storage of programs that
computer is running
The data is lost when computer is off
¾
ROM (Read Only Memory) – contains
programs and information essential to
operation of the computer
The information cannot be changed by use,
and is not lost when power is off
– It is called nonvolatile memory
HANEL
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
22
www.elsolucionario.org
INSIDE THE
COMPUTER
Internal
Organization of
Computers
(cont’)
Address bus
Memory
CPU
(RAM, ROM)
Peripherals
(monitor,
printer, etc.)
Data bus
HANEL
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
23