0% found this document useful (0 votes)
41 views50 pages

6G - IoT

6 Generation
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views50 pages

6G - IoT

6 Generation
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Tl

T1
... "

THE 8051 MICRO CONTROLLER


AND EMBEDDED SYSTEMS

Using Assembly and C

SECOND EDITION

Muhammad Ali Mazidi


Janice Gillispie Mazidi
Rolin D. McKinlay

--
PEARSON
Prentice
Hall

pper addle River, ew Jersey


Columbus, Ohio
ealato ing-in-Publicalion Data
tlbnryofCongrtM g and C / Muhammad
~lJ.t~h.Muh.1mmad All. d bedded systems: using Assembly,
I luoconlrolltr an em . J
Tht: 80S m , . • Mazidi Rolin 0 McKtn ay.
Ah MUldl, Janke Gillispie 1

I : ~~ 1,l.119402-X (alk. paper) d I" systems. I McKinlay, Rolin D.IL


I ~rilmmi!blt conirollers.2. Embedde compu
Tl\k

IJ22] P76MJ78 2006


.2989 dell
2005054446

Edllor: Kale unsnar ..


Edltorl.l Assistant; Lara Dimmick
Production Editor: Kevin Happell
o..lgn Coordlnalo" Diane Ernsberger
Covar Deslgne" Bryan Huber
Cover Art; Super Stock
Production Managa" Matt Ollenweller
Stnlor Marketing Coordinator: Liz Farrell
Stnlor Marketing Managar: Ben Leonard

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

Pearson Prentice Hall'" is a trademark of Pearson Education, Inc.


Pearson- is a registered trademark of Pearson pic
Pr nUce Hall- is a registered trademark of Pearson Education, Inc.
Peal>Ol1Education Ltd.
P arson Education Singapore, Pte., Ltd. Pearson Education Australia PTY, Lintited
Pearson Education Canada, Ltd.
Petlt!On Education-Japan Pearson Education North Asia Ltd.
Pearson Educacion de Mexico, SA de C. V.
Pearsoll Education MalaYSia, Pte. LId.
Pearson Education lnc., Upper Saddle River, New Jersey

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

CHAPTER 0: INTRODUCTION TO COMPUTING 1


Section 0.1: Numbering and coding systems 2
Section 0.2: Digital primer 9
Section 0.3: Inside the computer 13

CHAPTER 1: THE 8051 MICROCONTROLLERS 23


Section 1.1: Microcontrollers and embedded processors 24
Section 1.2: Overview of the 8051 family 28

CHAPTER 2: 8051 ASSEMBLY LANGUAGE PROGRAMMING 37


Section 2.1: Inside the 8051 38
Section 2.2: Introduction to 8051 Assembly programming 41
Section 2.3: Assembling and running an 8051 program 44
Section 2.4: The program counter and ROM space in the 8051 46
Section 2.5: 8051 data types and directives 49
Section 2.6: 8051 flag bits and the PSW register 52
Section 2.7: 8051 register banks and stack 55

CHAPTER 3: JUMP, LOOP, AND CALL INSTRUCTIONS 69


Section 3.1: Loop and jump instructions 70
Section 3.2: Call instructions 75
Section 3.3: Time delay for various 8051 chips 80

CHAPTER 4: I/O PORT PROGRAMMING 93


Section 4.1: 8051 I/O programming 94
Section 4.2: I/O bit manipulation programming 100

CHAPTER 5: 8051 ADDRESSING MODES 109


Section 5.1: Immediate and register addressing modes 110
Section 5.2: Accessing memory using various addressing modes 112
Section 5.3: Bit addresses for I/O and RAM 122
Section 5.4: Extra 128-byte on-chip RAM in 8052 13\

CHAPTER 6: ARITHMETIC & LOGIC INSTRUCTIONS


AND PROGRAMS 139
Section 6.1: Arithmetic instructions 140
Section 6.2: Signed number concepts and arithmetic operations 150
Section 6.3: Logic and compare instructions 155
Section 6.4: Rotate instruction and data serialization 161
Section 6.5: BCD, ASCII, and other application programs :167

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

CHAPTER 8: 8051 HARDWARE CONNECTION AND


INTEL HEX FILE
217
Section 8.1: Pin description of the 805 I
218
Section 8.2: Design and test of DS89C4xO trainer
224
Section 8.3: Explaining the Intel hex file
232

CHAPTER 9: 8051 TIMER PROGRAMMING


IN ASSEMBLY AND C
239
Section 9.1: Programming 8051 timers
240
Section 9.2: Counter programming
255
Section 9.3: Programming timers 0 and I in 8051 C 260

CHAPTER 10: 8051 SERIAL PORT PROGRAMMING


IN ASSEMBLY AND C
277
Section 10.1: Basics of serial communication 278
Section 10.2: 8051 connection to RS232 285
Section 10.3: 8051 serial port programming in Assembly 287
Section 10.4: Programming the second serial port 300
Section 10.5: Serial port programming in C 306

CHAPTER II: INTERRUPTS PROGRAMMING


IN ASSEMBLY AND C 317
Section I 1.1: 8051 interrupts 318
Section 11.2: Programming timer interrupts 322
Section I 1.3: Programming external hardware interrupts 326
Section 11.4: Programming the serial communication interrupt 333
Section I1.5: Interrupt priority in the 8051/52 337
Section 11.6: Interrupt programming in C 340

CHAPTER 12: LCD AND KEYBOARD INTERFACING 351


Section 12.1: LCD interfacing 352
Section 12.2: Keyboard interfacing 363

CHAPTER 13: ADC, DAC, AND SENSOR INTERFACING 373


Section 13. I: Parallel and serial ADC 374
Section 13.2: DAC interfacing 398
Section 13.3: Sensor interfacing and signal conditioning 403

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

CHAPTER 15: 8051INTERFACING WITH THE 8255 449


Section 15.1: Programming the 8255 450
Section 15.2: 8255 interfacing 458
Section J 5.3: 8051 C programming for the 8255 462

CHAPTER 16: DS12887 RTC INTERFACING


AND PROGRAMMING 467
Section 16.1: DS 12887 RTC interfacing 468
Section 16.2: OSI2887 RTC programming in C 476
Section 16.3: Alarm, SQW, and IRQ features of the
DS 12887 chip 479

CHAPTER 17: MOTOR CONTROL: RELAY, PWM, DC,


AND STEPPER MOTORS 491
Section 17.1: Relays and optoisolators 492
Section 17.2: Stepper motor interfacing 498
Section 17.3: DC motor interfacing and PWM 507

APPENDIX A: 8051 INSTRUCTIONS, TIMING, AND REGISTERS 523

APPENDIX B: BASICS OF WIRE WRAPPING 563

APPENDIX C: IC TECHNOLOGY AND SYSTEM DESIGN ISSUES 567

APPENDIX D: FLOWCHARTS AND PSEUDOCODE 587

APPENDIX E: 8051 PRlMER FOR X86 PROGRAMMERS 592

APPENDIX F: ASCll CODES 593'

APPENDIX G: ASSEMBLERS, DEVELOPMENT RESOURCES,


AND SUPPLIERS 594

APPENDIX H: DATA SHEETS 596

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

..",.n,·. ICp ppr .h I used to cover various aspects of


1.1II,1I"",£;e
1)1' • mrrung and interfacing. Many examples and
n 10 I tI f) the concept and provide students with an
din' Rev le\\ qu lions are provided at the end of each
m n POlOt of lh ecuon.
ver num f t m (bmary. decimal, and hex), and provides
J • nd mpuier terminology. This is designed
h nical engineering students, who have not
\ ho need to refresh their memory on these

