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

(Computer communications and networks) sid katzen BSc, MSc, DPhil, MIEE, MIEEE, CEng (auth ) the quintessential PIC® microcontroller springer verlag london (2005)

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 (6.25 MB, 574 trang )

Computer Communications and Networks


The Computer Communications and Networks series is a range of textbooks,
monographs and handbooks. It sets out to provide students, researchers and
non-specialists alike with a sure grounding in current knowledge, together with
comprehensible access to the latest developments in computer communications and networking.
Emphasis is placed on clear and explanatory styles that support a tutorial approach, so that even the most complex of topics is presented in a lucid and
intelligible manner.
Also in this series:
An Information Security Handbook
John M.D. Hunter
1-85233-180-1
Multimedia Internet Broadcasting: Quality, Technology and Interface
Andy Sloane and Dave Lawrence (Eds.)
1-85233-283-2
Information Assurance: Surviving in the Information Environment
Andrew Blyth and Gerald L. Kovacich
1-85233-326-X
UMTS: Origins, Architecture and the Standard
Pierre Lescuyer (Translation Editor: Frank Bott)
1-85233-676-5
Designing Software for the Mobile Context: A Practitioner’s Guide
Roman Longoria
1-85233-785-0
OSS for Telecom Networks: An Introduction to Network Management
Kundan Misra
1-85233-808-3
From P2P to Web Services and Grids: Peers in a Client/Server World
Ian Taylor
1-85233-869-5




Sid Katzen

The Quintessential PIC
Microcontroller
Second Edition

®


Sid Katzen, BSc, MSc, DPhil, MIEE, MIEEE, CEng
School of Electrical and Mechanical Engineering, University of Ulster,
Northern Ireland
Series Editor
Professor A.J. Sammes, BSc, MPhil, PhD, FBCS, CEng
CISM Group, Cranfield University, RMCS, Shrivenham, Swindon SN6 8LA, UK

British Library Cataloguing in Publication Data
A catalogue record for this book is available from the British Library
Library of Congress Cataloging-in-Publication Data
Katzen, Sid.
The quintessential PIC® microcontroller/Sid Katzen.—2nd ed.
p. cm. — (Computer communications and networks)
Includes bibliographical references and index.
ISBN 1-85233-942-X (alk. paper)
1. Programmable controllers. I. Title. II. Computer communications and networks.
TJ223.P76K38 2005
629.8′9—dc22
2005042504

Apart from any fair dealing for the purposes of research or private study, or criticism or review,
as permitted under the Copyright, Designs and Patents Act 1988, this publication may only be
reproduced, stored or transmitted, in any form or by any means, with the prior permission in
writing of the publishers, or in the case of reprographic reproduction in accordance with the terms
of licences issued by the Copyright Licensing Agency. Enquiries concerning reproduction outside
those terms should be sent to the publishers.
The following are registered trademarks of Microchip Technology Incorporated in the United
States of America and other countries: dsPIC, MPLAB, PIC, and PICSTART.
The following are trademarks of Microchip Technology Incorporated in the United States of
America and other countries: ICSP, In-Circuit Serial Programming, and MPASM.
Computer Communications and Networks ISSN 1617-7975
ISBN-10: 1-85233-942-X
ISBN-13: 978-1-85233-942-5
First Edition ISBN: 1-85233-309-X
Springer Science+Business Media
springeronline.com
© Springer-Verlag London Limited 2005
First Edition published 2001
The use of registered names, trademarks, etc., in this publication does not imply, even in the
absence of a specific statement, that such names are exempt from the relevant laws and regulations
and therefore free for general use.
The publisher makes no representation, express or implied, with regard to the accuracy of the
information contained in this book and cannot accept any legal responsibility or liability for any
errors or omissions that may be made.
Typesetting: Output-ready electronic files provided by the author.
Printed and bound in the United States of America
(MVY)
9 8 7 6 5 4 3 2 1 Printed on acid-free paper



In memory of Eva Jones


Contents

Preface to the Second Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

IX

Preface to the First Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

XI

Part I

The Fundamentals

1.

Digital Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

2.

Logic Circuitry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

17

3.


Stored Program Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

43

Part II

The Software

4.

The PIC16F84 Microcontroller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

71

5.

The Instruction Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

95

6.

Subroutines and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

7.

Interrupt Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

8.


Assembly Language Code Building Tools . . . . . . . . . . . . . . . . . . . . . 213

9.

High-Level Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247

Part III

The Outside World

10. The Real World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
11. One Byte at a Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
12. One Bit at a Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331


VIII

Contents

13. Time Is of the Essence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
14. Take the Rough with the Smooth . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
15. To Have and to Hold . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
16. Enhancing the Family . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
17. A Case Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
Appendices
A.

Acronyms and Abbreviations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549


B.

Special-Purpose Register Structure for the PIC16F87XA . . . . . . . 555

C.

C Instruction Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559


Preface to the Second Edition

A second edition of this book has given me the opportunity to respond
to suggestions from both students and correspondents from around the
world, from disparate regions ranging from Scotland to Hawaii. Since the
time of the first edition written in the late 1990s, the Microchip PIC range
has become the largest volume selling 8-bit MCU. The mid-range family used in the original edition has continued to expand vigorously, with
some of the exemplars used becoming essentially obsolete. In addition,
the enhanced-range 16-bit instruction line has been enlarged from virtually nothing to form a significant proportion of the family. At the same
time, new introductions to the original low- (or base-) end architecture
continue apace. Because of the close relationship between the low-, mid-,
high- and enhanced-range lines, the focus of the new edition has stayed
with the mid-range line up.
Virtually all diagrams have been modified, many extensively, and numerous additional new figures have been added. Throughout the text,
special attention has been paid to clarify the basic concepts. In Part I,
Chapter 3 has been extensively rewritten with this in mind and to better integrate with Chapters 4 and 5 in Part II, both of which bear only
a superficial relation to the original text. Chapter 7, covering interrupt
handling, has also been largely rewritten to elucidate a difficult topic.
Part III not only has been revised to use current exemplars, but has been

