Tải bản đầy đủ (.pdf) (687 trang)

MICROPROCESSORS FROM ASSEMBLY LANGUAGE TO C USING THE PIC18FXX2 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (9.59 MB, 687 trang )


MICROPROCESSORS


LIMITED WARRANTY AND DISCLAIMER OF LIABILITY
THE CD-ROM THAT ACCOMPANIES THE BOOK MAY BE USED ON A SINGLE PC
ONLY. THE LICENSE DOES NOT PERMIT THE USE ON A NETWORK (OF ANY
KIND). YOU FURTHER AGREE THAT THIS LICENSE GRANTS PERMISSION TO USE
THE PRODUCTS CONTAINED HEREIN, BUT DOES NOT GIVE YOU RIGHT OF
OWNERSHIP TO ANY OF THE CONTENT OR PRODUCT CONTAINED ON THIS
CD-ROM. USE OF THIRD-PARTY SOFTWARE CONTAINED ON THIS CD-ROM IS
LIMITED TO AND SUBJECT TO LICENSING TERMS FOR THE RESPECTIVE PRODUCTS.
CHARLES RIVER MEDIA, INC. (“CRM”) AND/OR ANYONE WHO HAS BEEN INVOLVED IN THE WRITING, CREATION, OR PRODUCTION OF THE ACCOMPANYING CODE ("THE SOFTWARE") OR THE THIRD-PARTY PRODUCTS CONTAINED
ON THE CD-ROM OR TEXTUAL MATERIAL IN THE BOOK, CANNOT AND DO NOT
WARRANT THE PERFORMANCE OR RESULTS THAT MAY BE OBTAINED BY USING
THE SOFTWARE OR CONTENTS OF THE BOOK. THE AUTHOR AND PUBLISHER
HAVE USED THEIR BEST EFFORTS TO ENSURE THE ACCURACY AND FUNCTIONALITY OF THE TEXTUAL MATERIAL AND PROGRAMS CONTAINED HEREIN. WE
HOWEVER, MAKE NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, REGARDING THE PERFORMANCE OF THESE PROGRAMS OR CONTENTS. THE SOFTWARE IS SOLD “AS IS” WITHOUT WARRANTY (EXCEPT FOR DEFECTIVE
MATERIALS USED IN MANUFACTURING THE DISK OR DUE TO FAULTY WORKMANSHIP).
THE AUTHOR, THE PUBLISHER, DEVELOPERS OF THIRD-PARTY SOFTWARE,
AND ANYONE INVOLVED IN THE PRODUCTION AND MANUFACTURING OF
THIS WORK SHALL NOT BE LIABLE FOR DAMAGES OF ANY KIND ARISING OUT
OF THE USE OF (OR THE INABILITY TO USE) THE PROGRAMS, SOURCE CODE, OR
TEXTUAL MATERIAL CONTAINED IN THIS PUBLICATION. THIS INCLUDES, BUT
IS NOT LIMITED TO, LOSS OF REVENUE OR PROFIT, OR OTHER INCIDENTAL OR
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THE PRODUCT.
THE SOLE REMEDY IN THE EVENT OF A CLAIM OF ANY KIND IS EXPRESSLY LIMITED TO REPLACEMENT OF THE BOOK AND/OR CD-ROM, AND ONLY AT THE
DISCRETION OF CRM.
THE USE OF “IMPLIED WARRANTY” AND CERTAIN “EXCLUSIONS” VARIES FROM
STATE TO STATE, AND MAY NOT APPLY TO THE PURCHASER OF THIS PRODUCT.



MICROPROCESSORS
FROM ASSEMBLY LANGUAGE TO C
®

USING THE PIC18FXX2

ROBERT B. REESE
MISSISSIPPI STATE UNIVERSTIY

DA VINCI ENGINEERING PRESS
Hingham, Massachusetts