the 0 I and feature of other 8051 fam-


I. n ~OOO.and D 9C4xO. It also pro-
hIp •
r hue lure 0/ the 0 I and explains the
-to-run program. It also explores the
In Chapter 3 the topics of loop, jump, and call instructions are discussed,
with many programming examples.
Chapter 4 is dedicated to the discussion of I/O ports. This allows students
who are working on a project to start experimenting with 8051 I/O interfacing and
start the project as soon as possible.
Chapter 5 covers the 8051 addressing modes and explains how to use the
code space of the 8051 to store data, as well as how to access data.
Chapter 6 is dedicated to arithmetic, logic instructions, and programs.
The C programming of the 8051 is covered in Chapter 7.
In Chapter 8 we discuss the hardware connection of the 8051 chip.
Chapter 9 describes the 8051 timers and how to use them as event counters.
Chapter 10 is dedicated to serial data communication of the 8051 and its
interfacing to the RS232. It also shows 8051 communication with COM ports of
the [BM PC and compatible computers. In addition, the second serial port of the
DS89C4xO is also covered.
Chapter 11 provides a detailed discussion of 8051 interrupts with many
examples on how to write interrupt handler programs.
Chapter 12 shows 8051 interfacing with real-world devices such as LCDs
and keyboards.
Chapter 13 shows 8051 interfacing with real-world devices such as DAC
chips, ADC chips, and sensors.
In Chapter 14 we cover 8031/51 interfacing with external memories, both
ROM and RAM.
Chapter 15 addresses the issue of adding additional ports to the 8031/51
using an 8255 chip.
Chapter 16 shows how to connect and program the DS 12887 real-time
clock chip.
Finally, Chapter 17 shows basic interfacing to relays, optoisolators, and
motors.
The appendices have been designed to provide all reference material
required for the topics covered in the book. Appendix A describes each 8051
instruction in detail, with examples. Appendix A also provides the Clock count for
instructions, 8051 register diagrams, and RAM memory maps. Appendix B
describes basics of wire wrapping. Appendix C covers IC technology and logic
families, as well as 8051 I/O port interfacing and fan-out. Make sure you study this
before connecting the 8051 to an external device. 1n Appendix D, the use of flow-
charts and psuedocode is explored. Appendix E is for students familiar with x86
architecture who need to make a rapid transition to 8051 architecture. Appendix F
provides the table of ASCII characters. Appendix G lists resources for assembler
shareware, and electronics parts. Appendix H contains data sheets for the 8051 and
other IC chips.

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

S","",," ""00."'_""",, ,"'" . f


The solutions manual was produced wu. h tee h h Ipo. f Ardeshirfor theI rm
dr (0
\ _
harif University). The solutions manual and PowerPotnt@ slide ir Pr mi e
ings are available online for instructors only. Instructors Can Contact the
Ila/l representative or visit wWw.prenhall.eom.
Acknowledgments

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.

Another interesting Web site is www.8052.com. which contains more


discussion on the microcontroller. Finally, the following site provides useful Intel
manuals:

http://developer.intel.com/design/mcs51/docs_mcs51.htm

SafariX: Where the Web Meets Textbooks for Student Savings!


SafariX Textbooks Online" is an exciting n'ew choice for students looking
to save money. As an alternative to purchasing the print textbook, students can
subscribe to the same content online and save up to 50% off the suggested list
price of the prim text. With a SafariX WebBook, students can search the text,
make notes online, print out reading assignments that incorporate lecture notes,
and bookmark important passages for later review. For more information, or to
subscribe to the SafariX WebBook, visit http://www.safarix.com.

- 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.

The authors can be Contacled aI/he fol/oll In -m "


any comments or suggestions, or if you find II) cit I •
Sol mdebooks@yahoo.com
mmazidi@microdigi,aJed.com
hari rmckinJaY@microdigi,aled.com
rngs
Iiall
Ack
This volume is dedicated to the memory of Dr. A. Davoodi,
Professor of Tehran University, who in the tumultuous years a/my youth
taught me the importance of an independent search for truth.
- Muhammad Ali Mazidi

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

Upon completion of this chapter, you will be able to:

» 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

SECTION0.1: NUMBERING AND CODING SYSrE S


'ngs usc base 10 (de 1fI1I,{) ruhm II .. mpurc u
Whereas h uman bel .
rbebase 2 (binary) system. In this section we expl In h \\ 10 nv 'n fr Ih d _
imaI systemto the binary system, and VICCversa, Th nvem 01 rep :tll:tllon of
binarynumbers in base 16,called hexadecimal, I 0 I c v red 10.. 11, Ih bma-
ry formatof the alphanumeric code, called ASCII. IS pi n:d

Decimal and binary number systems


Although there has been speculation rh t Ih ' on '10 of th rem
is the fact that human beings have 10 fingers. th re I s IUlel
aboulthe reason behind the usc of the binarysystem In om pule Th
em is used in computers because I and 0 represent the I'" \ III
and off. Whcreas in base 10 therc arc 10 di lin I ym Is. O. I .••
therearc only two, 0 and J, with which to generate num r a
I
its 0 through 9; binary contains digits 0 and I only. Th 1\\ In
I, arc commonly referred to as bits.

Convertingfrom decimal to binary