extended to cover additional peripherals such as the Analog Comparator
and Voltage Reference modules. A new chapter covers the enhancedrange PIC18FXXX range.
With the exception of the first two and last chapters, all chapters have
both fully worked examples and self-assessment questions. As an extension to this, an associated Web site at
/>has the following facilities:






Solutions to self-assessment questions.
Further self-assessment questions.
Additional material.
Source code for all examples and questions in the text.
Pointers to development software and data sheets for devices used in
the book.


X

Preface to the Second Edition

• Errata.
• Feedback from readers.
The manuscript was typeset by the author on a variety of Microsoft®
Windows™ PCs using a Y&Y implementation of LATEX 2ε and the Lucida
Bright font family. Line drawings were created or modified with Autocad
R13 and incorporated as encapsulated PostScript files. Photographs were
taken by the author using several Olympus digital cameras—which are

absolutely full of microcontrollers!
Hopefully, any gremlins have been exorcised, but if you find any or
have any other suggestions, I will be happy to acknowledge such communications via the Web site.
Sid Katzen
University of Ulster at Jordanstown
July 2005


Preface to the First Edition

Microprocessors and their microcontroller derivatives are a widespread,
if rather invisible, part of the infrastructure of our twenty-first-century
electronic and communications society. In 1998, it was estimated1 that
hidden in every home there were about 100 microcontrollers and microprocessors: in the singing birthday card, washing machine, microwave
oven, television controller, telephone, personal computer and so on.
About 20 more lurked in the average family car, for example, monitoring in-tire radio pressure sensors and displaying critical data through a
control area network (CAN).
Around 4 billion such devices are sold each year to implement the
intelligence of these “smart” electronic devices, ranging from smart eggtimers through to aircraft management systems. The evolution of the
microprocessor from the first Intel device introduced in 1971 has revolutionised the structure of society, effectively creating the second Industrial Revolution at the beginning of the twenty-first century. Although the
microprocessor is better known for its role in powering the ubiquitous PC,
where raw computing power is the goal, sales of microprocessors such
as the Intel Pentium represent only around 2% of the total volume. The
vast majority of sales are of low-cost microcontrollers embedded into a
dedicated-function digital electronic device, such as the smart card. Here
the emphasis is on the integration of the core processor with memory
and input/output resources in the one chip. This integrated computing
system is known as a microcontroller.
In seeking to write a book in this area, the overall objective was to
get the reader up-to-speed in designing small embedded microcontrollerbased systems, rather than using microcontrollers as a vehicle to illustrate computer architecture in the traditional sense. This will hopefully give the reader confidence that, even at such an introductory level,

he/she can design, construct, and program a complete working embedded system.
Given the practical nature of this material, real-world hardware and
software products are used throughout to illustrate the material. The
microcontroller market is dominated by devices that operate on 8-bit
data (although 4- and 16-bit examples are available) like early microprocessors and unlike the 64-bit Intel Pentium and Motorola Power PC
1 New

Scientist, vol. 59, no. 2141, 4 July 1998, p. 139.


XII

Preface to the First Edition

“heavy brigade”. In contrast, the essence of the microcontroller lies in its
high system-integration/low-cost profile. Power can be increased by distributing processors throughout the system. Thus, for example, a robot
arm may have a microcontroller for each joint implementing simple local
processes and communicating with a more powerful processor making
overall executive decisions.
In choosing a target architecture, acceptance in the industrial market, easy availability, and low-cost development software have made the
Microchip family one of the most popular choices as the pedagogic vehicle in learning microprocessor/microcontroller technology at all levels
of electronic engineering from grade school to university. In particular,
the reduced instruction set, together with the relatively simple innovative
architecture, reduces the learning curve. In addition to their industrial
and educational roles, the PIC® MCU families are also the mainstay of
hobbyist projects, as a leaf through any electronics magazine will show.
Microchip, Inc., is a relatively recent entrant to the microcontroller
market with its family of Harvard architecture PIC devices introduced
in 1989. By 1999, Microchip was the second largest producer of 8-bit
units—behind only Motorola.

This book is split into three parts. Part I covers sufficient digital, logic
and computer architecture to act as a foundation for the microcontroller
engineering topics presented in the rest of the text. Inclusion of this
material makes the text suitable for stand-alone usage, as it does not
require a prerequisite digital systems module.
Part II looks mainly at the software aspects of the mid-range PIC
microcontroller family, its instruction set, how to program it at assembly
and high-level C coding levels, and how the microcontroller handles subroutines and interrupts. Although the 14-bit PIC family is the exemplar,
both architecture and software are comparable to both the 12- and 16-bit
ranges.
Part III moves on to the hardware aspects of interfacing and interrupt
handling, with the integration of the hardware and software being a constant theme throughout. Parallel and serial input/output, timing, analog,
and EEPROM data-handling techniques are covered. A practical build and
program case study integrates the previous material into a working system, as well as illustrating simple testing strategies.
Sid Katzen
University of Ulster at Jordanstown
December 2000


PART I

The Fundamentals

This book is about microcontrollers (MCUs). These are digital engines
modeled after the architecture of a stored-program computer and integrated onto a single very largescale integrated circuit together with
support circuitry, memories and peripheral interface devices. Although
the microcontroller is often confused with its better-known cousin, the
microprocessor, in its role as the driving force of the ubiquitous personal computer, the vast majority of both microprocessors and microcontrollers are embedded into a variety of other digital components. The
first microprocessors in the early 1970s were marketed as an alternative
way of implementing digital circuitry. Here the task would be determined

by a series of instructions encoded as binary code groups in read-only
memory. This is more flexible than the alternative approach of wiring
hardware integrated circuits in the appropriate manner. The microcontroller is simply the embodiment of this original role of the integrated
computer.
We will look at embedded microcontrollers in a general digital processing context in Parts II and III. Here our objective is to lay the foundation for this material. We will be covering:





