Interfacing PIC Microcontrollers Interfacing PIC Microcontrollers Embedded Design by Interactive Simulation Martin Bates AMSTERDAM(cid:129)BOSTON(cid:129)HEIDELBERG(cid:129)LONDON NEWYORK(cid:129)OXFORD(cid:129)PARIS(cid:129)SANDIEGO SANFRANCISCO(cid:129)SINGAPORE(cid:129)SYDNEY(cid:129)TOKYO NewnesisanimprintofElsevier Newnes is an imprint of Elsevier TheBoulevard, Langford Lane, Kidlington, Oxford OX5 1GB, UK 225WymanStreet, Waltham, MA 02451, USA Firstedition 2008 Secondedition2014 Copyright r 2014 MartinBates andElsevierLtd. All rights reserved No part ofthis publicationmay bereproduced, stored ina retrieval system, or transmitted inany formor byany means, electronic,mechanical, photocopying, recording, orotherwise, withoutthe prior written permission ofthe publisher. Permissionsmay be sought directlyfrom Elsevier’sScience & Technology Rights Department inOxford, UK: phone (144)(0) 1865843830; fax (144)(0) 1865 853333; email:[email protected]. Alternatively you can submityourrequest online by visitingthe Elsevierweb site at http://elsevier.com/locate/permissions, andselecting Obtainingpermission to use Elseviermaterial. Notice No responsibility is assumed by the publisher for anyinjuryand/or damage topersons or property asa matter of products liability, negligence orotherwise, orfrom any use oroperation ofany methods,products, instructions or ideas containedinthe material herein.Because ofrapid advancesinthe medical sciences, inparticular, independent verification of diagnoses anddrug dosages should be made. British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library. Library of Congress Cataloging-in-Publication Data A catalog recordfor this book is availabe from the Library ofCongress. ISBN: 978-0-08-099363-8 Forinformation onall Newnespublications visit ourWeb site at store.elsevier.com Typeset byMPS Limited,Chennai, India www.adi-mps.com Printed inthe UK 14 15 16 17 10 9 8 7 6 8 4 3 2 Preface The PIC is one of the biggest selling small microcontrollers. When it first became available, it was not only technically innovative, but helped to make the teaching of microelectronics much more interesting. A small controller with flash memory meant that a great variety of student projects could be realised quickly and easily. It helped that the development toolkit was free as well. It has always been a problem in electronics that you cannot see a circuit working in the same way that a mechanical engineer can see a steam engine pumping up and down. Sure, we can see the screen flickering on a television, or an electric motor spinning, but you cannot see electrons or volts directly. As a result, it has always been that bit more difficult to learn electronics. Interactive electronic design software is the answer. The Proteus VSM (Virtual System Modelling) software used in this book has been developed by Labcenter Electronics in the UK. It brings circuits to life on the computer screen and makes learning electronics more effective and more fun. It is also a full-scale professional product, and will take the student electronic engineer seamlessly into commercial design work. This book is intended to support electronic learning wherever it takes place, at college, at work or in the home. Please enjoy! Martin Bates March 2013 xiii Introduction This book is the second edition of the sequel to ‘PIC Microcontrollers, an Introduction to Microelectronics’, which attempted to provide introduction to the subject via a single type of microcontroller. It explores the basic techniques for connecting the PIC to peripheral devices and the outside world. It shows how to connect simple input and output devices, such as switches, sensors, displays and motors, as well as demonstrating communication methods that allow the PIC to communicate data with other devices, including intelligent sensors. The second edition has been extensively revised, updated and expanded. A domestic inkjet printer is an example of a product that contains a range of sensors, drives and displays. It typically has a wireless data link to receive the page data, at least two motors to feed the paper and position the print head, and a microcontroller to output the signals to the print cartridge inkjets and generally coordinate the action. Take an old one apart and have a look! Another good example is the digital camera. In fact, most small electronic products contain a microcontroller that provides its core functions. A smoke detector with a PIC microcontroller is shown in Figure I.1. r The PIC microcontroller was the first widely available device to use flash memory, which made it ideal for prototyping and experimental work. Flash memory, as used in memory cards and sticks, allows the application program to be replaced quickly and easily with a new version. Cheap flash memory microcontrollers have also transformed the teaching of microelectronics (cid:1) they are re-usable and the internal architecture is fixed, making them easier to understand. The small instruction set of the PIC is also a major advantage (cid:1) there are only 35 instructions to learn in the main microcontroller unit (MCU) used in this book. r r The free development system MPLAB provided by Microchip Inc. is another reason for using the PIC range. In addition to the program editor and project management features, it includes a text-based simulator which allows the program to be tested prior to downloading, potentially saving a lot of time debugging in hardware. However, this only tests the program itself, not the circuit in which it is connected. xv xvi Introduction Figure I.1 Smoke detector withPIC controller. r r Proteus from Labcenter Electronics allows a PIC to be simulated in circuit. It consists of two main parts, ISIS and ARES. ISIS is the schematic capture and interactive simulation package used to create the circuit schematic and to test the circuit prior to building the real hardware. On-screen buttons and virtual signal sources provide inputs to the circuit. Output (analogue or digital) can be displayed on a signal probe, a virtual instrument or graph. An MCU can be dropped on the screen, the circuit drawn, a program attached and tested immediately on screen. When the application is working correctly in simulation mode, a PCB can be designed by exporting a netlist (list of components and connections) from ISIS into the ARES layout package. The resulting PCB files can be output to a production system or sent to a specialist manufacturer. The final stage is then to assemble the board and test the hardware. After using Proteus VSM, it should work first time! This book is built around particular devices and tools, because it allows specific examples to be used. It is assumed that at a later stage, with more experience, the reader will be able to evaluate these against competing products and choose the most appropriate for any given design task. Each topic is illustrated by designs based on the well-established PIC 16F877A, but it will be replaced in the readers’ own designs with a more recent device such as the 16F887 chip. All the circuits are available on the support website www.picmicros.org.uk. All schematics were produced using ISIS (cid:1) and you can produce them to the same standard in your Introduction xvii Host development software Programmer connector Prototyping target board Prototype Programmer circuit module components Figure I.2 PICDEMdevelopmentkit. Courtesy of Microchip Inc. own reports. The microcontroller models can be purchased in packages for institutional or professional use from www.labcenter.com. Currently, a Proteus starter kit including models for the PIC 16F84A, 16F877A and 18F452 can be purchased for only d150. Microchip provides an extensive range of demonstration and development kits to support their microcontroller product range (cid:1) see www.microchip.com for all product details. A basic development kit is illustrated in Figure I.2, which consists of a prototyping target board, a selection of hardware components including some small PIC chips, programmer module and development system software that is loaded onto a host PC. A circuit is built on the prototyping area, for example a motor interface, and a suitable PIC chip fitted into one of the sockets on the board. The motor control program can be written in MPLAB on the host computer, debugged in MPSIM, the Microchip simulator, downloaded to the target board and the hardware tested. However, it is preferable to test the application before constructing the hardware, in case changes are needed. It is much quicker and easier to change the circuit, or the program, on screen, rather than in hardware. ISIS allows the program to be entered, assembled and attached to the on-screen chip for interactive testing within the virtual circuit. A typical simulation screen is shown in Figure I.3. The book is structured in three parts. Part 1 reviews PIC microcontroller architecture and programming, Part 2 introduces PIC interfacing techniques and Part 3 covers PIC system design and implementation. xviii Introduction FigureI.3 Base board simulation screenshot. In Chapter 1, a standard PIC microcontroller, the 16F877A, is described in detail, based mainly on its data sheet. Chapter 2 outlines the programming process, using only MPLAB tools, and Chapter 3 describes the application development process using Proteus VSM. In Chapter 4, basic input and output devices are introduced, while Chapter 5 describes the techniques for data representation and conversion, Chapter 6 covers all aspects of analogue signal conditioning and, in Chapter 7, power output interfacing is introduced, concentrating on motor drives. Chapter 8 outlines the operation of the main PIC serial communication ports, and Chapter 9 describes a wide range of sensors for monitoring and measurement applications. Chapter 10 concludes with a consideration of MCU selection and the principles and practice of system design. The book was originally designed to support project development by students at all levels. It may therefore sometimes state what is obvious to more experienced engineers; hopefully this is not too irritating, and they too will find something of interest within! Links and Acknowledgements Support Website www.picmicros.org.uk Author’s website with related PIC books and application file downloads Follow the link to demo applications in INTAPPS2.ZIP containing: (cid:129) VSM project file app2.pdsprl (cid:129) PIC source code app2.asm (in MCU folder) (cid:129) PIC debug file debug.cof (in MCU folder) Labcenter Electronics www.labcenter.com Manufacturer and supplier of Proteus VSM electronic design system (cid:129) Download demo version of VSM (cid:129) Purchase MCU package licence (cid:129) Tutorials and product information Microchip Technology Inc. www.microchip.com Manufacturer of the PIC microcontroller range and MPLAB IDE (cid:129) Download data sheets (cid:129) Information on development tools (cid:129) Download MPLAB development system Custom Computer Services Inc. www.ccsinfo.com Manufacturer and supplier of PIC CCS ‘C’ Compilers Please search online by name for product datasheets other than Microchip. Use of all manufacturers’ trademarks and data is gratefully acknowledged. Thanks in particular to Iain Cliffe at Labcenter Electronics. xix CHAPTER 1 PIC Hardware Summary (cid:129) The microcontrollercontains aprocessor, memoryand input/output devices (cid:129) The program is stored in flash ROM memoryin numbered locations (cid:129) The P16F877A family uses only 35instructions (cid:129) The P16F877A stores a maximum8k 3 14 instructions inflash ROM (cid:129) The P16F877A has 368 bytesof RAM and 5 ports(33 I/O pins) (cid:129) The program is executed insequence, unless there is ajump instruction (cid:129) The program counter tracks the current instruction address (cid:129) Aconfiguration word is need toselect the clocktype andotherchip options (cid:129) The program source code (.ASM) is assembled into machine code (.HEX) (cid:129) Machine code is downloadedto the chip and the application hardware tested The microcontroller is a complete computer on a chip. When introduced, it was one of the most significant developments in electronics since the invention of the microprocessor itself and is essential in the operation of such devices as mobile phones, DVD players, video cameras, and most self-contained electronic systems. Working sometimes with other chips, but often on its own, the microcontroller unit (MCU) provides the key element in the vast range of small, programmed devices that are now commonplace. Although small, microcontrollers are complex, and we have to look carefully at the way the hardware and firmware (control program) work together to understand the processes at work. This book will then show how to connect the popular PIC range of microcontrollers to the outside world and put them to work. To keep things simple, we will concentrate on one device, the PIC 16F877A, which has a good range of features that allows most of the essential techniques to be explained. It has a set of serial ports built in that are used to transfer data to and from other devices, as well as analogue inputs, which allow measurement of inputs such as temperature. All microcontrollers work in a similar way, so analysis of the PIC MCU will go a long way to understanding all such devices. This chip has been around some time and is no longer the best choice for new designs. The 16F887 is a more recent equivalent and should be used as a pin compatible InterfacingPICMicrocontrollers. DOI:http://dx.doi.org/10.1016/B978-0-08-099363-8.00001-7 3 ©2014MartinBatesandElsevierLtd.Allrightsreserved.