Docsity

PIC Microcontrollers - 50 Projects for Beginners and Experts, Manuais, Projetos, Pesquisas de Eletrônica

Pré-visualização parcial do texto

Baixe PIC Microcontrollers - 50 Projects for Beginners and Experts e outras Manuais, Projetos, Pesquisas em PDF para Eletrônica, somente na Docsity!

PIC Microcontrollers 50 Projects for Beginners and Experts Bert van Dam Qlektor 7 PIC Microcontrollers fia 50 Projects for Beginners and Experts Author: Bert van Dam This hands-on book covers a series of exciting and fun projects, such as: e silent alarm e people sensor e radar * night buzzer e clock * VU meter * RGB fader e serial network e poetry box * sound super-compression You can use it as projects book and build more than 50 projects for your own use. The clear explanations, schematics, and pictures of each project on a breadboard make this a fun activity. You can also use it as a study guide. The technical background information in each project explains why the project is set up the way it is, including the use of datasheets. This way you" learn a lot about the project and the microcontroller being used, and you can expand the project to suit your own needs...making it ideal for use in schools and colleges. This book can also be used as a reference guide. The explanation of the JAL program- ming language and all of the expansion libraries used is unique and found nowhere else. Using the index, you can easily locate projects that serve as examples for the main commands. But even after you've built all the projects it will still be a valuable reference guide to keep next to your PC. Four microcontrollers are discussed, the 121675, 16f628, 16f876A, and 161877, as well as how to migrate programs from one microcontroller to another. AM software used in this book can be downloaded for free, including all of the source code, a program editor, and the JAL open source programming language. This powerful and vet easy to learn language is used by hobbyjists and professionals worldwide. A hardware kit is also available for purchase separately that contains all the parts to get you started, including a few microcontrollers. There is even a free support website with additional information, FAQ, and links. ISBN: 978-0-905705-70-5 Elektor Electronics ektor www.elektor-electronics.co.uk ad AI rights reserved. No part of this book may be repraduced in any material form, including Photocopying, or storing in any medium by clectronic means and whether or not transiently or incidentaily to some other use of this publication, without the written permission of the copyright holder except in accordanee with the provisions of the Copyright, Desigos and Patenis Act 1988 or under the terms of a licence issucd by the Copyright Licensing Agency Ltd, 90 Tottenham Court Road, London, England WIP 9HE. Applications for the copyright holder's written permission to reproduce any part of this publication should be addressed to the publishers. The publishers have used their best efforts in ensuring the correctness of the information contained in this book. They do not assume, and hereby disclaim, any ility to any party for any loss or damage caused by errors or omissions in this book, whether such errors or omissions result from negligence, accident or any other cause. British Library Cataloguing in Publication Data A catalogue record tor this book is available trom the British Library ISBN 978-0-005705-70-5 NUR 980 Translation: Bert van Dam Prepress production: Autronic, Blaricum Design cover: D-Vision, Oss First published in the United Kingdom 2007 Second edition November 2008 Printed in lhe Netherlands by Wilco, Amerstoort & Elektor international Media BV 2007 0690 1 n/UK Content INTRODUCTION 8 1 WHAT IS A PIC” MICROCONTROLLER? 9 2 WHAT YOU WILL NEED E] 2.1 NECESSARY ITEMS NH 2.2 OPTIONAL. ITEMS (NICE TO HAVE) 20 3 TUTORIAL PROJECT 25 SL THE HARDWARE 25 3,2 THE SOFTWARE 32 3.3 COMPILING AND DOWNLOADING 36 3.4 DEBUGGING 37 3.5 DONE! 39 3.6 OTHER RESULTS 39 4 SWITCHES 42 4.1 TIMER 42 4.2 TWO-WAY SWITCH 50 4.3 Dicr s3 4.4 SECRET DOORBELL 57 5 A/D CONVERSION 73 5.) VARIABLE SPEED 1 ,ASHING LED 74 5.2 DARK ACTIVATED SWITCH 78 5,3 VOLTMETER so 5.4 PHOTUMETER WITH LCD DISPLAY aa 5.5 SAMPLING 91 6 R$232 COMMUNICATION 97 6.1 SERIAL COUNTER 98 6.2 SILENT ALARM 101 6.3 IN-CIRCUIT DEBUGGING tos 6.4 SERIAL HARDWARE. 14 7 ELECTRIC MOTOR 120 7.1 ELECTRIC MOTOR CONTROL (pio) 7.2 PULSE WIDTII MODULATION CONTROI. OF AN ELECTRIC MOTOR 126 7.3 CONSTANT MOTOR SPEED THROUGH FEEDBACK 136 7A TACHOMETER 146 13.1.4 Railroad crossing 13.1.5 RGB fader 13.2 THE [6F628 13.2.2 16F628 bert library 13.2,3 Demo program 13.2.4 Electric candle 13.2.5 Adjustable clock frequency 13.2.6 Upgrade using HEX view 13.3 THE I6FR76A 13,3,2 I6F876A bert library 13,3.3 Demo program 13,3.4 VU meter 13.3.5 Infrared RS232 13.4 PORTADILITY 14 APPENDIX 14.1 TAL 14.1.1] General 14.1.2 Syntax 14.2 165877 BERT LIBRARY [4.3 OTHFR LIBRARIES 14,4 PROGRAMMER PASS-THROUGH 4.4.1 MICterm communication program 14.4,2 DIY software in Visual Basic 14.4.3 Terminal program using a batch file 14.5 TRANSISTOR DATA 14.6 CONTENTS OF THE DOWNLOAD PACKAGE 14.7 HEX FILES 14.8 Tip$ AND TRICKS Index 351 354 aso 362 363 366 369 373 374 376 37” 380 382 388 39 391 391 392 4” 417 420 421 422 426 a2s 432 437 439 442 Introduction This book covers a series of exciting and fun projects such as a silent alarm, a people sensor, radar, a night buzzer, a clock, a VU meter, an RGB fader, a scrial network, and à poetry box, You can use it as projects bouk and build more than 50 projects for your own usc, The clear explanations, schematics, and pictures of each project on a breadbnard make this a fun activily. You can also use it as a study guide. The technical background information in cach project explains why the project is set up the way it is, including the usc of datasheets. This way you'll lean a lot about the project and the microcontroller being used, and you can expand the project to suit your own needs. For this reason most of the projects use the same microcontroller, the PIC"? 16F877. In Chapter 13, threc other microcontrollers are discussed, the 12F675, 16F628 and 16F876A. Also explained is how to migrate programs from one microcontroller to another. Apart from that the book can be used as a reference guide. The explanation of the JAL programming language and all of the expansion libraries uscd in the hook is unique and nowhere else to be found. Using the index, you can easily locate projects that serve as examples for the main commands. Even after you"ve built all the projects in this book it will still be a valuable reference guide to keep next to your PC. At this point 1 would like to thank Stef Mientki and Wouter van Ooijen tor their help with the techmical editing of this book, and also for their support many years ago when 1 first got my feet wet in the exciting world of microcontrollers. For the English version 1 would like to thank Mike Kerna and Kyle York for their help with the final editing Bert van Dam 1 What is a PICTM micracontraller? background is explained in detail. This allows you to understand how and why these projects work. so that you can easily adapt them to your own needs and ideas. The book starts very simply with a tutorial project and step-by-step instructions. As vou go along the projecis increase in difficulty and only the new concepts are explained. By the end of the book you will no longer be a beginner! Chapter 14 contains a complete and unique overview of all of the commands of the JAL programming language and the expansion libraries used. You won't find this anywhere else! You can use it as reference guide, but also to find fun and useful commands and options. All of the software used in the book is cxplaincd and shown. To avoid having to type in the programs yourself, they arc included along with the other software needed in a complete package, which you can download for free at http://www .boekinfo.tk, To get started quickly, a hardware kit is also avaitabie. It contains all of the parts needed for the first few projects. The kit is designed for your convenience, but of course you can also obtain and use your own parts. More information on ordering and international shipping can be found at http://www.boekinfo.tk. AN of the projects in this book are built on a breadboard. This allows you to easily build and expand them before you design a printod circuit board. It is more fun to see the projects in this book as a starting point for something that suíts your own needs rather than finished projects. Okay, enough talk... let's get started syilh our First PIC!M microcontroller project! 10 2 What you will need This chapter covers all of the materials needed to start with this fascinating hobby. And there are many different options to choose from. Other choices may work just as well as the ones chosen here. What's important is that the selected materials work together perfectly. The Wisp628 programmer, Xwisp2, JALedit IDE, JAL v2 and the 16F877 bert library are just such a combination; a perfect fit, giving you many hours of fun. Of course you also need this book - preferably your own copy - so you can make notes and even write in the book. Your own experiences and notes about the parts that you use will make this book even more valuable. Writing in a new book for the first time may not be easy, but it gets easier in time. A lot of the software (at least the most important software) can be downloaded from the website http://www.boekinfo.tk in the section “download packet”. You'll also find information on a hardware kit that you can buy, links to suppliers, and lots of other information. 2.1 Necessary items Breadhoard Unless you're the type that goes for the soldering iron immediately it is a wise idea to use a breadboard. This will allow you to build and modify projects quickly, making it ideal for prototyping. Programmer Figure 2. Breadboard with decoupling capacitors. To prevent power issues, decoupling capacitors of 0.1 uF arc inserted on the power strips at the corners. In one of the comers a stabilizing capacitor of 22 uF is used, as well as a small LED (reverse mounted), which serves as a limited safety diode against an accidental reversed power connection. n 2.1 Necessary items How much “sufficient” is depends on your project. Several hundred milliamps is usually enough. If you use more power it is advisable to equip the UA7805 with a heat sink to provide cooling. Programmer (Wisp628) Once you have written a program it needs to be transferred into the PIC", This is the task of a programmer. Some programmers can program a PICTM while it is still in the circuit on the breadboard. This is called “in-circuit programming”. Trust me, you really, really, REALLY want this feature. It gets annoying very quickly when you have to wriggle the PIC!” off the breadboard each time you forgot a comma in your program. We'll be using the Wisp628, an intelligent programmer than can handle a wide array of different PIC!” microcontrollers. The intelligence of this programmer is, by the way, contained in its own PIC"”, so you can make your own updated version of the program software if needed. Also important is that this programmer has a pass-through feature. Without touching as much as a single wire you can communicate from your PC directly with the PIC!” This makes it ideal for debugging your software. Figure 4. The Wispó28 in-circuit programmer. PICTM (16F877) Which PIC!M to choose is a difficult question. There are dozens of different types, each with different options and cost. For the majority of the projects in this book the 161877 is used. This 16F877 is loaded with possibilities and features, and has many pins. This is an advantage because it means fewer combined pins are used, which is always a difficulty for starting programmers. Besides, it's a popular microcontroller, which means a lot of information (and thus help) is available on the Web. A disadvantage is that this PIC!” is relatively expensive, US$ 7 to US$ 11. 13 2 What you will need Figure 5. The 16F877 with 40 pins. At this point the specifications probably don”t mean too much yet: Item Specification Program memory 8192 words (14 bit) RAM size 368 bytes EEPROM size 256 byte V'O pins 33 Analog inputs 8 (10 bit cach) USART Yes (c6/c7) Speed 5 mips Cost US$7to 11 pC yes (c3/c4) When you order this microcontroller you will need to know which package you want. This doesn't refer to the box the microcontroller is shipped to you in, but to the external connections of the chip. For instance, whether the PICIM should have long pins that fit into a breadboard or printed circuit board, or small pins for surface mounting. The package shown in Figure 5 is called PDIP (or Plastic DIP), and this is what we'll be using. 14 2 What you will need Library (16F877 hert) Libraries contain additional functionality thar can be added to JAL. You can, for cxample, make serial connections, read an analog signal, and much more. In this book, the standard library 16F877 beré is used. It is basically a combination of the most used libraries off the Internet.” that all fit together perfectly, and is included as part of the download package. By using this standard library a lot of additional functionality is made available: Functionality (with the standard library) 16F377 Serial communication - hardware Y Serial communication - soliware Pulse Width Modulation (PWM) A/D conversion Program memory EEPROM memory ASCI Delay Registers and variables Asas asa as Random numbers Because part of the library functionality is automalically loaded, a few of the pins are assigned by default. If you want to use these pins for another purpose you will need to modify the standard library. This will rarely be necessary. FA silly name for a library. When 1 made the first one many years ago | called it bert to distinquish it from the libraries T got off the Intemet. The library became much more popular than | imagined, and since they are so well know | can't get nid of the name anymore! * Mainly written hy JAL expert Ste Mientki. 16 2.1 Necessary ilems This is the default configuration: 16E877 pins Function 13 and 14 crystal 16 PWM(C) 17 PWM(L) 25 RS232 hardware TX 26 RS232 hardware RX 39 R$232 software RX 40 R$232 software TX 2to Sand? to 10 analog in (adaptablc) other pins digital in/out The analog pins 2 to 10 can be switched to digital using a command from the library (see section 14.2). Editor (JALedit) You can use JAL from the command line, but an editor is much more convenient. The recommended editor is JALedit. This freeware editor has a number of unique advantages: TAL cammands are color coded Overview of variables, constants, and procedures used in a program Block comment add vr remove One click compile and download Builtin ASCII table, calculator, and terminal Source export to html (for publication on a website) Automatic backup Compiler output in a separate window Load multiple files at the same time * Jump to offending program line on error y 2.1 Necessary items Fle Help Bent van Dano [ej2o06 pr foton PO & Coml € Con [O Com? € tamA PIC comem speed é 2 cu css About fe) 2006 Bar van Dam Cau [0] Version 32 J 115715115 115 115 115 115 115 115115715 113946246 TE 0000000 UU0D0000900NICONNO] 41222 42 405 5559 64 72 8 5 ES BS 85 6685 97 [06719 129 141 154 154 173 190 188 198 2/0 224 232 258 225221 215 211 209 198 196 197 18 176 166 158 148 [2013] 122 118123139 141 150 156 154 188 172 169 151 [200 202 136 178 12 150 139 134 133 135 144 152 160 171 E NS 161 15 140146 147 140 AD HEI MG 150 [162 169 173 175 175 160 161 150 141 1497140 140 145155 [163 17] 175 179 180 177 171 164 185 748 143139 195135 34 135139 196 14515 162 167 172175 177 177 176171 5 816 142 6 Não 12 Ze VT ISA TES à 6 es 15 16 9 163 66 18 16810 00 00 200 20 200 200 200 200 200. 152189 Ui 185199 153 193 118 105 10ê 102 103 702 S4 50 52 726560 E) 615161616161 51616161 s MSVISNSNE NS ENS NES MENS iNS “Micterm microcontroller t terminal | o E AR | FE Enstdo este [| has gr 4 | vetogo [TE set , o SendFie | [E se m fes) door | Figure 7. MiCterm. MiCterm is part of the download package. 19 2 What you will need 2.2 Optional items (nice to have) TM The following items are not necessary to work with PIC” microcontrollers, but they are very convenient. Development software on the PC (Visual Basic) Since the PIC!” can communicate with the PC it's fun to write special sofiware for this. kt is not necessary, since you can do all the projects in this book with the supplied software in the download package. If you own Visual Basic, for example, you could write beautiful applications for your projects such as a voltmeter, several graphs on one page, and visualization of measurements. Many PC applications in the download package have been written in Visual Basic, and in many cases the source code is included. You can of course use another language if you want. voltmeter Figure 8. Visual Basic. Visual Basic” is commercial software, so it's not part of the free download”. É Visual Basic is supplied by Microsoft. See http:/Avww microsoft.com * For free PC development software try http://www .thefreecountry.com. 20