be ~ne method of converting from de imollo blndry I I dlVI Ih
r 2 'elm I
D~ th y repeatedly, keeping track of the re/lUlllld'''' Thi pr
UDh e quohcnl becomcs zero Th . nllnu
10obtain Ihc binary number Th" , ~ remalndcn. re lh n \\ nil n In r \ rd r
Eumple 8-1 ' IS IS emonstraled In Empie 0-1

Convert 2510 to binary,

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

-
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

1101012 = Decimal Binary


1X20 = 1x1 = 1 1
OX21 = Ox2 = 0 00
2
1X2 = 1X4 4 100
3
OX2 = Ox8 = 0 0000
1x24 1X16 16 10000
1X25 = 1x32 = J2 100000
53 110101

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,

CHAPTER 0: INTRODUCTION TO COMPUTING 3


Table 0-1: Base 16
Hexadecimal system Number Systems
Base 16, or the hexadecimal system as it is called in
computer literature, is used as a convenient representation Decimal Binary Hex
of binary numbers. For example, it is much easier for a o 0000 o
human being 10 represent a string of Os and I s such as 0001
100010010110 a its hexadecimal equivalent of896H. The 2 0010 2
binary ystem has 2 digits, 0 and I. The base 10 system has J 0011 J
10 digits, 0 through 9. The hexadecimal (base 16) system 4 0100 4
ha 16 digits. In base 16, the first 10 digits, 0 to 9, arc the 5 0101 5
same as in decimal, and for the remaining six digits, the let- 6 0110 6
ters A, B, C, D, E, and F are used. Table 0-1 shows the 7 0111 7
equivalent binary, decimal, and hexadecimal representa- 8 1000 8
tions for 0 10 15. 9 1001 9
10 1010 A
Converting between binary and hex 11 1011 B
. To represent a binary number as its equivalent hexa- 12 1100 c
decimal number, start from the right and group 4 bi ts at a 13 1101 o
lime, replacing cach 4-bit binary number with its hex equiv- 14 1110 E
alent shown III Table 0-1. To convert from hex to bi 15 1111 F
each h ex die:
Iglt IS rep Iaced with its 4-bit binary e uivalmary,
ee Examples 0-4 and 0-5. q cnt,

Example 0-4

Represent binary 100111110 I 0 I in hex.

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.

32 16 .8. 1 2 1 First, convert to binary.


I o 1 1 o I 32 + 8 + 4 + I ~ 45

(b) Convert 62910 to hex.

512 256 128 64 32 16 .8. 1 2 1


I 0 0 1 I I o I o I

62910,;, (512 + 64 + 32 + 16 + 4 + I) ~ 0010 01 J I 01012 = 275 hex

(c) Convert 171410 to hex.

1024 512 256 128 64 32' 16 .8. 1 2 1


1 101 0 I 1 o o 1 o

17141O~ (1024+512+ 128+32+ 16+2)=0110 ion 00102~682hex

Converting from hex to decimal


Conversion from hex to decimal can also be approached in two ways:
l. Convert from hex to binary and then to decimal. Example 0-7 demonstrates
this method of converting from hex to decimal.
2. Convert directly from hex to decimal by summing the weight of all digits.

Example 0-7

Convert the following hexadecimal numbers to decimal.

(a) 68216 ~ OlIO lOll 00102


1024 512 256' 128 64 32 16 .8. 1 2 1
I 1 o I o I I o o I o
1024 + 512 + 128 + 32 + 16 + 2 ~ 171410

(b) 9F2DJ6 = 1001 1111 0010 11012


32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 .8. 1 2 1
1 0 0 I I I I I 00 I 0 110 I

32768 + 4096 + 2048 + 1024 + 512 + 256 + 32 + 8 + 4 + 1= 40,749


10

CHAPTER 0: INTRODUCTION TO COMPUTING 5

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

Addition and subtraction of hex numbers


In studying issues related to software and hardware of computers, it is
often necessary to add or subtract hex numbers. Mastery of these techniques is
essential. Hex addition and subtraction are discussed separately below.
Addition of hex numbers
This section describes the process of adding hex numbers, Starting with the
least significant digits, the digits are added together. 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. The best way to explain this is by
example, as shown in Example O-JO,

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

Subtraction of hex numbers


In subtracting two hex numbers, if the second digit is greater than the first,
borrow 16 from the preceding digit. See Example 0-11.
ASCII code
The discussion so far has revolved around the representation of number
systems, Since all information in the computer must be represented by Os and Is,
binary patterns must be assigned to letters and other characters, In the 1960s a
standard representation called ASCII (American Standard Code for Information
Interchange) was established, The ASCII (pronounced "ask-E") code assigns

CHAPTER 0: INTRODUCTION TO COMPUTING 7

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

SECTION 0.2: DIGITAL PRIMER

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.

Figure 0-2. Binary Signals


Logic gates
Logical AND Function
Binary logic gates are simple circuits that
take one or more input signals and send out one out- Inputs Output
put signal. Several of these gates are defined below.
XY XANDY
00 0
AND gate oI 0
The AND gate takes two or more inputs and I0 0
performs a logic AND on them. See the truth table I 1 1
and diagram of the AND gate. Notice that if both
inputs to the AND gate are 1, the output will be I.
~:=:[)- X AND V
Any other combination of inputs will give a 0 output.
The example shows two inputs, x and y. Multiple Logical OR Function
outputs are also possible for logic gates. In the case Inputs Output
of AND, if all inputs are I, the output is I. If any
input is 0, the output is zero.: XY XORY
00 0
OR gate
011
The OR logic function will output a I if one 101
or more inputs is I. If all inputs are 0, then and only I 1 1
then will the output be O.
~=D-XORV
Tri-state buffer
Buffer
A buffer gate does not change the logic level
of the input. It is used to isolate or amplify the sig- X--j)-V
nal.
Control -J

CHAPTER 0: INTRODUCTION TO COMPUTING 9

___ c _
Inverter

The inverter, also called NOT, outputs the


value opposite to that input to the gate. That IS: a I Logical Inverter
input will give a 0 output, while a 0 input will give a
I output. Input Output
x NOT X
XORgate o
The XOR gate performs an exclusive-OR
o
operation on the inputs. Exclusive-OR produces a I x -{>o-- NOT X
output if one (but only one) input IS I. II both
operand arc 0, the output is zero. Likewise, if both
operands arc I, the output is also zero. Notice from Logical XOR Function
the XOR truth table, that whenever the two mpurs Inputs Output
arc the same, the output is zero. This function can be
used to compare two bits to sec if they arc the same. Xy X XOR Y
00 0
NAND and NOR gates o1 I
10 I
The NAND gate functions like an AND gate I I 0
with an inverter on the output. It produces a zero out-
put when all inputs are I; otherwise, it produces a I ~=D-XXORY
output. The NOR gate functions like an OR gate with
an inverter on the output. It produces a I if all inputs
arc 0; otherwise, it produces a O. NAND and NOR
gates arc used extensively in digital design because Logical NAND Function
they are easy and inexpensive to fabricate. Any cir- Inputs Output
cuit that can be designed with AND, OR, XOR, and
INVERTER gates can be implemented using only XY X NANDY
00 I
AND and NOR gates. A simple example of this is
given below. Notice in NAND, that if any input is oI I
zero, the output is one. Notice in NOR, that if any J 0 1
Input lone, the output is zero. I I 0

~=D-XNANDY
Logic design using gates

Next we will show a simple logic design to


add two binary digits. If we add two binary digits Logical NOR Function
there are four Possible Outcomes:
Inputs Output
X y X NOR Y
Carry Sum 00 1
0+0= 01
0+/=
o 0 0
1+0=
o 1 10 0

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

Figure 0-3, Two Implementations of a Half-Adder

Figure 0-4 shows a block dia-


gram of a half-adder. Two half-adders X Sum
can be combined to form an adder that Half-
can add three input digits. This is called Adder
a full-adder. Figure 0-5 shows the logic y
Carry
diagram of a full-adder, along with a . out
block diagram that masks the details of
the circuit. Figure 0-6 shows a 3-bit F' 0-4 81 k 0' f H If d
. 3 full -a dd ers.
a dd er usmg igure . oc iagram 0 a a -Ad er

x X Half- Carry
y Sum y Adder

Sum

Half-
- C out
Adder Carry
C in
C in
Final Sum

Figure 0-5. Full-Adder Built from a Half-Adder

CHAPTER 0: INTRODUCTION TO COMPUTING 11


uecoaers
Another example of the application
of logl gates is the decoder. Decoders arc xo
widely u ed for address decoding In corn-
Full- r-----so
pUlcr. d ign Figure 0-7 shows decoders
. . for rt YO
Adder
9(1001 binary) and 5 (0101) usmg mvc _
Carry
rs nd A D gate .

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

() ddre, deCOder for 9 (binary 100 I) -{)o-


I he output of Ihe AND gate will be , (b) Address decoder for 5 (binary 0101)
II und only If Ihe Inpur i~ binary 100 I.
The output of the AND gate will be I
ifand only iflhe input is binary 0101.
OecOders

D
elk D
Q No
II. x
Ik 1- 0
1- I
Q
(~)
'rcull draglllm
Q - x .:::::
don', care

(b) BlOck diagram


(c) Truth table

-
/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.

SECTION 0.3: INSIDE THE COMPUTER

Tnthis section we provide an introduction to the organization and internal


working of computers. The model used is generic, but the concepts discussed are
applicable to all computers, including the IBM PC, PS/2, and compatibles. Before
embarking on this subject, it will be helpful to review definitions of some of the
most widely used terminology in computer literature, sueh as K, mega, giga, byte,
ROM, RAM, and so on.

Some important terminology


One of the most important features of a computer is how mueh memory it
has. Here we review terms used to describe amounts of memory in IBM pes and
compatibles. Recall from the discussion above that a bit is a binary digit that can
have the value 0 or 1. A byte is defined as
8 bits. A nibble is half a byte, or 4 bits. A Bit a
word is two bytes, or 16 bits. The display is Nibble 0000
intended to show the relative size of these Byte 0000 0000
units. Of course, they could all be com- Word 0000 0000 0000 0000
posed of any combination of zeros and
ones.
A kilobyte is 210 bytes, which is 1024 bytes. The abbreviation K is often
used. For example, some floppy disks hold 356K bytes of data. A megabyte, or
meg as some call it, is 220 bytes. That is a little over I million bytes; it is exaetly
1,048,576 bytes. Moving rapidly up the scale in size, a gigabyte is 230 bytes (over
I billion), and a terabyte is 240 bytes (over 1 trillion). As an example of how some
of these terms are used, suppose that a given computer has 16 megabytes of mem-
ory. That would be 16 x 220, or 24 x 220, which is 224 Therefore, 16 megabytes
is 224 bytes.
Two types of memory commonly used in microcomputers are RAM, which
stands for "random access memory" (sometimes called read/write memory), and
ROM, which stands for "read-only memory." RAM is used by the computer for
temporary storage of programs that it is running. That data is lost when the COIll-
puter is turned off. For this reason, RAM is sometimes called volatile memory.
ROM contains programs and information essential to operation of the computer.
The information in ROM is permanent, cannot be changed by the user, and is not
lost when the power is turned off. Therefore, it is called nonvolatile memory,

CHAPTER 0: INTRODUCTION TO COMPUTING 13


Internal organization of computers
The internal working of every computer can be broken down into three
parts: CPU (central processing unit), memory, and I/O (input/output) devices (see
Figure 0-9). The function of the CPU is to execute (process) information stored in
memory. The function of I/O devices such as the keyboard and video monitor is
to provide a means of communicating with the CPU. The CPU is connected to
memory and I/O through strips of wire called a bus. The bus carries infomlation
from place to place inside a computer just as a street bus carries people from place
to place. In every computer there are three types of buses: address bus, data bus,

and control bus.


For a device (memory or I/O) to be recognized by the CPU, it must be
assigned an address. The address assigned to a given device must be unique; no
two devices are allowed to have the same address. The CPU puts the address (in
binary, of course) on the address bus, and the decoding circuitry finds the device.
Then the CPU uses the data bus either to get data from that device or to send data
to it. The control buses are used to provide read or write signals to the device to
indicate if the CPU is asking for information or sending it information. Of the
three buses, the address bus and data bus determine the capability of a given CPU.

Address Bus

Memory Peripherals
CPU
(monitor,
(RAM, ROM) printer, etc.)

Data Bus

Figure 0-9. Inside the Computer

More about the data bus


incc data lines arc used to carry information'
data lines available, the better the CPU If m and out of a CPU, the more
lane, it is clear that more lanes pro id . bone thinks of data lines as highway
. VI e a ettcr pathw b
external devices (such as printers, RAM ROM ay etween the CPU and its
same token, that increase in the b f' .' etc., see FIgure 0-10). By the
M num cr 0 lanes m
. rc data bu es mean a more expensive CPU an creases the cost of construction.
line IScalled data bus. The avcragc si d computer. The grouping of data
d 64 E e size of data b . C
an . arly computers such as Apple 2 u d uses m PUs varies between 8
puters ueh as Cray use a 64-bit data bus D: an S-bit data bus, while supercom-
PU must use them either to receive or t~ s a~ dbusesare bidirectional, since the
eomputcr IS related to the size f' en ata. The processin
a time, but a 16-bit bOlts buses, since an 8-bit bu g power of a
us can send OUI 2 bytes I' S can send out I byte
a a lime , W hiic h iIS twice
. as fast
14 • .
More about the address bus
Since the address bus is used to identify the devices and memory connect-
ed to the CPU, the more address buses available, the larger the number of devices
that can be addressed. In other words, the number of address buses for a CPU
I
determines the number of locations with which it can communicate. The number
of locations is always equal to 2x, where x is the number of address lines, regard-
less of the size of the data bus. For example, a CPU with 16 address lines can pro-
vide a total of 65,536 (216) or 64K bytes of addressable memory~ Each location
can have a maximum of I byte of data. This is due to the fact that all general-pur-
pose microprocessor CPUs are what is called byte addressable. As another exam-
ple, the IBM PC AT uses a CPU with 24 address lines and 16 data lines. In this'
case the, total accessible memory is 16 megabytes (224 = 16 megabytes). In this
example there would be 224
locations, and since each location is one byte, there
would be 16 megabytes of memory. The address bus is a unidirectional bus,
which means that the CPU uses the address bus only to send out addresses. To
summarize: The total number of memory locations addressable by a given CPU is
always equal to 2x where x is the number of address bits, regardless of the size of
the data bus.

Address Bus


RAM ROM
~
Printer •
Disk •
Monitor •
Keyboard
CPU

Data Bus
Read/write
Control Bus

Figure 0-10. Internal Organlzation of Computers

CPU and its relation to RAM and ROM


For the CPU to process information, the data must be stored in RAM or
ROM. The function of ROM in computers is to provide information that is fixed
and permanent. This is information such as tables for character patterns to be dis-
played on the video monitor, or programs that are essential to the working of the
computer, such as programs for testing and finding the total amount of RAM
installed on the system, or programs to display information on the video monitor.
In contrast, RAM is used to store information that is not permanent and can change
with time, such as various versions of the operating system and application pack-
ages such as word processing or tax calculation packages. These programs are
loaded into RAM to be processed by the CPU. The CPU cannot get the informa-

CHAPTER 0: INTRODUCTION TO COMPUTING 15


. ., slow. In other wor d s, th e CPU first
tion directly from the disk since the disk IS~~M (or ROM). Only ifit IS not there
seeks the information to be processed fromdeviee such as a disk, and then It trans-
h CPU seek it from a mass storage RAM and ROM are sometimes
docs t e . RAM For this reason, Figure
fers the information to . d di k are called secondary memory .
.' . memOlY an IS s . h PC
referred to as primary f the internal organizatIOn of t e .
0-11 shows a block diagram 0

Program Counter

Flags Instruction Register


ALU
lnstruction
decoder, timing,
and control

Internal
buses
Register A
Register B
Register C
Register 0

Figure 0-11. Internal Block Diagram of a CPU

Inside CPUs

A program stored in memory provides instructions to the CPU to perform


an action. The action can simply be adding data such as payroll data or control-
ling a machine such as a robot. It is the function of the CPU to fetch these instruc-
tions from memory and execute them. To perform the actions of fetch and execute,
all P s arc equipped with resources such as the following:

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

Internal working of computers


To demonstrate some of the concepts discussed above, a step-by-step
analysis of the process a CPU would go through to add three numbers is given
next. Assume that an imaginary CPU has registers called A, B, C, and D. It has an
8-bit data bus and a 16-bit address bus. Therefore, the CPU can access memory
from addresses 0000 to FFFFH (for a total of 10000H locations). The action to be
performed by the CPU is to put hexadecimal value 21 into register A, and then add
to register A values 42H and 12H. Assume that the code for the CPU to move a
value to register A is 1011 0000 (BaH) and the code for adding a value to register
A is 0000 a 100 (04H). The necessary steps and code to perform them are as fol-
lows.

Action Code Data


Move value 21H into register A BOH 21H
Add value 42H to register A 04H 42H
Add value 12H to register A 04H 12H

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:

Memory address Contents of memory address


1400 (BO)code for moving a value to register A
1401 (21)value to be moved
1402 (04)code for adding a value to regist'er A
1403 (42)value to be added
1404 (04)code for adding a value to register A
1405 (12)value to be added
1406 (F4)code for halt

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

CHAPTER 0: INTRODUCTION TO COMPUTING 17


loaded with the address of the first instruction, the CPU is ready to execute"
2, The CPU puts 1400H on the address bus and sends it out. The memory CIr-
cuitry finds the location while the CPU activates the READ signal, mdleatmg
to memory that it wants the byte at location 1400B. This causes the contents
of memory location 1400H, which is 80, to be put on the data bus and brought
into the CPU,
3, The CPU decodes the instruction 80 with the help of its instruction decoder
dictionary, When it finds the definition for that instruction it knows it must
bring into register A of the CPU the byte in the next memory location,
Therefore, it commands its controller circuitry to do exactly that. When it
brings in value 21 H from memory location 140 I, it makes sure that the doors
of all registers are closed except register A, Therefore, when value 21 H comes
into the CPU it will go directly into register A, After completing one instruc-
tion, the program counter points to the address of the next instruction to be exe-
cuted, which in this case is 1402H, Address 1402 is sent out on the address bus
to fetch the next instruction,
4, From memory location 1402H it fetches code 04H. After decoding, the CPU
knows that it must add to the contents of register A the byte sitting at the next
address (1403), After it brings the value (in this case 42H) into the CPU, it pro-
vidcs the contents of register A along with this value to the ALU to perform the
addition, It then takes the result of the addition from the ALU's output and puts
u m register A, Meanwhile the program counter bceomes 1404, the address of
tile next instruction,
5, Address 1404H is put on the address bus and the code is fetched into the CPU
decoded, and executed, This code is again adding a value to register A, The
program counter IS updated to 1406H,
6, Finally. tbc contents of address 1406 arc fetched in and executed, This HALT
fmstrucuon
th tells
' the CPU
, to stop incrementing the program counter and asking'
or e next instruction. In the absence of the HALT tl CPU Id '
t pd ti h ,1e wou connnue
I a II1g t e program counter and fetching instructions,
Now suppose that address 1403H contain d I '
would the CPU distinguish between data 04 to b: adv:,~e 04 Instead of 42H. How
that code 04 for this CPU means move the next val e . and code 04? Remember
the PU wi II not try to decodc the next value. It si uc Into register A. Therefore,
Iollowing mcmory location into reg' t A mply moves the contents of the
IS er ,regardless of its value,

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

The binary number system represents all numbers with a combination of


the two binary digits, 0 and I. The use of binary systems is necessary in digital
computers because only two states can be represented: on or off.' Any binary num-
ber can be coded directly into its hexadecimal equivalent for the convenience of
humans. Converting from binary/hex to decimal, and vice versa, is a straightfor-
ward process that becomes easy with practice. The ASCII code is a binary code
used to represent alphanumeric data internally in the computer. It is frequently
used in peripheral devices for input and/or output.
The logic gates AND, OR, and Inverter are the basic building blocks of
simple circuits. NAND, NOR, and XOR gates are also used to implement circuit
design. Diagrams of half-adders and full-adders were given as examples of the use
of logic gates for circuit design. Decoders are used to detect certain addresses.
Flip-flops are used to latch in data until other circuits are ready for it.
The major components of any computer system are the CPU, memory, and
I/O devices. "Memory" refers to temporary or permanent storage of data. In most
systems, memory can be accessed as bytes or words. The terms kilobyte,
megabyte, gigabyte, and terabyte are used to refer to large numbers of bytes.
There are two main types of memory in computer systems: RAM and ROM. RAM
(random access memory) is used for temporary storage of programs and data.
ROM (read-only memory) is used for permanent storage of programs and data that
the computer system must have in order to function. All components of the com-
puter system are under the control of the CPU. Peripheral devices such as 1/0
(input/output) devices allow the CPU to communicate with humans or other com-
puter systems. There are three types of buses in computers: address, control, and
data. Control buses are used by the CPU to direct other devices. The address bus
is used by the CPU to locate a device or a memory location. Data buses are used
to send information back and forth between the CPU and other devices.
Finally, this chapter gave an overview of digital logic.

CHAPTER 0: INTRODUCTION TO COMPUTING 19

7
PROBLEMS

SECTIO 0.1: NUMBERING AND CODING SYSTEMS

I. Convert the following decimal numbers to binary.


(a) 12 (b) 123 (c) 63 (d) 128 (c) 1000
2. Convert the following binary numbers to decimal.
(a) 100100 (b) 1000001 (c) 11101 (d) 1010 (e) 00100010
3. Convert the values in Problem 2 10 hexadecimal.
4. onvert the following hex numbers to binary and decimal.
(a) 2891-1 (b) 1'441-1 (e) 9121-1 (d) 2BI-I (c) FFFFI-I
S. onvcrt the values in Problem I to hex.
6. Find the 2 's complement of the following binary numbers.
(a) 1001010 (b) 111001 (e) 10000010 (d) 111110001
7. Add the following hex values.
(a) 2CI-I + 3FI-1 (b) 1'341-1+ SD61-1 (c) 200001-1 + 12FFI-I (d) FFFFI-I + 22221-1
8. Perform hex subtraction for the following.
(a) 241'1-1- 1291-1 (b) FE91-1- SCCI-I (c) 2FFFFI-I - FFFFFI-I (d) 9FF2SI-I-
4DD991-1
9. Show the ASCII codes for numbers 0, 1,2, 3, ...,9 in both hex and binary.
'10. Show the ASCII code (in hex) for the following string:
"U.S.A. is a country" CR, LF
"in North America" CR, LF
CR is carriage return
LF is line feed

ECTION 0.2: DIGITAL PRIMER

II. Draw a 3-input OR gate using a 2-input OR gate .


• 12. Show the truth table for a 3-input OR gate.
13. Draw a 3-inpllt AND gate using a 2-input AND gate .
• 14. Sho:" the truth table for a 3-input AND gate. __
IS. Design a 3-mpllt XOR gate with a 2-input XOR te Sh
a 3-inpllt XOR. ga e. ow the truth table for
16. List the truth table for a 3-input NAND.
'17. List the truth table for a 3-inpllt NOR.
18. how the decoder for binary 1100.
19. ~ow the decoder for binary 110 II .
• 20. List the truth table for a D-FF. I

E TIO 0.3: INSIDE TI-IE COMPUTER

21. Answer the follOWing:


(a) How many nibbles are 16 bits?
(b) How many bytes are 32 bi ? .
(c) If ItS.
a word is defined as 16 bits h
(d) Wh . I S, ow many ds :
at IS the exact value (in de' I) war S IS a 64-bit data itemry
erma of I meg? .
20
(e) How many K is 1 meg?
(t) What is the exact value (in decimal) of I giga?
(g) How many K is I giga?
(h) How many meg is I giga?
(i) If a given computer has a total of 8 megabytes of memory, how many
bytes (in decimal) is this') How many kilobytes is this')
22. A given mass storage device such as a hard disk can store 2 gigabytes of infor-
mation. Assuming that each page of text has 25 rows and each row has 80
columns of ASCII characters (each character = I byte), approximately how
many pages of information can this disk store?
23. In a given byte-addressable computer, memory locations 10000H to 9FFFFH
are available for user programs. The first location is 10000H and the last loca-
tion is 9FFFFH. Calculate the following:
(a) The total number of bytes available (in decimal)
(b) The total number of kilobytes (in decimal)
24. A given computer has a 32-bit data bus. What is the largest number that can
be carried into the CPU at a time?
25. Below are listed several computers with their data bus widths. For each com-
puter, list the maximum value that can be brought into the CPU at a time (in
both hex and decimal).
(a) Apple 2 with an 8:bit data bus
(b) IBM PC with a lti-bit data bus
(c) IBM PC with a 32-bit data bus
(d) Cray supercomputer with a 64-bit data bus .
26. Find the total amount of memory, in the units requested, for each of the fol-
lowing CPUs, given the size of the address buses.
(a) I 6-bit address bus (in K)
(b) 24-bit address bus (in megabytes)
(c) 32-bit address bus (in megabytes and gigabytes)
(d) 48-bit address bus (in megabytes, gigabytes, and terabytes)
27. Regarding the data bus and address bus, which is unidirectional and which is
bidirectional?
28. Which register of the CPU holds the address of the instruction to be fetched?
29. Which section ofthe CPU is responsible for performing addition?
30. List the three bus types present in every CPU.

ANSWERS TO REVIEW QUESTIONS

SEcrrON 0.1: NUMBERING AND CODING SYSTEMS

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

CHAPTER 0: INTRODUCTION TO COMPUTING 21

7
8. 38 30 78 38 36 2043 50 55 73

SECTIO 0.2: DIGITAL PRIMER

I. AND
2. OR
3. XOR
4. BulTer
5. Storing data
6. Decoder

ECTION 0.3: INSIDE THE COMPUTER

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

Upon completion of this chapter, you will be able to:

» Compare and contrast microprocessors and microcontrollers


» Describe the advantages of microcontrollers for some applications
» Explain the concept of embedded systems
» Discuss criteria for considering a microcontroller
» Explain the variations of speed, packaging, memory, and
cost per unit and how these affect choosing a microcontroller
» Compare and contrast the various members of the 8051 family
» Compare 8051 microcontrollers offered by various manufacturers

- 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.

SECTION 1.1: MICROCONTROLLERS AND EMBEDDED


PROCESSORS

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.

Microcontroller versus general-purpose microprocessor


What is the difference between a microprocessor and rnicrocontrollcr? By
microprocessor is meant the gencral-purpose microprocessors such as Intel's x86
family (8086,80286,80386,80486, and the Pcntium) or Motorola's 680xO fami-
ly (68000,68010,68020,68030,68040, etc.). These microprocessors contain no
RAM, no ROM, and no I/O ports on the chip itself. For this reason, they are com-
monly referred to as general-purpose microprocessors.

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

, eSSor ystem Contrasted With M'


Icrocontroller System
A system dcsigner using a general .
Pcmium or the 68040 must add RAM RO~urpose mlcroproccssor such as the
makc them functional. Although th addi ' I/O ports, and timers externally to
ports makes these systems bUlkier an~ ~u !tlon of external RAM, ROM, and I/O
tage of versatility SUchthat the desi ch more expenSive, they have the adva
and I/O esigncr can decide th n-
ports needed to fit the t k h
trollers. A microcontroller has :scpa~
amOunt of RAM ROM I/O
td
. on e arnounr of RAM ROM
. This IS not the ease with mi~rocon~
a microprocessor) in add't'
the processor, RAM ROM it~rts, and a timer all on a single chip ; Ion ~o a fixed
chip; thercfore the de .' ports, and timer are all embedd d' n ot cr words,
Thc fixed amo~nt of o~~ner cannot add any cxtcrnal memo e togcthcr on one
trollers makcs them idea~~IP ROM, RAM, and number of 116' 110, or umer to it.
or many applteations in whi h ports in mlCrocon_
24 IC Cost and space are. Cntl-' .
Home cal. In many applications, for example a TV remote control, there
Appliances is no need for the computing power of a 486 or even an 8086
Intercom microprocessor. In many applications, the space it takes, the
Telephones power it consumes, and the price per unit are much more critical
Security systems considerations than the computing power. These applications
Garage door openers most often require some I/O operations to read signals and tum on
Answering machines .'and off certain bits. For this reason some call these processors
Fax machines
lBP, "itty-bitty processors" (see "Good Things in Small Packages
Home computers
TVs Are Generating Big Product Opportunities" by Rick Grehan,
Cable TV tuner BYTE magazine, September 1994; www.byte.com, for an excel-
VCR lent discussion of microcontrollers).
Camcorder It is interesting to note that some microcontroller manu-
Remote controls facturers have gone as far as integrating an ADC (analog-to-digi-
Video games tal converter) and other peripherals into the microcontrolJer.
Cellular phones
Musical instruments Microcontrollers for embedded systems
Sewing machines
In the literature discussing microprocessors, we often see
Lighting control
Paging the term embedded system. Microprocessors and microcontrollers
Camera are widely used in embedded system products. An embedded
Pinball machines product uses a microprocessor (or microcontroller) to do one task
Toys and one task only. A printer is an example of embedded system
Exercise equipment since the processor inside it performs only one task; namely, get-
Office ting the data and printing it. Contrast this with a Pentium-based
Telephones PC (or any x86 IBM-compatible PC). A PC can be used for any
Computers number of applications such as word processor, print server, bank
Security systems teller terminal, video game player, network server, or internet ter-
Fax machine minal. Software for a variety of applications can be loaded and
Microwave run. Of course the reason a PC can perform myriad tasks is that it
Copier
has RAM memory and an operating system that loads the appli-
Laser printer
Color printer cation software into RAM and lets the CPU run it. In an embed-
Paging ded system, there is only one application software that. is typical-
Auto ly burned into ROM. An x86 PC contains or is connected to var-
Trip computer IOUS embedded products such as the keyboard, printer, modem,

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

CHAPTER 1: THE 8051 MICROCONTROLLERS 25


National Semiconductor, Inc.) have targeted their microprocessor for the high end
of the embedded market. While Intel and AMD push their x86 processors for both
the embedded and desktop PC markets, Freescale is determined to keep the 68000
family alive by targeting it mainly for the high end of embedded systems now that
Apple no longer uses the 680xO in their Macintosh. In the early 1990s Apple com-
puter began using Power PC microprocessors (604,603,620, erc.) in place of the
680xO for the Macintosh. The Power PC microprocessor is ajoint venture between
IBM and Freeseale, and is targeted for the high end of the embedded market as
well as the PC market. It must be noted that when a company targets a general-
purpose microprocessor for the embedded market it optimizes the processor used
for embedded systems. For this reason these processors arc often called high-end
embedded processors. Very often the terms embedded processor and microcon-
'roller arc used interchangeably.
One of the most critical needs of an embedded system is to decrease power
consumption and space. This can be achieved by integrating more functions into
the CPU chip. All the embedded processors based on the x86 and 680xO have low
po~er consumptron tn addition to some forms of I/O, COM port, and ROM all
a single chip. In "high-perform anee em b e dd ed processors, the trend IS. to .Integrateon
more and more functions on the CPU chip and let the designer decide which fea-
Norn he/she wants to use. This trend is invading PC system design as well
ormally, tn designing the PC motherboard we need a CPU plus a cbi .
tainmg I/O, a cache controller, a flash ROM containing BIOS and f
ondary cache memory. New designs are erne . . . '
I~;
et eon-
tna y a se -
g
has announced that it is working on a chi t;gtn tn tndustry. For example, yrix
DRAM. In other words, we arc about to :~e an contams the enure PC, except for
Currently, because of MS-DOS ; e;.lre computer on a chip.
embedded systems arc using x86 PC I an indows standardization many
end embedded applications not onl s. n many cases using x86 PC for the high-
t' . Y saves money but I h
nne since there is a vast II'b f f a so s ortens development
W' rary 0 so tware al d .
indows platforms. The fact th t W' d . rea y written for the DOS and
I r. a tn ows IS a id I
p auorm means that developing a Windows_ WI e Y used and well understood
co t and shortens the development time bdased embedded product reduce the
consi erably.

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.

2. The second criterion in choosing a microcontroller is how easy it is to devel-


op products around it. Key considerations include the availability of an assem-
bler, debugger, a code-efficient C language compiler, emulator, technical sup-
port, and both in-house and outside expertise. In many cases, third-party ven-
dor (that is, a supplier other than the chip manufacturer) support for the chip is
as good as, if not better than, support from the chip manufacturer.

3. The third criterion in choosing a microcontroller is its ready availability in


needed quantities both now and in the future. F6r some designers this is even
more important than the first two criteria. Currently, of the leading 8-bit
microcontrollers, the 8051 family has the largest number of diversified (multi-
ple source) suppliers. By supplier is meant a producer besides the originator
of the microcontroller. In the case of the 8051, which was originated by Intel,
several companies also currently produce (or have produced in the past) thc
8051. These companies include: Intel, Atmel, Philips/Signetics, AMD,
Infineon (formerly Siemens), Matra, and Dallas Semiconductor. See Table 1-2.
Table 1-2: Some of the Companies Producing a It should be noted that
Member of the 8051 Family . Freescale, Zilog, and Microchip
Technology have all dedicated
Company Web Site massive resources to ensure wide
Intel www.intel.com/design/mcs5l and timely availability of their
Atmcl www.atmel.com product since their product is sta-
Philips/Signetics www.semiconductors.philips.com ble, mature, and single sourced.
Infineon www.infineon.com In recent years they also have
Dallas SemilMaxim www.maxim-ic.com begun to sell the ASIC library
cell of the microcontroller.

CHAPTER 1: THE 8051 MICROCONTROLLERS 27


Review Questions . than microproees-
True or false. Mieroeontrollers are normally less expensive
I.
sors. . .
b d based on a microcontru IIer an d a general-pur-
2 When companng a system oar . ?
. . which one IS cheaper. h' ?
pose mIcroprocessor, . h f the following devices on-e ip:
3. A mieroeontroller normally has whic 0 ) I/O (d) all of the above
(a) RAM (b) ROM (C needs whieh of the following
4. A general-purpose mlerop;oeessor norma y
II

devices lO be attached to It. ) I/O (d) all of thc above


(a) RAM (b) ROM (c ?
An embedded system is also called a dedIcated system. Why.
5. ?
What docs the term embedded system mean.
~: Why docs having multiple sources of a given product matter?

SECTION 1.2: OVERVIEW OF THE 8051 FAMILY

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.

A brief history of the 8051


In 1981, Intel Corporation introduced an 8-bit microcontroller called the
8051. This microcontroller had 128 bytes of RAM, 4K bytes of on-chip ROM, two
timers, one serial port, and four ports (each 8-bits wide) all on a single chip. At the
timc it was also referred to as a "system on a chip." The 8051 is an 8-bit proecs-
or, meaning that the CPU can work on only 8 bits of data at a time. Data larger
than 8 bits has to be broken into 8-bit pieces to be processed by the CPU. The
8051 has a total of four I/O ports, each 8 bits wide. See Figure 1-2. Although the
8051 can have a maximum of 64K bytes of on-chip ROM, many manufacturers
have put only 4K bytes on the chip. This will be discussed in more detail later.
The 8051 became widely popular after Intel allowed other manufacturers
to make and market any flavors of the 8051 they please with the condition th t
they remain code-compatible with the 8051. This has Icd to many versions of the
8051 With different speeds and amounts of on-chip ROM marketed by more than
haifa dozen manufacturers. Next we review some of them It' .
. . IS Imponant to note
that although there arc different flavors of the 8051 in
terms of speed and amount of on-chip ROM the II Table \-3: Features of the 8051
ibl . , yare a
compau
. e with the original 8051 as far as the' IIlstruc-
Feature Quantity
lion arc concerned. This means that if y .
ou wntc your ROM 4K bytes
program for one, it will run on any of them re ardlc
the manufacturer. g ss of RAM 128 byte
Timer 2
8051 microcontroller I/O pins 32
Serial port I
The 8051 i the original memb f
ily. Intel refers to it as MCS-51 T bl ~r 0 the 8051 fam. Interrupt sources 6
features of the 8051. . a e -3 shows the main Note' . ROM amounlmdlcates
'. on-chip
pl"Ogram space.
28
EXTERNAL
INTERRUPTS

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

Figure 1-2. Inside the 8051 Microcontroller Block Diagram

Other members of the 8051 family


There are two other members in the 8051 family of microcontrollers. They
arc the 8052 and the 8031.

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.

CHAPTER 1: THE 8051 MICROCONTROLLERS 29


8031 microcontroller . .. ft n
b fthe 805J family is the 803J chip. This chip I 0 e
Another mem er 0 . e it has OK bytes of on-chip ROM. To u c thl
referred to as a ROM-less 8051 sme . Thi temal ROM must contain thc pro-
I. t add external ROM to n. ISex . h
c lip you mus . . h d cute Contrast that to the 8051 in which t e
~~~~hitha~~~ 8cO:nlta~~1 t~e:cpr;;ra:~o be' fetched and exceuted but is limitcd to
only 4~ bytes of code. The ROM containing the program attached to the 8031 can
be as large as 64K bytes. In the process of adding external ROM to the 8031, you
lose two ports. That leaves only 2 ports (of the 4 ports) for I/O opcrauons, To solve
this problem, you can add external I/O to the 8031. Intcrfacmg the 803 I with
memory and I/O ports such as the 8255 chip is discussed m Chapter 14. There arc
also various speed versions of the 8031 available from dlffcrcnt comparncs.
Various 8051 microcontrollers
Although the 8051 is the most popular mcmbcr of the 8051 family, you will
not see "805 I" in the part numbcr. This is bccause the 8051 is available in differ-
ent memory types, such as UV-EPROM, flash, and NV-RAM, all of which have
different part numbers. A discussion ofthc various types of ROM will be given in
Chapter 14.The UV-EPROM version of the 8051 is the 8751. The flash ROM ver-
sion is marketed by many companies including Atmel Corp. and Dallas
Semiconductor. The Atrnc] Flash 8051 is called AT89C51, while Dallas
Semiconductor calls theirs DS89C4xO (DS89C420/430/440). The NV-RAM ver-
sion of the 8051 made by Dallas Semiconductor is called DS5000. There i al 0
an OTP (one-time programmable) version of the 8051 made by various manufac_
turers. they
where Nextarcweused.
discuss briefly each of the above chips and describe applications

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 from Dallas Semiconductor (MaXim)


Many popular 8051 chips have on chi .
ry. The AT89C5, fromAtmel C . - tp ROM III the form of flash memo-
This is idcal for fast develo orp. ISone example of an 805 I with flash ROM
pment Since flash m .
compared to the twenty minut emory can be erased in seconds
AT 9 . es or more needed f h 8
8 C511susedinplaeeOfthe8751 j" or r e 751. For this reason the
thc chip and thcreby spced up thc d t~ e IInlnate the waiting time needed to crase
op a rnieroeontrOl!er_basedsyst
hem eve °pment time. Using the AT89C5 I to devel_
reqUires a ROM b
mcmory; owevcr, a ROM crase'" umcr that sUpports flash
30 r IS not ncedcd. Notice that in flash mCm
ory you
must erase the entire contents of ROM in order to program it again. This erasing
of flash is done by the PROM burner itself, which is why a separate eraser is not
needed. To eliminate the need for a PROM burner, Dallas Semiconductor, now
part of the Maxim Corp., has a version of the S051/52 called DSS9C4xO
(DSS9C420/430/ ...) that can be programmed via the serial COM port of an IBM
PC.
Notice that the on-chip ROM for the DSS9C4xO is in the form of flash.
The DSS9C4xO (420/430/440/450) comes with an on-chip loader, which allows
the program to be loaded into the on-chip flash ROM while it is in the system.
This can be done via the serial COM port of an IBM PC, This in-system program
loading of the DSS9C4xO via a PC serial COM port makes it an ideal home devel-
opment system. Dallas Semiconductor also has an NV-RAM version of the S051
called DS5000.I
The advantage ofNV-RAM is. the ability to change the ROM con-
tents one byte at a time. The DS5000 also comes with a loader, allowing it to be
programmed via the PC's COM port. See Table 1-5. From Table J -5, notice that
the DSS9C4xO is a really an S052 chip since it has 256 bytes of RAM and 3 timers.
More details of this chip are given throughout the book.

Table 1-5: Versions of 8051152Microcontroller From Dallas Semiconductor (Maxim)


Part Number ROM RAM I/O pins Timers Interrupts Vee
DSS9C420/30 16K (Flash) 256 32 3 6 5V
DSS9C440 32K (Flash) 256 32 3 6 5V
DSS9C450 64K (Flash) 256 32 3 6 5V
DS5000 SK (NVRAM) . 12S 32 2 6 5V
DSSOC320 0 K 256 32 3 6 5V
DSS7520 16K (UVROM) 256 32 3 6 5V
Source: www.maxim-ic.comlproducts/microcontrollers/S051_ drop_in.cfm

DS89C4xO Trainer

In Chapter S, we discuss the design of DSS9C4xO Trainer extensively. The


MDES051 Trainer is available from www.MicroDigitaIEd.com.This Trainer
allows you to program the DSS9C4xO chip from the COM port of the xS6 IBM
PC, with no need for a ROM burner.

For a DS89C4xO·based trainer see www.MicroDigitaIEd.com.

AT89C51 from Atmel Corporation


The Atmel Corp. has a wide selection of 8051 chips, as shown in Tables 1_
6 and 1-7. For example, the ATS9C51 is a popular and inexpensive chip used in
many small projects. It has 4K bytes of flash ROM. Notice the AT89C51-12PC,
where "C" before the 51 stands for CMOS, which has a low power consumption,
"12" indicates 12 MHz, "P" is for plastic DIP package, "C" is for commercial.

CHAPTER 1: THE 8051 MICROCONTROLLERS 31


· 6 V . of 8051 From Almel (All ROM Flash)
Table ,-; erslOns -
Vce Packaging
- ROM RAM I/O pins Timer Interrupt
Part Number 5V 40
'A'T89C51 4K 128 32 2 6 40
3V
AT89LV51 4K 128 32 2 6 20
3V
AT89CIOSI IK 64 15 I 3 20
3V
AT89C20S1 2K 128 IS 2 6 40
5V
AT89C52 8K 128 32 3 8 40
3V
AT89LV52 8K 128 32 3 8
Note: "C" in thc part number indicates CMOS.

Table 1-7: Various Speeds of 8051 From Atmel

Part Number Speed Pins Packaging Use


AT89C51-12PC 12 MHz 40 DIP plastic commercial
AT89CSI-16PC 16 MHz 40 DIP plastic commercial
AT89C51-20PC 20 MHz 40 DIP plastic commercial

OTP version of the 8051


There are also OTP (one-time-programmable) versions of the 805 t avail-
able from different sources. Flash and NV-RAM versions are typically used for
product development. When a product is designed and absolutely finalized, thc
OTP version of the 8051 is used for mass production since it is much cheaper in
terms of price per unit.
8051 family from Philips
Another major producer of thc 8051 family is Philips Corporation. Indeed,
they havc onc of thc largest selections of 80S I mierocontrollcrs. Many of their
products include features such as A-to-D converters, D-to-A converters, extended
I/O, and both OTP and flash. For the list of companies producing the 8051 c. .,
Iy see thc We b sues
si . h
m t c box below. rarrn

See the following Web sites for 8051 d


f pro ucts and their
eatures from various companies:

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

This chapter discussed the role and importance of microcontrollers in


everyday life. Microprocessors and microcontrollers were contrasted and com-
pared. We discussed the use of microcontrolJers in the embedded market. We also
discussed criteria to consider in choosing a microcontroller such as speed, memo-
ry, I/O, packaging, and cost per unit. The second section of this chapter described
various family members of the 805t, such as the 8052 and 8031, and their features.
In addition, we discussed various versions of the 8051 such as the AT89C51 and
DS89C4xO, which are marketed by suppliers other than 1ntel.

PROBLEMS

SECTION 1.1: MICROCONTROLLERS AND EMBEDDED PROCESSORS

I. True or False, A general-purpose microprocessor has on-chip ROM.


2. True or False. A microcontroller has on-chip ROM.
3. True or False. A microcontrotler has on-chip I/O ports.
4. True or False. A microcontroller has a fixed amount of RAM on the chip.
5. What components are normally put together with the microcontroller into a
single chip?
6. 1ntel's Pentium chips used in Windows PCs need external and
chips to store data and code.
7. List three embedded products attached to a PC.
8. Why would someone want to use an x86 as an embedded processor?
9. Give the name and the manufacturer of some of the most widely used 8-bit
microcontrollers.
10. In Question 9, which microcontrolJer has the most manufacture sources?

CHAPTER 1: THE 8051 MICROCONTROLLERS 33


II. In a battery-based embedded product, what is the most important fact r in
choosing a microcontroller? . . ROM why docs the size of the ROM
12. In an embedded controller with on-chip ,

13. matter? . ortant is it to have multiple ouree


In choosing a microcontroller, how Imp for
I· ')
that Clip. " ?

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.

SECTION 1.2: OVERVIEW OF THE 8051 FAMILY

16. The 8751 has __ bytes of on-chip ROM.


17. The AT89C51 has __ bytes of on-chip RAM.
18. The 8051 has _ on-chip timer(s).
19. The 8052 has __ bytes of on-chip RAM.
20. The ROM-less version of the 8051 uses __ as the pan number.
21. The 8051 family has __ pins for I/O.
22. The 8051 family has circuitry to support __ serial ports.
23. The 8751 on-chip ROM is of type _.
24. The AT89C51 on-chip ROM is of type __ .
25. The DS5000 on-chip ROM is of type __ .
26. The DS89C420/30 on-chip ROM is of type _.
27. Give the amount of ROM and RAM for the following chips.
(a) AT89C51 (b) DS89C420/30 (e) DS89C440
28. Of the 8051 family, which memory type is the most cost effective if you are
using a million of them in an embedded product?
29. What is the difference between the 8031 and 805 I?
30. Of the 8051 mieroeontrollers, which one is the best for a home development
environment? (You do not have access to a ROM burner.)

ANSWERS TO REVIEW QUESTIONS

SE nON 1.1: MICROCONTROLLERS AND EMBEDDED PROCESSORS


I. True
2. A l1licrocontrollcr.bascdsystem
J. (d)
4. (d)
5. II is dediealed since it is dediealed I d .
6.
7
Embedded syslem mcam that th °1lcallon
, omg one Iype of job.
'I' I. e app and proc .
• 1- avtng mu tlplc SOurcesfor a . essor are combined into a sin' I
. I . given part means g e system
Important y. eompelition a"'ong Suppliers brin 'S y:u are nOI hOstage 10 one supplier. Mor~
g a Out lower Cost for that Product.

You might also like