Microcontroller Theory and Applications With The PIC18F 1st Edition M. Rafiquzzaman Download
Microcontroller Theory and Applications With The PIC18F 1st Edition M. Rafiquzzaman Download
https://ebookultra.com/download/microcontroller-theory-and-applications-
with-the-pic18f-1st-edition-m-rafiquzzaman/
https://ebookultra.com/download/microeconomics-theory-applications-
with-calculus-jeffrey-m-perloff/
https://ebookultra.com/download/bolometers-theory-types-and-
applications-theory-types-and-applications-1st-edition-torrence-m-
walcott/
https://ebookultra.com/download/fundamentals-of-digital-logic-and-
microcomputer-design-fifth-edition-m-rafiquzzamanauth/
https://ebookultra.com/download/hidden-markov-processes-theory-and-
applications-to-biology-m-vidyasagar/
Elementary Number Theory with Applications 2nd Edition
Koshy
https://ebookultra.com/download/elementary-number-theory-with-
applications-2nd-edition-koshy/
https://ebookultra.com/download/stochastic-differential-games-theory-
and-applications-2012th-edition-kandethody-m-ramachandran/
https://ebookultra.com/download/derivative-with-a-new-parameter-
theory-methods-and-applications-1st-edition-atangana/
https://ebookultra.com/download/elements-of-quasigroup-theory-with-
applications-in-coding-and-cryptology-1st-edition-shcherbacov/
https://ebookultra.com/download/modern-electromagnetic-scattering-
theory-with-applications-1st-edition-andrey-v-osipov/
Microcontroller Theory and Applications with the
PIC18F 1st Edition M. Rafiquzzaman Digital Instant
Download
Author(s): M. Rafiquzzaman
ISBN(s): 9780470947692, 0470947691
Edition: 1
File Details: PDF, 14.62 MB
Year: 2011
Language: english
Microcontroller Theory
and Applications with the PIC18F
i
Microcontroller Theory
and Applications with
the PIC18F
First Edition
M. RAFIQUZZAMAN, Ph.D.
Professor
California State Polytechnic University, Pomona
and
President
Rafi Systems, Inc.
Diamond Bar, California
Vice President and Executive Publisher Don Fowley
Associate Publisher Daniel Sayre
Marketing Manager Chris Ruel
Production Manager Micheline Frederick
Cover Designer Wendy Lai
Editorial Assistant Katie Singleton
Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
This title was set by the author and printed and bound by Hamilton Printing.
Founded in 1807, John Wiley & Sons, Inc. has been a valued source of knowledge and understanding for more
than 200 years, helping people around the world meet their needs and fulfill their aspirations. Our company is
built on a foundation of principles that include responsibility to the communities we serve and where we live
and work. In 2008, we launched a Corporate Citizenship Initiative, a global effort to address the environmental,
social, economic, and ethical challenges we face in our business. Among the issues we are addressing are
carbon impact, paper specifications and procurement, ethical conduct within our business and among our
vendors, and community and charitable support. For more information, please visit our website: www.wiley.
com/go/citizenship.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any
means, electronic, mechanical, photocopying, recording, scanning, or otherwise, except as permitted under
Section 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the
Publisher, or authorization through payment of the appropriate per‑copy fee to the Copyright Clearance Center,
Inc., 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600, or on the Web at
www.copyright.com. Requests to the Publisher for permission should be addressed to the Permissions
Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-
6008, or online at http://www.wiley.com/go/permission.
Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best efforts in
preparing this book, they make no representation or warranties with respect to the accuracy or completeness
of the contents of this book and specifically disclaim any implied warranties of merchantability or fitness for a
particular purpose. No warranty may be created or extended by sales representatives or written sales materials.
The advice and strategies contained herein may not be suitable for your situation. You should consult with a
professional where appropriate. Neither the publisher nor the author shall be liable for any loss of profit or any
other commercial damages, including but not limited to special, incidental, consequential, or other damages.
“Evaluation copies are provided to qualified academics and professionals for review purposes only, for use
in their courses during the next academic year. These copies are licensed and may not be sold or transferred
to a third party. Upon completion of the review period, please return the evaluation copy to Wiley. Return
instructions and a free of charge return shipping label are available at www.wiley.com/go/returnlabel. Outside of
the United States, please contact your local representative.”
ISBN 13 978-0470-94769-2
10 9 8 7 6 5 4 3 2 1
To my wife, Kusum; my son, Tito; and my brother, Elan
Contents
PREFACE XIII
CREDITS XVII
1. INTRODUCTION TO MICROCONTROLLERS 1
1.1 Explanation of Terms 2
1.2 Microcontroller Data Types 6
1.2.1 Unsigned and Signed Binary Numbers 6
1.2.2 ASCII and EBCDIC Codes 8
1.2.3 Unpacked and Packed Binary‑Coded‑Decimal Numbers 9
1.3 Evolution of the Microcontroller 9
1.4 Typical Microcontroller Applications 11
1.4.1 A Simple Microcontroller Application 12
1.4.2 Embedded Controllers 12
2. MICROCONTROLLER BASICS 15
2.1 Basic Blocks of a Microcomputer 15
2.1.1 System Bus 16
2.1.2 Clock Signals 17
2.2 Microcontroller Architectures 18
2.3 Central Processing Unit (CPU) 19
2.3.1 Register Section 19
2.3.2 Control Unit 26
2.3.3 Arithmetic and Logic Unit (ALU) 28
2.3.4 Simplified Explanation of Control Unit Design 28
2.4 Basic Concept of Pipelining 29
2.5 RISC vs. CISC 31
2.6 Functional Representation of a Typical Microcontroller—The PIC18F4321 32
Questions and Problems 34
3. INTRODUCTION TO PROGRAMMING LANGUAGES 37
3.1 Basics of Programming Languages 37
3.2 Machine Language 38
3.3 Assembly Language 38
3.3.1 Types of Assemblers 39
3.3.2 Assembler Delimiters 40
3.3.3 Specifying Numbers by Typical Assemblers 41
3.3.4 Assembler Directives or Pseudoinstructions 41
3.3.5 Assembly Language Instruction Formats 43
3.3.6 Typical Instruction Set 45
3.3.7 Typical Addressing Modes 50
3.3.8 Subroutine Calls in Assembly Language 52
3.4 High‑Level Language 52
3.5 Choosing a Programming Language 53
3.6 Flowcharts 53
Questions and Problems 55
vii
viii Microcontroller Theory and Applications with the PIC18F
4. MICROCONTROLLER MEMORY AND INPUT/OUTPUT (I/O) 57
4.1 Introduction to Microcontroller Memory 57
4.1.1 Main Memory 57
4.1.2 READ and WRITE Timing Diagrams 60
4.1.3 Main Memory Organization 62
4.2 Microcontroller Input/Output (I/O) 66
4.2.1 Overview of Digital Output Circuits 67
4.2.2 Simple I/O Devices 69
4.2.3 Programmed I/O 70
4.2.4 Unconditional and Conditional Programmed I/O 72
4.2.5 Interrupt I/O 73
Questions and Problems 77
5. PIC18F ARCHITECTURE AND ADDRESSING MODES 81
5.1 Introduction 81
5.2 PIC18F Register Architecture 85
5.3 PIC18F Memory Organization 88
5.3.1 PIC18F Program Memory Map 89
5.3.2 PIC18F Data Memory Map 89
5.4 PIC18F Addressing Modes 91
5.4.1 Literal or Immediate Addressing Mode 92
5.4.2 Inherent or Implied Addressing Mode 92
5.4.3 Direct or Absolute Addressing Mode 92
5.4.4 Indirect Addressing Mode 93
5.4.5 Relative Addressing Mode 97
5.4.6 Bit Addressing Mode 99
Questions and Problems 100
6. ASSEMBLY LANGUAGE PROGRAMMING WITH THE PIC18F: PART 1 103
6.1 Basic Concepts 103
6.2 PIC18F Instruction Format 108
6.3 PIC18F Instruction Set 110
6.3.1 Data Movement Instructions 112
6.3.2 Arithmetic Instructions 118
6.3.3 Logic Instructions 128
6.3.4 Rotate Instructions 131
6.3.5 Bit Manipulation Instructions 138
QUESTIONS AND PROBLEMS 142
7. ASSEMBLY LANGUAGE PROGRAMMING WITH THE PIC18F: PART 2 145
7.1 PIC18F Jump/Branch Instructions 145
7.2 PIC18F Test, Compare, and Skip Instructions 147
7.3 PIC18F Table Read/Write Instructions 152
7.4 PIC18F Subroutine Instructions 156
7.5 PIC18F System Control Instructions 159
7.6 PIC18F Hardware vs. Software Stack 159
7.7 Multiplication and Division Algorithms 167
7.7.1 Signed Multiplication Algorithm 167
7.7.2 Unsigned Division Algorithm 170
7.7.3 Signed Division Algorithm 173
Contents ix
7.8 Advanced Programming Examples 174
7.9 PIC18F Delay Routine 179
Questions and Problems 182
8. PIC18F HARDWARE AND INTERFACING: PART 1 187
8.1 PIC18F Pins and Signals 187
8.1.1 Clock 191
8.1.2 PIC18F Reset 192
8.1.3 A Simplified Setup for the PIC18F4321 194
8.2 PIC18F4321 I/O Ports 194
8.2.1 PIC18F I/O Instructions 196
8.2.2 Configuring PIC18F4321 I/O Ports 197
8.2.3 Interfacing LEDs (Light Emitting Diodes) and Seven‑segment
Displays 199
8.3 PIC18F Interrupts 204
8.3.1 Interrupt Procedure 205
8.3.2 PIC18F Interrupt Types 206
8.3.3 PIC18F External Interrupts in Default Mode 206
8.3.4 Interrupt Registers and Priorities 211
8.3.5 Setting the Triggering Levels of INTn Pin Interrupts 213
8.3.6 Return from Interrupt Instruction 213
8.3.7 PORTB Interrupt‑on‑Change 214
8.3.8 Context Saving During Interrupts 214
8.4 PIC18F Interface to an LCD (Liquid Crystal Display) 217
8.5 Interfacing PIC18F4321 to a Hexadecimal Keyboard and a Seven‑segment
Display 219
8.5.1 Basics of Keyboard and Display Interface to a Microcontroller 221
8.5.2 PIC18F4321 Interface to a Hexadecimal Keyboard and a
Seven‑segment Display 223
Questions and Problems 230
9. PIC18F HARDWARE AND INTERFACING: PART 2 235
9.1 PIC18F Timers 235
9.1.1 Timer0 236
9.1.2 Timer1 240
9.1.3 Timer2 244
9.1.4 Timer3 247
9.2 Analog Interface 247
9.2.1 On‑chip A/D Converter 250
9.2.2 Interfacing an External D/A (Digital‑to‑Analog) Converter to the
PIC18F4321 257
9.3 Serial Interface 259
9.3.1 Synchronous Serial Data Transmission 259
9.3.2 Asynchronous Serial Data Transmission 260
9.3.3 PIC18F Serial I/O 260
9.4 PIC18F4321 Capture/Compare/PWM (CCP) Modules 267
9.4.1 CCP Registers 268
9.4.2 CCP Modules and Associated Timers 268
9.4.3 PIC18F4321 Capture Mode 269
x Microcontroller Theory and Applications with the PIC18F
9.4.4 PIC18F4321 Compare Mode 271
9.4.5 PIC18F4321 PWM (Pulse Width Modulation) Mode 272
9.5 DC Motor Control 274
Questions and Problems 277
10. BASICS OF PROGRAMMING THE PIC18F USING C 283
10.1 Introduction to C Language 283
10.2 Data Types 287
10.3 Bit Manipulation Operators 287
10.4 Control Structures 289
10.4.1 The if‑else Construct 289
10.4.2 The switch Construct 292
10.4.3 The while Construct 293
10.4.4 The for Construct 294
10.4.5 The do‑while Construct 296
10.5 Structures and Unions 296
10.6 Functions in C 298
10.7 Macros 299
10.8 Configuring PIC18F4321 I/O Ports Using C 299
10.9 Programming PIC18F4321 Interrupts Using C 304
10.9.1 Specifying Interrupt Address Vector using the C18 Compiler 305
10.9.2 Assigning Interrupt Priorities Using the C18 Compiler 305
10.9.3 A Typical Structure for Interrupt Programs Using C 305
10.10 Programming the PIC18F4321 Interface to LCD Using C 310
10.11 PIC18F on‑chip Timers 314
10.12 Programming the PIC18F4321 on‑chip A/D Converter Using C 316
10.13 Interfacing an External D/A (Digital‑to‑Analog) Converter Using C 319
10.14 PIC18F SPI Mode for Serial I/O Using C 320
10.15 Programming the PIC18F4321 CCP Modules Using C 322
10.16 DC Motor Control Using PWM Mode and C 325
Questions and Problems 328
APPENDIX A: ANSWERS TO SELECTED PROBLEMS 337
APPENDIX B: GLOSSARY 345
APPENDIX C: PIC18F INSTRUCTION SET (ALPHABETICAL ORDER) 357
APPENDIX D: PIC18F INTRUCTION SET — DETAILS 363
APPENDIX E: PIC18F4321 SPECIAL FUNCTION REGISTERS 405
APPENDIX F: TUTORIAL FOR ASSEMBLING AND DEBUGGING
A PIC18F ASSEMBLY LANGUAGE PROGRAM USING THE
MPLAB 407
APPENDIX G: TUTORIAL FOR COMPILING AND DEBUGGING A
C‑PROGRAM USING THE MPLAB 437
APPENDIX H: INTERFACING THE PIC18F4321 TO PERSONAL
COMPUTER USING PICKIT™ 3 465
H.1 INITIAL HARDWARE SETUP FOR THE PIC18F4321 465
Contents xi
H.2 CONNECTING THE PERSONAL COMPUTER (PC) TO THE
PIC18F4321 VIA PICkit3 466
H.3 PROGRAMMING THE PIC18F4321 FROM PERSONAL
COMPUTER USING THE PICkit3 468
BIBLIOGRAPHY 471
INDEX 473
PREFACE
Microcontrollers play an important role in the design of digital systems. They are found
in a wide range of applications including office automation systems (copiers and fax
machines), consumer electronics (microwave ovens), digital instruments, and robotics.
This book is written in a very simplified manner to present the fundamental
concepts of assembly and C language programming and interfacing techniques associated
with typical microcontrollers. Microchip Technology’s PIC18F4321 is used for this
purpose. The PIC18F family continues to be popular. The PIC18F family is an excellent
educational tool for acquiring an understanding of both hardware and software aspects of
typical microcontrollers.
The PIC18F uses Harvard architecture with a RISC‑based CPU. Conventional
CPUs complete fetch, decode, and execute cycles of an instruction in sequence. However,
the PIC18F uses pipelining, in which instruction fetch and execute cycles are overlapped.
This speeds up the instruction execution time of the PIC18F. A brief coverage of CPU
architectures, RISC vs. CISC, pipelining, assembly/C language programming, and I/O
techniques associated with typical microcontrollers is provided in the first part of the
book. These topics are then related to a popular member of the PIC18F family such as the
PIC18F4321 in the second part of the book.
As far as the programming is concerned, assembly language programming
is mostly covered in this book using the PIC18F. An adequate coverage of C is also
provided. Although writing programs using C is easier than using assembly language,
assembly language programming will provide an exposure to the internal architecture of
microcontrollers. Furthermore, programming in assembly language may sometimes be
useful for real‑time systems.
Several assembly and some C language programs along with I/O examples
are developed using Microchip’s MPLAB and PICkit™3. The MPLAB software
package includes a text editor, PIC18F assembler, C compiler, and a simulator. The
PICkit™3 is a programmer provided by Microchip. One can build an inexpensive
PIC18F‑based system on a breadboard using one of the PIC18F devices such as the
PIC18F4321. The programmer can download the compiled or assembled programs
using the PICkit™3 from the personal computer or laptop, and then perform
meaningful experiments. This is the most inexpensive way of implementing laboratory
experiments using a typical microcontroller such as the PIC18F4321. Note that
Appendix F provides a tutorial showing step‑by‑step procedure for assembling and
debugging a PIC18F assembly language program using Microchip MPLAB PIC18F
assembler/debugger. Appendix G, on the other hand, includes a tutorial showing
step‑by‑step procedure for compiling and debbuging a C program using the MPLAB C18
compiler/debugger.
The book is self‑contained and includes a number of basic topics. A background in
basic digital logic and C language programming is assumed. Characteristics and principles
common to typical microcontrollers are emphasized and basic microcontroller interfacing
techniques are demonstrated via examples using the simplest possible devices, such as
switches, LEDs, A/D and D/A converters, the hexadecimal keyboard, and seven‑segment
xiii
xiv Microcontroller Theory and Applications with the PIC18F
and LCD displays. Most of the examples are implemented successfully in the laboratory.
The text is divided into 10 chapters. In Chapter 1, we provide a review of
terminology, number systems, and evolution of microcontrollers. A comparison of the basic
features of some members of the PIC18F family and typical microcontroller applications
are also included.
Chapters 2 through 5 provide basic concepts needed to understand the material
presented in Chapters 6 though 10. Chapter 2 covers typical microcontroller architectures.
The concepts of CPU architecture, program and data memory units, pipelining, and RISC
vs. CISC are included.
Chapter 3 contains programming concepts associated with typical microcontrollers.
Topics include machine, assembly, and C language programming, typical addressing
modes, and instruction sets.
Chapter 4 is focused on the memory organization and I/O (Input / Output)
techniques associated with typical microcontrollers. The basic concepts associated
with main memory array design, including memory maps, are also covered. Typical
microcontroller input/output techniques including programmed I/O and interrupt I/O are
included.
Chapter 5 includes PIC18F architecture and addressing modes. The PIC18F
pipelining, register architecture, memory maps, and addressing modes are provided.
Chapters 6 through 9 form the nucleus of the book. The concepts of assembly
language programming covered in Chapter 3 are demonstrated in Chapters 6 and 7 by
means of a typical 8‑bit microcontroller. A specific device from the PIC18F family such
as the PIC18F4321 is used to illustrate the concepts. Several PIC18F assembly language
programming examples are included.
The I/O techniques covered in Chapter 4 are demonstrated in Chapters 8 and 9
using the PIC18F4321. Several I/O examples using PIC18F assembly language are also
included. These chapters also demonstrate how the software and hardware work together
by interfacing simple I/O devices such as switches, LEDs, and seven‑segment displays to
more advanced devices such as LCDs (Liquid Crystal Displays), hexadecimal keyboard,
and A/D and D/A converters. The PIC18F timers and CCP (Compare/Capture/PWM)
module along with Serial I/O are also covered. Typical examples include designing a
PIC18F4321‑based voltmeter using both programmed and interrupt I/O.
The concepts of C language programming covered in Chapter 3 are demonstrated
in Chapter 10 using the PIC18F4321 microcontroller from an introductory point‑of‑view.
Chapter 10 starts with a brief coverage of basics of C language, and then implements most
of the assembly language programming examples in Chapters 8 and 9 using C. Typical
C programs include I/O examples with LEDs and switches, PIC18F‑based voltmeter, A/D
and D/A converters, LCD displays, timers, and motor control using PWM (Pulse Width
Modulation).
The book can easily be adopted as a text for a one‑ semester or one‑quarter course
in microcontrollers taught at the undergraduate level in electrical/computer engineering
and computer science departments. The students are expected to have a background
in C language and digital logic (both combinational and sequential) design. The book
will also be useful for practicing microcontroller system designers. Practitioners of
microcontroller‑based applications will find more simplified explanations, together with
examples and comparison considerations, than are found in manufacturers’ manuals.
As mentioned before, emphasis is given in this book on assembly language
programming using a typical microcontroller such as the PIC18F4321. Adequate coverage
Preface xv
of I/O and interfacing using C is included.
Since C language programming is prerequisite for this course on microcontrollers,
coverage of I/O and interfacing using C would suffice. A basic coverage of assembly
language programming using a typical microcontroller such as the PIC18F is provided.
The author is especially indebted to his colleague, Dr. R. Chandra, of California
State Poly University, Pomona; to his student, Luke Stankiewicz; and to others for their
valuable comments and for making constructive suggestions. The author also wishes to
express his sincere appreciation to his student, Michael Nguyen for drawing several figures
in the book, and to CJ Media of California for preparing the final version of the manuscript.
The author is also grateful to his student, Sevada Isayan, and to Marc McComb and Rob
Stransky of Microchip Technology, Inc. for their inspiration and support throughout the
writing effort. Finally, the author is indebted especially to his deceased parents, who were
primarily responsible for his accomplishments.
xvii
Random documents with unrelated
content Scribd suggests to you:
The Project Gutenberg eBook of Outo
intohimo: Kertomus Ludvig XIV:n ajalta
This ebook is for the use of anyone anywhere in the United States
and most other parts of the world at no cost and with almost no
restrictions whatsoever. You may copy it, give it away or re-use it
under the terms of the Project Gutenberg License included with this
ebook or online at www.gutenberg.org. If you are not located in the
United States, you will have to check the laws of the country where
you are located before using this eBook.
Author: E. T. A. Hoffmann
Translator: J. E. Eteläpää
Language: Finnish
OUTO INTOHIMO
Kertomus Ludvig XIV:n ajalta
Kirj.
E. T. A. HOFFMANN
Suom. J. E. Eteläpää
*****
Baptisten pelko oli täysin oikeutettu. Juuri siihen aikaan oli Parisi
mitä julmimpien hirmutöiden näyttämönä, juuri siihen aikaan oli eräs
pirullinen keksintö antanut helvetin voimille mitä helpoimmat keinot
niiden suorittamiseen.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookultra.com