Cover Page

MICROCONTROLLER THEORY AND APPLICATIONS WITH THE PIC18F

 

Second Edition

M. RAFIQUZZAMAN Ph.D., PE

Professor
California State Polytechnic University
Pomona, California

 

and

 

President
Rafi Systems, Inc.
Diamond Bar, California

 

 

 

 

Wiley Logo

To my wife, Kusum; and our grandsons (triplets), Ayaaz, Adyan, and Safir

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.

The second edition of 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 is inexpensive, and continues to be popular. Emphasis in this edition is given on chip-level design and implementation. This is very essential for the students to understand the hardware and software aspects of a specific microcontroller from chip level in a first course on microcontrollers. The PIC18F family is an excellent educational tool for this purpose.

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

As far as the programming is concerned, assembly and C language programming are covered in this book using the PIC18F. Coverage of assembly language programming will provide an exposure to the internal architecture of microcontrollers. Note that although microcontrollers in typical industrial applications include mostly C and some assembly, an introduction to assembly language programming for students is essential.

Several assembly and C language programs along with I/O examples are developed using Microchip's MPLAB, and PICkit3. The MPLAB software package includes a text editor, PIC18F assembler, C compiler, and a simulator. The PICkit3 is a programmer interface 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 PICkit3 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. This will enable the students with a thorough and basic background in microcontroller-based applications from chip level.

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, seven-segment displays and LCDs (Liquid Crystal Displays). Most of the examples are implemented successfully in the laboratory.

The text is divided into 11 chapters. In Chapter 1, we provide a review of terminology, number systems, and evolution of microcontrollers. Finally, a comparison of the basic features of some members of the PIC18F family and typical microcontroller applications are also included.

Chapters 2 through 11 form the nucleus of the book. 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 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 4 contains programming concepts associated with typical microcontrollers. Topics include machine, assembly and C language programming, typical addressing modes, and instruction sets.

Chapter 5 includes PIC18F architecture and addressing modes. The PIC18F pipelining, register architecture, memory maps, and addressing modes are provided.

The concepts of assembly language programming covered in Chapter 4 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 3 are demonstrated in Chapters 8 and 9 using the PIC18F4321. Several I/O examples (programmed, polled, and interrupt I/O) using PIC18F assembly and C languages are also included. These chapters also demonstrate how the software and hardware work together by interfacing simple I/O devices such as switches, LEDs, seven-segment displays, LCDs and a hexadecimal keyboard.

The PIC18F timers and analog modules (on-chip hardware timers and ADC) are described in Chapter 10. Typical examples include the design and successful implementation of a PIC18F4321-based voltmeter using both assembly and C.

The basics of CCP (Compare/Capture/PWM) and Serial I/O relating them to the PIC18F are covered in Chapter 11. Also, a DC motor is successfully interfaced to the PIC18F4321 using PWM (Pulse Width Modulation) mode of the CCP module. Both SPI and I2C modes of the PIC18F serial I/O are illustrated by means of successful implementation in the laboratory.

Courses on “Introduction to C” and “DC Circuits” are normally prerequisites for the “Digital Logic Design”. A course on “Introduction to Microcontrollers” is typically a core course for electrical engineering curriculum at the undergraduate level. With more than 40 years of academic and industrial experiences, the author believes that electrical engineers should have a background in “Introduction to Assembly Language”. This will provide students with exposure to internal architecture of a typical microcontroller. Note that assembly language programming is not normally a core course in electrical engineering curriculum. In addition, hands-on experience with I/O (Programmed, Interrupt, ADC, Timers, CCP) using C is essential. Students with experience in both assembly and C using a typical microcontroller such as the PIC18F will be better prepared for industries after graduation. A book like this will provide such a background.

The book can easily be adopted as a text for a one- semester or one - quarter course in microcontroller taught at the undergraduate level in electrical/computer engineering and computer science departments. The students are expected to have a background in “Introduction to C” and “Introduction to Digital Logic Design”. The book will also be useful for practicing microcontroller system designers. Practitioners of microcontroller-based applications will find more simplified explanations in this book, together with examples and comparison considerations, compared to that found in manufacturers' manuals.

The author is especially indebted to Sandra Grayson, editor, Wiley, UK for her inspiration to write this second edition. The author would also like to express his sincere appreciation to his students, Imed Abdellaoui, Loc Phan, Zhuohui Li, Carlos Landaverde, Luke Stankiewicz, Thomas Leung, and to others; to his colleague Professor R. Chandra of California State Polytechnic University, Pomona for making some constructive suggestions. The author is grateful to CJ Media of California for preparing the final version of the manuscript; to Mary Vang of Rafi Systems Inc. for drawing some of the figures in the book; to Lauren Le for initial editing of the manuscript. Finally, the author wishes to express his sincere appreciation to his editor, Brett Kurzman of Wiley, USA for his personal commitment, dedication, and excellent job in bringing this book to publication.

M. RAFIQUZZAMAN
Pomona, California

CREDITS

The material cited here is used by permission of the sources listed below.

Copyright of Microchip Technology, Inc. 2009, Used by Permission: PIC18F4321 Family Data Sheet, DS39689F. All mnemonics of Microchip PIC18F Microcontroller Family are courtesy of Microchip Technology, Inc.

The following are registered trademarks of Microchip in the U.S.A. and other countries: AnyRate, Atmel, AVR, AVR Freaks, BitCloud, CHIPKIT, CryptoMemory, CryptoRF, dsPIC, FlashFlex, flexPWR, Heldo, JukeBLOX, KEELOQ, Kleer, LANCheck, LINK MD, maXStylus, maXTouch, megaAVR, MediaLB, MOST, MPLAB, OptoLyzer, PIC, picoPower, PICSTART, Prochip Designer, QTouch, RightTouch, SAM-BA, SST, SuperFlash, tinyAVR, UNI/O and XMEGA.

The following are registered trademarks of Microchip in the U.S.A: ClockWorks, ETHERSYNCH, Hyper Speed Control, HyperLight Load, IntelliMOS, KleerNet, mTouch, Precision Edge, QUIET-WIRE and The Embedded Control Solutions Company. The following are registered trademarks of Microchip in other countries: BeaconThings, GestIC, Silicon Storage Technology.

The following are trademarks of Microchip in the U.S.A. and other countries: Adjacent Key Suppression, AKS, Analog-for-the-Digital Age, Any Capacitor, AnyIn, AnyOut, BeaconThings, BodyCom, CodeGuard, CryptoAuthentication, CryptoCompanion, CryptoController, dsPICDEM, dsPICDEM.net, Dynamic Averaged Matching, DAM, ECAN, EtherGREEN, ICSP, In-Circuit Serial Programming, Inter-Chip Connectivity, JitterBlocker, Mindi, MiWi, motorBench, MPASM, MPF, MPLIB, MPLINK, MultiTRAK, NetDetach, Omniscient Code Generation (OCG), PICDEM, PICDEM.net, PICkit, PICtail, PureSilicon, QMatrix, REAL ICE, Ripple Blocker, SAM-ICE, SMART-I.S., SQI, Serial Quad I/O, SuperSwitcher, SuperSwitcher II, Total Endurance, TSHARC, USBCheck, VariSense, ViewSpan, WiperLock, Wireless DNA and ZENA.

The following is a service mark of Microchip in the U.S.A.: SQTP