6G - IoT
6G - IoT
T1
... "
SECOND EDITION
--
PEARSON
Prentice
Hall
op)'Tlght 0 2006 by Pearson Edncation, Ine., Upper Saddle River, New Jersey 074~8.Pearson Prentice Hall. All rights
"""rved. Printed in the United States of America. This publication is protected by Copyright and permission should be obtaine
d
from the publisher prior to any prohibited reproduction, storage in a retrieval system, or tranSmission in any form or by any means,
electrcmc, mechanical,
Pcmllssiol\s Department. photocopying, recording, or likewise. For information regarding pennission(s), write to: Rights and
ISBN: o-l3-119402-X
... man's glory lieth in his knowledge,
his upright conduct, his praiseworthy character,
his wisdom, and not in his nationality or rank.
- Baha'u'llah
CONTENTS AT A GLANCE
CHAPTERS
I
0: Introduction to Computing 23
I: The 8051 Microcontrollers 37
2:
8051 Assembly Language Programming
69
3: Jump, Loop, and Call Instructions
9
4: I/O Port programming
109
s: 80SI Addressing Modes
1'9
6: Arithmetic & Logic Instructions and Programs
I I
7: 80SI Programming in C
80SI Hardware Connection and Intel Hex File 217
8:
8051 Timer Programming in Assembly and C 2 9
9:
Ed 10: 80SI Serial Port Programming in Assembly and C 277
Ed II: Interrupts Programming in Assembly and C 317
PI 51
De 12: LCD and Keyboard Interfacing
Co 13: ADC. DAC. and Sensor Interfacing 37
Co 14: 8051 Interfacing to External Memory 411
PI
So IS: 8051 Interfacing with the 8255 449
So 16: DS12887 RTC Interfacing and Programming 467
17: Motor Control: Relay, PWM. DC. and Stepper Motors 491
APPENDICES
A: 8051
B. Instructions
. I
Timing and Registers
,
.
52
B: asrcs of WIre Wrapping
C: Ie Technology adS . Issues 56
0:
FI h n ystem DeSIgn 567
Owe arts and Pseudocode
E: 8051 Primer for X86 P 7
F: ASCII Codes rogrammers 592
G: Assemblers. Development R 593
H: Data Sheets eSOurces. and Suppliers 594
596
CONTENTS
v
-
CHAPTER 7: 8051 PROGRAMMING IN C
181
Section 7.1: Data types and time delay in 805 I C
182
Section 7.2: I/O programming in 8051 C
188
Section 7.3: Logic operations in 8051 C
194
Section 7.4: Data conversion programs in 8051 C
199
Section 7.5: Accessing code ROM space in 8051 C
204
Section 7.6: Data serialization using 8051 C
209
VI
CHAPTER 14: 8051 INTERFACING TO EXTERNAL MEMORY 411
Section 14.1: Sem iconductor memory 412
Section 14.2: Memory address decoding 422
Section 14.3: 803 1/51 interfacing with external ROM 425
Section 14.4: 8051 data memory space 430
Section 14.5: Accessing external data memory in 8051 C 440
INDEX 617
vii
K .. ,ad....,
h J In III Iroduc 1(1) dlgilal 'our e. Knowledge of
hIp/iii ut I n t neces ry. A hhough the book is
tt lund in,~ 'mhly language programming, stu-
pcncnce w ill be able 10 gain a ma tery of
nd I.m on their proje ts right away. For the 8051
~ k, a ha", knowledge of C programming is
ix
ond edition .. of 0-1 program-
What is new in the sec . edition is the addition A sembI languag
The biggest change inWhile
this new
Chapters I t hough
r 6 blyand
use language pr _
ming throughou t ,the book.
ith ChapterI 7, we haddition
ve both Assem
includes t h e following new
. Iy, starting .ws discussed. The secon e
exclusive
grams for all the topic
features: . (Chapter 7)
8051 C programming . ers ( ection 9.3)
Anew chapter 00 he 80" C programm ing of ";89<:4'0 ch ip ( ecti n 10:')
A new secuon on 1 d serial port of the D d erial pan ( e lion
A new section on the ~~;~ C programming of the econ
A new secnon on the . I I 6)
10.5) . . of interrupts
th 8051 C programming ( c lion 14.4)
0 h' (Section .
A new secuon on e KB SRAM of the DS89C4x C Ip ( ecti n 14. )
Programming of", , ro ramming of ~I"",, memory 16)
A new section on the 8051 ~:7 :TC (real-time clock) chip ( hapter
A new chapter
A new chapter on
on motors,
the DSI2relays, and optoisolators (Chapter 17)
E
E
P
lab manual h b k b the
The lab manual and suppo~ rnat~.rialslEd' and otite.er
for this Web
authors can be found at the www.MlcroDlglla.eom
This book is the result of the dedication and encouragement of many indi-
viduals. Our sincere and heartfelt appreciation goes to all of them.
First, we Would like 10 thank PrOfessor Danny Morse, the rno t knowl-
edgeable and experienced person on the 8051 that we know. He felt a trong need
for a book such as this, and due to his lack of time he encOuraged 1I 10 write it. He
i the one who introduced us to this microcontroller and was always there, ready
10 discuss issues related to 8051 architecture.
0 Also we Would like to express our sincere thanks to Profe SOr Iyde
book.
K ,hI o f De Vry Univers. 0/ for hi, h"p'o' ""'''''"' 00 the ,~'" ,f the '''''00
in .dll'"" the""ow'"8 pmf,",," ,"d _"" 'Oo"' '"'" whII," ing
the""" in '" "P"""",oo
'h'm """"y, ,,"r.~,
f,""
in "'" m
PhilG"Id~"diobo B,"Y"
"",,'01" II" '"""' ,,' we mM'
Do V~ UoI;",~, R,bon
lV'gh"",,, p,,,,,,,, "'",""
U"'i,m '" "m boo, D"" B'.m", J'Im H'.. ''''
~ Rob."", y'''Y Ch""" N"
J,m" Doo'" Ro,," MI,,,,, I B,,,, ''''00
"0
Hoang, and Trevor Isra.
II,J"" ~ ""Y H'", """
J"h"~",J"" K,,,,:M""" I M'"h;", M,"
Acknowledgments for the second edition
Thanks to the reviewers of this edition: Jack Adams, Merrimack College;
Hector Gutierrez, Florida Institute of Technology; Martin Hebel, Southern Illinois
University; Gary Hill, California State University - Long Beach; Juan Vargas,
University of Central Florida; Richard W. Wall, University of Idaho; and Rainer
Weschke, SUNY - Suffolk.
Numerous professors, students, and individuals sent us comments and
errors found in the first edition. Many others made suggestions for the second
edition. We would like to thank all of them sincerely for their enthusiam and support.
We hope to create a list of their names on our Web site at
www.MicroDigitaIEd.com. They are Danny Morse, Clyde Knight, Shah ram
Rohani (all from DeVry University), Javad Rasti (Esfahan University), Nassir
Abedi (KN. Toosi University), Hooman Shayani (BIHE), Jaco Vosloo
(Stellenbosh University), Mohammad Khalid Kerani, Hassan Mohammadi Abdar
(Azad University), Jeff Solinsky, Alijah Ballard, Jacob Stauffer, Vaibhav Joshi,
Michael Kelley, and Dan Masterson.
Finally, we would like to thank the people at Prentice Hall, in particular
our editor Kate Linsner, who continues to support and encourage our writing, and
our production editor Kevin Happell, who made the book a reality.
We enjoyed writing this book, and hope you enjoy reading it and using it
for your courses and projects. Please let us know if you have any suggestions or
find any errors .
. Assemblers
The following gives sites where you can download assemblers:
www.MicroDigitalEd.com
www.keil.com for Keil Corporation
www.fsinc.com for Franklin Software, Inc.
http://developer.intel.com/design/mcs51/docs_mcs51.htm
- xi
AtlUU I 1m: I\U II """ ..
Whati
Ali Mazidi went to Tabrll
Muhamma d U . ahe
fi both Southern Methodi l 0I1e1'l11)
degrees rom d hi Ph D In a
Dallas. He is currenlly a.b. : on. . .
Department of Southern Methodist VOile II) lie I
textbook, The 80x86 IBM PC and CIlI/lf'</IIh1l'
Prentice Hall. He leaches microproce t-
A University in Dallas, Texas. .
• A Janice Gillispie Mazidi has a M rer 01
A Science from the University of orth Texas. h h " c
A as a software engineer in Dallas. he h been hid 'ChilI I \ III r
10. lionmanager, and was responsible for solhl re ckl I pm III a
• A Iy used textbook, The 8Ox86 IBM PC anti 1I1IIp</lr"l tN I/' 1/
P from Prentice Hall.
• A
A
Rolin McKinlay has a BSEET fr m De\/) llll\1;
working on his Masler's degree and PE licen . In Ih' ,.
I'I I
, A
rently self-employed as a programmer and ir UII rd d I '/I r, r
in MicroDigitaIEd.com.
To Betsy.for all the love, encouragement, and sacrifice she has made/or me.
- Rolin D. Mckinlay
xiii
r
r
"
f>
I'
CHAPTER 0
INTRODUCTION TO
COMPUTING
OBJECTIVES
» Convert any number from base 2, base 10, or base 16 to either of the
other two bases
» Add and subtract hex numbers
» Add binary numbers
» Represent any binary number in 2's complement
» Represent an alphanumeric string in ASCII code
» Describe logical operations AND, OR, NOT, XOR, NAND, NOR
» Use logic gates to diagram simple circuits
» Explain the difference between a bit, a nibble, a byte, and a word
» Give precise mathematical defmitions of the terms kilobyte, megabyte,
gigabyte, and terabyte
» Explain the difference between RAM and ROM and describe their use
» Describe the purpose of the major components of a computer system
» List the three types of buses found in computers and describe the
purpose of each type of bus
» Describe the role of the CPU in computer systems
» List the major components of the CPU and describe the purpose of each
1
ana 1I'1lu·.....·- ...... - . _. )' ~
To understand the software basic concep!. underlymg mput r d ign
ter some very , be II d .
one must first mas d'tl'on of digital comput rs n h pIer
iem, hi h 10 the tra I d
I this chapter (w ic 'and coding 'yslems rc pre enre Iter an
On)the fundamentals of numbenng llew of the work 109 In rd the c mpul r is
, , I' gates, an overv " f' P
'ntroduetIon to ogle . we give a brief hi I ry o r hlle lure
I 'I' the last sectIOn , d f .
given, Fmal y, 10 have an adequale ba kgrourt 10 10'10) rh I pic
Although some,readers maYendcdthat the matcrial be 00 d. h wever bneny.
of Ihis chapter, It IS rccomIO
SoluMa:
Quot:ient
25/2 = 12 Rema:inder
12/2 = 6 1 LSB (leas
6/2 =
3
o 81 01 c n b1 )
3/2 =
1
o
1/2 = 1
o
1
MSB (moSt signi
Therefore,2510" Jl001 c 0 1t)
2·
- --::------
2
-
Converting from binary to decimal 74068310
To convert from binary to decimal, it is
important to understand the concept of weight ~ 10°
~ 3
101 = 80
associated with each digit position. First, as an 6 x
102 = 600
analogy, recall the weight of numbers in the base 0 x
103 = 0000
10 system, as shown in the diagram. By the same 4 104
x = 40000
token, each digit position in a number in base 2 7 X 105 = 700000
has a weight associated with it: 740683
Knowing the weight of each bit in a binary number makes it simple to add
them together to get its decimal equivalent, as shown in Example 0-2.
Example 0-2
Convert 110012 to decimal.
SOlution:
Weight: 16 8 4 2 1
Digits: 1 I 0 0 1
Sum: 16 + 8+ 0+ 0+ 1=2510
Knowing the weight associated with each binary bit position allows one to
convert a decimal number to binary directly instead of going through the process
of repeated division. This is shown in Example 0-3.
Example 0-3
Use the concept of weight to convert 39, 0 to binary.
Solution:
Weight: 32 16 8 4 2 1
1 0 0 I 1 1
32 + 0+ 0+ 4+ 2+ 1=39
Therefore, 3910 = 1001112,
Example 0-4
Solution:
First the number is d .
Then each groupe. mto sets of 4 bits: 100 I I11I 0101
group of 4 bits IS replaced with its hex e . I .
1001 1111 0101 quiva ent:
9 F 5
Therefore, 100111110 I0 12 = 9F5 hexadecimal.
Example 0-5
Convert hex 29B to binary.
I Solulion:
2 9 B
Dropping the I~ding ~e~~~ gi 100 II0 II 0 II
ves 0011011
Converting from decimal to hex .
onvertmg from deCimal t h
I. Convert to bimary first and0 ex could be a
th pproaehed in two
method of conven: . en Convert to h ways:
2. C nvcn direetlye~lng ddeclmal to hex. ex. Example 0-6 shows this
. om eelmal to I b
remamders. Experimentin . lex y repeated divisio .
g With this method is left n, keepmg track of the
to the reader.
4
Example 0-6
(a) Convert 4510 to hex.
Example 0-7
7
Table 0-2: Counting in Bases . bases 10, 2 , and 16
Counting In
Decimal Binary Hex To show the relationship between all
o 00000 o . bl 0 2 we show the sequence
three bases, In Ta e - . d . I along with
00001 of numbers from 0 to 31 In ecimar,
2 00010 2 the equiva- Table 0-3: Binary Addition
3 00011 3 lent binary Sum
4 00100 4 A+ B Carry
5 00101 5 numbers. 0 I
6 00110 6 . 0+ I
Notice 111 0 I
7 00111 7 1+0
each base I 0
8 01000 8 that when 1.~+...!I
1 __ L;__ ..::....
__
9 01001 9 one more IS
10 01010 A added to
II 01011 B
the highest digit, that digit becomes zero and a
12 01100 C
I is carried to the next-highest digit position.
13 01101 o For example, in decimal, 9 + I = 0 with a carry
14 01110 E
15
to the next-highest position. In binary, I + I =
01111 F
16 10000
o with a carry; similarly, in hex, F + I = 0 with
10 a carry.
17 10001 II
18 10010 12 Addition of binary and hex numbers
19 10011 13
20 10100 14 The addition of binary numbers is a
21 10101 15 very straightforward process. Table 0-3 shows
22 10110 16 the addition of two bits. The discussion of sub-
23 10111 17 traction of binary numbers is bypassed since all
24 11000 18 computers usc the addition process to imple-
25 11001 19 ment SUbtraction. Although computers have
26 11010 IA adder circuitry, there is no separate circuitry for
27 11011 IB sUbtractors. Instead, adders arc used in con-
28 11100 IC junction with 2 s complement circuitry to per-
29 11101 ID form SUbtraction. In other words, to implement
30 11110 IE "x - y", the computer takes the 2's complement
31 11111 IF ofy and adds it to x. The concept of2's com-
plement is reviewed next. Example 0-8 shows
the addition of binary numbers.
Example 0-8
Add the following binary numbers. Cheek against their decimal equivalents.
Solution:
Binary
Decimal
1101
13
+ lllill
10110
-.2
22
6
·2's complement
To get the 2's complement of a binary number, invert all the bits and then
add I to the result. Inverting the bits is simply a matter of changing all Os to ls
and J s to Os. This is called the I :\'complement, See Example 0'-9,
Example 0-9
Take the 2's complement of JOOIIIOl.
Solution:
10011101 binary number
01100010 I's complement
+ 1
01100011 2's complement
Example 0-10
Perform hex addition: 2309 + 94BE,
Solution:
2309 LSD: 9 + 14 = 23 23 - 16 = 7 with a carry
+ 94BE I + 13 + II = 25 25 -16 = 9 with a carry
B897 1+3+4=8
MSD: 2+ 9=B
7
bimary P attcrns
, for numbers 0 to 9, all the Hex Symbol Hex Symbol
letters of the English alphabet, both 41 A 61 a
uppercase (capital) and lowercase, and 42 B 62 b
many control codes and punctuation :~ C 63 c
marks, The great advantage of this sys- D 64 d
tem is that it is used by most computers, 59
y 79 y
so that information can be shared among 5A
computers, The ASCII system uses a Z 7A z
total of? bits to represent each code, For LF-igu-r-e
":'"0--1-, S=-e":'"le-e':-te"":d"":A-::S:::C::-n:-C;:-:-od;e:::s--
example, 100 0001 is assigned to the
uppercase letter "A" and 110 000 I is for "" ,
the lowercase "a", Often, a zero is placed in the most significant bit position to
make it an 8-bit code, Figure 0-1 shows selected ASCII codes, A complete list of
ASCII codes is given in Appendix F. The usc of ASCII is not only standard for
keyboards used in the United States and many other countries but also provides a
standard for printing and displaying characters by output devices such as printers
and monitors,
Notice that the pattern of ASCII eodcs was designed to allow for easy
manipulation of ASCII data, For example, digits 0 through 9 are represented by
ASCII eodcs 30 through 39, This enables a program to easily convert ASCII to
decimal by masking off the "3" in the upper nibble, Also notice that there is a rela-
tionship between the uppercase and lowercase letters, The uppercase letters arc
represcnted by ASCII codes 41 through 5A while lowercase letters arc represent-
ed by codes 61 through 7A, Looking at the binary code, the only bit that is differ-
ent between the uppercase "A" and lowercase "a" is bit 5, Therefore, conversion
between uppercase and lowercase is as simple as changing bit 5 of the ASCII code,
Example 0-11
Perform hex Subtraction: 59F 2B8,
olution:
59F
LSD: 8 from 15 ~ 7
- 2B8
2E7 J 1 1T0m 25 (9 + 16) = 14 (E)
21T0m4(5-1)=2
ReView Questions
I.
Why do computcrs use thc binary number system instead of the dcei nal
tern? I sys-
2, Convert 3410 to binary and hex,
3. Convert /10 I012 to hex and dccimal.
4, Pcrfonll binary addition: 101100 + 101.
5, Convert 101/002 to its 2 's complemcnt representation
6, Add 36BH + F6H, '
7, SUbtract 36BH _ F6H,
8, Write "80x86 CPUs" in its ASC/J code (in hex 1'0 )
rm).
8
4
This section gives an overview of digital logic and design. First, we cover
binary logic operations, then we show gates that perform these functions. Next,
logic gates are put together to form simple digital circuits. Finally, we cover some
logic devices commonly found in microcontroller interfacing.
~--------,
Binary logic
Sf-
As mentioned earlier, computers use the
binary number system because the two voltage lev- 4 Logic I
els can be represented as the two digits 0 and l. 3f-
Signals in digital electronics have two distinct volt-
age levels. For example, a system may define 0 Vas 2f-
logic 0 and +5 V as logic I. Figure 0-2 shows this
system with the built-in tolerances for variations in· If-
the voltage. A valid digital signal in this example Logic 0
Of-
should be within either of the two shaded areas.
___ c _
Inverter
~=D-XNANDY
Logic design using gates
1+/=
o I I J 0
I 0
;=D-XNORY
10
-
2
Notice that when we add I + I we get 0 with a carry to the next higher
place. We will need to determine the sum and the carry for this design. Notice that
the sum column above matches the output for the XOR function, and that the carry
column matches the output for the AND function. Figure 0-3 (a) shows a simple
adder implemented with XOR and AND gates. Figure 0-3 (b) shows the same
logic circuit implemented with AND and OR gates. . .
X
x l--- Sum
Y ---'---I
Y -.--1-1-/
}---Sum
X---I
Y
'-_~----,)--- Carry
X-----.,
y-- }------- Carry
(a) Half-Adder Using XOR and AND (a) Half-Adder Using AND, OR, Inverters
x X Half- Carry
y Sum y Adder
Sum
Half-
- C out
Adder Carry
C in
C in
Final Sum
Flip-flops
X1
A widely u cd component in digital Y1
Full· r-.-----S1
) ,Icm I the nip-nop. Frequently, nip- Adder
IIIlfl' Me u. cd to tore data. Figure 0-8 Carry
ho"" the logic diagram, block dIagram,
nd lrulh wble f r a nip-nop.
Thc 0 nip.nop (D-FF) is widely X2
u\(d to latch data. oucc from the truth Y2 Full- r-----_S2
I ble th(ll 0·1'1' grab the data at the input Adder Carry
Ihe clock is tivated. A D·FF holds the r-----_S3
d.1l long s the POwer is n. ';F;::j'::'gu:-r:-c';>O_-;6,.-.
3:;---;;B-;;jt-;A-:;d::;dc:::r:-;u'7:s7:in::g:-::3~F;;:u:-;III'A;;;:dd;;:c::r-:s
__
1~1l
LSU
D
elk D
Q No
II. x
Ik 1- 0
1- I
Q
(~)
'rcull draglllm
Q - x .:::::
don', care
-
/2
-
Review Questions
1. The logical operation gives a I output when all inputs are I.
2. The logical operation __ gives a 1 output when I or more of its inputs is I.
3. The logical operation __ is often used to compare if two inputs have the
same value .
. 4. A __ gate does not change the logic level of the input.
5. Name a common use for flip-flops.
6. An address is used to identify a predetermined binary address.
Address Bus
Memory Peripherals
CPU
(monitor,
(RAM, ROM) printer, etc.)
Data Bus
Address Bus
•
RAM ROM
~
Printer •
Disk •
Monitor •
Keyboard
CPU
Data Bus
Read/write
Control Bus
Program Counter
Internal
buses
Register A
Register B
Register C
Register 0
Inside CPUs
I. Foremost among the resources at the disposal of the CPU are a number of reg-
isters. The CPU uses registers to store information temporari Iy. The informa-
tion could be two values to be processed, or the address of the value needed to
be fetched from memory. Registers inside the CPU can be 8-bit, 16-bit, 32-bit,
or even 64-bit registers, depending on the CPU. In general, the more and big-
ger the registers, the better the CPU. The disadvantage of more and bigger reg-
isters is the increased cost of such a CPU.
2. The CPU also has what is called theALU (arithmetic/logic unit). The ALU sec-
tion of the PU is responsible for performing arithmetic functions such as add
ubtract, multiply, and divide, and logic functions such as AND, OR, and NOT:
Every CPU has what is called a program COUnter. The function of the program
16
7
counter is to point to the address of the next instruction to be executed. As each
instruction is executed, the program counter is incremented to point to the
address of the next instruction to be executed. The contents of the program
counter are placed on the address bus to find and fetch the desired instruction.
In the IBM PC, the program counter is a register called IP, or the instruction
pointer. .
4. The function of the instruction decoder is to interpret the instruction fetched'
into the CPU. One can think of the instruction decoder as a kind of dictionary,
storing the meaning of each instruction and what steps the CPU should take
upon receiving a given instruction. Just as a dictionary requires more pages the
more words it defines, a CPU capable of understanding more instructions
requires more transistors to design. i
If the program to perform the actions listed above is stored in' memory
locations starting at 1400H, the following would represent the contents for each
memory address location:
The actions performed by the CPU to run the program above would be as
follows:
1. The CPU's program counter can have a value between 0000 and FFFFH. The
program counter must be set to the value 1400H, indicating the address of the
first instruction code to be executed. After the program counter has been
Review Questions
I, How many bytes is 24 kilobytes?
2. What docs "RAM" stand Cor? Ho ' ,
3. " , r W IS It u"ed '
W hat docs "ROM" stand f< ? H " s 111 computer systems?
4 Wh ' RA or, ow IS It used i '
. . Y IS M called volatile mcmor ') n computer systems?
5. List the three major com y,
6. What docs "CPU" t d ~onents of a computcr system
' san for? Explain it f' '
7, L I t the three types of b 'c ,s unction in a computer
uses round 111 COl '
purpose of each type of bus nputer systems and state b ' fl
8, tate which of the followi " " ne y the
ng IS unidirectional and hi h i "
W IC IS bidirectional.
18
(a) data bus (b) address bus
9. If an address bus for a given computer has 16 lines, what is the maximum
amount of memory it can access?
10. What does "ALU" stand for? What is its purpose?
II . How are registers used in computer systems?
12. What is the purpose of the program counter?
13. What is the purpose of the instruction decoder?
SUMMARY
7
PROBLEMS
I. Computers use the binary system because each bit can have one of two voltage levels: on and
off.
2. 3410 = 1000102 = 2216
3. 1101012=3516=5310
4. 1110001
5. 010100
6. 461
7. 275
7
8. 38 30 78 38 36 2043 50 55 73
I. AND
2. OR
3. XOR
4. BulTer
5. Storing data
6. Decoder
I. 24,576 .
2. Random access memory; it is used for temporary storage of programs that the CPU IS run-
ning, such as the operating system, word processing programs, etc.
3. Read-only memory; it is used for permanent programs such as Ihose that control the keyboard.
etc.
4, The contents of RAM arc lost when the computer is powered 01T.
5. The CPU, memory, and 110 devices
6. Central processing unit; it can be consideredthe "brain" or the computer; it executes the pro.
grams and controls all other devices in the computer.
7. The address bus carries Ihe location (address) needed by the CPU; the data bus carries infor-
mation in and out ofthe CPU; the control bus is used by the CPU 10 send signals controlling
I/O devices,
R. (aJ bidirectional (b) unidirectional
9. 64K. or 65,536 byres
10. Arithmetic/logic unit; it performs all arithmetic and logic operations
1I. It IS for temporary storage of information
12. It holds the address of the next instruclion to be executed,
13. It tells the CPU what steps to perform for each instruction,
CHAPTER 1
THE 8051
MICROCONTROLLERS
OBJECTIVES
- 23
This chapter begins with a discussion of the role and importance of micro-
controllers in everyday life. In Section 1.1 we also discuss cntena to consider In
choosing a microcontroller, as well as the usc ofmicrocontroUers In the embedded
market. Section 1.2 covers various members of the 8051 family such as the 8052
and 8031, and their features. In addition, we discuss vanous versions of the 8051
such as the 8751, AT89C51, and OS5000.
In this section we discuss the need for microcontrollers and contrast them
with general-purpose microprocessors such as the Pentium and other x86 micro-
processors. We also look at the role of microcontrollers in the embedded market.
In addition, we provide some criteria on how to choose a microcontroller.
D ata b us
CPU
r I .i. I I
CPU RAM ROM
General-
Purpose RAM Serial
ROM VO Timer
Miero- COM
Port
processor Port I/O Timer Serial
T I I I I COM
Addresss bus Port
Engine control disk controller, sound card, CD-ROM driver, mouse, and so on.
Air bag Each one of these peripherals has a microcontroller inside it that
ABS performs only one task. For example, inside every mouse there is
Instrumentation a microcontroller that performs the task of finding the mouse
Security system position and sending it to the Pc. Table I-I lists some embedded
Transmission control products.
Entertainment
Climate control X86 PC embedded applications
Cellular phone
Keyless entry Although microcontrollers are the preferred choice for
many embedded systems, there are times that a microcontroller is .
Table I-I: Some inadequate for the task. For this reason, in recent years many
Embedded Products manufacturers of general-purpose microprocessors such as Intel,
Using
Freescale Semiconductor Inc. (formerly Motorola), AMD
Microcontrollers
(Advanced Micro Devices, lnc.), and Cyrix (now a division of
Choosing a microcontroller
There arc four rna' '.
Intel's 8051 Zilo ' ~or 8-blt mlerOcontrollers Th
mieroeontr;lIers ~a: :8~ and PIC 16X from Microchip
not compatible w'th nlque instruction set and reoi
T~~tei Freeseale's 6811,
no ogy. Each of these
I each othe P egrste- set: th f
ers. There arc also 16-bit and r. rograms written for one will ere ore, they are
crs, With all these dl'fIi 32-bll mleroeontrollers d not run on the oth-
. hoosi erent mie ma e by vari .
111 COOing one? Three . . roeontrollers, what eri' anous chIp mak-
meeling the eompulin entena 111 choosing mieroeo ten a do deSIgners consider
(2) availability of soft g needs of the task at hand ffi ntrollers are as follows' (I)
debugger, and (J) wi;are development tools sue~ lelently and cost effecti~e1
ext we elaborate limh e availability and reliable s as compilers, aSScmblers and
cr on each of h ourees of the' ,
t e above criteria mleroController.
26
Criteria for choosing a microcontroller
1. The first and foremost criterion in choosing a microcontroller is that it must
.meet the task at hand efficiently and cost effectively. In analyzing the needs
of a microcontroller-based project, we must first see whether all 8-bit, 16-bit,
or 32-bit microcontroller can best handle the computing needs of the task most
effectively. Among other considerations in this category are:
(a) Speed. What is the highest speed that the microcontroller supports?
(b) Packaging. Does it come in a 40-pin DIP (dual inline package) or a QFP
(quad flat package), or some other packaging format? This is important in
terms of space, assembling, and prototyping the end product.
(c) Power consumption. This is especially critical for battery-powered prod-
ucts.
(d) The amount of RAM and ROM on chip.
(e) The number of I/O pins and the timer on the chip.
(f) How easy it is to upgrade to higher-performance or lower power-con-
sumption versions.
(g) Cost per unit. This is important in terms of the final cost of the product in
which a microcontroller is used. For example, there are microcontrollers
that cost 50 cents per unit when purchased 100,000 units at a time.
In this section we first look atthc various members of the 8051 family of
mjcrocontrollcrs and their internal features. Plus we see who are the different man-
ufacturers of the 8051 and what kind of products they offer.
ON-CHIP o
Y 't ROM o
c
INTERRUPT 1--._. . for ETC ~
__ .. program
CONTROL
'-----,-----'-
~.
.-- code
ON-CHIP
RAM
TIMER 0 f-oc--} - g]
TIMER 1 _- Z
I • '?-
1J
c
Y oJ
~
IA I
CPU
\
A A .lJ
BUS
~
4 I/O
~7
SERIAL
CONTROL PORTS PORT
I
A
t .~
f t f
PO P2 P1 P3
I
Y
TXD RXD
I
...... .
-
ADDRESS/DATA
8052 micr.ocontroller
The 8052 is another member of the 8051 family The 8052 has all the stan-
dard features of the 8051 as well as an extra 128 bytes of RAM and an extra timer.
In other words, the 8052 has 256 bytes of RAM and 3 timers. It also has 8K bytes
of on-chip program ROM instead of 4K bytes. See Table 1-4.
Table 1-4: Comparison of 8051 Family Members
Feature 8051 8052 8031
ROM (on-chip program space in bytes) 4K 8K OK
RAM (bytes) 128 256 128
Timers 2 3 2
I/O pins 32 32 32
Serial port 1
Interrupt sources 6 8 6
As can be seen from Table 1-4, the 8051 is a subset of the 8052; therefore,
all programs written for the 8051 will run on the 8052, but the reverse is not true.
8751 microcontroller
This 8751 chip has only 4K bytes of on-Chip UV-EPROM. Using this chip
for development requires acecss to a PROM bumer, as well as a UV-EPROM eras-
er to crase the COntentsof UV-EPROM inside the 8751 chi b C
gram It. agarn.
. Beeausc the on-Chip . ROM for the 875/ is tpUV-EPROM
e,ore you can pro-
it takes
around
Id 20 mmutes to erase the 8751 before it can be d' ' .
e many manUfacturers to Introduce
. flash and NV RAM .
programme agam. This has
'11di - verSions of the 8051 as
;~:~iff~~~~~~~~~~n~~re arc also various speed versions of the 875 I availa'ble
DS89C4xO Trainer
WWW.8052.com/chips.phtml
WWW.MicroDigilaIEd.com
2
, ,,Review Questions
c..,V
(}t . I. Name three features of the 8051.
~ 2. What is the major di fference between the 805 I and 8052 microcontrollers?
3. Give the size of RAM in each of the following.
(a) 8051 (b) 8052 (c) 8031
4. Give the size of the on-chip ROM in each of the following.
(a) 8051 (b) 8052 (c) 8031
5. The 8051 is a(n) -bit microprocessor.
6. State a major difference between the 8751, the AT89C5 I, and the
DS89C420/30.
7. True or false. The DS89C420/30 is really an 8052 chip.
8. True or false. The DS89C420/30 has a loader embedded to the chip, therefore
eliminating the need for ROM burner.
9. The DS89C420/30 chip has bytes of on-chip ROM.
10. The DS89C420/30 chip has bytes of RAM.
SUMMARY
PROBLEMS
14. What does the term "third-party s~PP~rttl ~~~~'and 16-bir ver ions, which of
15. If a mierocontroller arehlteetur~ as 0 1
the following statements IStrue. .
(a) The 8-bit software will run on the 16-blt system.
(b) The 16-bit software will run on the 8-blt system.