Copyright 2005 by DA VINCI ENGINEERING PRESS, an imprint of CHARLES RIVER MEDIA, INC.
All rights reserved.
Selected figures reprinted with permission of the copyright owner, Microchip Technology,
Incorporated. All rights reserved. No further reprints or reproductions may be made without
Microchip Technology Inc.’s prior written consent.
No part of this publication may be reproduced in any way, stored in a retrieval system of any
type, or transmitted by any means or media, electronic or mechanical, including, but not limited
to, photocopy, recording, or scanning, without prior permission in writing from the publisher.
Editor: David Pallai
Cover Design: Tyler Creative
DA VINCI ENGINEERING PRESS
CHARLES RIVER MEDIA, INC.
10 Downer Avenue
Hingham, Massachusetts 02043
781-740-0400
781-740-8816 (FAX)


www.charlesriver.com
This book is printed on acid-free paper.
Robert B. Reese. Microprocessors: From Assembly Language to C Using the PIC18Fxx2.
ISBN: 1-58450-378-5
eISBN: 1-58450-645-8
All brand names and product names mentioned in this book are trademarks or service marks of
their respective companies. Any omission or misuse (of any kind) of service marks or trademarks
should not be regarded as intent to infringe on the property of others. The publisher recognizes
and respects all marks used by companies, manufacturers, and developers as a means to distinguish their products.
Library of Congress Cataloging-in-Publication Data
Reese, Robert Bryan, 1958Microprocessors : from assembly language to C using the PIC18Fxx2 / Robert B. Reese.
p. cm.
Includes bibliographical references and index.
ISBN 1-58450-378-5 (hardcover with cd-rom : alk. paper)
1. Microprocessors. 2. Microprocessors. I. Title.
TK7895.M5R44 2005
2005008835
Printed in the United States of America
05 7 6 5 4 3 2 First Edition
CHARLES RIVER MEDIA titles are available for site license or bulk purchase by institutions, user
groups, corporations, etc. For additional information, please contact the Special Sales Department at 781-740-0400.
Requests for replacement of a defective CD-ROM must be accompanied by the original disc, your
mailing address, telephone number, date of purchase, and purchase price. Please state the nature of the
problem, and send the information to CHARLES RIVER MEDIA, INC., 10 Downer Avenue,
Hingham, Massachusetts 02043. CRM’s sole obligation to the purchaser is to replace the disc, based on
defective materials or faulty workmanship, but not on the operation or functionality of the product.


To Donna, who puts up with me



This page intentionally left blank


Contents
Acknowledgments
Preface
1

Number System and Digital Logic Review
1.1 Learning Objectives
1.2 Binary Data
1.3 Unsigned Number Conversion
1.4 Binary and Hex Arithmetic
1.5 Combinational Logic Functions
1.6 Combinational Building Blocks
1.7 Sequential Logic
1.8 Sequential Building Blocks
1.9 Encoding Character Data
Summary
Review Exercises

2

The Stored Program Machine
2.1 Learning Objectives
2.2 Problem Solving the Digital Way
2.3 Finite State Machine Design
2.4 A Stored Program Machine

2.5 Modern Computers
Summary
Review Problems

3

Introduction to the PIC18Fxx2
3.1
3.2
3.3
3.4

Learning Objectives
Introduction to Microprocessors and Microcontrollers
The PIC18Fxx2 Microcontroller
Data Memory Organization and Data Transfer

xv
xvii
1
1
2
5
6
10
17
21
25
27
29

29

31
31
32
34
39
47
47
47

51
51
52
53
55

vii


viii

Contents

3.5 Basic Arithmetic and Control Instructions
3.6 A PIC18 Assembly Language Program
3.7 The Clock and Instruction Execution
Summary
Review Problems


4

Unsigned 8-Bit Arithmetic, Logical, Conditional Operations

61
64
73
73
74

77

4.1 Learning Objectives
4.2 Bitwise Logical Operations, Bit Operations
4.3 The STATUS Register
4.4 Unsigned Conditional Tests
4.5 Looping
4.6 Shifts and Rotates
Summary
Review Problems

5

77
78
83
85
94
97
100

100

Extended Precision and Signed Operations

103

