ThisPageisIntentionallyLeftBlank www.engbookspdf.com PIC Microcontrollers An Introduction to Microelectronics Second Edition Martin Bates AMSTERDAM • BOSTON • HEIDELBERG • LONDON NEW YORK • OXFORD • PARIS • SAN DIEGO SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO Newnes is an imprint of Elsevier www.engbookspdf.com Newnes An imprint of Elsevier Linacre House, Jordan Hill, Oxford OX2 8DP 200 Wheeler Road, Burlington, MA 01803 First published 2000 by Arnold Second edition 2004 Copyright © 2004, Martin Bates All right reserved Appendix A has been reprinted with permission of the copyright owner, Microchip Technology Incorporated © 2001 All rights reserved No further reprints or reproductions may be made without Microchip Technology Inc.’s prior written consent Information contained in this publication regarding device applications and the like is intended as suggestion only and may be superseded by updates No representation or warranty is given, and no liability is assumed by Microchip Technology Inc with respect to the accuracy or use of such information, or infringement of patents arising from such use or otherwise Use of Microchip Technology Inc products as critical components in life support systems is not authorized except with express written approval by Microchip Technology Inc No licenses are conveyed implicitly or otherwise under any intellectual property rights The right of Martin Bates to be identified as the author of this work has been asserted in accordance with the Copyright, Designs and Patents Act 1988 No part of this publication may be reproduced in any material form (including photocopying or storing in any medium by electronic means and whether or not transiently or incidentally to some other use of this publication) without the written permission of the copyright holder except in accordance with the provisions of the Copyright, Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licensing Agency Ltd, 90 Tottenham Court Road, London, England WIT 4LP Applications for the copyright holder’s written permission to reproduce any part of this publication should be addressed to the publisher Permissions may be sought directly from Elsevier’s Science & Technology Rights Department in Oxford, UK: phone: (+44) 1865 84830, fax: (+44) 1865 853333, e-mail: permissions@elsevier.co.uk You may also complete your request on-line via the Elsevier homepage (http://www.elsevier.com), by selecting ‘Customer Support’ and then ‘Obtaining Permissions’ British Library Cataloguing in Publication Data A catalogue record for this book is available from the British Library Library of Congress Cataloguing in Publication Data A catalogue record for this book is available from the Library of Congress ISBN 7506 6267 For information on all Newnes publications visit our website at http:// books.elsevier.com Typeset in 10/12 pt Times by Integra Software Services Pvt Ltd, Pondicherry, India www.integra-india.com Printed and bound in Meppel, The Netherlands by Krips bv www.engbookspdf.com Contents Preface to the First Edition x Preface to the Second Edition xii Introduction xiii PART A MICROELECTRONIC SYSTEMS Chapter Computer Systems 1.1 The PC System 1.2 Wordprocessor Operation 1.3 PC Microprocessor System 1.4 PC Engineering Applications 1.5 The Microcontroller Summary Questions Activities Chapter Information Coding 2.1 Number Systems 2.2 Machine Code Programs 2.3 ASCII Code Summary Questions Answers Activities www.engbookspdf.com 3 11 14 15 18 18 19 20 20 25 28 29 29 30 30 vi Contents Chapter Microelectronic Devices 3.1 Digital Devices 3.2 Combinational Logic 3.3 Sequential Logic 3.4 Data Devices 3.5 Simple Data System 3.6 4-Bit Data System Summary Questions Activities Chapter Digital Systems 4.1 Encoder and Decoder 4.2 Multiplexer, Demultiplexer and Buffer 4.3 Registers and Memory 4.4 Memory Address Decoding 4.5 System Address Decoding 4.6 Counters and Timers 4.7 Serial and Shift Registers 4.8 Arithmetic and Logic Unit 4.9 Processor Control Summary Questions Answers Activities 32 32 36 39 41 43 44 47 47 48 49 49 51 51 51 54 55 56 57 58 59 59 60 60 Chapter Microcontroller Operation 61 5.1 Microcontroller Architecture 5.2 Program Operations Summary Questions Answers Activities 61 65 73 73 74 75 PART B THE PIC MICROCONTROLLER Chapter A Simple PIC Application 6.1 6.2 6.3 6.4 Hardware Design Program Execution Program BIN1 Assembly Language www.engbookspdf.com 77 79 79 83 85 87 Contents vii Summary Questions Answers Activities 90 90 91 91 Chapter PIC Program Development 7.1 Program 7.2 Program 7.3 Program 7.4 Program 7.5 Program 7.6 Program 7.7 Program 7.8 Program Summary Questions Answers Activities Design Editing Structure Analysis Assembly Simulation Downloading Testing Chapter PIC 16F84 Architecture 8.1 Block Diagram 8.2 Program Execution 8.3 Register Set Summary Questions Activities Chapter Further Programming Techniques 9.1 Program Timing 9.2 Hardware Counter/Timer 9.3 Interrupts 9.4 More Register Operations 9.5 Special Features 9.6 Program Data Table 9.7 Assembler Directives 9.8 Special Instructions 9.9 Numerical Types Summary Questions Answers Activities www.engbookspdf.com 92 94 96 101 101 105 109 112 114 115 115 116 116 117 117 119 120 126 127 127 129 129 131 135 140 144 148 150 153 154 155 155 156 156 viii Contents PART C APPLICATIONS Chapter 10 Application Design 10.1 Design Requirements 10.2 Block Diagram 10.3 Hardware Design 10.4 Software Design 10.5 Program Implementation 10.6 Source Code Documentation Summary Questions Activities Chapter 11 Program Debugging 11.1 Syntax Errors 11.2 Logical Errors 11.3 MPLAB Tools 11.4 Test Schedule 11.5 Hardware Testing Summary Questions Activities Chapter 12 Prototype Hardware 12.1 Hardware Design 12.2 Hardware Construction 12.3 Demo Board 12.4 Demo Board Applications Summary Questions Activities Chapter 13 Motor Applications 13.1 Motor Control Methods 13.2 Motor Applications Board 13.3 Control Methods 13.4 Position Control 13.5 Closed Loop Speed Control 13.6 Commercial Application Summary Questions Activities www.engbookspdf.com 157 159 160 162 162 164 171 174 175 175 176 177 177 179 183 184 186 189 189 189 191 191 192 196 200 210 210 211 213 213 214 218 219 221 231 232 232 233 Contents ix PART D MORE CONTROLLERS Chapter 14 More PIC Microcontrollers 14.1 Common Features of PIC Microcontrollers 14.2 Selecting a PIC 14.3 Advanced PIC Features 14.4 Serial Communications Summary Questions Activities Answers Chapter 15 More PIC Applications and Devices 15.1 16F877 15.2 16F818 15.3 12F675 15.4 18F452 Summary Questions Answers Activities Application Application Application Application Chapter 16 More Control Systems 235 237 237 242 244 251 254 255 255 255 256 256 273 274 275 278 278 279 279 280 16.1 Other Microcontrollers 16.2 Microprocessor System 16.3 Control Technologies 16.4 Control System Design Summary Questions Activities 280 282 288 298 299 301 301 Appendix A PIC 16F84 Data Sheet 302 Appendix B DIZI-2 Board and Lock Application 347 Index 367 www.engbookspdf.com Preface to the First Edition The Microchip™ PIC 16F84 microcontroller is an unremarkable looking 18-pin chip – so why write a whole book on it? The answer is that it contains within its ordinary looking plastic case most of the technology that students of microelectronics need to know about in order to understand microprocessor and computer systems It also represents a significant new development in microelectronics and, importantly, it offers an easier introduction to the world of digital processing and control than conventional microprocessors The microcontroller is a self-contained, programmable device, and the student, hobbyist or engineer can put it to use without knowing in too much detail how it works On the other hand, we can learn a great deal about microelectronics by looking inside Studying the PIC chip will give the user a valuable insight into the technology behind the explosion in microprocessor-controlled applications which has occurred in recent years, which has been based on cheap, mass-produced digital circuits Mobile phones, video cameras, digital television, satellite broadcasting and microwave cookers – there are not many current electronic products which not contain some kind of microprocessor Industrial control systems have seen similar developments, where complex computer control systems have steadily increased productivity, quality and reliability The key, of course, is the increase in power of microprocessors and related technology, while the cost of these clever little chips continues to fall The microcontroller is essentially a computer on one chip, which can carry out a complex programmed sequence of actions, with the minimum of additional components As an example, in this book a motor control circuit will be described which allows the motion of a small dc motor to be programmed and controlled by the PIC chip The only additional major components required are power transistors to provide the current drive to the motor In the past, equivalent control and interface circuits for such an application would have required many more components, and been much more complicated and expensive to design and produce The small microcontroller also makes it easier for a device such as a motor to be individually controlled as part of a larger system When I first came across the PIC chip a few years ago, it was immediately obvious that this would be an ideal device for teaching and learning microprocessor software techniques, especially for students with minimal prior knowledge and skills It is relatively cheap, and, even better, it has non-volatile program memory that is electrically reprogrammable (Flash ROM) In addition, the manufacturers, Arizona Microchip, had the foresight to make development system software required to develop programs for the chip widely available Packages are available for DOS and Windows, and the support hardware and software are being added to all the time, by the manufacturers, independent suppliers and enthusiasts On the other hand, a complete set of more powerful development tools is also available for the professional user Both DOS and Windows versions of the PIC development system have been used to prepare the sample applications in this book, and the programs downloaded using the PICSTART-16B www.engbookspdf.com