Digital code patterns.
Binary arithmetic.
Digital circuitry.
Computer architecture and programming.

This will by no means be a comprehensive review of the subject, but
there are many other excellent texts in this area1 which will launch you
into greater depths.

1 Such as S.J. Cahill’s Digital and Microprocessor Engineering, 2nd ed., Prentice Hall,
Englewood Cliffs, NJ, 1993.


2

The Quintessential® PIC Microcontroller

Peeking into the silicon.



CHAPTER 1

Digital Representation

To a computer or microcontroller, the world is seen in terms of patterns
of digits. The decimal (or denary) system represents quantities in terms
of the ten digits 0,· · ·,9. Together with the judicious use of the symbols
+, − and . any quantity in the range ±∞ can be depicted. Indeed nonnumeric concepts can be encoded using numeric digits. For example
the American Standard Code for Information Interchange (ASCII) defines
the alphabetic (alpha) characters A as 65, B = 66,· · ·,Z = 90 and a = 97, b
= 98,· · ·,z = 122, etc. Thus the string “Microcontroller” could be encoded
as “77, 105, 99, 114, 111, 99, 111, 110, 116, 114, 111, 108, 108, 101, 114”.
Provided you know the context—that is, what is a pure quantity and what
is text—just about any symbol can be coded as numeric digits.1
Electronic circuits are not very good at storing and processing a multitude of different values. It is true that the first American digital computer,
the Electronic Numerical Integrator And Calculator (ENIAC) in 1946 did
its arithmetic in decimal form,2 but all computers since then have handled data in binary (base 2) form. The decimal (base 10) system is really
only convenient for humans, in that we have ten fingers.3 Thus, in this
chapter we will solely look at the properties of binary digits, their groupings and processing. After reading it you will:
• Understand why a binary data representation is the preferred base for
digital circuitry.
• Know how a quantity can be depicted in natural binary, hexadecimal
and binary coded decimal.
• Be able to apply the rules of addition and subtraction for natural binary
quantities.
• Know how to multiply by shifting left.
• Know how to divide by shifting right and propagating the sign bit.
• Understand the Boolean operations of NOT, AND, OR and XOR.
The information technology revolution is based on the manipulation,
computation and transmission of digitized information. This informa1 Of course, there are lots of digital encoding standards; for instance, the 6-dot Braille

code for the visually impaired.
2 As did Babbage’s mechanical computer of a century earlier.
3 And ten toes, but base-20 systems are rare though not unknown.


4

The Quintessential PIC® Microcontroller

tion is virtually universally represented as aggregrates of binary digits
(bits).4 Most of this processing is effected using microprocessors5 and
microcontrollers, and it is sobering to reflect that there is more computing power in a singing birthday card than existed on the entire planet in
1950!
Binary is the universal choice for data representation, as an electronic
switch is just about the easiest device that can be implemented using a
transistor. Such 2-state switches are very small; they change state very
quickly and consume little power. Furthermore, as there are only two
states to distinguish between, a binary depiction is likely to be resistant
to the effects of noise. The upshot of this is that both the packing density on a silicon chip and switching rate can be very high. Although a
switch on its own does not represent much computing power, 5 million
switches changing at 100 million times a second manage to present at
least a façade of intelligence!
The two states of a bit are conventionally designated logic 0 and
logic 1, or just 0 and 1. A bit may be represented by two states of any
number of physical quantities; for instance, electric current or voltage,
light, or pneumatic pressure. Most microcontrollers use 0 V (or ground)
for state 0 and 3 – 5 V for state 1, but this is not universal. For instance,
the RS232 serial port on your computer uses nominally +12 V for state 0
and −12 V for state 1.
A single bit on its own can only represent two states. By dealing with

groups of bits, rather more complex entities can be coded. For example,
the standard alphanumeric characters can be coded using 7-bit groups
of digits, as listed in Table 1.1. Thus the ASCII code for “Microcontroller”
becomes:
1001101 1101001 1100011 1110010 1101111 1100011 1101111 1101110
1110100 1110010 1101111 1101100 1101100 1100101 1110010

Unicode is an extension of ASCII and with its 16-bit code groups is able to
represent characters from many languages and mathematical symbols.
The ASCII code is unweighted, as the individual bits do not signify a
particular quantity; only the overall pattern has any significance. Other
examples are the die code on gaming dice and the 7-segment code of
Fig. 6.8 on page 161. Here we will deal with natural b 114, 211, 227, 236
––
extern, 226, 236
––
global, 226, 236
––
high, 177, 503
––
if, 356
––
include, 212, 222, 237
––
local, 221, 238
––
low, 177, 503
––
macro, 221, 236
––

org, 176, 190, 214, 236
––
pagesel, 241
––
radix, 489
––
res, 226, 236
––
udata_ovr, 225, 227, 236
––
udata, 225, 226, 236

– Label, 83, 106, 132, 216
––
$, 498
––
Arithmetic, 236
– Macro, 220, 356, 373
––
Addf, 237
––
Bcc, 238
––
Bne, 221
––
Countdown, 239
––
Delay_1ms, 221
––
Delay_600, 356

––
Delay_cycles, 238
––
Exgwf, 237
––
Movlf, 238
– Number base
––
Binary, 235
––
Decimal, 235
––
Hexadecimal, 235
– Relocating, 222–231
– 2’s complement, 118
Assembly-level language, 55, 211–242
Bank switching, see Data store, Bank
switching
Baud rate, 372
Borrow out, 78, 118
Brownout, see Reset, Brownout
Buried register, 84
Bus, 35
C language, see High-level language, C
Carry out, 78
CCP module, 408, 412–423, 515
– Capture modes, 413–416, 427
– Compare modes, 413, 416–417, 425
– Enhanced, 423
– PWM mode, 420–423

Central Processing Unit (CPU), 44, 45
Clock, 76, 274–276
– ADC module, 451
– External, 270
– Start-up, 281
Code
– ASCII, 3, 5, 235, 372
– Binary Coded Decimal (BCD), 79, 204
––
Bi-quinary, 135
––
Packed, 92, 202, 521
– Binary coded decimal (BCD), 6
– Decimal, 3