5.1 Learning Objectives
5.2 Extended Precision Integers
5.3 Extended Precision Operations
5.4 Signed Number Representation
5.5 Two’s Complement Overflow
5.6 Operations on Signed Data
5.7 Branch Instruction Encoding
Summary
Review Problems

6

Subroutines and Pointers
6.1
6.2
6.3
6.4
6.5
6.6
6.7

Learning Objectives
Subroutines
The Stack and Call/Return

Implementing Subroutines in Assembly Language
Arrays and Pointers in C
Arrays and Pointers in Assembly Language
Accessing Table Data from Program Memory

103
104
105
114
119
120
127
129
129

133
133
134
136
141
146
152
160


Contents

6.8 Subroutines and Stack Frames: Dynamic Allocation
Summary
Review Problems


7

Advanced Assembly Language: Higher Math
7.1 Learning Objectives
7.2 Multiplication
7.3 Division
7.4 Fixed-Point and Saturating Arithmetic
7.5 Floating-Point Number Representation
7.6 BCD Arithmetic
7.7 ASCII Data Conversion
Summary
Review Problems

8

The PIC18Fxx2: System Startup and Parallel Port IO
8.1
8.2
8.3
8.4
8.5
8.6
8.7
8.8

Learning Objectives
High-Level Languages versus Assembly Language
C Compilation for the PIC18F242
PIC18F242 Startup Schematic

ledflash.c–The First C Program for PIC18F242 Startup
Datasheet Reading–A Critical Skill
PIC18Fxx2 Reset Sources
Experimenting with RESET, SLEEP, and the Watchdog Timer

8.9 Parallel Port Operation
8.10 LED/Switch IO and State Machine Programming
8.11 Interfacing to an LCD Module
Summary
Review Problems

9

Asynchronous Serial IO
9.1
9.2
9.3
9.4

Learning Objectives
IO Channel Basics
Synchronous Serial IO
Asynchronous Serial IO

ix
162
169
170

175

175
176
183
188
192
197
199
204
204

207
207
208
210
216
220
223
225
228
231
237
242
249
249

253
253
254
257
259



x

Contents

9.5 The PIC18Fxx2 USART
9.6 The RS232 Standard
9.7 Serial IO Examples
Summary
Review Problems

10

Interrupts and a First Look at Timers
10.1 Learning Objectives
10.2 Interrupt Basics
10.3 PIC18 Interrupt Details
10.4 Interrupt-Driven Asynchronous Serial Data Input
10.5 Using a Software FIFO with Interrupt-Driven IO
10.6 Other Interrupt Sources, Sleep Mode
10.7 State Machine Programming for Interrupt-Driven IO
10.8 The Timer Subsystem: Timer2
10.9 Switch Debouncing Using a Timer
10.10 A Rotary Encoder Interface
10.11 A Numeric Keypad Interface
10.12 On Writing and Debugging ISRs
Summary
Review Problems


11

Synchronous Serial IO
11.1 Learning Objectives
11.2 The PIC18 and Synchronous Serial IO
11.3 USART Synchronous Mode
11.4 The Serial Peripheral Interface (SPI)
11.5 SPI Examples: A Digital Potentiometer and a Serial EEPROM
11.6 The I2C Bus
11.7 The I2C on the PIC18Fxx2
11.8 The 24LC515 Serial EEPROM
11.9 Double Buffering for Interrupt-Driven Writes
Summary
Review Problems

263
270
273
277
278

281
281
282
284
287
291
296
299
304

307
309
315
319
321
322

327
327
328
329
331
334
345
348
356
364
366
367


Contents

12

Data Conversion
12.1 Learning Objectives
12.2 Data Conversion Basics
12.3 Analog-to-Digital Conversion
12.4 PIC18Fxx2 Analog-to-Digital Converter

12.5 Digital-to-Analog Conversion
12.6 Digital-to-Analog Converter Example: The MAXIM 518
Summary
Review Problems

13

