ebook img

Easy PIC’n: A Beginner’s Guide to Using Pic16/17 Microcontrollers from Square 1 PDF

169 Pages·1996·16.01 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Easy PIC’n: A Beginner’s Guide to Using Pic16/17 Microcontrollers from Square 1

,.....---------------------------.. , A BEGINNER'S GUIDE TO USING PIC MICROCONTROLLERS FROM VERSION 3.1 David Benson Ple'n A BEGINNER'S GUIDE TO USING PIC MICROCONTROLLERS FROM DAVID BENSON VERSION 3.1 NOTICE The material presented in this book is for the education and amusementofstudents, hobbyists, technicians and engineers. Every effort has been made to assure the accuracy ofthis infor mation and its suitability for this purpose. Square 1Electronics and the author assume no responsibility for the suitability ofthis information for any application nor do we assume any liability resulting from use ofthis information. No patent liability is assumed for use ofthe information contained herein. All rights reserved. No part ofthis book shall bereproduced or transmitted by any means (including photo copying) without written permission from Square 1Electronics and the author. Copyright © 1997 Revised April, 1999 TRADEMARKS PIC is a registered trademark ofMicrochip Technology Inc. in the U.S.A. PICmicro is aregistered trademarkofMicrochip Technology, Inc. PIC16j17 is aregistered trademark of Microchip Technology, Inc. MPLAB is aregistered trademarkof Microchip Technology, Inc. MPASM is aregistered trademark of Microchip Technology, Inc. MPSIM is aregistered trademarkof Microchip Technology, Inc. PICSTARTPlus is aregistered trademark ofMicrochip Technology, Inc. MS-DOS is a registered trademark of Microsoft Corporation. Windows is a registered trademark of Microsoft Corporation. ISBN 0-9654162-0-8 PUBLISHER Square 1 Electronics P.O. Box 501 Kelseyville,CA 95451 U.S.A. voice (707)279-8881 FAX (707)279-8883 EMAIL [email protected] http://www.sq-1.com A BEGINNER'S GUIDE TO USING PIC MICROCONTROLLERS FROM INTRODUCTION 1 PIC PRODUCT OVERVIEW 3 PIC16F84 5 Pins and Functions 5 Package 5 Clock Oscillator 6 Configuration bits 7 Reset 7 Ports 8 Special Features 8 Watchdog Timer 8 Power-up Timer 8 Sleep Mode 8 PIC16 Architecture 9 Program Memory 9 Weird Hex Notation 9 File Registers 10 Working Register (W) 11 Option Register 11 Stack 11 Reset Vector 11 Interrupt Vector 11 Option Register 12 Program Counter 13 Status Register 15 CIRCUIT FOR EXPERIMENTS 16 '84 On A Board 17 CHOOSING DEVELOPMENT TOOLS 20 MPLAB/Windows vs. MPASM/MS-DOS 20 Choosing A Device Programmer 20 USING AN ASSEMBLER 22 Source Code For The Assembler 22 Semicolon (;) 23 Tabs 24 Style 25 Headers 26 Labels 26 Equates 27 Literals 28 Origin 29 End 30 Program Format 30 Files Used By Assembler 31 Files Created By Assembler 31 Preventing Some Gotchas 33 USING THE MS-DOS BASED MPASM ASSEMBLER 34 How To Assemble A Program 34 MPLAB OVERVIEW - GETTING STARTED 37 Using MPLAB - Getting Started 37 Toolbars 37 Select Development Mode And Device Type 38 Project 38 Text Editor 39 Edit Project 39 Assembler 40 More MPLAB Operations 40 PROGRAMMING A DEVICE 41 WRITING PROGRAMS 43 Programming Concepts 43 Instruction Set 46 Weird Move Instruction 48 Instruction Format For The Assembl~r 49 Byte-Oriented Instructions 49 Bit-Oriented Instructions 50 Literal Instructions 50 Control Instructions (CALL and GOTO) 50 Destination Designator (d) 51 Hexadecimal Numbers vs . MPASM Assembler 52 Binary And Decimal Numbers vs. MPASM Assembler 53 ASCII Characters vs . MPASM Assembl er 53 Addressing Modes 54 Immediate addressing 54 Direct Addressing 54 Indirect Addressi ng 54 Rel at ive Addressing 55 Use Of The RETLW Instruction For Accessing 56 Tables of Data Via Rel at i ve Addressing using The Ports 58 Data Direction 58 Port Read/Write 58 Flags 59 Simple Data Transfers 59 Loop - Endless 61 Loop With a Counter 62 Loop Until 65 Comparisons 66 Bit Manipulation Using Bit Manipulation 69 Instructions Bit Manipulation Using Logic Instructions 71 Using Bit Manipulation 73 Sequencing 77 Subroutines 80 Time Delay Loop 82 Lookup Tables 91 INTERRUPTS 94 Interrupt Control Register 95 Interrupt Sources 96 External 96 Timer/Counter 96 Port B Interrupt On Change - Bits 7,6,5,4 97 Global Interrupt Enable Flag 97 Saving Status During An Interrupt (Context Saving) 97 Where To Put The Interrupt Service Routine 98 Interrupt Latency 99 Multiple Interrupt Sources 100 Example - External Interrupt 101 TIMINGANDCOUNTING 107 Digital Output Waveforms 107 Timing And Counting Using The PIC16F84's TMRO 108 On-Board Timer/Counter Prescaler 110 Changing Prescaler Assignment 111 Using The Timer/Counter 112 Timer/Counter Experiments 113 Digital Output Using TMRO - Internal Clock 113 Single Time Interval - Internal Clock 115 Single Time Interval - External Clock 118 Free Running Mode - Internal Clock 121 Free Running Mode - External Clock 125 Counting Events (Pulses) 126 PIC16C54 131 Pins And Functions 132 Packages 132 Clock Oscillator 133 Configuration bits 133 Ports 133 Architecture 134 Program Memory 134 File Registers 134 Stack 135 Reset Vector 135 Program Counter 136 Option Register 136 Status Register 137 No Timer/Counter Overflow Interrupt/Flag Out put 137 Fewer Instructions 137 PIC16C54 Programming Example 138 Timing And Counting 139 Timer/Counter Description 141 Using The Timer/Counter 142 Timer/Counter Experiment 142 MENDING OUR WAYS 143 Sink vs. Source 143 File Register Bank Switching 144 Bank Switching Demo 145 Interrupts And Bank Switching 148 Program Memory Paging 148 Include Files 148 I/O CONVERSION 151 Input Conversion 151 Output Conversion 154 MORE PIC'n BOOKS 157 APPENDICES Appendix A - Sources 159 Appendix B - Hexadecimal Numbers 161 Appendix C - Program Listings vs. Page Number 162 INTRODUCTION The PIC was originally designed as a Peripheral Interface Controller (PIC) for a lri-bit micro processor. It was essentially an I/O controllerand was designed to be very fast. It had a small microcoded instruction set. This design became the basis for Microchip Technology's PICI6C5X family ofmicrocontrollers. The PICI6F84 (or PICI6F84A) microcontrolleris unique because its programmemory is made usingFlash technology. It can be programmed, tested in-circuit and reprogrammed ifnecessary in amatterofafew minutes and without the need for aUV EPROM eraser. It is asmall device (18-pin), readily available to all including hobbyists and students at a cost of $6.00 (at this writ ing) in single quantity. The PICI6F84replaces the PICI6C84 which will soon be obsolete. Think ofthe PICI6F84 as a custom I/O handler. It looks at its inputs and, based onwhat it sees, it sends signals out its outputs. Youcan customize it to do what you wantviaprogramming. It is not a heavy duty numbercruncheror data manipulator. The MPASM (tm) assemblerfrom Microchip Technology (manufacturer ofPIC family) is the PIC assemblerofchoice for use in this book because: • It is free from Microchip. • Most people who are into PICs speak "MPASM". • Most examples in magazines and on the Internet are written in the MPASM dialect. • It is included in MPLAB (tm), a populardevelopment software package (available free from Microchip). A variety ofprogrammers are available for the PICI6F84 from independent programmermanu facturers (see sources in Appendix A). Some range incost from $40 to $80. Some come with their own assemblers but will accept hex files created by MPASM (detailslater). Microchip makes the PICSTART Plus (tm) programmer which will program all PIC microcon trollers. The cost is currently about $200. The PICI6F84, MPASM assembler (either stand alone or aspart ofMPLAB) and a low cost PICI6F84 programmermake avery inexpensive development system and a great way to get started creating microcontroller based projects. What you learn using the PICI6F84 is directly applicable to the whole line of PIC microcon trollers. The approach taken with the PICI6F84 in the first part ofbook is backwards compati ble with the PICI6C54 (explained toward the end of the book). The hasslesof file register bank switching and program memory paging are avoided as theyare confusing to a beginner but man ageable after some experience is gained. 1 Learning how PICs work and how to apply them involves study in three areas: • IBM compatiblecomputer use (as needed). • Assembler. • PIC itself. PICs are not easy to write code for by hand because they are not intuitive. Many instructions require settingor clearing a bit to specify the destination for the result ofexecuting the instruc tion. This is OK as an initial learning experience, but would become tedious very soon. So use of "power tools" is essential. This means learning to use an IBM compatiblecomputerifyou haven'talreadydone so. It also means learning to use an assemblerwhich converts English-like readable instructions into machine language understood by the PIC itself. Finally,learning about the PIC's inner workings ispossible once use ofthe power tools is under stood. The objectis to make this process as easy and enjoyable as possible. Once you get through this and you have programmed a PIC16F84 for the first time, a whole new world awaits. You will be able to create more interesting projects and have more fun! The usual approach to teaching the use of the PIC has been to get into all ofthe assemblercom mands and then show advanced examples. There has been no simple example showing how to get started. As usual,only 5 percent of this information is needed to get started, but which 5 per cent. The approach taken here will be to give you the 5 percentyou need to get going. The assumptionis made that you know how to do the following on an IBM compatible comput er: • Create a new directory called \PIC. • Copy the contents of a floppy disk to the directory\PIC. • Use a simple text editor to create a text file, save it,make a copy ofit, printit, and copy it to the directory\PIC. 2 PIC16/17 PRODUCT OVERVIEW This book uses the PIC16F84 and later the PIC16C54 as examples to get you started. The emphasis is on the base-line (12-bit core) and mid-range (14-bit core) products. The 12-bitcore designates the instruction word length. Youreally won'tcare how many bits there are in an instruction word, but it is the basis on which Microchip divides up their product line. Thefol lowing brieftables are for reference: Base-Line Mid-Range 12-bit Core 14-bit Core Stack Levels 2 8 Interrupts No Yes Instructions 33 35* Serial Programming No Yes File Registers 128 max ** 256 max ** Example PIC16C54 PIC16F84 * We will use 37 - details later ** Varies with part number PARTS IS PARTS As the Microchip rnicrocontrollerproduct line grows,and I am glad thatit is,the part numbering convention/system seems to be getting a bit involved. Also, the microcontroller product line designation has been changedfrom PIC16/17 (tm) toPICmicro (tm), Microchip is going to do what itwill. Youand I, however, need a way tokeep things straight between us. The real key to sanity here is to recognize that there are three fundamental product groups in terms ofhow they function and, to some extent, how program code must be writtenfor them. The three product groups have 12-bit, 14-bit and 16-bitcores. Wedon't reallycare how many bits arein an instruction word. Wejust need to know which set of rulesand features apply when writingcode for a selected chip. Here are some popularexamples: Device Core Size Pins PIC12C508 12 8 12C671 14 8 16C54 12 18 16C554 14 18 16C63 14 28 16C71 14 18 16C74 14 40 16F84 14 18 3

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.