560

Index

– Hexadecimal, 6
– 7-segment, 160, 176
– Unary, 314, 538
Code Condition register (CCR), 35
Code protection, see Program store, Code
protection
Comparator module, 437–443, 472
Comparator Voltage Reference module,
441–443, 462, 472
Compiler, 244

Configuration word, 277, 491
Counter, 38
D flip flop, see Flip flop, D
D latch, see Latch, D
Data bus, 20, 35
Data EEPROM memory, 480–485, 536
– Read from, 482
– Write to, 188, 484
Data store, 47, 51, 72, 79–90, 509
– Access bank, 510
– Bank switching, 53, 100–103, 190, 197,
239, 291, 293, 490, 509–510
––
Reset, 281
Debounce, see Switch, Debounce
Device programmer, 277, 542
Digital-to-analog conversion (DAC), 336,
419, 431–466
– PWM, 419–423, 461
Duplex transmission, 335
EEPROM memory, 72, 88–90, 387–392,
479–504
Erasable PROM (EPROM)
– 27C64, 27
Error detection
– Bi-quinary, 136
– Checksum, 143, 220
– Parity, 15, 144, 372, 378
Exclusive-OR, see Operation, eXclusiveOR
Executable code, see Machine code

Execute unit, 77–90, 508
Extension, signed, 11
Far goto/call, 98, 149, 522
Fast stack, 516
Fetch and execute, 48, 55–57
Fetch unit, 73–77, 507
File
– Absolute object, 217–220, 231
––
Intel format, 219
– Error, 217, 220
– Header, 213, 400, 460
– Include, 213, 279, 293
– Linker script, 224, 236
– Listing, 217
– Machine-code, see File, Absolute
object

– Macro, 222
– Object code, 55, 210
– Source code, 55, 210, 222
File register
– PCL, 177
– ADCON0, 194, 451, 456
– ADCON1, 295, 451, 453, 456
– ADRES, 456
– ADRESH, 454
– ADRESL, 454
– ANSEL, 451
– BSR, 509–510

– CCPR1H, 413, 422
– CCPR1L, 413, 422
– CMCON, 252, 438–443, 473
– CVRCON, 441, 443
– EEADR, 480
– EEADRH, 488
– EEADR, 88, 89, 482, 488
– EECON1, 88, 89, 188, 192, 194, 488
– EECON2, 88, 90, 482, 488
– EEDATA, 88, 89, 480, 488
– EEDATH, 488
– FSR, 84, 105, 164, 204, 357, 494
– FSR0, 512
– FSR1, 511
– FSR2, 511
– General-purpose (GPR), 100
– INDF, 83, 105, 164
– INDF2, 511
– INTCON, 85, 90, 185–205, 516
– LATX , 514
– OPTION_REG, 84, 85, 186, 303, 399
– OSCAL, 276
– PCL, 86, 507
– PCLATH, 86, 98, 149, 177, 241, 250,
507
––
Reset, 281
– PCLATU, 508
– PCON, 281
– PIE1, 195, 377

– PIE2, 196, 413, 488
– PIR1, 195, 377, 408
– PIR2, 196, 413, 488
– PORTB, 179, 186
– PORTX , 87, 88
– POSTDEC2, 511
– POSTINC2, 511
– PR2, 417, 421
– PREINC2, 511
– PRODH, 508, 513
– PRODL, 508, 513
– RCREG, 377
– RCSTA, 376
– SPBRG, 378
– SSPADD, 361, 363
– SSPBUF, 340, 345
– SSPCON, 341, 362
– SSPCON2, 362


Index














SSPSTAT, 340, 341, 362
STKPTR, 508
T0CON, 515
T1CON, 408
T2CON, 418
TBLAT, 507
TMR0, 84, 85
TMR1H, 408
TMR1L, 408
TMR2, 408, 417–423
TRISX , 87, 88, 107, 197, 291, 341,
440
– TXREG, 377
– TXSTA, 376
– WREG, 509, 521
– General-purpose (GPR), 81–83, 510
– Special-purpose (SPR), 81, 83–90, 103,
510
– Status, see Status register
File store, see Data store
Filter
– 3-point, 143, 477
Flag, 35, 78, 132
– C, 35, 51, 52, 78, 91, 115, 116, 118,
126, 237, 285, 520, 522
– DC, 79, 93, 285

– N, 509, 520
– OV, 509, 520
– Z, 35, 51, 52, 79, 91, 94, 107, 110,
112, 118, 120, 191, 237, 285, 520
– Interrupt, see Interrupt, Flag
Flip flop
– D, 32, 199
– T, 38
Flow chart, 65
Fuse, 277–280, 492
– BODEN, 278, 284
– CP, 277, 492
– CPD, 278, 492
– DEBUG, 278
– FOSC, 277
– LVP, 278
– MCLRE, 283
– PWRTE, 277, 281, 284
– WDTE, 277, 399
– WRT, 278, 492
– High-Voltage Programming, 276
– Low-Voltage Programming, 276, 533
– Programming, 542
– Setting in C, 279
Handshake, 292–295, 351
Harvard architecture, 45–57, 63, 72, 507
Hexadecimal code, see Code,
Hexadecimal
High-level language, 243–264
– C, 48, 147, 164, 245–264

––
!, 295
––
#bit, 256, 291, 476

––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––


561

#byte, 256, 291
#define, 255
#device ADC, 460
#include, 250
#int, 430, 469
#int_ext, 260
#port_b_pullups(), 309
#use delay(), 382
#use fast_io(), 308
#use i2c(), 370
#use rs232(), 380
#use standard_io(), 309
&, 255, 335
++, 253, 261
--, 249
>>, 259
bclr(), 335
bset(), 335
const, 262
delay_cycles(), 260, 295
delay_ms(), 260
delay_us(), 260
disable_interrupts(), 477
enable_interrupts(), 260, 419,
477