Timers
13.1 Learning Objectives
13.2 The Timer0 Subsystem
13.3 The Timer1 and Timer3 Subsystems
13.4 Pulse Width Measurement Using Capture Mode
13.5 Timer1/Timer3 Compare Mode
13.6 Using Capture Mode for Infrared Decoding
13.7 Timer2 and Pulse Width Modulation
13.8 Using Capture Mode for Frequency Measurement
Summary
Review Problems

14

Capstone: Audio Sampling, Monitoring System,
and Autonomous Robot
14.1 Learning Objectives
14.2 Design of an Audio Record/Playback System
14.3 Implementation of an Audio Record/Playback System
14.4 Design of a Home Monitoring System
14.5 The DS1621 Digital Thermometer
14.6 Using the Nonvolatile Storage on the PIC18Fxx2
14.7 Implementation of a Home Monitoring System

14.8 Design and Implementation of an Autonomous Robot
Summary
Suggested Project Modifications

xi
371
371
372
373
382
391
400
406
407

411
411
412
419
422
428
433
442
447
451
452

455
456
456

459
466
469
475
483
494
504
505


xii
15

Contents

Beyond the PIC18Fxx2

507

15.1 Learning Objectives
15.2 External Memory Interfacing
15.3 Other PIC Family Members
15.4 Bus Arbitration in I2C
15.5 The Controller Area Network (CAN)
15.6 The Universal Serial Bus (USB)
15.7 A Brief Survey of Non-PIC Microcontrollers
15.8 Real-Time Operating Systems
Summary
Suggested Survey Topics


507
508
513
516
528
523
527
531
533
534

Appendix A PIC18Fxx2 Architecture, Instruction Set, Register Summary

537

Appendix B Microchip MPLAB Quickstart

549

Appendix C HI-TECH PICC-18 C Compiler Demo for the PIC18F242

553

Appendix D Notes on the C Language

557

D.1
D.2
D.3

D.4

Formatted IO (PRINTF, SCANF, SPRINTF,
For C++ Programmers
For New Programmers
For Experienced C Programmers

SSCANF)

Appendix E Suggested Laboratory Exercises
E.1
E.2
E.3
E.4
E.5
E.6
E.7
E.8

Lab Setup
Experiment 1: A Stored Program Machine (Chapters 1, 2)
Experiment 2: PIC18xx2 Introduction (Chapter 3)
Experiment 3: Unsigned 8-Bit Operations (Chapter 4)
Experiment 4: Extended Precision and Signed Operations (Chapter 5)
Experiment 5: Pointers and Subroutines (Chapter 6)
Experiment 6: Hardware Startup (Chapter 8)
Experiment 7: LED/Switch IO and Introduction to Asynchronous
Serial IO (Chapters 8, 9)
E.9 Experiment 8: Interrupts (Chapter 10)


557
559
560
560

563
563
566
567
569
572
573
575
578
580


Contents

E.10 Experiment 9: More Interrupts, the I2C Bus, and a Serial EEPROM
(Chapter 11)
E.11 Experiment 10: Introduction to Data Conversion (Chapter 12)
E.12 Experiment 11: Timer Introduction and Waveform Generation
(Chapters 10, 13)
E.13 Experiment 12: Time Measurement and IR Waveform Decoding
(Chapter 13)
E.14 Experiment 13: Audio Record/Playback (Chapter 14)
E.15 Hardware Debugging Checklist
E.16 Instrumentation and Prototyping Hints


Appendix F The Jolt/Colt Serial Bootloaders
F.1
F.2
F.3
F.4

Programming the Jolt/Colt Firmware
Jolt Installation
Running Jolt
Colt Bootloader Installation and Execution

Appendix G Circuits 001
G.1 Voltage, Current, Resistance
G.2 Capacitors

xiii

583
585
587
593
594
595
598

601
601
605
608
610


613
613
618

Appendix H References

621

Appendix I Answers to Review Problems

625

I.1 Chapter 1
I.2 Chapter 2
I.3 Chapter 3
I.4 Chapter 4
I.5 Chapter 5
I.6 Chapter 6
I.7 Chapter 7
I.8 Chapter 8
I.9 Chapter 9
I.10 Chapter 10
I.11 Chapter 11
I.12 Chapter 12

625
627
629
630

631
633
638
639
643
644
646
647


xiv

Contents

I.13 Chapter 13

Appendix J About the CD-ROM
J.1 General System Requirements

Index

649

655
655

657


Acknowledgments


would like to thank the following individuals for their assistance in preparing
this book:

I
ON THE CD

ON THE CD

J.W. Bruce, for authoring Chapter 12 and for providing valuable feedback and
suggestions on the rest of the book.
Jane Moorhead, for being a partner in teaching this material to Mississippi
State University (MSU) electrical engineering, computer engineering, computer science, and software engineering students.
Martin Dubuc, for providing the publicly available Jolt/Colt serial bootloaders
for the PIC18. We make extensive use of these bootloaders in our microprocessors lab at MSU, and these bootloaders are included on this book’s
CD-ROM.
HI-TECH Software for providing the 120-day demo version of the PICC-18™
C compiler that is included on this book’s CD-ROM. I also want to thank Matt
Luckman at HI-TECH Software for his efforts in putting that distribution together.
Donna Reese, for her careful proofreading of the book material.

xv


This page intentionally left blank


Preface

his book is intended as an introduction to microprocessors and microcontrollers for either the student or hobbyist. The book structure is:


T

Chapter 1: Review of digital logic concepts.
Chapter 2: Computer architecture fundamentals.
Chapters 3 through 6: Coverage of assembly language programming in a C language context using the PIC18Fxx2 family.
Chapter 7: Advanced assembly language programming structured around
computer arithmetic topics.
Chapters 8 through 13: Fundamental microcontroller interfacing topics such
as parallel IO, asynchronous serial IO, synchronous serial IO (I2C and SPI),
interrupt-driven IO, timers, analog-to-digital conversion, and digital-to-analog
conversion.
Chapter 14: Presents three capstone projects involving topics from Chapters 8
through 13.
Chapter 15: Topics beyond the PIC18Fxx2 family, such as a survey of other microprocessor families, the CAN bus, and memory technologies.

USING THIS BOOK IN AN ACADEMIC ENVIRONMENT
At Mississippi State University, majors in Electrical Engineering (EE), Computer
Engineering (CPE), Computer Science (CS), and Software Engineering (SE) take
our first course in microprocessors. Previous to spring 2002, this course emphasized
xvii


xviii

Preface

X86 assembly language programming with the lab experience being 100-percent
assembly language based and containing no hardware component. We found that
students entering our senior design course who had the expectation of something

“real” being built were unprepared for doing prototyping activities or for incorporating a microcontroller component into their designs. We did offer a course in microcontrollers, but it was an elective senior-level course and many students had not
taken that course previous to senior design. In spring 2002, the Computer Engineering Steering committee reexamined our goals for the first course in microprocessors and the approach for this book was developed. This book is intended for
use as a first course in microprocessors using the PIC18Fxx2 microcontroller with
prerequisites of basic digital design and exposure to either C or C++ programming. The book begins with simple microprocessor architecture concepts, moves to
assembly language programming in a C language context, and then covers fundamental hardware interfacing topics such as parallel IO, asynchronous serial IO,
synchronous serial IO (I2C and SPI), interrupt-driven IO, timers, analog-to-digital
conversion, and digital-to-analog conversion. Programming topics are discussed
using both assembly language and C, while hardware interfacing examples uses C
to keep code complexity low and improve clarity. The book’s CD-ROM includes a
120-day demo version of the PICC-18 C compiler for the PIC18F242 from HITECH software. In addition to better preparing students for senior design, another
goal of this book is to enable students to take courses in advanced embedded systems or computer architecture. As such, a broad coverage of software and hardware
topics is included. The assembly language programming chapters emphasize the
linkage between C language constructs and their assembly language equivalent so
that students clearly understand the impact of C coding choices in terms of execution time and memory requirements. It is not a goal of this textbook to create students who are experts only in assembly language programming, with no
understanding of high-level language programming techniques and limited hardware exposure. Most embedded software is written in C for portability and complexity reasons, which argues favorably for reduced emphasis on assembly language
and increased emphasis on C. Embedded system hardware complexity is steadily
increasing, which means a first course in microprocessors that reduces assembly
language coverage (but does not eliminate it) in favor of hands-on experience with
fundamental interfacing allows students to begin at a higher level in an advanced
course in embedded systems. Hardware interface topics included in this book cover
the fundamentals (parallel IO, serial IO, interrupts, timers, A/D, D/A) using devices
that do not require extensive circuits knowledge because of the lack of a circuits
course prerequisite. The microcontroller interfacing topics presented in this text-


Preface

xix

book are sufficient for providing a skill set that is extremely useful to a student in a

senior design capstone course or in an advanced embedded system course.
Thus, the principle motivation for this book is that microcontroller knowledge
has become essential for successful completion of senior capstone design courses.
These capstone courses are receiving increased emphasis under ABET 2000 guidelines. This places increased pressure on Computer Engineering and Electrical Engineering programs to include significant exposure to embedded systems topics as
early in the curriculum as possible. A second motivation for this book is that the recently released ACM/IEEE Computer Engineering model curriculum recommends
17 hours of embedded system topics as part of the Computer Engineering curriculum core, which is easily satisfied by a course containing the topics in this book. A
third motivating factor is the increased pressure on colleges and universities to
reduce hours in engineering curriculums; this book shows how a single course
can replace separate courses in assembly language programming and basic microprocessor interfacing.
The course sequence used at Mississippi State University that this book fits
into is:
Basic digital design (Boolean algebra, combinational and sequential logic),
which is required by EE, CPE, CS, and SE majors.
Introduction to microprocessors (this book), which is required for EE, CPE,
CS, and SE majors.
Computer architecture as represented by the topic coverage of the Hennessy
and Patterson textbook “Computer Organization & Design: The Hardware/
Software Interface.” This includes reinforcement of the assembly language programming taught in the microprocessor course via a general-purpose instruction set architecture (e.g., the MIPS) along with coverage of traditional
high-performance computer architecture topics (pipelined CPU design, cache
strategies, parallel bus I/O). Required for CPE, CS, and SE majors.
Advanced embedded systems covering topics such as (a) real-time operating
systems, (b) internet appliances, (c) advanced interfaces such as USB, CAN,
Ethernet, FireWire, and (d) programming in alternate embedded languages
such as Java. Required for CPE majors.
Chapter 1 provides a broad review of digital logic fundamentals. Chapters 2
through 6 and 8 through 13 cover the core topics of assembly language programming and microcontroller interfacing. Chapter 14 contains three capstone projects
that integrate the material of the previous chapters. Chapters 7 and 15 have


xx


Preface

optional topics on advanced assembly language programming and microprocessor
interfacing, which can be used to supplement the core material. Appendix E, “Suggested Laboratory Exercises,” contains a sequence of 13 laboratory experiments
that comprise an off-the-shelf lab experience: one experiment on fundamental
computer architecture topics, four experiments on PIC18 assembly language, and
eight hardware experiments. The hardware labs cover all major subsystems on the
PIC18: A/D, timers, asynchronous serial interface, and the I2C interface. The hardware experiments are based on a protoboard/parts kit approach where the students
incrementally build a PIC18F242 system that includes a serial EEPROM, an external 8-bit D/A, and a RS-232 port. A protoboard/part kits approach is used instead
of a pre-assembled printed circuit board (PCB) for several important reasons:
When handed a pre-assembled PCB, a student tends to view it as a monolithic
element. A protoboard/parts kit approach forces a student to view each part individually and read datasheets to understand how parts connect to each other.
Hardware debugging and prototyping skills are developed during the painful
process of bringing the system to life. These hard-won lessons prove useful
later when the student must do the same thing in a senior design context. This
also provides students with the confidence that having done it one time they
can do it again, this time outside of a fixed laboratory environment with guided
instruction.
A protoboard/parts kit approach gives the ultimate flexibility to modify experiments from semester to semester by simply changing a part or two; also when
the inevitable part failures occur they are easily replaced.
In using this approach at Mississippi State University, I have seen a “Culture of
Competence” develop in regard to microcontrollers and prototyping in general. Almost all senior design projects now routinely include a microcontroller component
(not necessarily PIC-based). Students concentrate their efforts on design definition,
development, and refinement instead of spending most of their time climbing the
learning curve on prototyping and microcontroller usage.
This book’s C examples on hardware interfacing strive for educational value
first and optimization second. Subsystem configuration code uses named bit fields
and individual bit field assignments in C examples instead of whole-register assignments to emphasize bit field roles within those registers. Register values for
controlling baud rate, I2C bus speed, and periodic interrupt rates are hard-coded

