PIC Microcontrollers · PIC Microcontrollers An Introduction to Microelectronics 3rd Edition Martin...
Transcript of PIC Microcontrollers · PIC Microcontrollers An Introduction to Microelectronics 3rd Edition Martin...
PIC Microcontrollers
PIC MicrocontrollersAn Introduction to Microelectronics
3rd Edition
Martin Bates
AMSTERDAM • BOSTON • HEIDELBERG • LONDON
NEW YORK • OXFORD • PARIS • SAN DIEGO
SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO
Newnes is an imprint of Elsevier
Newnes is an imprint of ElsevierThe Boulevard, Langford Lane, Kidlington, Oxford, OX5 1GB225 Wyman Street, Waltham, MA 02451, USA
First published 2011
Copyright � 2012 Martin Bates. Published by Elsevier Ltd. All rights reserved.
The right of Martin Bates to be identified as the author of this work has been asserted in accordance with theCopyright, Designs and Patents Act 1988
No part of this publication may be reproduced or transmitted in any form or by any means, electronic or mechanical,including photocopying, recording, or any information storage and retrieval system, without permission in writingfrom the publisher. Details on how to seek permission, further information about the Publisher’s permissionspolicies and our arrangement with organizations such as the Copyright Clearance Center and the CopyrightLicensing Agency, can be found at our website: www.elsevier.com/permissions
This book and the individual contributions contained in it are protected under copyright by the Publisher (other thanas may be noted herein).
NoticesKnowledge and best practice in this field are constantly changing. As new research and experience broaden ourunderstanding, changes in research methods, professional practices, or medical treatment may become necessary.
Practitioners and researchers must always rely on their own experience and knowledge in evaluating and using anyinformation, methods, compounds, or experiments described herein. In using such information or methods theyshould be mindful of their own safety and the safety of others, including parties for whom they have a professionalresponsibility.
To the fullest extent of the law, neither the Publisher nor the authors, contributors, or editors, assume any liability forany injury and/or damage to persons or property as a matter of products liability, negligence or otherwise, or fromany use or operation of any methods, products, instructions, or ideas contained in the material herein.
British Library Cataloguing in Publication DataA catalogue record for this book is available from the British Library
Library of Congress Number: 2011930131
ISBN: 978-0-080-96911-4
For information on all Newnes publications visit ourwebsite at www.elsevierdirect.com
Printed and bound in the United Kingdom
11 12 13 14 10 9 8 7 6 5 4 3 2 1
Contents
Preface to the 3rd Edition................................................................................... ixIntroduction to the 3rd Edition ............................................................................ xi
Part 1 Getting Started
Chapter 1: Computer Systems ............................................................................ 3
1.1. Personal Computer System....................................................................................51.2. Word-Processor Operation ...................................................................................101.3. Microprocessor Systems ......................................................................................131.4. Microcontroller Applications...............................................................................17
Chapter 2: Microcontroller Operation ................................................................27
2.1. Microcontroller Architecture ...............................................................................282.2. Program Operations .............................................................................................33
Chapter 3: A Simple PIC Application .................................................................45
3.1. Hardware Design..................................................................................................463.2. Program Execution...............................................................................................503.3. Program BIN1 ......................................................................................................523.4. Assembly Language.............................................................................................55
Chapter 4: PIC Program Development ...............................................................61
4.1. Program Development .........................................................................................624.2. Program Design....................................................................................................654.3. Program Editing ...................................................................................................674.4. Program Structure ................................................................................................724.5. Program Analysis .................................................................................................744.6. Program Assembly...............................................................................................784.7. Program Simulation .............................................................................................824.8. Program Downloading .........................................................................................854.9. Program Testing ...................................................................................................88
v
Part 2 PIC Microcontrollers
Chapter 5: PIC Architecture..............................................................................93
5.1. Block Diagram ..................................................................................................... 955.2. Program Execution...............................................................................................965.3. File Register Set...................................................................................................98
Chapter 6: Programming Techniques ................................................................ 107
6.1. Program Timing ................................................................................................. 1096.2. Hardware Counter/Timer ................................................................................... 1096.3. Interrupts ............................................................................................................ 1156.4. Register Operations............................................................................................ 1226.5. Special Features ................................................................................................. 1296.6. Assembler Directives ......................................................................................... 1326.7. Pseudo-Instructions ............................................................................................ 1386.8. Numerical Types ................................................................................................ 1386.9. Data Table .......................................................................................................... 140
Chapter 7: PIC Development Systems .............................................................. 143
7.1. In-Circuit Programming..................................................................................... 1447.2. PICkit2 Demo System ....................................................................................... 1457.3. PIC 16F690 Chip ............................................................................................... 1467.4. Test Program ...................................................................................................... 1487.5. Analogue Input................................................................................................... 1507.6. Simulation Test .................................................................................................. 1517.7. Hardware Test .................................................................................................... 1527.8. Other PIC Demo Kits ........................................................................................ 1537.9. In-Circuit Debugging ......................................................................................... 1557.10. In-Circuit Emulation .......................................................................................... 157
Part 3 PIC Applications
Chapter 8: Application Design......................................................................... 161
8.1. Design Specification .......................................................................................... 1638.2. Hardware Design................................................................................................ 1658.3. Software Design................................................................................................. 1678.4. Program Implementation ................................................................................... 174
Chapter 9: Program Debugging ....................................................................... 181
9.1. Syntax Errors...................................................................................................... 1829.2. Logical Errors .................................................................................................... 1849.3. Test Schedule ..................................................................................................... 1919.4. Interactive Debugging........................................................................................ 1949.5. Hardware Testing ............................................................................................... 198
vi Contents
Chapter 10: Hardware Prototyping.................................................................. 201
10.1. Hardware Design................................................................................................ 20210.2. Hardware Construction ...................................................................................... 20310.3. Dizi84 Board Design ......................................................................................... 21110.4. Dizi84 Applications ........................................................................................... 213
Chapter 11: PIC Motor Applications................................................................ 233
11.1. Motor Control Methods ..................................................................................... 23411.2. Motor Application Board MOT2....................................................................... 23611.3. Motor Control Methods ..................................................................................... 23911.4. Test Programs for MOT2................................................................................... 24111.5. Closed Loop Speed Control............................................................................... 24311.6. Motor Control Modules ..................................................................................... 253
Part 4 Microcontroller Systems
Chapter 12: More PIC Microcontrollers ........................................................... 261
12.1. Common Features .............................................................................................. 26212.2. Device Selection ................................................................................................ 26812.3. Peripheral Interfaces .......................................................................................... 27512.4. Serial Ports ......................................................................................................... 278
Chapter 13: More PIC Applications ................................................................. 285
13.1. TEMCON2 Temperature Controller.................................................................. 28613.2. Simplified Temperature Controllers .................................................................. 30313.3. PIC C Programming........................................................................................... 304
Chapter 14: More Control Systems.................................................................. 311
14.1. Other Microcontrollers....................................................................................... 31214.2. Microprocessor Systems .................................................................................... 31514.3. Control Technologies ......................................................................................... 31914.4. Control System Design ...................................................................................... 329
Part 5 Appendices
Appendix A: Binary Numbers.......................................................................... 335
A.1. Number Systems ................................................................................................. 335A.2. Numerical Conversion ........................................................................................ 341A.3. Binary Arithmetic ............................................................................................... 344
Appendix B: Microelectronic Devices ............................................................... 349
B.1. Digital Devices.................................................................................................... 349B.2. Combinational Logic .......................................................................................... 353
Contents vii
B.3. Sequential Logic ................................................................................................. 357B.4. Data Devices ....................................................................................................... 359B.5. Simple Data System............................................................................................ 361B.6. Four-Bit Data System ......................................................................................... 362
Appendix C: Digital Systems........................................................................... 367
C.1. Encoder and Decoder.......................................................................................... 367C.2. Multiplexer, Demultiplexer and Buffer .............................................................. 368C.3. Registers and Memory........................................................................................ 371C.4. Memory Address Decoding................................................................................ 372C.5. Counters and Timers........................................................................................... 373C.6. Serial and Shift Registers ................................................................................... 374C.7. Arithmetic and Logic Unit ................................................................................. 375C.8. Processor Control................................................................................................ 376C.9. CPU System Operation....................................................................................... 376C.10. PIC16 MCU Operation ....................................................................................... 379
Appendix D: Dizi84 Demo Board .................................................................... 381
D.1. Circuit Design ..................................................................................................... 381D.2. Construction and Testing .................................................................................... 383D.3. Analogue Conversion.......................................................................................... 385D.4. EEPROM Storage ............................................................................................... 387D.5. LOCK Application.............................................................................................. 388
Appendix E: Dizi690 Demo Board .................................................................. 397
E.1. Circuit Design ..................................................................................................... 397E.2. Schematic Edit .................................................................................................... 398E.3. Program Edit ....................................................................................................... 401E.4. Circuit Simulation............................................................................................... 402E.5. PCB Design......................................................................................................... 406E.6. Package Assignment ........................................................................................... 407E.7. Layout Edit ......................................................................................................... 409E.8. Output Files......................................................................................................... 411E.9. PCB Fabrication.................................................................................................. 413
Answers to Questions...................................................................................... 417References and Links....................................................................................... 427Demo Files..................................................................................................... 429Index ............................................................................................................. 431
viii Contents
Preface to the 3rd Edition
p0010 The first edition of this book concentrated on a single chip that was widely used in hobby
electronics and education e the PIC� 16F84A. This has now been superseded by chips
that are faster, cheaper, and more complex and powerful. This has created a dilemma e
whether to still use this chip that is effectively obsolete or substitute a current chip that is
more complicated. In the end, I have done both e sticking with the ‘84 due to its relative
simplicity in the initial stages, and then moving on to more recent chips with extra
features, such as the 16F690. At the same time, I have taken advantage of simulation
software that is now available, which provides on-screen animated circuits and user-
friendly microcontroller program debugging.
p0015 All the main points in this book are illustrated by simple examples, which are downloadable
from the support website at www.picmicros.org.uk. Program source code can be modified,
reassembled and retested using Microchip’s MPLAB IDE development system, downloadable
free from www.microchip.com. This website also provides many of the technical references
and data sheets used in the book. The schematic capture and simulation software is from
Labcenter Electronics at www.labcenter.com. A demo version is available, but to create and
test your own applications, a license will be needed. A low-cost package, including a model for
the 16F84A, is currently available.
p0020 The book is aimed at beginners, so more experienced readers should skip over any bits that are
already familiar. Some basic principles have been moved to the appendices in this edition, to
make room for updated applications and examples. Again, these are aimed primarily at learners
at college or university, or independent hobbyists. Nevertheless, I hope that more experienced
readers will find some of the examples useful, and will perhaps see the advantages of some of
techniques described, particularly interactive simulations, which enrich the application
development experience at all levels, and potentially enhance productivity for the professional
electronic design engineer.
Martin BatesSussex, England, February 2011
ix
Introduction to the 3rd Edition
p0010 The microcontroller is now at the heart of many electronic products. Mobile
phones, microwave ovens, digital television, credit cards, the Internet and many other current
technologiesrely on these small, unobtrusive devices to make it all happen.
p0015 This book is an attempt to introduce the beginner to this ubiquitous yet complex technology.
Starting with the standard PC (on the basis that most people are familiar with its operation), the
basic concepts and terminology will be established: microprocessor systems, memory, input
and output and general digital systems ideas. We will then go on to study one of the biggest
selling products the general public has never heard of: the PIC� microcontroller (MCU). It
dominates the market for small-scale industrial applications, with the manufacturer Microchip
Inc. currently reporting annual sales over US $1 billion per annum.
p0020 We will start by studying a chip that is no longer commercially significant, but is relatively
simple, with the minimum of advanced features: the PIC 16F84A. This was one of the first
popular small microcontrollers with flash program memory, the kind found in memory sticks.
This allows it to be easily reprogrammed and therefore made it ideal for learning and hobby
electronics. We will learn how to connect up and program this chip, and design simple
applications, such as flashing output LEDs. In addition, simulation software will be
introduced, which makes the design process easier, and more fun. We will then move on to
the PIC 16F690, which has more features, and is representative of more recent products in the
PIC range. Many microcontrollers used in real applications such as motor vehicle engine
control or communications systems are more powerful, but the operating principles are just
the same. Other types of control technology will be reviewed for comparison with
microcontrollers.
p0025 The book uses numerous examples relating to motor control, because this is a very common
control requirement (used in disk drives, washing machines, conveyors, etc.). The small direct
current motor is inexpensive and can be easily connected to the output of a PIC via a simple
current driver interface. The response of the motor is easily observed, yet can be complex,
which demonstrates the problems associated with real-time system control. The motor also
provides a link to wider areas of engineeringemechatronics, robots, machine tools and
industrial systemsethat is useful for students and engineers in these disciplines.
xi
p0030 The big problem with microprocessors and microcontrollers is that, to fully understand how
they work, we have to understand both the hardware and the software at the same time.
Therefore, we have to we have to circle round the subject, looking at the system from different
angles, until a reasonable level of understanding is built up. The book will cover basic
hardware design, interfacing, program development, debugging, testing and analysis using
a range of simple examples. This is supported by appendices, which introduce basic concepts
to readers who do not have this essential background e number systems, digital principles and
microprocessor system concepts, as well as system design exercises. Appendix E covers the
whole design process using the Proteus VSM electronic design suite.
p0035 There is a summary at the start of each chapter, so that its content can be seen at a glance, as
well as a set of questions at the end for self-assessment or formal testing of students (with full
answers at the end of the book) and suggested activities which can be developed into practical
assessments if required. The style of the book is also intended as a model for students who need
to write technical reports for such practical assessments. The stages of application development
should be clearly identified in this case: specification, design, implementation and testing.
Another useful model can be seen in the application notes written by professional engineers,
such as those available on the Microchip website.
p0040 The content of each chapter is a compromise between maintaining overall continuity and
allowing each chapter to be read independently. There will therefore be a certain amount of
repetition between chapters, which I hope the reader will not find too irritating, and may aid
learning. It is always difficult to decide exactly what to include in this kind of book, where the
subject is vast and complex. My intention is always to keep it simple, and I hope my selection
will help the reader to begin to get to grips with the fascinating world of microcontrollers, and
go on to a reasonable understanding of real applications, progressing perhaps to a career in
microcontroller application design. However, an understanding of microcontrollers is essential
for any electrical engineer, since the technology is now central to most electronic products and
industrial systems.
xii Introduction to the 3rd Edition