––
––

––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––

––
––

for, 259
get_timer1(), 429
get_timer2(), 419
getch(), 382
i2c_read(), 370
i2c_start(), 370
i2c_stop(), 370
i2c_write(), 370
if else, 260
int, 252
long int, 248, 252, 469
main(), 250
printf(), 382
read_adc(), 460, 476
read_eeprom(), 495
return, 248, 250, 253
set-up_spi(), 348
set_adc_channel(), 460
set_timer1(), 429
set_timer2(), 419
set_tris(), 291
setup_adc(), 460, 476
setup_adc_ports(), 295, 460
setup_ccp1(), 429
setup_counters(), 401
setup_timer_1(), 429
setup_timer_2(), 419

sleep(), 476
spi_data_is_in(), 348
spi_read(), 348
spi_write(), 348
unsigned int, 248
void, 261
while, 249, 252, 255, 295


562

Index

––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––
––


write_eeprom(), 495
|, 255, 335
Absolute address, 255
Addition, 252
Arrays, 261
Automatic data, 227, 469
Bit twiddling, 256, 477
Cast, 259
Comments, 255
Function, 248
Fuses, 279, 399
Global data, 261, 469
Hexadecimal, 224, 255
Interrupt service routine (ISR), 260
Interrupts in, 419, 469
Pointer, 255
Static data, 227, 469

Input port, see Parallel I/O
Instruction, 43
– btg, 82
– addlw, 51, 52, 54, 97, 111, 118, 152
– addwfc, 520
– addwf, 53, 78, 97, 99, 112
– andlw, 123
– andwf, 122
– bc , 522
– bcf, 79, 82, 108, 117, 120, 256, 297
– bnc, 522

– bnn, 523
– bra, 522
– bsf, 79, 82, 108, 117, 120, 256
– btfsc, 114, 123, 132
– btfss, 116, 133, 152, 256
– btg, 521
– call, 97, 148–152, 241, 516, 522
– clrf, 79, 103, 112, 115
– clrw, 112, 517
– clrwdt, 79, 84, 273, 399–401, 402,
404
– comf, 121
– cpfseq, 523
– cpfsgt, 523
– cpfslt, 523
– daw, 521
– dcfsnz, 523
– decf, 116, 520
– decfsz, 133, 154, 239, 521
– goto, 57, 91, 94, 97, 131, 147, 241,
522, 522
– incf, 114, 116, 520
– incfsz, 134
– infsnz, 523
– iorlw, 124
– iorwf, 123
– lbsr, 510, 517
– lfsr, 511, 517
– movf, 52, 53, 91, 94, 110, 191, 510
– movff, 513, 517









movlw, 82, 109, 152
movwf, 52, 110
mullw, 508
mulwf, 508, 513
negf, 520
nop, 130, 134, 153, 273, 293, 355,
373, 488, 517, 522
– rcall, 522
– retfie, 147, 185, 191, 199, 516
– retlw, 150, 159, 177, 276
– return, 149, 152, 516, 522
– rlcf, 522
– rlf, 128, 314
– rlncf, 522
– rrcf, 522
– rrf, 126
– rrncf, 522
– setf, 522
– sleep, 79, 188, 253, 273, 274, 400,
441, 458, 476
– subwf, 117
– subfwb, 520

– sublw, 118
– subwf, 119
– subwfb, 520
– swapf, 111, 191
– tablrd, 507
– tablwt, 507
– tblrd, 161
– tris, 297
– tstfsz, 523
– xorlw, 125, 368
– xorwf, 124
– Execution time, 91, 132, 152–158
– Macro, see Assembler, Macro
– Read–modify–write, 117, 297, 515
– Two-word, 517
Integrated circuit
– DS18S20 digital thermometer, 392
– 24XXX EEPROM, 388–392, 396, 479
– MAX233 dual RS-232 transceiver, 385
– MAX485 RS-485 transceiver, 386
– MAX505 quad DAC, 465
– MAX506 quad DAC, 462, 478
– MAX518 quad DAC, 353–371
– MAX549A dual 8-bit SPI DAC, 336
– 27C64 EPROM, 27
– 6264 RAM, 40
– 74LS00 quad 2-I/P NAND, 18
– 74LS74 dual D flip flop, 32, 38
– 74LS138 Natural decoder, 22
– 74LS139 Natural decoder, 21, 40

– 74LS148 Priority encoder, 22
– 74HCT164 octal SIPO shift register,
328, 527
– 74LS244 octal 3-state buffer, 20
– 74LS283 Adder, 23
– 74LS373 Octal D latch, 34
– 74LS377 Octal D flip flop, 33


Index




74LS382 ALU, 24, 25
74HCT595 latched PISO shift register, 331, 332
– 74LS670 Register file, 40
– 74LS688 Equality detector, 22
Interrupt
– ADC module, 447, 516
– CCP module, 417
– Comparator change, 440, 474
– Context switching, 190, 199, 367
– EEPROM, 89
– Flag, 186, 363
– Hardware, 184–186, 515
– High-Priority, 516
– Latency, 185
– Low-Priority, 516
– Mask, 185, 187, 516

– Multiple-precision data, 193
– Port B Change, 186
– Port GP Change, 310
– Priority, 515
– Serial port, 377
– Timer 0, 402, 404–407
Interrupt handling, 183–208, 515–517
Interrupt service routine (ISR), 184–208,
365, 528
– in C, 260, 430, 469
– Timer 0, 404, 406, 407, 424, 469, 498,
529
Interrupt Vector, see Vector, Interrupt
Inverter, Programmable, see Programmable
inverter
Latch
– D, 31
– R S, 29
– R S, 30
Linker, 222
Loader, 211, 217
Look-up table, 26, 159–161, 178, 318,
484, 488–490, 503
Loop structure, 106, 249, 259
– Endless, 197, 399
Machine code, 54, 210, 217–220, 279
Macro, see Assembler, Macro
Master Synchronous Serial Port (MSSP),
339–349
Memory

– EEPROM, see EEPROM memory
– EPROM, 27
– RAM, 40
– Random Access (RAM), 40, 46
– ROM, 26
Microcontroller unit (MCU), 57
– 6801, 63
– 6805, 63, 242
– 68HC11, 63, 90
– 68HC12, 63






––
––

––
––
––
––
––
––
––






––




563

68HC16, 63
14500, 63
PIC10FXXX, 71, 289
PIC12C508/9, 310
PIC12F629, 269, 284
Clock, 276
EEPROM Data module, 480
PIC12F675, 269, 284, 408
ADC module, 451
Brown-out, 284
Clock, 276
Comparator, 438, 443
EEPROM Data module, 480
MCLR, 283
Timer 1, 411
PIC16C5XX, 401
PIC16C71, 194, 447
PIC16C74, 192, 408, 505
PIC16C74B, 543
PIC16C84, 72, 543
EEPROM Data module, 479
PIC16C924, 289

PIC16CXXX, 71, 401
PIC16F627, 72, 100, 103, 108, 192,
194–196, 224, 239, 250, 258, 269, 526–
543
––
ADC module, 449
––
Brown-out, 284
––
Program store, 74
– PIC16F628, 72, 81, 83, 85, 98, 100,
108, 192, 194–196, 239, 269, 526–543
– PIC16F62X, 269
––
EEPROM Data module, 480–485
– PIC16F648, 269
––
EEPROM Data module, 480
– PIC16F684
––
CCP, 423
––
Timer 1, 408, 411
– PIC16F73, 72, 449
– PIC16F74, 241
– PIC16F83, 72, 497
– PIC16F84, 72–90, 187, 192, 239, 265,
286, 526
––
Configuration word, 277

––
EEPROM Data module, 479
––
Parallel I/O port A, 299
– PIC16F84A, 211, 287, 357
– PIC16F873, 267
– PIC16F874, 72, 85, 88, 184, 268, 526
––
ADC module, 459
– PIC16F876, 241, 267
––
Program store, 74, 486
– PIC16F877, 72, 85, 86, 94, 98, 149,
184, 268, 311
––
Program store, 74, 486
– PIC16F877A, 367
– PIC16F87X, 161, 196, 273, 284, 295,
380
––
ADC module, 450


564

Index

––
Clock, 275
––

Configuration word, 277–279
––
EEPROM Data module, 479
––
Parallel I/O, 290
––
Program store, 485–492
– PIC16F87XA, 364
––
Comparator, 438, 443, 453
––
Configuration word, 279, 492
––
Program store, 486, 492–494
– PIC16LF87X, 269
– PIC17C42, 505
– PIC18CXXX, 71, 221, 240, 402, 505
– PIC18F242, 505
– PIC18F252, 505
– PIC18F442, 507
– PIC18F452, 507
– PIC18F8720, 515
– PIC18FXX2, 505–523
– PIC18XXXX, 86, 237, 410, 413
––
Clock, 274
Microprocessor unit (MPU)
– 4004, 6, 58
– 6502, 59
– 6800, 59

– 68008, 60
– 6802, 59
– 6809, 59
– 8008, 58
– 8080, 58
– 8085, 58
– 8086, 60
– 8088, 60
Modular programming, 146
MPLAB, 231–235, 254, 542
Multiprocessing, 348
Non-volatile memory, see EEPROM
memory
NOT, see Operation, NOT
Object code, see File, Object code
Odd binary numbers, 144
1’s complement, see Operation,
Complement
Open-collector, 19
Operating System (OS), 245
Operation
– Addition, 111
––
2’s complement, 520
– AND, 12, 122, 255, 335
– Arithmetic, 111
– Arithmetic shift left, 11
– Arithmetic shift right, 11
– Bit banging, 117
– Compare, 118–120, 315, 523

– Complement, 121
––
W, 122, 125
– Copy, 109
– Decrement, 115

––
2’s complement, 520
– Division, 11
– eXclusive-NOR, 14, 22
– eXclusive-OR, 14, 124, 186, 237
– Inclusive-OR, 13, 123, 255, 335
– Increment, 115
––
2’s complement, 520
– Logic shift left, 11
– Logic shift right, 11
– Multiple-precision shifting, 129
– Multiplication, 11, 511
––
Shift and add, 161
– NAND, 13, 18, 30
– NOR, 30
– NOT, 12
– OR, see Operation, Inclusive-OR
– Shifting, 126
– Skip, 49, 112
– Subtraction, 111, 117
––
2’s complement, 520

– Test for zero, 52, 110, 120, 523
Operation code (op-code), 25, 34, 52, 517
Option register, see File register,
OPTION_REG
OR, see Operation, Inclusive-OR
Oscillator, see Clock
– Crystal, 274
– Mode, 274–279
– RC, 275, 285
Output port, see Parallel I/O
Output structure
– Open-collector, 19, 302, 303, 352
– Three-state, 19, 296
Parallel I/O, 86–88, 276, 289–327, 513
– Expansion of, 311
– Output driver, 302
– Port A, 87, 88, 120, 131, 452, 527
––
RA4, 299, 302
– Port B, 81, 87, 119, 296, 299
––
Weak pull-up resistors, 304, 527
– Port C, 300
––
SSP, 362
– Port D, 300
– Port E, 290, 300, 452
– Port GP, 300
––
GP2, 299

Peripheral Interface Controller (PIC), 63
Peripheral interface, Parallel I/O
, see Parallel I/O
Pin
– ANn, 267, 445, 453
– C1OUT, 440
– C2OUT, 440
– CCP1, 420
– CCP2, 420
– CK, 376
– CP1, 413
– CP2, 413


Index

– DT, 376
– GP2, 299
– GP3, 302, 303
– INT, 184, 199, 406, 429
– INTn, 515
– MCLR, 270, 276, 280–283, 289, 527
– OSC1, 76, 269, 274, 286, 289, 528
– OSC2, 76, 274, 289, 528
– RA4, 299, 302
– RAn, 87, 131, 200, 267, 286
– RBn, 81, 87, 184, 199, 276
– RCn, 341, 360
– SCK, 341
– SCL, 360

– SDA, 360
– SDI, 341
– SDO, 341
– SS, 342, 345
– T0CKI, 84, 402, 403, 429
– TX, 377
Pipeline, 55, 75, 76, 337, 377, 507
– Flushing, 91, 132, 149
Port, Serial, see Serial I/O
Port X, see Parallel I/O, Port X
Power consumption, 271
Program, 43
– Array average, 141
– Asynchronous serial I/O, 374, 382
– Background, 184, 190, 365
– BCD incrementation, 93, 204, 521
– Bi-quinary error detection, 136
– Binary to BCD conversion, 137, 171,
534
– Bit position, 128
– Clearing an array, 104, 107
– Comparator, 316, 461
– Defibrillator, 475
– Delay, 152, 153, 155–157, 168, 179,
221, 320
––
Clock independent, 319
– Division, 138
– Double-precision decrementation, 135
– Foreground, 184

– Hardware interrupt handling, 189
– I2 C MSSP data acquisition, 368
– Keypad, 308, 320
– MAX549A SPI DAC, 338
– Multiple-precision addition, 115, 520
– Multiplication, 139, 168
– Multiplication of arrays, 513
– Parallel port handshake, 294
– Peak picking, 470
– Pseudo-random number generator, 264
– Reading the ADC module, 457
––
While sleeping, 459
– Real-time clock, 203
– Root mean square, 226, 259
– Serial transmission, 260
– 7-segment decoder, 160, 176, 534

565

– Software stack, 176
– SPI output, 534
– SPI Read, 335
– SPI Write, 330
– Square root, 172, 173, 229, 257
– Squaring, 228, 489
– Stepper motor, 319
– USART, 381
Program Counter (PC), 49, 57, 74, 76,
85–86, 98, 130, 147, 160, 177, 185,

241, 285, 507
Program store, 46, 51, 73, 85, 94, 97,
220, 241, 486, 508
– Code protection, 277, 491, 542
Program/Verify state, 276
Programmable inverter, 14, 24, 186
Programmer, see Device programmer
R S latch, see Latch, R S
Read cycle, 50
Read–modify–write instructions, see
Instruction, Read–modify–write
Register, see also File register, 32–40
– Counting, 38
Register bit
– ADCSn (ADCON0[7:6]), 451, 458, 460
– ADDEN (RCSTA[3]), 378
– ADFM (ADCON1[7]), 454
– ADIE (INTCON[6]), 194, 458
– ADIE (PIE1[6]), 516
– ADIF (ADCON0[1]), 194
– ADIF (PIR1[6]), 447, 458, 476, 516
– ADIP (IPR[6]), 516
– ADON (ADCON0[0]), 451
– BF (SSPSTAT[0]), 340, 345, 346, 363,
366
– BOR (PCON[0]), 284
– BRGH (TXSTA[2]), 379
– BSRn (BSR[3:0]), 510
– C1INV (CMCON[4]), 440
– C1OUT (CMCON[6]), 440

– C2INV (CMCON[5]), 440
– C2OUT (CMCON[7]), 440
– CCP1IE (PIE1[2]), 413, 416
– CCP1IF (PIR1[2]), 413, 416, 423
– CCP2IE (PIE2[0]), 413
– CCP2IF (PIR2[2]), 413
– CHSn (ADCON0[5:3]), 452, 456, 460
– CIS (CMCON[3]), 441, 473
– CKE (SSPSTAT[6]), 343, 365
– CKP (SSPCON[4]), 343, 364, 366
– CMIE (PIE2[6]), 440
– CMIF (PIR2[6]), 440, 443, 473
– CMn (CMCON[2:0]), 438
– CREN (RCSTA[4]), 378
– CVREN (CVRCON[7]), 441
– CVRn (CVRCON[3:0]), 441
– CVROE (CVRCON[6]), 443
– CVRR (CVRCON[5]), 441


566

























































Index

D/A (SSPSTAT[5]), 364
DC1Bn (CCP1CON[5:4]), 422
EEIE (INTCON[6]), 194
EEIE (PIE1[7]), 483
EEIE (PIE2[4]), 488
EEIF (EECON1[4]), 89, 194
EEIF (PIR1[7]), 483
EEIF (PIR2[4]), 488
EEPGD (EECON1[7]), 488
GIE (INTCON[7]), 185, 188, 190, 194,
260, 273, 365, 378, 404, 411, 443,
458, 459, 477, 483, 515

GIEH (INTCON[6]), 516
GO/DONE (ADCON0[2]), 456, 458, 460,
476
GPPU (OPTION_REG[7]), 303
GSEN (SSPCON2[7]), 364, 366
INTE (INTCON[4]), 187, 197, 260, 406
INTEDGE (OPTION_REG[6]), 186
INTF (INTCON[1]), 186, 190, 191, 199,
201, 256
IPEN (RC[7]), 517
IRP (STATUS[7]), 108, 240, 281
OERR (RCSTA[1]), 378, 380
P (SSPSTAT[4]), 363
PCFGn (ADCON1[3:0]), 453, 460
PD (STATUS[3]), 79, 273, 282, 400
PEIE (INTCON[6]), 195, 365, 378, 443,
476, 477
POR (PCON[1]), 281
PSA (OPTION_REG[3]), 84, 399, 402
PSn (OPTION_REG[2:0]), 84, 85, 398,
401
RBIE (INTCON[3]), 310
RBIF (INTCON[0]), 310
RBPU (OPTION_REG[7]), 303, 309
RCIE (PIE1[5]), 195, 378
RCIF (PIR1[5]), 378, 380
RD (EECON1[0]), 89, 482, 488
RP0 (STATUS[5]), 79, 81, 100, 190, 196,
197, 239, 281, 291
RP1 (STATUS[6]), 100, 196, 239, 281