for the clock frequency of the reference PIC18F242 system instead of hiding the calculations within C macros or functions that compute a register value for a desired


Preface

xxi

rate. This is done intentionally so students can be assigned different values within
the lab and homework exercises, forcing them to use the PIC18F242 datasheet formulas for computing new register values.

FOR THE HOBBYIST
This book assumes very little background, and thus is appropriate for readers with
widely varying experience levels. It is suggested that you begin by examining the experiments in Appendix E and find the ones that interest you. Then, read the chapter that is referenced by the experiment. The suggested revisions for the capstone
chapter projects (Chapter 14) are a good test of your knowledge once you have exhausted the experiments.
A Final Word
Writing this book has been a rewarding experience, and many people have helped
(see the Acknowledgments section). It has been akin to building a stone wall; each
day a little more is added, each section covering more distance, with the satisfaction
of seeing it grow as time and effort is expended. However, this book’s lifespan will
be a fraction of that of a sturdily built stone wall. But that is the fun of engineering—it is constantly changing, so you are constantly learning. I hope that you have
fun while learning about microprocessors and the PIC18Fxx2!
Bob Reese
Starkville, Mississippi


This page intentionally left blank


1


Number System and Digital
Logic Review

In This Chapter
Binary Data
Unsigned Number Conversion
Binary and Hex Arithmetic
Combinational Logic Functions
Combinational Building Blocks
Sequential Logic
Sequential Building Blocks
Encoding Character Data

his chapter reviews number systems, Boolean algebra, logic gates, combinational logic gates, combinational building blocks, sequential storage elements, and sequential building blocks.

T

1.1 LEARNING OBJECTIVES
After reading this chapter, you will be able to:
Create a binary encoding for object classification.
Convert unsigned decimal numbers to binary and hex representations, and
vice versa.

1


2

Microprocessors


Identify NOT, OR, AND, NOR, NAND, and XOR logic functions and their
symbols.
Evaluate simple Boolean functions.
Describe the operation of CMOS P and N transistors.
Identify the CMOS transistor level implementations of simple logic gates.
Compute clock period, frequency, and duty cycle given appropriate parameters.
Identify common combinational building blocks.
Identify common sequential building blocks.
Translate a character string into ASCII encoded data, and vice versa.
Binary number system representation and arithmetic is fundamental to all
computer system operation. Basic logic gates, CMOS transistor operation, and
combinational/sequential building block knowledge will help your comprehension
of the diagrams found in datasheets that describe microprocessor subsystem functionality. A solid grounding in these subjects ensures better understanding of the
microprocessor topics that follow in later chapters.

1.2 BINARY DATA
Binary logic or digital logic is the basis for all computer systems built today. Binary
means two, and many concepts can be represented by two values: true/false,
hot/cold, on/off, 1/0, to name a few. A single binary datum whose values are “1”
and “0” is referred to as a bit. Groups of bits are used to represent concepts that
have more than two values. For example, to represent the concepts hot/warm/
cool/cold, two or more bits can be used as shown in Table 1.1
TABLE 1.1 Digital Encoding Examples
Value

Encoding A

Encoding B

Encoding C


Cold

00

00

0001

Cool

01

10

0010

Warm

10

11

0100

Hot

11

01


1000

To encode n objects, the minimum number of bits required is k = log2 n ,
where is the ceiling function that takes the nearest integer
log2 n. For the four


×