Table Of ContentAbout the Authors
Martin P. Bates (Chapters 6, 7, 8, Appendices C, J) is the author of PIC Microcontrollers, 2E.
He is currently lecturing on electronics and electrical engineering at Hastings College, UK.
His interests include microcontroller applications and embedded system design.
Lucio Di Jasio (Chapters 24, 25, 26, 27, 28, 29) is the author of Programming 16-bit
Microcontrollers in C. He joined Microchip Technology in 1995 as a Field Application
Engineer. Since 2005, he has been in charge of the Application Segment Group, a cross-
divisional team of engineers that develops and promotes Microchip’s solutions across a wide
range of application segments, including: utility metering, intelligent power conversion, motor
control and lighting applications.
Chuck Hellebuyck (Chapters 15, 16, 17) is the author of Programming PIC Microcontrollers
using PIC Basic. He is founder and president of Elproducts, Inc., a fi rm specializing in devices
and project kits based on the PIC microcontroller. He writes a monthly column on the PIC
microcontroller for “Nuts and Volts” magazine.
Dogan Ibrahim (Chapters 1, 14) is the author of PICBasic Projects. He works for the
Transport for London in UK. He was formerly a lecturer at South Bank University and Head
of Department of Computer Engineering at Near East University, Cyprus.
John Morton (Chapters 9, 10, Appendices D, E, F, G, H, I) is the author of The PIC
Microcontroller. He is a Junior Research Fellow at St. John’s College, Oxford, investigating
experimental quantum computation using electron spins. He works in the Oxford University
Materials Department and Clarendon Laboratory and in collaboration with the Quantum
Information Processing IRC. His interests include PIC Microcontrollers.
D.W. Smith (Chapters 11, 12, 13) is the author of PIC in Practice. He has 30 years experience
in the Electronics Industry. Before arriving at MMU he worked as an Electronics Design
Engineer for ICL and Marconi. His teaching interests are focused on enabling Design and
Technology students to implement microcontroller designs into their projects.
Jack Smith (Chapters 18, 19, 20, 21, 22, 23) is the author of Programming the PIC
Microcontroller with MBasic. He is currently with Clifton Laboratories in Virginia. He was
www.newnespress.com
PPrreelliimmss--HH88661155..iinndddd xxiiiiii 77//1111//0077 1100::4411::3355 AAMM
PIC Microcontrollers
PPrreelliimmss--HH88661155..iinndddd ii 77//1111//0077 1100::4411::2299 AAMM
Newnes Know It All Series
PIC Microcontrollers: Know It All
Lucio Di Jasio, Tim Wilmshurst, Dogan Ibrahim, John Morton,
Martin Bates, Jack Smith, D.W. Smith, and Chuck Hellebuyck
ISBN: 978-0-7506-8615-0
Embedded Software: Know It All
Jean Labrosse, Jack Ganssle, Tammy Noergaard, Robert Oshana, Colin Walls, Keith Curtis,
Jason Andrews, David J. Katz, Rick Gentile, Kamal Hyder, and Bob Perrin
ISBN: 978-0-7506-8583-2
Embedded Hardware: Know It All
Jack Ganssle, Tammy Noergaard, Fred Eady, Lewin A.R.W. Edwards,
David J. Katz, Rick Gentile, Ken Arnold, Kamal Hyder, and Bob Perrin
ISBN: 978-0-7506-8584-9
Wireless Networking: Know It All
Praphul Chandra, Daniel M. Dobkin, Alan Bensky, Ron Olexa,
David Lide, and Farid Dowla
ISBN: 978-0-7506-8582-5
RF & Wireless Technologies: Know It All
Bruce Fette, Roberto Aiello, Praphul Chandra, Daniel Dobkin,
Alan Bensky, Douglas Miron, David Lide, Farid Dowla, and Ron Olexa
ISBN: 978-0-7506-8581-8
For more information on these and other Newnes titles visit: www.newnespress.com
PPrreelliimmss--HH88661155..iinndddd iiii 77//1111//0077 1100::4411::3300 AAMM
PIC Microcontrollers
Lucio Di Jasio
Tim Wilmshurst
Dogan Ibrahim
John Morton
Martin P. Bates
Jack Smith
D. W. Smith
Chuck Hellebuyck
AMSTERDAM • BOSTON • HEIDELBERG • LONDON
NEW YORK • OXFORD • PARIS • SAN DIEGO
SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO
Newnes is an imprint of Elsevier
PPrreelliimmss--HH88661155..iinndddd iiiiii 77//1111//0077 1100::4411::3311 AAMM
Newnes is an imprint of Elsevier
30 Corporate Drive, Suite 400, Burlington, MA 01803, USA
Linacre House, Jordan Hill, Oxford OX2 8DP, UK
Copyright © 2008, Elsevier Inc. All rights reserved.
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, or otherwise, without the prior written permission of the publisher.
Permissions may be sought directly from Elsevier’s Science & Technology Rights
Department in Oxford, UK: phone: ((cid:2)44) 1865 843830, fax: ((cid:2)44) 1865 853333,
E-mail: permissions@elsevier.com. You may also complete your request online via
the Elsevier homepage (http://elsevier.com), by selecting “Support & Contact” then
“Copyright and Permission” and then “Obtaining Permissions.”
Recognizing the importance of preserving what has been written, Elsevier prints
its books on acid-free paper whenever possible.
Library of Congress Cataloging-in-Publication Data
PIC microcontrollers : know it all / Lucio Di Jasio … [et al.].
p. cm. – (The Newnes know it all series)
ISBN-13: 978-0-7506-8615-0
1. Programmable controllers. 2. Microcomputers. 3. Microprocessors.
I. Di Jasio, Lucio.
TJ223. P76P52 2007
629.8(cid:3)95416–dc22
2007025364
British Library Cataloguing-in-Publication Data
A catalogue record for this book is available from the British Library.
ISBN: 978-0-7506-8615-0
For information on all Newnes publications
visit our Web site at www.books.elsevier.com
07 08 09 10 10 9 8 7 6 5 4 3 2 1
Printed in the United States of America
PPrreelliimmss--HH88661155..iinndddd iivv 77//1111//0077 1100::4411::3311 AAMM
Contents
About the Authors .........................................................................................................xiii
Section I. An Introduction to PIC Microcontrollers ..............................................................1
Chapter 1. The PIC Microcontroller Family ..............................................................................3
1.1 12-bit Instruction Word .................................................................................................6
1.2 14-bit Instruction Word .................................................................................................7
1.3 16-bit Instruction Word ...............................................................................................11
1.4 Inside a PIC Microcontroller .......................................................................................12
Chapter 2. Introducing the PIC® 16 Series and the 16F84A ...................................................39
2.1 The Main Idea—the PIC 16 Series Family .................................................................39
2.2 An Architecture Overview of the 16F84A ..................................................................42
2.3 A Review of Memory Technologies ............................................................................44
2.4 The 16F84A Memory ..................................................................................................46
2.5 Some Issues of Timing ................................................................................................51
2.6 Power-Up and Reset ....................................................................................................54
2.7 What Others Do—the Atmel AT89C2051 ..................................................................55
2.8 Taking Things Further—the 16F84A On-Chip Reset Circuit .....................................56
2.9 Summary .....................................................................................................................59
References ..........................................................................................................................59
Chapter 3. Parallel Ports, Power Supply and the Clock Oscillator ..........................................61
3.1 The Main Idea—Parallel Input/Output ........................................................................62
3.2 The Technical Challenge of Parallel Input/Output ......................................................62
3.3 Connecting to the Parallel Port ....................................................................................68
3.4 The PIC 16F84A Parallel Ports ...................................................................................71
3.5 The Clock Oscillator ...................................................................................................74
3.6 Power Supply...............................................................................................................78
3.7 The Hardware Design of the Electronic Ping-Pong ....................................................80
3.8 Summary .....................................................................................................................82
References ..........................................................................................................................82
PPrreelliimmss--HH88661155..iinndddd vv 77//1111//0077 1100::4411::3322 AAMM
vi Contents
Section II. Programming PIC Microcontrollers Using Assembly Language ............................83
Chapter 4. Starting to Program—An Introduction to Assembler ............................................85
4.1 The Main Idea—What Programs Do and How We Develop Them ..........................86
4.2 The PIC 16 Series Instruction Set, with a Little More on the ALU ..........................89
4.3 Assemblers and Assembler Format ...........................................................................92
4.4 Creating Simple Programs .........................................................................................94
4.5 Adopting a Development Environment .....................................................................97
4.6 An Introductory MPLAB Tutorial .............................................................................99
4.7 An Introduction to Simulation .................................................................................103
4.8 Downloading the Program to a Microcontroller .....................................................106
4.9 What Others Do—A Brief Comparison of CISC and RISC Instruction Sets .........108
4.10 Taking Things Further—The 16 Series Instruction Set Format ..............................109
4.11 Summary .................................................................................................................110
References ........................................................................................................................110
Chapter 5. Building Assembler Programs .............................................................................111
5.1 The Main Idea—Building Structured Programs .....................................................111
5.2 Flow Control—Branching and Subroutines ............................................................114
5.3 Generating Time Delays and Intervals ....................................................................118
5.4 Dealing with Data ....................................................................................................120
5.5 Introducing Logical Instructions .............................................................................125
5.6 Introducing Arithmetic Instructions and the Carry Flag .........................................125
5.7 Taming Assembler Complexity ...............................................................................130
5.8 More Use of the MPLAB Simulator .......................................................................132
5.9 The Ping-Pong Program ..........................................................................................136
5.10 Simulating the Ping-Pong Program—Tutorial ........................................................140
5.11 What Others Do—Graphical Simulators .................................................................143
5.12 Summary .................................................................................................................143
References ........................................................................................................................144
Chapter 6. Further Programming Techniques ........................................................................145
6.1 Program Timing .......................................................................................................145
6.2 Hardware Counter/Timer .........................................................................................147
6.3 Interrupts .................................................................................................................152
6.4 More Register Operations........................................................................................158
6.5 Special Features .......................................................................................................163
6.6 Program Data Table .................................................................................................167
6.7 Assembler Directives ...............................................................................................170
6.8 Special Instructions .................................................................................................173
6.9 Numerical Types ......................................................................................................174
6.10 Summary .................................................................................................................175
www.newnespress.com
PPrreelliimmss--HH88661155..iinndddd vvii 77//1111//0077 1100::4411::3322 AAMM
Contents vii
Chapter 7. Prototype Hardware ..............................................................................................177
7.1 Hardware Design .....................................................................................................177
7.2 Hardware Construction ............................................................................................178
7.3 Demo Board ............................................................................................................183
7.4 Demo Board Applications .......................................................................................186
7.5 Summary .................................................................................................................198
Chapter 8. More PIC Applications and Devices ....................................................................199
8.1 16F877 Application .................................................................................................199
8.2 16F818 Application .................................................................................................219
8.3 12F675 Application .................................................................................................220
8.4 18F452 Application .................................................................................................221
8.5 Summary .................................................................................................................226
Chapter 9. The PIC12F50x Series (8-pin PIC Microcontrollers) ..........................................227
9.1 Differences from the PIC16F54 ..............................................................................227
9.2 Example Project: PIC Dice ......................................................................................231
Chapter 10. Intermediate Operations Using the PIC12F675 .................................................237
10.1 The Inner Differences ..............................................................................................238
10.2 Interrupts .................................................................................................................242
10.3 EEPROM .................................................................................................................252
10.4 Analog to Digital Conversion ..................................................................................259
10.5 Comparator Module ................................................................................................264
10.6 Final Project: Intelligent Garden Lights ..................................................................270
Chapter 11. Using Inputs .......................................................................................................275
11.1 Switch Flowchart .....................................................................................................277
11.2 Program Development .............................................................................................278
11.3 Scanning (Using Multiple Inputs) ...........................................................................283
11.4 Switch Scanning ......................................................................................................283
11.5 Control Application—A Hot Air Blower ................................................................287
Chapter 12. Keypad Scanning ................................................................................................291
12.1 Programming Example for the Keypad ...................................................................291
Chapter 13. Program Examples .............................................................................................307
13.1 Counting Events ......................................................................................................307
13.2 Look-Up Table .........................................................................................................311
13.3 7-Segment Display ..................................................................................................311
13.4 Numbers Larger than 255 ........................................................................................321
13.5 Long Time Intervals ................................................................................................327
13.6 One Hour Delay .......................................................................................................330
www.newnespress.com
PPrreelliimmss--HH88661155..iinndddd vviiii 77//1111//0077 1100::4411::3333 AAMM
viii Contents
Section III. Programming PIC Microcontrollers Using PicBasic .........................................333
Chapter 14. PicBasic and PicBasic Pro Programming ...........................................................335
14.1 PicBasic Language ..................................................................................................335
14.2 PicBasic Pro Language ............................................................................................357
14.3 Liquid Crystal Display (LCD) Interface and Commands .......................................369
14.4 Interrupts .................................................................................................................380
14.5 Recommended PicBasic Pro Program Structure .....................................................381
14.6 Using Stepping Motors ............................................................................................381
14.7 Using Servomotors ..................................................................................................384
Chapter 15. Simple PIC Projects ...........................................................................................387
15.1 Project #1—Flashing an LED .................................................................................387
15.2 Project #2—Scrolling LEDs ....................................................................................391
15.3 Project #3—Driving a 7-Segment LED Display .....................................................397
Chapter 16. Moving On with the 16F876 ..............................................................................405
16.1 Project #4—Accessing Port A I/O ..........................................................................405
16.2 Project #5—Analog-to-Digital Conversion .............................................................412
16.3 Project #6—Driving a Servomotor ..........................................................................421
Chapter 17. Communication ..................................................................................................429
17.1 Project #7—Driving an LCD Module .....................................................................429
17.2 Project #8—Serial Communication .........................................................................439
17.3 Project #9—Driving an LCD with a Single Serial Connection ...............................447
Section IV. Programming PIC Microcontrollers Using MBasic...........................................463
Chapter 18. MBasic Compiler and Development Boards ......................................................465
18.1 The Compiler Package ............................................................................................465
18.2 BASIC and Its Essentials .........................................................................................467
18.3 Development Boards ...............................................................................................470
18.4 Programming Style ..................................................................................................473
18.5 Building the Circuits and Standard Assumptions ....................................................475
18.6 Pins, Ports and Input/Output ...................................................................................476
18.7 Pseudo-Code and Planning the Program .................................................................485
18.8 Inside the Compiler .................................................................................................487
References ........................................................................................................................491
Chapter 19. The Basics—Output ...........................................................................................493
19.1 Pin Architectures .....................................................................................................494
19.2 LED Indicators ........................................................................................................498
19.3 Switching Inductive Loads ......................................................................................503
www.newnespress.com
PPrreelliimmss--HH88661155..iinndddd vviiiiii 77//1111//0077 1100::4411::3333 AAMM
Description:The Newnes Know It All Series takes the best of what our authors have written over the past few years and creates a one-stop reference for engineers involved in markets from communications to embedded systems and everywhere in between. PIC design and development a natural fit for this reference seri