R/W (SSPSTAT[2]), 364, 366
RX9 (RCSTA[6]), 378
S (SSPSTAT[3]), 363
SEN (SSPCON2[0]), 364, 366
SMP (SSPSTAT[7]), 343
SPEN (RCSTA[7]), 376
SSPEN (SSPCON[5]), 342, 362
SSPIE (PIE1[3]), 345, 365
SSPIF (PIR1[3]), 345, 363–369
SSPM (SSPCON[3:0]), 342, 363
SSPOV (SSPCON[6]), 342, 345, 363
SYNC (TXSTA[4]), 376
T0CS (OPTION_REG[5]), 85, 402
T0IE (INTCON[5]), 402, 404
T0IF (INTCON[2]), 85, 402, 404, 425
T0SE (OPTION_REG[4]), 85, 402
T1CKPSn (T1CON[5:4]), 409

T1GINV (T1CON[7]), 411
T1G (T1CON[6]), 408, 411
T1SYNC (T1CON[2]), 409
T2CKPSn (T2CON[1:0]), 418
TMR1CS (T1CON[1]), 408
TMR1GE (T1CON[6]), 411
TMR1IE (PIE1[0]), 409
TMR1IE (PIE1[0]), 412
TMR1IF (PIR1[0]), 409
TMR1IF (PIR1[0]), 408
TMR1ON (T1CON[0]), 408
TMR1OSCEN (T1CON[3]), 408

TMR2IE (PIE1[1]), 418
TMR2IF (PIR1[1]), 418, 423
TMR2ON (T2CON[2]), 418
TOUTPSn (T2CON[5:2]), 418
TO (STATUS[3]), 281, 285
TO (STATUS[4]), 79, 85, 273, 281, 282,
285, 399–401, 403, 498
– TX9 (TXSTA[6]), 377
– TXEN (TXSTA[5]), 376
– TXIE (PIE1[4]), 377
– TXIF (PIR1[4]), 377, 380
– UA (SSPSTAT[1]), 364
– VREN (VRCON[7]), 443
– WCOL (SSPCON[7]), 342, 346, 363, 366
– WR (EECON1[1]), 89
– WREN (EECON1[2]), 89
– WRERR (EECON1[3]), 89, 483
Register transfer language (rtl), 97
Reset, 280–285
– ADC module, 295, 440, 454
– Brown-out, 283–284
––
Fuse, 278
– BSR, 510
– C, 78
– CCP, 413, 417
– Comparator, 438
– External, 280, 283
– GIEGIE, 185
– Interrupt mask bit, 187

– IRPIRP, 108
– Manual, 270, 527
– Parallel port, 197, 286, 292
– PCLPCLATH, 98
– PD, 79
– Power-on, 281–285, 399, 498
––
ADC module, 452
––
Analog modules, 438, 449, 451
– RBPU, 303
– RP0RP0, 81, 100
– SENSEN, 364
– SSPENSSPEN, 342, 362
– Timer 0, 402
– Timer 1, 408
– Timer 2, 418
– TO, 79
– TRISTRIS, 299
– USART module, 376






















Index




Vector, see Vector, Reset
Watchdog timer, 84, 399, 400, 402,
403, 498
Resource budget, 61
RS-232, see Signaling standard
Serial Communication Interface (SCI), 376
Serial I/O, 328–397
– 1-Wire, 392–395, 396
– Asynchronous, 371–386, 429
– I2 C, 349–371
– SPI, 335–349, 533
7-segment display, 158, 321, 328, 527
Shift register, 36
Signaling standard

– RS-232, 382–385
– RS-422, 383
– RS-423, 383
– RS-485, 383, 386
Sign bit, see 2’s complement, Signed
numbers
Simplex transmission, 335
Simulator, 234–235
Sleep state, 188, 195, 273–274, 285, 345,
365, 400
– ADC module, 451, 456–459
– C code for, 476
– Comparator module, 441
– Interrupt, 285
– Port B Change, 310
– Timer 1, 410
Smart card, 328
Software, 43, 46
Source code, see File, Source code
Special purpose register (SPR), nnn, see
File register, nnn
Special Test/Configuration memory, 277,
484, 492
Stack, 77, 148, 530
– Hardware, 148, 185
– Software, 164–168
Status Register (STATUS), 35, 51, 78, 81,
100, 114, 117, 281, 509
Stepper motor, 317
Subroutine, 147–180

– Nested, 148, 185, 530
– Recursive, 150
Switch
– Debounce, 31, 179, 530

567

– Interface, 303
Synchronous Serial Port (SSP), 339
10’s complement, 9
Text editor, 215
3-state buffer, 20, 296
Timer (TMR0), 84–85, 186, 401–407, 423,
425, 497–501, 515, 528, 529
Timer (TMR1), 407–417, 425, 430, 440,
528
Timer (TMR2), 342, 408, 417–423
Timer (TMR3), 515
2’s complement
– Dividing by shifting, 11
– Number, 9–12, 118, 143
– Overflow, 10, 11, 15, 24, 509
– Signed Number, 9–12, 509, 520
Universal Asynchronous Receiver
Transmitter (UART), 375–382
Universal Synchronous-Asynchronous
Receiver Transmitter (USART), 376–
382
Vector, 224
– High-Priority Interrupt, 516

– Interrupt, 190, 192, 197, 261, 404,
536
– Low-Priority Interrupt, 516
– Reset, 75, 190, 197, 231, 281, 282,
284, 285, 400, 403, 507, 536
Voltage Reference module, see Comparator Voltage Reference module
von Neumann architecture, 44
Watchdog timer, 79, 84–85, 285, 397–
401, 404, 496–501, 515
– EEPROM Data module, 483
– Sleep state, 273, 274, 285
Word size
– Byte (8), 6
– Long-word (32), 6
– Nybble (4), 6
– Quad-word (64), 6
– Word (16), 6
Working register (W), 33, 36, 43, 51, 78,
509, 517
Write cycle, 50



×