PIC in Practice PIC in Practice A Project-Based Approach D. W. Smith AMSTERDAM(cid:1)BOSTON(cid:1)HEIDELBERG(cid:1)LONDON NEWYORK(cid:1)OXFORD(cid:1)PARIS(cid:1)SANDIEGO SANFRANCISCO(cid:1)SINGAPORE(cid:1)SYDNEY(cid:1)TOKYO NewnesisanimprintofElsevier NewnesisanimprintofElsevier LinacreHouse,JordanHill,OxfordOX28DP 30CorporateRoad,Burlington,MA01803 Firstpublished2002 Reprinted2003(twice),2005 Secondedition2006 Copyright(cid:1)2006,DaveSmith.Allrightsreserved Nopartofthispublicationmaybereproduced,storedinaretrievalsystemortransmittedinany formorbyanymeanselectronic,mechanical,photocopying,recordingorotherwisewithoutthe priorwrittenpermissionofthepublisher Permission may be sought directly from Elsevier’s Science & Technology Rights Department inOxford,UK:phone(+44)(0)1865843830;fax(+44)(0)1865853333;email:permissions@ elsevier.com.AlternativelyyoucansubmityourrequestonlinebyvisitingtheElsevierwebsiteat http://elsevier.com/locate/permissions,andselectingObtainingpermissiontouseElseviermaterial Notice Noresponsibilityisassumedbythepublisherforanyinjuryand/ordamagetopersonsorproperty as a matter of products liability, negligence or otherwise, or from any use or operation of any methods, products, instructions or ideas contained in the material herein. Because of rapid advances in the medical sciences, in particular, independent verification of diagnoses and drug dosagesshouldbemade BritishLibraryCataloguinginPublicationData AcataloguerecordforthisbookisavailablefromtheBritishLibrary LibraryofCongressCataloginginPublicationData AcataloguerecordforthisbookisavailablefromtheLibraryofCongress ISBN13:978-075-066826-2 ISBN10:0-75-066826-1 ForinformationonallNewnespublicationsvisit ourwebsiteatbooks.elsevier.com TypesetbyCephaImagingPvtLtd,Bangalore,India PrintedandboundinGreatBritain Contents Introduction ix 1 Introduction to the PIC microcontroller 1 The aim ofthe book 1 Program memory 2 Microcontroller clock 3 The microcontroller system 3 Types of microcontroller 4 Microcontroller specification 5 Using the microcontroller 6 1 Microcontroller hardware 6 2 Programming the microcontroller 9 2 Programming the 16F84 microcontroller 11 Microcontroller inputsand output(I/O) 12 Timing withthe microcontroller 12 Programming the microcontroller 12 Entering data 13 The header forthe 16F84 14 Program example 16 Saving andassembling the code 19 PICSTART PLUSprogrammer 23 Programming flowchart 26 Problem: flashing twoLEDs 26 Solution to problem,flashing twoLEDs 27 3 Introductory projects 29 LED_Flasher2 29 SOS 30 Code forSOScircuit 30 Flashing 8 LEDs 33 Chasing 8 LEDs 35 Traffic lights 39 More than8 outputs 45 4 Headers, porting code – which micro? 47 Factors affectingthe choice ofthe microcontroller 47 Choosing the microcontroller 48 Headers 49 vi Contents 5 Using inputs 64 Switch flowchart 66 Program development 67 Scanning(using multiple inputs) 73 Switch scanning 73 Control application– a hotairblower 77 6 Understanding the headers 82 The 16F84 82 16F84memory map 87 The 16F818 88 7 Keypad scanning 93 Programming examplefor the keypad 94 8 Program examples 110 Countingevents 110 Lookuptable 115 7-Segmentdisplay 115 Numbers largerthan255 126 Long timeintervals 133 Onehour delay 136 9 The 16C54 microcontroller 139 Headerfor the 16C54 139 16C54 memorymap 142 10 Alpha numeric displays 143 Display pinidentification 144 Configuring the display 145 Writing to thedisplay 146 Program example 146 Program operation 160 Display configuration 161 Writing to thedisplay 162 Displaying a number 163 11 Analogue to digital conversion 166 Making anA/Dreading 167 Configuring the A/Ddevice 168 Analogue headerfor the 16F818 171 A/Dconversion – example,a temperature sensitive switch 174 Program code 176 Anotherexample – avoltage indicator 178 Contents vii 12 Radio transmitters and receivers 186 Measuring the received pulsewidth 189 13 EEPROM data memory 199 Example usingthe EEPROM 200 14 Interrupts 207 Interrupt sources 208 Interrupt control register 208 Program usinganinterrupt 209 15 The 12 series 8 pin microcontroller 216 Pin diagramof the12C508/509 216 Pin diagramof the12F629 and12F675 216 Features of these12series 217 The memorymap ofthe 12C508 217 Oscillator calibration 218 I/O PORT,GPIO 219 Delays with the12series 220 Header for 12C508/9 220 Program applicationfor 12C508 222 Program applicationusing the 12F629/675 225 16 The 16F87X Microcontroller 229 16F87X familyspecification 229 The 16F872microcontroller 230 16F87X memorymap 232 The 16F872header 233 16F872 application– a greenhousecontrol 236 Programming the 16F872 microcontroller usingPICSTART PLUS 242 Reconfiguring the 16F872header 243 17 The 16F62X Microcontroller 245 16F62X oscillatormodes 245 16F62X and16F84Pinouts 247 16F62X portconfiguration 247 16F62X memorymap 248 The 16F62Xheaders 248 HEAD62RC.ASM 250 A 16F627application – flashinganLED onand off 252 The 16F627LED flasher code 253 Configuration settings forthe 16F627 255 Other features of the 16F62X 255 viii Contents 18 Projects 257 Project1 Electronic dice 257 Project2 Reaction timer 266 Project3 Burglaralarm 272 Faultfinding 282 Development kits 285 19 Instruction set, files and registers 287 The PICmicrocontroller instruction set 287 Registers 289 Instruction set summary 292 Appendix A Microcontroller data 299 Appendix B Electrical characteristics 301 Appendix C Decimal, binaryand hexadecimalnumbers 303 Appendix D Usefulcontacts 306 Index 307
Description: