• No results found

Programming the PIC microcontroller in c.pdf

N/A
N/A
Protected

Academic year: 2021

Share "Programming the PIC microcontroller in c.pdf"

Copied!
801
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Programming฀the฀

(3)
(4)

AMSTERDAM • BOSTON • HEIDELBERG • LONDON NEW YORK • OXFORD • PARIS • SAN DIEGO SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO

Newnes is an imprint of Elsevier

Programming฀the฀

PIC฀Microcontroller฀with฀MBasic฀

by฀Jack฀R.฀Smith

(5)

Linacre฀House,฀Jordan฀Hill,฀Oxford฀OX2฀8DP,฀UK Copyright฀©฀2005,฀Elsevier฀Inc.฀All฀rights฀reserved. No฀part฀of฀this฀publication฀may฀be฀reproduced,฀stored฀in฀a฀retrieval฀system,฀or฀฀ transmitted฀in฀any฀form฀or฀by฀any฀means,฀electronic,฀mechanical,฀photocopying,฀฀ recording,฀or฀otherwise,฀without฀the฀prior฀written฀permission฀of฀the฀publisher.฀ Permissions฀may฀be฀sought฀directly฀from฀Elsevier’s฀Science฀&฀Technology฀Rights฀฀ Department฀in฀Oxford,฀UK:฀phone:฀(+44)฀1865฀843830,฀fax:฀(+44)฀1865฀853333,฀฀ e-mail:฀[email protected].฀You฀may฀also฀complete฀your฀request฀online฀฀ via฀the฀Elsevier฀homepage฀(http://www.elsevier.com),฀by฀selecting฀“Customer฀฀ Support”฀and฀then฀“Obtaining฀Permissions.” Recognizing฀the฀importance฀of฀preserving฀what฀has฀been฀written,฀฀ Elsevier฀prints฀its฀books฀on฀acid-free฀paper฀whenever฀possible. Library฀of฀Congress฀Cataloging-in-Publication฀Data ฀ (Application฀submitted.) British฀Library฀Cataloguing-in-Publication฀Data A฀catalogue฀record฀for฀this฀book฀is฀available฀from฀the฀British฀Library. ISBN:฀0-7506-7946-8 For฀information฀on฀all฀Newnes฀publications฀ visit฀our฀website฀at฀www.books.elsevier.com 05฀฀06฀฀07฀฀08฀฀09฀฀10฀฀฀฀10฀฀9฀฀8฀฀7฀฀6฀฀5฀฀4฀฀3฀฀2฀฀1 Printed฀in฀the฀United฀States฀of฀America

(6)

Preface฀... x฀ Acknowledgments฀... xii฀ What’s฀on฀the฀CD-ROM?฀... xiii CHAPTER฀1:฀What฀is฀a฀PIC®?฀฀... 1 PICs฀“101”฀... 1 How฀Do฀I฀Tell฀Them฀Apart?฀... 2 Which฀One฀Should฀I฀Use?฀... 4 How฀Do฀I฀Pick฀One?฀... 7 So,฀Which฀One฀Do฀I฀Really฀Want฀to฀Use?฀... 8 Basic฀Micro’s฀MBasic876฀Compiler฀... 8 References฀... 9 CHAPTER฀2:฀MBasic฀Compiler฀and฀Development฀Boards฀... 10 The฀Compiler฀Package฀... 10 BASIC฀and฀Its฀Essentials฀... 11 Development฀Boards฀... 13 Programming฀Style฀... 15 Building฀the฀Circuits฀and฀Standard฀Assumptions฀฀... 16 Pins,฀Ports฀and฀Input/Output฀... 17 Pseudo-Code฀and฀Planning฀the฀Program฀... 23 Inside฀the฀Compiler฀... 25 References฀... 27 CHAPTER฀3:฀The฀Basics฀–฀Output฀... 28 Pin฀Architectures฀... 28 LED฀Indicators฀... 31 Switching฀Inductive฀Loads฀... 34 Low฀Side฀Switching฀... 36 Isolated฀Switching฀... 45 Special฀Purpose฀Switching฀... 50 Fast฀Switching—Sound฀from฀a฀PIC฀... 50 References฀... 51 CHAPTER฀4:฀The฀Basics฀–฀Digital฀Input฀... 53 Introduction฀... 53 Switch฀Bounce฀and฀Sealing฀Current฀... 58 Isolated฀Switching฀... 62

(7)

Reading฀a฀Keypad฀... 63 References฀... 66 CHAPTER฀5:฀LCD฀Modules฀... 67 Selecting฀a฀Display฀... 67 VFD฀Displays฀... 69 Connection฀to฀PIC฀... 69 Hello฀World฀... 72 LCD฀Module฀Memory,฀Shifts฀and฀Lines฀... 74 Font฀Selection฀... 79 Custom฀Characters฀... 80 References฀... 85 CHAPTER฀6:฀Reading฀Complex฀Input฀Switches฀... 86 Pin฀Saving฀Techniques฀... 86 Rotary฀Encoders฀... 91 Reading฀a฀Relative฀Encoder฀... 95 Dual฀Encoders฀and฀LCD฀... 100 References฀... 106 CHAPTER฀7:฀Seven-Segment฀LED฀Displays฀... 107 LED฀Display฀Selection฀... 107 Circuit฀Design฀... 108 References฀... 119 CHAPTER฀8:฀Introductory฀Stepper฀Motors฀... 120 Stepper฀Motor฀Basics฀... 120 Programs฀... 133 References฀... 150 CHAPTER฀9:฀RS-232฀Serial฀Interface฀... 151 How฀to฀Connect฀to฀Your฀PC฀... 151 Voltage฀Levels฀in฀RS-232฀and฀Level฀Conversion฀... 152 Standard฀Pin฀Connections฀... 154 Asynchronous฀Transmission,฀Start฀Bits,฀Stop฀Bits฀and฀Bit฀Order฀... 154 MBasic’s฀Procedures฀for฀Serial฀Communications฀... 156 Programs฀... 159 References฀... 186 CHAPTER฀10:฀Interrupts฀and฀Timers฀in฀MBasic฀... 187 Interrupts฀and฀Timers—Overview฀... 187 Interrupts฀... 188 Timers฀... 194 Capture฀and฀Compare฀... 203 References฀... 210 CHAPTER฀11:฀Analog-to-Digital฀Conversion฀... 211 Introduction฀to฀Analog-to-Digital฀Conversion฀... 211 Resolution฀and฀Accuracy฀... 212 Self-Contained฀DVM฀... 218 References฀... 230

(8)

CHAPTER฀12:฀Digital฀Temperature฀Sensors฀and฀Real-Time฀Clocks฀... 231 DS18B20฀Temperature฀Sensor฀... 231 DS1302฀Real-Time฀Clock฀... 243 Combination฀Date,฀Time฀and฀Temperature฀... 252 References฀... 259 CHAPTER฀13:฀Assembler฀101฀... 260 The฀Basics฀... 260 OpCodes฀... 267 References฀... 280 CHAPTER฀14:฀In-Line฀Assembler฀... 281 Adding฀Assembler฀to฀MBasic฀Programs฀... 281 Bolt-In฀Assembler฀Functions฀... 295 References฀... 316 CHAPTER฀15:฀Interrupt฀Handlers฀and฀Timers฀in฀Assembler฀... 317 ISRASM฀–฀MBasic’s฀Gateway฀to฀Assembler฀Interrupt฀Service฀Routines฀... 317 Program฀Examples฀... 323 References฀... 334 CHAPTER฀16:฀Digital-to-Analog฀Conversion฀... 335 Introduction฀to฀Digital-to-Analog฀Conversion฀... 335 Resolution฀–฀Accuracy฀and฀Signal-to-Noise฀Ratio฀... 336 Henry฀Nyquist฀and฀his฀Sampling฀Theorem฀... 337 DAC฀Circuit฀Design฀... 339 Alternative฀Analog฀Output฀Solutions฀... 352 References฀... 358 CHAPTER฀17:฀DTMF฀Tone฀Decoding฀and฀Telephone฀Interface฀... 360 What฀is฀Touch-Tone฀Signaling?฀... 360 Generating฀Touch-Tone฀Signals฀... 361 Decoding฀a฀Touch-Tone฀Signal฀... 361 References฀... 388 CHAPTER฀18:฀External฀Memory฀... 389 I2C-Bus฀Devices฀... 389 Practical฀Use฀of฀External฀EEPROM฀... 403 Parallel฀Access฀Memory฀... 408 References฀... 416 CHAPTER฀19:฀Advanced฀Stepper฀Motors฀... 418 Microstepping฀... 418 Programs฀... 420 References฀... 452 CHAPTER฀20:฀X-10฀Home฀Automation฀... 453 How฀X-10฀Works฀... 453 Programs฀... 459 References฀... 486

(9)

CHAPTER฀21:฀Digital฀Potentiometers฀and฀Controllable฀Filter฀... 487 Getting฀Started฀with฀an฀MCP41010฀... 489 RS-232฀Control฀of฀an฀MCP41010฀... 493 Daisy฀Chaining฀Multiple฀MCP42010฀Devices฀... 498 RS-232฀Command฀of฀Multiple฀Daisy฀Chained฀MCP42010฀Devices฀... 501 Logarithmic฀Response฀for฀Audio฀Volume฀Control฀... 506 Electronically฀Tunable฀Low-Pass฀Filter฀Using฀MCP42010฀... 511 References฀... 515 CHAPTER฀22:฀Infrared฀Remote฀Controls฀... 517 Common฀Encoding฀Standards฀... 518 IR฀Receiver฀... 520 Characterizing฀Wide/Narrow฀Pulse฀Intervals฀... 522 Decoding฀a฀REC-80฀Controller฀... 532 References฀... 541 CHAPTER฀23:฀AC฀Power฀Control฀... 542 Introduction฀to฀Triacs฀... 543 Snubberless฀versus฀Standard;฀dV/dt฀and฀dI/dt฀Issues฀... 545 Triggering฀a฀Triac฀฀... 548 Phase฀and฀Cycle฀Control฀... 549 Power฀Control฀Board฀... 551 Programs฀... 555 References฀... 565 CHAPTER฀24:฀DC฀Motor฀Control฀... 567 Introduction฀to฀Control฀Theory฀... 567 Measure฀Motor฀Speed฀(Tachometer฀Output฀Pulse฀Width)฀... 568 Error฀=฀Target฀Width฀–฀Measured฀Width฀฀... 572 The฀Control฀Algorithm฀... 572 Motor฀Control฀Programs฀... 573 References฀... 594 CHAPTER฀25:฀Bar฀Code฀Reader฀... 595 Bar฀Codes฀“101”฀... 595 Bar฀Code฀Wand฀... 599 Programs฀... 602 References฀... 631 CHAPTER฀26:฀Sending฀Morse฀Code฀... 633 Morse฀Code฀101฀... 633 Programs฀... 635 References฀... 660 CHAPTER฀27:฀Morse฀Code฀Reader฀... 661 Sending฀and฀Receiving฀Morse฀... 661 Tone฀Detector฀Circuit฀... 663 Programs฀... 668 References฀... 689

(10)

CHAPTER฀28:฀Weather฀Station฀and฀Data฀Logger฀... 691 Sensor฀Selection฀... 691 Connecting฀the฀Sensors฀and฀Memory฀... 698 Initial฀Tests฀... 700 References฀... 728 CHAPTER฀29:฀Migrating฀from฀v5.2.1.x฀to฀5.3.0.0฀and฀the฀Undocumented฀MBasic฀... 729 Migrating฀from฀v5.2.1.x฀to฀5.3.0.0฀... 729 Undocumented฀MBasic฀... 733 APPENDIX฀A:฀Parts฀List฀and฀Suppliers฀... 745 Suppliers฀... 745 Generic฀Components฀Required฀... 746 Specific฀Components฀... 748 APPENDIX฀B:฀Function฀Index฀... 755 About฀the฀Author฀... 760฀ Index฀... 761

(11)

My฀introduction฀to฀computers฀was฀in฀the฀days฀of฀IBM’s฀Model฀29฀card฀punch.฀You฀first฀carefully฀printed฀ your฀FORTRAN฀code฀on฀a฀coding฀sheet,฀then฀punched฀a฀card฀deck฀and฀finally฀walked฀your฀cards฀over฀to฀ the฀campus฀computer฀center.฀There,฀one฀of฀the฀high฀acolytes฀of฀the฀IBM฀360—in฀reality฀a฀grad฀student—ac-cepted฀the฀deck฀with฀a฀faint฀look฀of฀disdain.฀You฀might฀even฀catch฀a฀glimpse฀of฀the฀computer฀itself฀through฀ the฀glass฀wall฀of฀the฀computer฀center.฀The฀following฀day,฀if฀you฀were฀fortunate,฀your฀card฀deck฀was฀ready฀for฀ pick-up,฀wrapped฀in฀the฀green฀bar฀paper฀output฀your฀job฀elicited.฀If฀you฀were฀really฀lucky,฀the฀output฀made฀ sense฀and฀you฀could฀go฀on฀to฀your฀next฀task.฀If฀you฀were฀less฀fortunate,฀the฀printout฀identified฀your฀errors.฀ And,฀if฀you฀were฀really฀having฀a฀bad฀day,฀your฀card฀deck฀was฀hidden฀inside฀an฀inch-thick฀core฀dump฀print-out,฀dense฀with฀hexadecimal฀register฀and฀memory฀values.฀฀ Today,฀we฀have฀as฀much฀computing฀power฀on฀our฀desktops฀as฀was฀behind฀the฀glass฀wall฀when฀I฀was฀punch-ing฀card฀decks.฀Computers฀are฀now฀embedded฀in฀almost฀every฀imaginable฀electronic฀device.฀One฀of฀the฀ pioneers฀in฀embedded฀computers฀was฀General฀Instruments,฀which฀in฀1976฀released฀the฀1650฀“programmable฀ intelligent฀computer,”฀the฀grandfather฀of฀today’s฀PICs.฀(There฀is฀a฀raging฀debate฀among฀the฀cognoscenti฀over฀ the฀“true”฀name฀behind฀the฀PIC฀acronym,฀with฀“peripheral฀interface฀controller”฀often฀being฀cited.฀GI’s฀1977฀ data฀sheet฀for฀the฀PIC1650,฀though,฀confirms฀the฀term฀“programmable฀intelligent฀computer.”฀Microchip฀ Technology฀Incorporated,฀who฀acquired฀GI’s฀PIC฀business฀in฀the฀mid฀1980s,฀wisely฀stays฀out฀of฀the฀debate฀ and฀just฀calls฀its฀products฀“PICmicro®฀microcontrollers.”)฀ This฀book฀focuses฀on฀programming฀Microchip’s฀mid-range฀PIC฀line฀with฀MBasic,฀a฀powerful,฀but฀easy฀to฀ learn฀programming฀language,฀developed฀by฀Basic฀Micro฀of฀Murrieta,฀California.฀Since฀a฀PIC฀by฀itself฀is฀not฀ all฀that฀useful,฀I฀will฀illustrate฀MBasic’s฀abilities฀through฀a฀series฀of฀construction฀projects,฀some฀simple฀and฀ some฀more฀advanced.฀I฀will฀also฀dip฀into฀assembler฀language,฀as฀there฀are฀some฀applications฀that฀require฀us฀ to฀become฀more฀intimate฀with฀the฀PIC’s฀internals฀than฀possible฀in฀MBasic. The฀projects฀assume฀the฀user฀has฀MBasic฀Professional฀version฀5.3.0.0฀compiler,฀the฀associated฀ISP-PRO฀ programmer฀and฀a฀2840฀development฀board,฀all฀available฀from฀Basic฀Micro.฀Almost฀all฀examples฀use฀a฀ 16F877A฀PIC฀and฀a฀20฀MHz฀resonator.฀However,฀both฀the฀code฀and฀supporting฀circuitry฀are฀easily฀portable฀ to฀many฀other฀PICs฀supported฀by฀MBasic.฀More฀importantly,฀almost฀every฀project฀in฀this฀book฀can฀be฀built฀ with฀the฀free฀MBasic876฀compiler฀included฀in฀the฀accompanying฀CD-ROM.฀In฀a฀few฀cases,฀the฀16F876฀ doesn’t฀have฀enough฀I/O฀pins฀to฀support฀the฀project.฀ Reading฀Basic฀Micro’s฀message฀board,฀and฀questions฀from฀beginners฀posted฀to฀the฀PIC฀microcontroller฀ discussion฀list,฀reveals฀a฀need฀for฀information฀showing฀how฀the฀smorgasbord฀of฀functions,฀procedures฀and฀ code฀snippets฀found฀in฀the฀MBasic฀User’s฀Guide฀might฀be฀put฀together฀to฀actually฀do฀something฀useful.฀And,฀ since฀doing฀“something฀useful”฀with฀a฀PIC฀inevitably฀requires฀some฀associated฀circuitry,฀electronics฀ques-tions฀are฀sprinkled฀liberally฀throughout฀these฀fora฀as฀well.฀

(12)

I’ve฀tried฀to฀address฀both฀the฀software฀and฀hardware฀aspects฀of฀working฀with฀PICs,฀with฀my฀imagined฀reader฀ having฀an฀interest฀in฀both฀programming฀and฀electronics,฀but฀without฀specialized฀training.฀Although฀I’ve฀tried฀ to฀err฀on฀the฀side฀of฀inclusion฀over฀brevity,฀this฀book฀can’t฀replace฀a฀basic฀understanding฀of฀electronics,฀nor฀ an฀elementary฀grasp฀of฀how฀one฀goes฀about฀writing฀BASIC฀programs.฀I฀trust฀that฀readers฀experienced฀in฀ electronics฀will฀forgive฀the฀simplifications฀necessitated฀in฀this฀endeavor,฀and฀that฀experienced฀programmers฀ understand฀that฀they฀will฀not฀necessarily฀find฀elegant฀algorithms฀or฀code฀in฀every฀case.฀But,฀this฀work฀is฀not฀ intended฀to฀replicate฀Knuth’s฀The฀Art฀of฀Computer฀Programming,฀nor฀Horowitz฀and฀Hill’s฀The฀Art฀of฀Elec-tronics.฀And,฀I฀couldn’t฀duplicate฀either฀if฀I฀tried฀my฀best฀for฀the฀next฀decade.฀ Finally,฀I’ve฀never฀found฀the฀impersonal฀passive฀technical฀writing฀style฀conducive฀to฀learning฀a฀new฀subject.฀ After฀all,฀“the฀code฀was฀transferred฀to฀the฀PIC”฀isn’t฀what฀actually฀happened฀was฀it?฀Someone—probably฀ you,฀but฀certainly฀not฀some฀disembodied฀entity—programmed฀the฀PIC฀using฀the฀MBasic฀software.฀Why฀not฀ say฀so?฀Likewise,฀although฀we฀know฀that฀a฀PIC’s฀output฀pin฀doesn’t฀“see”฀a฀load฀resistance฀through฀physical฀ eyes,฀these฀anthropomorphic฀analogies฀are฀easier฀to฀understand฀than฀reading฀“the฀equivalent฀resistance฀that฀ would฀be฀measured฀by฀an฀appropriate฀impedance฀measuring฀instrument฀connected฀in฀place฀of฀the฀pin฀and฀ applying฀a฀+5฀volt฀dc฀stimulus฀signal฀to฀the฀load฀resistance.”฀Hence,฀I฀make฀no฀apologies฀for฀the฀chatty฀style. This฀book฀is฀not฀an฀“official”฀publication฀of฀Basic฀Micro,฀and฀its฀contents฀reflect฀my฀views,฀not฀those฀of฀ Basic฀Micro,฀or฀its฀employees฀or฀owners.฀ In฀a฀book฀of฀this฀length฀and฀detail,฀there฀will฀inevitably฀be฀errors฀and฀omissions,฀despite฀the฀best฀efforts฀of฀ the฀author฀and฀editors.฀Some฀are฀the฀unavoidable฀byproduct฀of฀simplifying฀complex฀subjects฀for฀an฀intro-ductory฀level฀presentation฀and฀others฀are฀just฀plain฀dumb฀mistakes.฀Regardless฀of฀the฀category,฀I฀accept฀full฀ responsibility.฀I฀may฀be฀contacted฀by฀e-mail฀at฀[email protected]฀to฀report฀errors฀or฀omissions. Jack฀Smith฀ June฀2005,฀Clifton,฀VA฀

(13)

Acknowledgments฀are฀due฀to฀my฀wife฀Janet,฀who฀has฀tolerated฀with฀good฀grace฀the฀innumerable฀hours฀I’ve฀ spent฀in฀the฀basement฀workshop฀or฀in฀front฀of฀the฀computer฀screen.฀I฀also฀wish฀to฀thank฀the฀people฀at฀Basic฀ Micro,฀including฀Nathan฀Scherdin,฀and฀Dale฀Kubin,฀for฀their฀assistance฀in฀writing฀this฀book.฀I฀also฀wish฀to฀ thank฀Larry฀and฀Janet฀Phipps฀for฀the฀hospitality฀shown฀to฀me.฀

(14)

The฀content฀of฀the฀CD-ROM฀was฀developed฀with฀Windows®฀XP,฀and฀has฀been฀verified฀as฀readable฀with฀ Windows®฀2000.฀It฀has฀not฀been฀tested฀with฀other฀operating฀systems. Its฀contents฀are฀organized฀in฀a฀series฀of฀directories: MBasic876 Basic฀Micro฀Inc.฀has฀provided฀a฀free฀MBasic฀compiler,฀MBasic876,฀with฀all฀features฀of฀MBasic฀Profession-al,฀but฀restricted฀to฀program฀only฀16F876฀and฀16F876A฀devices.฀An฀installation฀program,฀MBasic876_Setup,฀ to฀add฀MBasic876฀to฀your฀computer฀is฀contained฀in฀this฀directory.฀Launching฀MBasic876_Setup฀will฀start฀ the฀installation฀process. The฀directory฀MBasic\Documents฀contains฀the฀MBasic฀User’s฀Guide฀and฀data฀sheets฀on฀Basic฀Micro’s฀฀ programming฀board฀and฀development฀and฀prototype฀boards. Basic฀Micro’s฀website,฀http://www.basicmicro.com/,฀has฀an฀active฀MBasic฀user’s฀forum฀that฀I฀highly฀฀ recommend.฀ Linear฀Technology฀Circuit฀Simulation฀Software Linear฀Technology฀Corporation฀has฀provided฀two฀programs฀for฀circuit฀simulation.฀Both฀programs฀were฀used฀ in฀developing฀and฀illustrating฀the฀circuits฀in฀this฀book.฀ The฀directory฀Linear฀Technology฀Circuit฀Simulation฀Software\FilterCAD฀contains฀the฀installation฀program,฀ FilterCADv300.exe,฀which฀installs฀FilterCAD฀version฀3.00฀on฀your฀computer.฀FilterCAD฀is฀a฀powerful฀tool฀ for฀designing฀and฀simulation฀active฀filters. The฀directory฀Linear฀Technology฀Circuit฀Simulation฀Software\SWCADIII฀contains฀the฀installation฀program฀ swcadiii.exe,฀which฀installs฀a฀program฀called฀LTspice/SwitcherCAD฀III฀on฀your฀computer.฀This฀is฀a฀full-fea-tured฀general-purpose฀electronic฀circuit฀simulation฀program.฀After฀installing฀LTspice,฀I฀recommend฀you฀use฀ the฀automatic฀update฀feature฀to฀download฀the฀most฀recent฀version. You฀may฀also฀wish฀to฀join฀the฀LTspice฀user’s฀group,฀via฀the฀home฀page฀at฀http://groups.yahoo.com/group/ LTspice/. MBasic฀Programs All฀of฀the฀programs฀in฀this฀book฀are฀contained฀in฀this฀directory.฀The฀programs฀are฀organized฀with฀a฀separate฀ directory฀for฀each฀chapter.฀Within฀each฀chapter฀directory฀are฀separate฀directories฀with฀program฀versions฀ compatible฀with฀MBasic฀(and฀MBasic876)฀version฀5.3.0.0฀and฀with฀earlier฀versions฀(5.2.1.1.)฀I฀developed฀ the฀programs฀with฀version฀5.2.1.1฀originally,฀but฀have฀revised฀and฀tested฀each฀for฀compatibility฀with฀version฀ 5.3.0.0.฀฀

(15)

Data฀Sheets฀and฀Application฀Notes

Data฀sheets฀for฀many฀of฀the฀transistors,฀diodes฀and฀integrated฀circuits฀used฀in฀this฀book’s฀circuits฀are฀pro-vided฀in฀the฀directory฀data฀sheets฀and฀application฀notes.฀In฀addition,฀I฀have฀included฀a฀selection฀of฀relevant฀ application฀notes฀and฀other฀material฀from฀key฀semiconductor฀manufacturers.

(16)

C H A P T E R ฀

1

PICs฀“101”

What฀is฀a฀PIC

®

?฀

PICs฀are฀inexpensive฀one-chip฀computers฀designed฀and฀manufactured฀by฀Microchip฀Technology,฀Inc.฀ The฀acronym฀originally฀stood฀for฀Programmable฀Intelligent฀Computer,฀but฀Microchip’s฀official฀name฀for฀ these฀devices฀is฀now฀PICmicro®฀microcontrollers.฀We฀will฀call฀them฀PICs.฀In฀1977,฀General฀Instruments,฀ Microchip’s฀predecessor,฀developed฀the฀original฀PIC,฀the฀PIC1650.฀The฀PIC1650฀can฀be฀though฀of฀as฀the฀ grandfather฀of฀today’s฀PICs,฀and฀its฀architecture,฀programming฀approach฀and฀other฀features฀directly฀corre-spond฀to฀those฀found฀in฀modern฀PICs.฀Its฀instruction฀set฀and฀register฀arrangement฀mirror฀current฀PICs฀with฀ only฀minor฀differences. General฀Instruments฀sold฀its฀microcontroller฀business฀in฀the฀mid-1980s฀to฀the฀entity฀that฀later฀became฀ Microchip.฀Microchip’s฀current฀product฀line฀includes฀nearly฀200฀PIC฀models฀with฀MBasic฀supporting฀more฀ than฀half.฀Microchip฀has฀sold฀more฀than฀2฀billion฀PICs฀since฀the฀mid-1980s,฀and฀in฀2002฀was฀number฀one฀ worldwide฀in฀8-bit฀microcontroller฀sales,฀based฀on฀number฀of฀units฀shipped. PICs฀are฀microprocessors,฀akin฀to฀the฀ones฀inside฀personal฀computers,฀but฀significantly฀simpler,฀smaller฀ and฀cheaper,฀optimized฀to฀deal฀with฀the฀real฀world—operating฀relays,฀turning฀lamps฀off฀and฀on,฀measuring฀ sensors฀and฀responding฀to฀changed฀readings฀with฀specific฀actions—instead฀of฀running฀word฀processing฀or฀ spread฀sheet฀programs.฀To฀emphasize฀the฀outside฀world฀connection,฀the฀term฀“microcontroller”฀was฀coined฀ to฀distinguish฀it฀from฀a฀“microprocessors.”฀GI฀envisioned฀its฀PIC1650฀as฀a฀means฀to฀replace฀dozens฀of฀ discrete฀logic฀chips฀in฀computers฀using฀its฀CP1600฀microprocessor,฀but฀immediately฀recognized฀the฀power฀ of฀its฀flexible,฀programmable฀design฀serving฀as฀a฀stand-alone฀microcontroller.฀Figure฀1-1฀illustrates฀the฀main฀ elements฀inside฀a฀PIC: •฀ A฀processing฀engine:฀The฀central฀processing฀unit,฀or฀ CPU,฀is฀the฀microcontroller’s฀intelligence.฀It฀performs฀ the฀logical฀and฀arithmetic฀functions฀of฀the฀PIC฀follow-ing฀instructions฀it฀reads฀from฀the฀program฀memory.฀It฀ reads฀from฀and฀writes฀to฀data฀memory฀and฀the฀input/ output฀module. •฀ Program฀memory:฀Holds฀instructions฀for฀the฀CPU.฀The฀ CPU฀reads฀program฀memory฀but฀is฀physically฀prevented฀ in฀most฀model฀PICs฀from฀writing฀to฀program฀memory. •฀ Data฀memory:฀Holds฀memory฀that฀the฀programmer฀ may฀use฀for฀variables.฀The฀CPU฀reads฀from฀and฀writes฀ to฀data฀memory. •฀ Input/output:฀How฀the฀PIC฀communicates฀with฀the฀ world฀outside฀the฀chip;฀for฀example,฀pins฀that฀go฀be-tween฀logical฀0฀and฀logical฀1. Figure฀1-1:฀Main฀elements฀of฀a฀PIC.

(17)

฀ Peripherals:฀Special฀purpose฀functions฀built฀into฀the฀PIC,฀such฀as฀timers,฀analog-to-digital฀converters฀ and฀pulse฀width฀modulators.฀ If฀you฀are฀familiar฀with฀the฀Intel฀microprocessors฀used฀in฀IBM-compatible฀personal฀computers,฀you฀may฀no-tice฀one฀striking฀difference฀in฀Figure฀1-1;฀the฀program฀memory฀and฀data฀memory฀are฀separate.฀In฀computer฀ techno-speak,฀PICs฀follow฀the฀Harvard฀architecture฀model,฀while฀Intel’s฀microprocessors฀(and฀those฀of฀most฀ other฀manufacturers฀as฀well)฀implement฀von฀Neumann’s฀architecture,฀sharing฀common฀memory฀between฀ program฀and฀data฀as฀necessary.฀Fortunately,฀MBasic฀hides฀the฀details฀of฀this฀difference฀from฀us฀and฀we฀ seldom฀need฀to฀delve฀into฀it.฀One฀place฀this฀difference฀is฀critical,฀though,฀is฀since฀data฀memory฀and฀program฀ memory฀capacities฀are฀separately฀specified฀in฀PICs,฀both฀must฀be฀sized฀to฀accommodate฀the฀job฀at฀hand.

How฀Do฀I฀Tell฀Them฀Apart?

Microchip฀identifies฀PICs฀with฀a฀multipart฀identifier฀such฀as฀a฀16F877A-E/P: Microchip฀groups฀its฀PIC฀line฀in฀three฀performance฀and฀three฀memory฀type฀categories: Microchip’s฀General฀Purpose฀PIC฀Line฀Name฀(Instruction฀Word฀Length)฀and฀MBasic฀Support

Program฀Memory฀Type Base-Line฀(12-bit) Mid-Range฀(14-bit) High-End฀(16-bit)

EPROM/EEPROM None฀supported฀by฀MBasic 12C,฀12CE,฀16C฀and฀16CE-series฀EPROM฀and฀EEPROM 17C-series฀and฀18C-series.฀ Not฀supported฀by฀MBasic Read-only฀Memory฀(ROM) 16CR-series.฀Not฀supported฀by฀MBasic Not฀produced฀by฀Microchip Flash฀(electronically฀erasable) Some฀12F-series฀and฀ 16F-series 18F-series.฀Not฀presently฀ supported฀by฀MBasic฀ (See฀Note฀1) 1.฀18F-series฀support฀is฀under฀development฀by฀Basic฀Micro฀and฀will฀be฀included฀in฀a฀future฀MBasic฀release. It฀would฀have฀been฀logical฀for฀Microchip฀to฀use฀the฀series฀identifier฀to฀point฀to฀the฀instruction฀word฀length,฀ but฀it฀missed฀that฀opportunity.฀Thus,฀we฀have฀the฀12C508A,฀a฀12-bit฀device฀and฀the฀12F629,฀a฀14-bit฀device.฀ And,฀we฀have฀the฀16C54C,฀a฀12-bit฀device฀and฀the฀16C554,฀a฀14-bit฀device.฀In฀almost฀all—but฀unfortunately฀ not฀entirely฀all—instances฀a฀16-series฀device฀is฀a฀mid-range฀PIC฀with฀a฀14-bit฀instruction฀word,฀but฀to฀be฀ sure฀we฀must฀consult฀Microchip’s฀reference฀documents.฀ 16 F 877 A -xx E /P

Case style P,JW, SO,SP,ML,SS,PT Temperature range, E (extended), I (industrial) or C (commercial) Maximum clock frequency in MHz (omitted if only one frequency rating applies for the PIC.)

Silicon die layout revision suffix Device type number

Program memory type; C, CR, CE & F Family number; 12, 16, 17 & 18

(18)

The฀instruction฀word฀length฀is฀not฀related฀to฀the฀program฀memory฀size,฀but฀rather฀defines฀how฀many฀unique฀ machine฀code฀instructions฀may฀be฀implemented.฀It฀isn’t฀necessary฀to฀go฀into฀details฀as฀MBasic฀takes฀care฀ of฀this฀for฀us,฀but฀many฀machine฀level฀operands฀include฀an฀8-bit฀literal฀value,฀such฀as฀moving฀a฀defined฀byte฀ value฀(the฀“literal”)฀into฀the฀CPU.฀Since฀the฀8-bit฀literal฀is฀part฀of฀the฀instruction,฀a฀12-bit฀instruction฀word฀ leaves฀only฀four฀bits฀for฀instructions฀containing฀a฀literal,฀resulting฀in฀only฀16฀possible฀unique฀instructions.฀ Moving฀to฀a฀14-bit฀word฀increases฀the฀potential฀instruction฀set฀with฀a฀literal฀to฀64.฀(The฀problem฀of฀limited฀ program฀instruction฀width฀also฀shows฀up฀in฀assembler฀jump฀or฀“go฀to”฀instructions.)฀In฀any฀event,฀since฀ MBasic฀currently฀supports฀only฀midlength฀(14-bit)฀PICs,฀we฀can฀file฀this฀information฀in฀the฀“interesting฀but฀ not฀immediately฀useful”฀category฀in฀the฀back฀of฀our฀minds,฀at฀least฀until฀we฀start฀to฀mix฀assembler฀language฀ routines฀with฀MBasic. Program฀memory฀in฀a฀PIC฀may฀consist฀of฀three฀types: • Read-only:฀Read-only฀memory฀means฀exactly฀that;฀the฀memory฀is฀configured฀at฀the฀time฀of฀manufactur-ing฀to฀contain฀the฀program฀code฀and฀may฀not฀be฀subsequently฀altered,฀something฀economically฀feasible฀ only฀in฀high฀volume฀products.฀MBasic฀does฀not฀support฀PICs฀with฀read-only฀memory.฀ • EPROM฀and฀EEPROM:฀EPROM฀(erasable฀programmable฀read-only฀memory)฀and฀EEPROM฀(electri-cally฀erasable฀programmable฀read-only฀memory)฀memory฀may฀be฀written฀to฀electronically฀through฀the฀ application฀of฀a฀programming฀voltage฀to฀the฀PIC.฀Once฀written,฀EPROM฀memory฀may฀not฀be฀re-written,฀ and฀is฀thus฀becomes฀read-only฀afterwards.฀Microchip฀refers฀to฀these฀devices฀as฀“one฀time฀program-mable”฀or฀OTP฀products.฀EEPROM฀devices,฀however,฀may฀be฀erased฀through฀several฀minutes’฀exposure฀ to฀ultraviolet฀light.฀Electrically,฀Microchip’s฀EPROM฀and฀EEPROM฀chips฀use฀the฀same฀technology,฀ with฀EPROM฀chips฀being฀encapsulated฀in฀opaque฀epoxy.฀EEPROM฀chips฀have฀a฀quartz฀window฀through฀ which฀UV฀light฀may฀reach฀the฀chip฀surface.฀(After฀programming,฀you฀cover฀the฀window฀with฀an฀opaque฀ label฀to฀prevent฀erasure฀through฀ambient฀sunlight฀or฀fluorescent฀light฀exposure.)฀EPROM฀PICs฀may฀be฀ useful฀in฀small฀to฀medium฀volume฀production,฀but฀both฀EPROM฀and฀EEPROM฀devices฀are฀rapidly฀be-ing฀supplanted฀by฀flash฀memory฀PICs. • Flash:฀Flash฀memory฀may฀be฀written฀to฀and฀erased฀electronically฀through฀the฀application฀of฀a฀program-ming฀voltage฀to฀the฀PIC.฀Flash฀memory฀may฀be฀written฀to฀hundreds฀of฀thousands฀of฀times฀without฀error฀ and,฀at฀room฀temperature,฀based฀on฀extrapolated฀life฀testing,฀will฀retain฀data฀for฀100฀years.฀Flash฀is฀ideal฀ for฀developing฀programs฀and฀learning฀MBasic,฀as฀revising฀code฀and฀writing฀the฀revised฀program฀to฀flash฀ requires฀well฀under฀a฀minute฀for฀all฀but฀the฀longest฀programs. Looking฀at฀the฀price฀of฀chips฀of฀similar฀performance฀and฀capacity฀with฀EPROM,฀EEPROM฀and฀flash฀ memory฀types,฀it’s฀easy฀to฀see฀why฀flash฀devices฀are฀taking฀an฀increasing฀share฀of฀the฀market.฀

Memory฀Type Part฀Number Packaging Unit฀Cost฀

EEPROM฀(UV฀erasable) 16CE625/JW Ceramic฀windowed฀18-pin฀DIP฀(CDIP)฀Type฀“JW” $10.64 EPROM฀(one-time฀programming) 16CE625/P Plastic฀18-pin฀DIP฀(PDIP)฀Type฀“P” $4.38

Flash 16F628A Plastic฀18-pin฀DIP฀(PDIP)฀Type฀“P” $3.05 Finally,฀within฀each฀category,฀Microchip฀offers฀standard฀voltage฀(5฀volt฀nominal)฀and฀extended฀voltage฀ (minimum฀voltage฀dependent฀upon฀memory฀type;฀compatible฀with฀5฀volt฀supply;฀some฀with฀built-in฀regula-tor฀for฀operation฀from฀higher฀voltages.)฀PIC’s฀also฀have฀a฀wide฀variety฀of฀memory฀size,฀internal฀peripheral฀ options,฀temperature฀ranges,฀maximum฀operating฀frequency฀and฀packaging.฀These฀variants฀are฀identified฀ through฀associated฀alphanumeric฀designators.

(19)

Memory฀and฀Voltage฀Designators Memory/Voltage฀ Letter Memory/Voltage฀Type C EPROM CR ROM CE One-time฀programmable฀(EPROM)฀and฀ EEPROM฀(erasable) F Flash HV High฀Voltage฀(15V) LF Low฀Voltage฀Flash LC Low฀Voltage฀One-time฀programmable LCR Low฀Voltage฀ROM Temperature฀Range฀Designators Temperature฀ Letter Temperature฀Range C Commercial฀0°C฀to฀+85°C฀ I ฀Industrial฀-40°C฀to฀+85°C E Extended฀-40°C฀to฀+125°C฀ Partial฀List฀of฀Package฀Designators Package฀Option฀฀Letter Package฀ JW Ceramic฀window฀(EEPROM฀only) P฀ Plastic฀DIP SP/PJ฀for฀28฀pin฀x฀0.3฀(“skinny-dip”) SN,฀OA,฀SM,฀SL,฀OD,฀SO,฀SI SOIC-plastic฀small฀outline;฀surface฀mount PQ QFP-Plastic฀quad฀flatpack฀surface฀mount SS SSOP-plastic฀shrink฀small฀outline฀surface฀ mount ML Chip฀scale฀package ST TSSOP-Plastic฀thin฀shrink฀small฀outline฀ surface฀mount PT TQFP-plastic฀thin฀quad฀flatpack

Which฀One฀Should฀I฀Use?

Let’s฀look฀at฀the฀PICs฀supported฀by฀MBasic.฀ PICS฀Supported฀by฀MBasic ฀฀฀Device Data฀ RAM ADC Program฀

Memory Serial฀I/O Speed Timers

Low฀Voltage฀ Device PIC12CE673 128 4 1024 10 1+WDT PIC12LCE673 PIC12CE674 128 4 2048 10 1+WDT PIC12LCE674 PIC12F629 64 - 1024 20 2+WDT PIC12F629 PIC12F675 64 4 1024 20 2+WDT PIC12F675 PIC16C554 80 - 512 20 1+WDT PIC16LC554 (continued)

(20)

PICS฀Supported฀by฀MBasic

฀฀฀Device

Data฀ RAM ADC

Program฀

Memory Serial฀I/O Speed Timers

Low฀Voltage฀ Device PIC16C558 128 2048 20 1+WDT PIC16LC558 PIC16C620 80 512 20 1+WDT PIC16LC620 PIC16C620A 96 512 40 1+WDT PIC16LC620A PIC16C621 80 1024 20 1+WDT PIC16LC621 PIC16C621A 96 1024 40 1+WDT PIC16LC621A PIC16C622 128 2048 20 1+WDT PIC16C622 PIC16C622A 128 2048 40 1+WDT PIC16LC622A

PIC16C62A 128 2048 I²C,฀SPI 20 3+WDT PIC16LC62A

PIC16C62B 128 2048 I²C,฀SPI 20 3+WDT PIC16LC62B

PIC16C63 192 4096 USART,฀I²C,฀SPI 20 3+WDT PIC16LC63

PIC16C63A 192 4096 USART,฀I²C,฀SPI 20 3+WDT PIC16LC63A

PIC16C642 176 4096 20 1+WDT PIC16LC642

PIC16C64A 128 2048 I²C,฀SPI 20 3+WDT PIC16LC64A

PIC16C65A 192 4096 USART,฀I²C,฀SPI 20 3+WDT PIC16LC65A

PIC16C65B 192 4096 USART,฀I²C,฀SPI 20 3+WDT PIC16LC65B

PIC16C66 368 8192 USART,฀I²C,฀SPI 20 3+WDT PIC16LC66

PIC16C662 176 4096 20 1+WDT PIC16LC662

PIC16C67 368 8192 USART,฀I²C,฀SPI 20 3+WDT PIC16LC67

PIC16C71 36 4 1024 20 1+WDT PIC16LC71 PIC16C710 36 4 512 20 1+WDT PIC16LC710 PIC16C711 68 4 1024 20 1+WDT PIC16LC711 PIC16C712 128 4 1024 20 3+WDT PIC16LC712 PIC16C715 128 4 2048 20 1+WDT PIC16LC715 PIC16C716 128 4 2048 20 3+WDT PIC16LC716

PIC16C717 256 6 2048 I²C,฀SPI 20 3+WDT PIC16LC717

PIC16C72 128 5 2048 I²C™,฀SPI™ 20 3+WDT PIC16LC72

PIC16C72A 128 5 2048 I²C,฀SPI 20 3+WDT PIC16LC72A

PIC16C73A 192 5 4096 USART,฀I²C,฀SPI 20 3+WDT PIC16LC73A

PIC16C73B 192 5 4096 USART,฀I²C,฀SPI 20 3+WDT PIC16LC73B

PIC16C745 256 5 8192 USB,฀USART 24 3+WDT

PIC16C74A 192 8 4096 USART,฀I²C,฀SPI 20 3+WDT PIC16LC74A

PIC16C74B 192 8 4096 USART,฀I²C,฀SPI 20 3+WDT PIC16LC74B

PIC16C76 368 5 8192 USART,฀I²C,฀SPI 20 3+WDT PIC16LC76

PIC16C765 256 8 8192 USB,฀USART 24 3+WDT

PIC16C77 368 8 8192 USART,฀I²C,฀SPI 20 3+WDT PIC16LC77

PIC16C770 256 6 2048 I²C,฀SPI 20 3+WDT PIC16LC770

PIC16C771 256 6 4096 I²C,฀SPI 20 3+WDT PIC16LC771

PIC16C773 256 6 4096 USART,฀I²C,฀SPI 20 3+WDT PIC16LC773

PIC16C774 256 10 4096 USART,฀I²C,฀SPI 20 3+WDT PIC16LC774

PIC16C923 176 4096 I²C,฀SPI 8 3+WDT PIC16LC923

PIC16C924 176 5 4096 I²C,฀SPI 8 3+WDT PIC16LC924

PIC16CE623 96 512 30 1+WDT PIC16LCE623

PIC16CE624 96 1024 30 1+WDT PIC16LCE624

PIC16CE625 128 2048 30 1+WDT PIC16LCE625

(21)

PICS฀Supported฀by฀MBasic

฀฀฀Device

Data฀ RAM ADC

Program฀

Memory Serial฀I/O Speed Timers

Low฀Voltage฀ Device

PIC16F627 224 1024 USART 20 3฀+฀WDT PIC16LF627

PIC16F628 224 2048 USART 20 3฀+฀WDT PIC16LF628

PIC16F73 192 5 4096 I²C,฀SPI,฀USART 20 3+WDT PIC16LF73

PIC16F74 192 8 4096 I²C,฀SPI,฀USART 20 3+WDT PIC16LF74

PIC16F76 368 5 8192 I²C,฀SPI,฀USART 20 3+WDT PIC16LF76

PIC16F83 36 512 10 1+WDT PIC16LF83

PIC16F84 68 1024 10 1+WDT PIC16LF84

PIC16F84A 68 1024 20 1+WDT PIC16LF84A

PIC16F870 128 5 2048 USART 20 3+WDT PIC16LF870

PIC16F871 128 8 2048 USART 20 3+WDT PIC16LF871

PIC16F872 128 5 2048 I²C,฀SPI 20 3+WDT PIC16LF872

PIC16F873 192 5 4096 USART,฀I²C,฀SPI 20 3+WDT PIC16LF873

PIC16F873A 192 5 4096 USART,฀I²C,฀SPI 20 3+WDT PIC16LF873A

PIC16F874 192 8 4096 USART,฀I²C,฀SPI 20 3+WDT PIC16LF874

PIC16F874A 192 8 4096 USART,฀I²C,฀SPI 20 3+WDT PIC16LF874A

PIC16F876 368 8 8192 USART,฀I²C,฀SPI 20 3+WDT PIC16LF876

PIC16F876A 368 5 8192 USART,฀I²C,฀SPI 20 3+WDT PIC16LF876A

PIC16F877 368 8 8192 USART,฀I²C,฀SPI 20 3+WDT PIC16LF877

PIC16F877A 368 8 8192 USART,฀I²C,฀SPI 20 3+WDT PIC16LF877A This฀list฀may฀seem฀bewildering฀at฀first,฀so฀let’s฀go฀through฀the฀table’s฀parameters: Device—This฀is฀simply฀a฀short฀form฀of฀the฀device฀part฀number.฀ Data฀RAM —Data฀RAM฀specifies฀the฀amount฀(in฀bytes)฀of฀random฀access฀memory฀available฀to฀hold฀vari-ables฀in฀your฀MBasic฀program.฀Since฀MBasic฀requires฀some฀RAM฀for฀its฀internal฀use฀not฀all฀the฀Data฀ RAM฀will฀be฀available฀for฀your฀programs.฀RAM฀contents฀are฀lost฀whenever฀the฀power฀is฀removed฀from฀ the฀PIC.฀(Many฀devices฀include฀nonvolatile฀EEPROM฀memory฀as฀well.฀We’ll฀use฀EEPROM฀memory฀in฀ several฀sample฀programs฀in฀later฀chapters.)฀ Program฀Memory—Since฀PICs฀are฀Harvard฀architecture฀devices,฀the฀program฀and฀data฀memory฀are฀ separate.฀The฀Program฀Memory฀column,฀following฀Microchip’s฀documentation,฀identifies฀the฀program฀ memory฀size฀in฀program฀words.฀In฀the฀case฀of฀the฀PICs฀supported฀by฀MBasic,฀the฀word฀length฀is฀14฀ bits.฀The฀MBasic฀compiler,฀however,฀reports฀program฀memory฀use฀in฀8-bit฀bytes,฀as฀shown฀in฀Figure฀ 1-2.฀Should฀you฀wish฀to฀convert฀between฀the฀two,฀the฀compiler฀reports฀one฀14-bit฀word฀as฀1.75฀bytes,฀ and฀conversely,฀1฀byte฀represents฀0.57143฀ 14-bit฀words.฀ ADC—An฀analog-to-digital฀converter฀(ADC)฀ allows฀the฀PIC฀to฀read฀the฀value฀of฀an฀ analog฀voltage฀and฀convert฀it฀to฀a฀numeri-cal฀value.฀Depending฀upon฀the฀model,฀the฀ ADC฀may฀have฀8-bit,฀10-bit฀or฀12-bit฀ resolution.฀Chapter฀11฀shows฀how฀to฀use฀ the฀ADC฀to฀build฀a฀digital฀voltmeter. Figure฀1-2:฀Understanding฀MBasic’s฀memory฀usage฀report.

(22)

Serial฀I/O—Certain฀PICs฀have฀specialized฀hardware฀support฀of฀up฀to฀three฀serial฀standard฀protocols:฀The฀

USART฀ (universal฀synchronous฀asynchronous฀receiver฀transmitter)฀supports฀the฀common฀RS-232-type฀asynchronous฀protocol,฀as฀well฀as฀others;฀I²C฀(inter-integrated฀circuit)฀and฀SPI฀(serial฀peripheral฀ interface)฀are฀both฀primarily฀used฀to฀communicate฀between฀the฀PIC฀and฀other฀integrated฀circuits,฀such฀as฀ add-on฀memory,฀temperature฀sensors,฀serial฀number฀generators฀and฀the฀like.฀MBasic฀however,฀imple-ments฀RS-232-type฀serial฀communications฀(the฀SerIn฀and฀SerOut฀procedures),฀as฀well฀as฀I2C฀(the฀

I2Cin฀and฀I2Cout฀procedures)฀and฀SPI฀(via฀the฀ShiftIn฀and฀ShiftOut฀procedures)฀in฀software,฀

so฀all฀three฀protocols฀are฀available฀whether฀or฀not฀the฀PIC฀has฀the฀associated฀specialized฀hardware.฀ Indeed,฀in฀some฀respects฀MBasic’s฀software฀solution฀is฀superior,฀as฀it฀permits฀user-defined฀pin฀assign-ments,฀while฀Microchip’s฀hardware฀implementation฀is฀tied฀to฀specific฀pins.฀However,฀MBasic฀supports,฀ through฀the฀HserOut฀and฀HSerIn฀procedures,฀certain฀aspects฀of฀the฀USART฀hardware฀for฀those฀PICs฀ so฀equipped.฀We’ll฀see฀how฀this฀works฀in฀later฀chapters. Speed—The฀maximum฀clock฀speed฀in฀MHz฀that฀the฀PIC฀device฀type฀supports.฀Microchip฀produces฀lower฀ than฀maximum฀speed฀versions฀of฀some฀devices,฀however,฀so฀when฀purchasing฀a฀PIC฀check฀the฀speed฀ suffix.฀Don’t฀buy฀a฀PIC16F876-04/SP฀(4฀MHz฀maximum)฀if฀you฀want฀a฀PIC16F876-10/SP฀(10฀MHz)฀ or฀a฀PIC16F876-20/SP฀(20฀MHz)฀product!฀The฀price฀difference฀between฀the฀slower฀speed฀version฀of฀a฀ device฀type฀and฀the฀maximum฀speed฀version฀is฀usually฀modest. Timers—Timers฀are฀programmable฀internal฀counters.฀Among฀their฀many฀uses฀is฀to฀set฀up฀a฀periodic฀interrupt฀ signal฀that฀causes฀the฀PIC฀to฀perform฀the฀code฀at฀the฀interrupt฀handler.฀The฀watch฀dog฀timer฀(WDT)฀is฀ a฀specialized฀timer฀that฀may฀be฀used฀to฀detect฀and฀take฀action฀upon฀the฀main฀software฀freezing.฀Timers฀ and฀interrupts฀are฀the฀subject฀of฀Chapter฀10.

Low฀Voltage฀Device—Historically,฀PICs฀have฀required฀a฀5-volt฀power฀supply,฀or฀VDD฀voltage.฀With฀the฀trend฀ towards฀lower฀voltage฀logic,฀Microchip฀has฀responded฀with฀low฀voltage฀alternatives฀of฀its฀standard฀PIC฀ lineup.฀The฀low฀voltage฀chips฀are฀identified฀with฀an฀“L”฀in฀the฀suffix,฀and฀operate฀with฀as฀little฀as฀2.0฀V฀ VDD ,฀although฀slower฀speed฀may฀be฀necessary฀at฀the฀lower฀end฀of฀the฀operating฀voltage฀range.฀Fortu-nately,฀Microchip’s฀low฀voltage฀PICs฀also฀function฀with฀the฀traditional฀5฀V฀supply฀so฀they฀may฀be฀used฀ with฀Basic฀Micro’s฀ICP฀and฀development฀boards.฀

How฀Do฀I฀Pick฀One?

The฀first฀step฀is฀to฀identify฀your฀requirements฀and฀then฀find฀the฀matching฀devices. •฀ How฀many฀I/O฀pins฀do฀you฀need? •฀ How฀much฀RAM฀is฀required?฀Each฀byte฀variable฀declared฀in฀MBasic฀consumes฀one฀byte฀of฀RAM,฀each฀ word฀variable฀two฀bytes฀and฀each฀long,฀four฀bytes. •฀ How฀much฀program฀memory฀is฀required?฀As฀a฀crude฀estimate฀of฀the฀MBasic฀program฀size฀that฀fits฀into฀a฀ particular฀program฀memory฀capacity,฀you฀may฀assume฀400−1300฀words฀for฀library฀functions฀and฀8−20฀ words฀per฀line฀of฀executable฀code,฀depending฀upon฀the฀compiler฀optimization฀choice฀(minimum฀size฀or฀ maximum฀speed),฀the฀mix฀of฀instructions฀used฀and฀the฀length฀of฀the฀program.฀The฀more฀different฀proce-dures฀and฀functions฀used,฀the฀larger฀the฀library฀requirement. •฀ How฀fast฀a฀device฀meets฀your฀speed฀requirements? •฀ Do฀you฀need฀special฀purpose฀functions,฀such฀as฀an฀A/D฀converter,฀a฀USART,฀specific฀timers฀or฀an฀inter-nal฀clock฀oscillator? •฀ How฀does฀the฀cost฀fit฀into฀the฀project฀budget? •฀ Do฀you฀want฀a฀one-time-programmable฀or฀a฀flash฀memory฀device? •฀ Are฀there฀physical฀package฀preferences?฀ •฀ Is฀low-voltage฀operation฀necessary?

(23)

If฀your฀requirements฀are฀uncertain,฀start฀with฀the฀largest,฀most฀fully฀equipped฀PIC฀available,฀and฀refine฀your฀ device฀selection฀later฀as฀you฀are฀further฀along฀the฀process.฀MBasic฀makes฀this฀process฀especially฀easy,฀as฀ the฀same฀MBasic฀code฀runs฀on฀any฀supported฀PIC,฀except,฀of฀course,฀for฀a฀handful฀of฀instructions฀dependent฀ upon฀particular฀hardware฀features.

So,฀Which฀One฀Do฀I฀Really฀Want฀to฀Use?

While฀your฀choice฀of฀PIC฀may฀be฀critical฀if฀you฀are฀planning฀a฀production฀run฀of฀100,000฀products,฀for฀ general฀experimentation฀and฀education,฀I฀prefer฀the฀16F876฀and฀16F877฀devices,฀either฀the฀original฀or฀the฀ ‘876A/‘877A฀versions.฀(For฀most฀purposes,฀there฀isn’t฀a฀significant฀difference฀between฀the฀original฀and฀ A฀suffixed฀‘876฀and฀‘877฀PICs.)฀฀Of฀the฀chips฀currently฀usable฀with฀MBasic,฀these฀four฀devices฀offer฀the฀ maximum฀available฀program฀memory฀(8192฀words),฀the฀maximum฀RAM฀(368฀bytes)฀and฀have฀the฀other฀ “bells฀and฀whistles”฀offered฀by฀Microchip,฀such฀as฀a฀USART฀and฀an฀A/D฀converter.฀For฀a฀particular฀project,฀ the฀choice฀between฀the฀two฀is฀driven฀by฀the฀number฀of฀input/output฀pins฀required,฀with฀the฀‘876฀chips฀having฀ a฀maximum฀of฀22฀possible฀I/O฀pins,฀while฀the฀‘877฀chips฀increase฀to฀33฀possible฀I/O฀pins.฀And,฀of฀course,฀ these฀are฀flash฀memory฀devices฀so฀we฀need฀not฀worry฀about฀UV฀erasers. For฀smaller฀projects,฀the฀16F628฀is฀worthy฀of฀consideration.฀It฀is฀available฀in฀an฀18-pin฀package,฀so฀it฀must฀ be฀used฀with฀Basic฀Micro’s฀0818฀development฀board.฀The฀‘628฀has฀a฀maximum฀I/O฀capacity฀of฀16฀pins,฀and฀ has฀generous฀224฀bytes฀RAM฀and฀2048฀words฀of฀program฀memory.฀It฀does฀not฀have฀an฀ADC. Finally,฀for฀jobs฀that฀require฀a฀tiny฀PIC,฀the฀12F629฀and฀12F675฀devices฀are฀useful.฀Both฀have฀a฀small฀ footprint฀(8-pin฀DIP฀package),฀1024฀words฀of฀program฀memory฀and฀64฀bytes฀of฀RAM.฀The฀12F629฀does฀not฀ have฀an฀A/D฀converter,฀while฀the฀12F675฀does.฀Both฀permit฀up฀to฀six฀of฀their฀eight฀pins฀to฀be฀used฀for฀I/O฀ purposes.฀Either฀chip฀may฀be฀used฀with฀Basic฀Micro’s฀0818฀development฀board.

Basic฀Micro’s฀MBasic876฀Compiler

The฀CD-ROM฀accompanying฀this฀book฀includes฀a฀free฀MBasic876฀compiler฀from฀Basic฀Micro.฀MBa-sic876฀is฀a฀complete,฀100%฀functional฀version฀of฀MBasic,฀limited฀in฀that฀it฀works฀only฀with฀the฀16F876฀and฀ 16F876A฀devices.฀To฀use฀MBasic876฀as฀intended,฀with฀integrated฀debugging฀and฀interactive฀programming,฀ you฀will฀need฀to฀purchase฀Basic฀Micro’s฀in-circuit฀programmer฀(ISP-PRO)฀and฀its฀2840฀Development฀Board.฀ Or,฀if฀you฀are฀willing฀to฀sacrifice฀integrated฀debugging฀and฀interactive฀programming—both฀features฀of฀great฀ benefit—you฀may฀use฀MBasic876’s฀output฀HEX฀code฀with฀third-party฀PIC฀programmers.฀We’ll฀look฀at฀the฀ ISP-PRO฀and฀2840฀Development฀Board฀in฀Chapter฀2. You฀should฀not฀regard฀MBasic876’s฀restriction฀to฀the฀‘876฀and฀‘876A฀devices฀as฀a฀serious฀limit,฀as฀these฀ chips฀are฀feature฀and฀performance฀rich฀and,฀in฀fact,฀are฀the฀most฀advanced฀mid-range฀PICs฀available฀in฀a฀ 28-pin฀package.฀With฀only฀a฀handful฀of฀exceptions—where฀more฀I/O฀pins฀are฀required฀than฀are฀available฀ in฀a฀28-pin฀package—every฀circuit฀in฀this฀book฀can฀be฀constructed฀with฀an฀‘876/876A,฀and฀the฀associated฀ programs฀compiled฀by฀MBasic876.฀

(24)

References

[1-1]฀฀ Microchip฀provides฀a฀wealth฀of฀PIC฀information,฀available฀for฀free฀downloading,฀on฀its฀Internet฀website,฀ http://www.microchip.com.฀All฀are฀worth฀reading,฀but฀of฀particular฀interest฀to฀beginners฀are฀the฀intro-ductory฀tutorials฀found฀at฀http://www.microchip.com/1010/suppdoc/design/toots/index.htm฀including: •฀ Analog-to-digital฀conversion:฀http://www.microchip.com/download/lit/suppdoc/toots/adc.pdf฀ •฀ Device฀configuration:฀http://www.microchip.com/download/lit/suppdoc/toots/config.pdf •฀ Power฀considerations:฀http://www.microchip.com/download/lit/suppdoc/toots/power.pdf •฀ On-chip฀Memory:฀http://www.microchip.com/download/lit/suppdoc/toots/ramrom.pdf ฀ Product฀line฀card:฀http://www.microchip.com/1010/pline/picmicro/index.htm฀contains฀a฀detailed฀table฀ identifying฀the฀capabilities฀of฀the฀Microchip฀product฀line. [1-2]฀฀A฀complete฀data฀sheet฀for฀most฀PICs฀comprises฀two฀elements;฀(a)฀a฀detailed฀“family”฀reference฀manual฀ and฀(b)฀the฀particular฀device฀data฀sheet.฀MBasic฀supports฀only฀PICs฀from฀Microchip’s฀“midrange”฀fam-ily฀and฀the฀associated฀PICmicro™฀Mid-Range฀MCU฀Family฀Reference฀Manual฀may฀be฀downloaded฀ at฀http://www.microchip.com/download/lit/suppdoc/refernce/midrange/33023a.pdf.฀This฀is฀a฀688-page฀ document,฀in฀almost฀mind฀numbing฀detail,฀but฀nonetheless฀is฀an฀essential฀reference฀to฀a฀complete฀ understanding฀of฀PICs.฀For฀individual฀PIC฀family฀member฀datasheets,฀the฀easiest฀source฀is฀to฀go฀to฀ http://www.microchip.com/1010/pline/picmicro/index.htm฀and฀select฀either฀the฀PIC12฀or฀PIC16฀group฀ and฀from฀that฀link฀then฀select฀the฀individual฀PIC฀device. General฀note฀on฀web฀addresses:฀Manufacturers฀periodically฀reorganize฀their฀websites,฀so฀the฀URLs฀in฀this฀ book฀may฀change฀from฀those฀given฀as฀references.฀The฀documents,฀however,฀may฀be฀easily฀found฀through฀the฀ manufacturer’s฀home฀page฀search฀function,฀or฀through฀a฀general฀search฀engine฀such฀as฀Google.฀

(25)

C H A P T E R ฀

2

MBasic฀Compiler฀and฀

Development฀Boards

The฀Compiler฀Package

A฀Note฀on฀Compiler฀Versions By฀the฀time฀this฀book฀is฀published,฀Basic฀Micro฀will฀have฀released฀an฀updated฀MBasic฀compiler฀(version฀ 5.3.0.0)฀and฀rationalized฀its฀compiler฀family,฀dropping฀its฀“standard”฀version฀compiler,฀making฀the฀former฀ “professional”฀version฀its฀flagship฀PIC฀compiler.฀(If฀you฀are฀still฀using฀version฀5.2.,฀check฀with฀Basic฀Micro฀ for฀upgrade฀information.฀Owners฀of฀MBasic฀Professional฀version฀5.2฀qualify฀for฀a฀free฀upgrade,฀while฀MBa-sic฀Standard฀owners฀qualify฀for฀a฀reduced฀price฀upgrade฀to฀MBasic-Professional.)฀In฀addition,฀Basic฀Micro฀ has฀a฀made฀available฀a฀free฀version฀of฀its฀MBasic฀Professional฀compiler,฀MBasic876฀on฀the฀CD-ROM฀as-sociated฀with฀this฀book.฀MBasic876฀is฀a฀complete,฀100%฀functional฀version฀of฀MBasic฀Professional,฀limited฀ to฀working฀only฀with฀the฀16F876฀and฀16F876A฀devices. All฀programs฀in฀this฀book฀were฀originally฀developed฀and฀tested฀with฀MBasic฀Professional,฀version฀5.2.1.1฀ and฀have฀been฀verified฀with฀a฀pre-release฀version฀of฀5.3.0.0.฀However,฀bug-fixes฀and฀other฀“tweaking”฀to฀the฀ official฀release฀version฀5.3.0.0฀may฀occur฀that฀introduce฀minor฀incompatibilities฀between฀the฀code฀in฀this฀ book฀and฀Basic฀Micro’s฀ultimately฀released฀compiler.฀The฀CD-ROM฀associated฀with฀this฀book฀provides฀both฀ 5.2.1.1-compliant฀and฀5.3.0.0-compilant฀source฀code.฀Chapter฀29฀summarizes฀the฀differences฀between฀ver-sion฀5.3.0.0฀and฀5.2.1.1. Unless฀specifically฀noted,฀this฀book฀assumes฀you฀are฀using฀MBasic฀or฀MBasic876,฀version฀5.3.0.0.฀The฀ printed฀program฀listings฀are฀for฀version฀5.3.0.0. MBasic฀Compiler As฀used฀in฀this฀book,฀Basic฀Micro’s฀MBasic฀compiler฀comprises฀three฀main฀elements: 1.฀ MBasic฀Compiler฀Software—From฀version฀5.3.0.0฀onward,฀Basic฀Micro฀offers฀one฀version฀of฀its฀MBa-sic฀compiler,฀the฀“Professional”฀version.฀MBasic฀runs฀under฀Microsoft’s฀Windows฀operating฀system฀in฀ any฀version฀from฀Windows฀95฀to฀Windows฀XP.฀The฀computer฀requires฀an฀RS-232฀port฀for฀connection฀to฀ the฀ISP-PRO฀programmer฀board.฀A฀second฀RS-232฀port,฀although฀not฀essential,฀is฀useful฀to฀capture฀any฀ serial฀information฀from฀the฀program฀you฀are฀developing.฀If฀your฀computer฀does฀not฀have฀a฀second฀serial฀ port,฀but฀does฀have฀a฀USB฀port,฀you฀may฀wish฀to฀add฀one฀using฀an฀inexpensive฀USB-to-serial฀converter. 2.฀ ISP-PRO฀Programmer—MBasic,฀after฀the฀assembly฀stage฀completes,฀generates฀Microchip-compatible฀ standard฀HEX฀code฀file฀that฀must฀be฀loaded฀into฀the฀PIC.฀Basic฀Micro฀offers฀a฀programmer,฀the฀ISP-PRO,฀well฀integrated฀with฀the฀MBasic฀compiler฀that฀automatically฀loads฀HEX฀code฀file.฀A฀major฀plus฀ of฀Basic฀Micro’s฀ISP-PRO฀is฀real-time฀debugging฀through฀its฀“in-circuit฀debugging”฀or฀ICD฀capabil-ity.฀Although฀it฀would฀be฀possible฀to฀substitute฀a฀third-party฀programmer฀for฀the฀ISP-PRO,฀losing฀both฀

(26)

seamless฀integration฀with฀the฀compiler฀and฀ICD฀ability฀more฀than฀offsets฀any฀cost฀savings.฀The฀ISP-PRO฀communicates฀with฀the฀computer฀running฀MBasic฀via฀an฀RS-232฀cable,฀and฀with฀the฀PIC฀to฀be฀ programmed฀through฀a฀6-wire฀RJ11฀telephone-type฀cable฀for฀Basic฀Micro’s฀development฀and฀prototype฀ boards,฀or฀a฀10-pin฀standardized฀header฀for฀other฀boards. 3.฀ Development฀Board—Basic฀Micro฀offers฀plug฀board฀style฀development฀boards฀and฀solder-in฀prototype฀ boards฀for฀8-฀and฀18-pin฀and฀28-฀and฀40-pin฀PICs.฀The฀experiments฀in฀this฀book฀assume฀the฀user฀has฀ Basic฀Micro’s฀development฀boards.฀These฀boards฀have฀an฀RJ11฀connector฀for฀the฀ISP-PRO฀connection฀ and฀an฀uncommitted฀RS-232฀port฀that฀may฀be฀used฀by฀the฀PIC฀for฀communications฀to฀the฀outside฀world. Note฀on฀Serial฀Ports:฀The฀single฀largest฀source฀of฀trouble฀reported฀in฀calls฀to฀Basic฀Micro’s฀help฀line฀ concerns฀unreliable฀serial฀port฀connections฀with฀laptop฀computers.฀The฀built-in฀serial฀port฀on฀many฀laptop฀ computers฀cannot฀reliably฀operate฀at฀115.2฀kb/s,฀the฀default฀speed฀at฀which฀the฀PC-to-ISP-PRO฀communica-tions฀link฀operates.฀In฀those฀cases,฀Basic฀Micro฀suggests฀using฀an฀inexpensive฀add-on฀USB-to-serial฀adapter฀ to฀substitute฀for฀the฀built-in฀serial฀port฀and฀recommends฀Bafo฀Technologies’฀BF-180฀USB-to-serial฀adapter.฀ A฀slightly฀more฀expensive฀alternative฀that฀I฀have฀had฀reliable฀results฀with฀is฀Belkin’s฀F5U109,฀sold฀as฀a฀ “USB฀PDA฀Adapter,”฀but฀which฀is,฀in฀fact,฀a฀straight฀USB-to-serial฀adapter.฀Many฀other฀USB-to-serial฀adapters฀likely฀will฀ provide฀reliable฀results. In฀addition฀to฀the฀development฀and฀prototype฀boards,฀the฀ISP-PRO฀is฀compatible฀with฀Basic฀Micro’s฀Universal฀Adapter.฀The฀ Universal฀Adapter,฀however,฀does฀not฀contain฀an฀oscillator฀or฀ the฀other฀circuitry฀needed฀to฀actually฀run฀a฀PIC฀program,฀and฀ is฀intended฀for฀programming฀only.฀

BASIC฀and฀Its฀Essentials

This฀book฀is฀not฀intended฀to฀teach฀BASIC฀programming฀from฀the฀ground฀up.฀There฀are฀many฀good฀“BA-SIC฀programming฀for฀the฀beginner”฀books฀and฀we฀assume฀the฀reader฀has฀at฀least฀passing฀familiarity฀with฀ program฀control฀statements,฀mathematic฀procedures฀and฀variable฀assignment฀and฀structure.฀It฀also฀assumes฀ the฀reader฀has฀installed฀the฀MBasic฀compiler฀(either฀the฀full฀version฀or฀MBasic876,฀version฀5.3.0.0฀as฀of฀ the฀date฀of฀writing)฀and฀has฀familiarized฀himself฀with฀the฀first฀80฀pages฀or฀so฀in฀the฀MBasic฀User’s฀Guide.฀ Incidentally,฀because฀MBasic฀is,฀in฀some฀respects,฀a฀return฀to฀the฀early฀days฀of฀micro฀computer฀language฀ implementation,฀I’ve฀found฀20-year฀old฀reference฀documents฀for฀IBM’s฀Personal฀Computer฀BASIC฀benefi-cial฀in฀refreshing฀my฀memory฀on฀some฀of฀the฀finer฀points฀of฀BASIC฀syntax฀or฀procedure฀and฀of฀considerably฀ more฀help฀than฀modern฀books฀detailing,฀for฀example,฀Visual฀Basic.฀A฀visit฀to฀your฀local฀used฀bookstore฀may฀ turn฀up฀useful฀reference฀material.฀I’ve฀provided฀the฀names฀of฀a฀few฀of฀my฀favorite฀long-out-of-print฀BASIC฀ references฀in฀this฀chapter’s฀reference฀section.฀ As฀a฀guide฀to฀finding฀the฀appropriate฀procedure,฀Table฀2-1฀groups฀MBasic’s฀commands฀into฀a฀logical฀฀ classification.฀ Table฀2-1:฀Taxonomy฀of฀MBasic฀functions฀and฀procedures.

Group Procedure Group Procedure

Program฀Flow Repeat/Until While/WEND Do/While For/Next If/Then/Else/EndIf GoTo GoSub/Return Branch Hardware฀Related ADIN ADIN16 Count HPWM SetCapture GetCapture SetCompare Figure฀2-1:฀ISP-PRO฀and฀RJ-11฀jumper฀cable.

(27)

Table฀2-1:฀Taxonomy฀of฀MBasic฀functions฀and฀procedures.

Group Procedure Group Procedure

Pin฀Related Button Low PulsIn PulsOut RCTime Reverse Toggle SetPullups INxx Outxx Dirxx Miscellaneous DeBug End Let Nap Sleep Stop EEPROM฀ Data Read ReadDM Write WriteDM Variables Clear Swap I/O I2Cin I2Cout Owin Owout SerDetect SerIn SerOut ShiftIn฀ ShiftOut HSerIn HSerOut Sound฀and฀Sound฀Related DTMFOut DTMFOut2 FreqOut PWM Sound Sound2 LCD LCDWrite฀ LCDRead LCDInit Data฀Table LookDown LookUp Timing Pause PauseUs PauseClk Memory฀Related Peek Poke

Random฀Generator Random Program฀Memory ReadPM

WritePM On฀Reset OnPOR OnBOR OnMOR Explicit฀External฀ Device฀Support Servo SPMotor Xin Xout Interrupts Enable Disable OnInterrupt SetExtInt SetTmr0 SetTmr1 SetTmr2 IsrASM GetTimer1 Assembler ASM฀{} ISRASM

(28)

Table฀2-1:฀Taxonomy฀of฀MBasic฀functions฀and฀procedures.

Group Procedure Group Procedure

Command฀Modifiers Dec Hex Bin Str Sdec Shex Sbin Ihex Ibin ISHex ISBin REP Real WaitStr Wait Skip Math฀Operators฀ and Functions + -* LowMult HighMult FractionalMult / // ABS SIN COS DCD SQR BIN2BCD BCD2BIN Max Min Dig Rev Bitwise฀Operators ! & | ^ >> << Comparison฀Operators = <> < > <= >= Logical฀Operators And Or Xor Not฀And Not฀Or Not฀Xor Floating฀Point฀Conversion ToInt ToFloat FloatTable

Development฀Boards

Basic฀Micro฀offers฀two฀breadboard฀style฀development฀boards;฀models฀0818฀for฀8-฀and฀18-pin฀DIP฀PICs,฀ (Figure฀2-2),฀and฀the฀2840฀for฀28฀and฀40-pin฀DIP฀PICs,฀(Figure฀2-3).฀Both฀boards฀have฀a฀small฀solderless฀ plug-in฀area฀for฀additional฀components฀and฀are฀full฀assembled฀with฀surface฀mount฀components.฀Sockets฀are฀ installed฀for฀the฀PICs.฀An฀expanded฀development฀board,฀is฀under฀development฀and฀may฀be฀available฀by฀the฀ time฀this฀book฀is฀published.

Figure฀ 2-2:฀ Basic฀ Micro’s฀ 0818฀ development฀฀ board.

Figure฀ 2-3:฀ Basic฀ Micro’s฀ 2840฀ development฀ board.

Additionally,฀Basic฀Micro฀offers฀corresponding฀semi-permanent฀prototype฀boards,฀models฀08/18,฀Figure฀ 2-4฀and฀28/40,฀Figure฀2-5฀differing฀from฀the฀development฀boards฀in฀that฀additional฀components฀are฀to฀be฀ soldered฀in฀rather฀than฀plugged฀into฀a฀solderless฀breadboard.฀These฀are฀sold฀as฀bare฀boards,฀but฀Basic฀Micro฀ also฀offers฀an฀inexpensive฀complete฀parts฀kit.฀The฀prototype฀boards฀use฀through-hole฀components.

(29)

All฀four฀boards฀permit฀in-circuit฀programming—that฀is,฀the฀ PIC฀may฀be฀programmed฀without฀removing฀it฀from฀your฀ board,฀or฀disconnecting฀its฀pins฀from฀whatever฀you฀may฀have฀ connected฀to฀them.฀Figure฀2-6,฀a฀simplified฀block฀diagram฀of฀ the฀28/40฀prototype฀board,฀shows฀how฀this฀is฀possible.฀Three฀ of฀the฀pins฀required฀for฀programming,฀RB4,฀RB6฀and฀RB7,฀ are฀switched฀through฀a฀74HC4053฀analog฀multiplexer/de-multiplexer฀between฀their฀normal฀connection฀to฀the฀PIC฀pin฀ header฀and฀the฀RJ11฀socket฀that฀connects฀to฀the฀ISP-PRO฀ programmer.฀For฀our฀purpose,฀the฀74HC4053฀can฀be฀regarded฀ as฀an฀electronic฀three-pole฀double฀throw฀switch,฀controlled฀by฀ the฀ISP-PRO.฀The฀MCLR฀(master฀clear)฀pin฀is฀the฀fourth฀con-nection฀required฀for฀programming฀and฀is฀directly฀connected฀ to฀the฀RJ11฀programming฀socket. The฀0818฀and฀08/18฀boards฀follow฀a฀similar฀design,฀but฀with฀ extra฀configuration฀jumpers฀necessitated฀by฀the฀multiple฀func-tions฀Microchip฀assigned฀to฀certain฀pins฀of฀PICs฀produced฀in฀8฀ and฀18-pin฀packages.฀The฀0818฀and฀08/18฀data฀sheets฀should฀ be฀consulted฀before฀programming฀these฀small฀PICs. All฀four฀boards฀bring฀the฀various฀PIC฀pins฀to฀logically฀ labeled฀headers;฀for฀example,฀A0,฀A1,฀so฀you฀don’t฀have฀to฀continually฀cross-reference฀physical฀pin฀numbers฀ with฀their฀logical฀assignments.฀ In฀working฀with฀Basic฀Micro’s฀development฀boards฀and฀ISP-PRO฀programmer฀watch฀out฀for฀the฀following: •฀ These฀are฀sold฀as฀bare฀boards,฀with฀unprotected฀traces฀on฀the฀bottom.฀Don’t฀put฀them฀down฀on฀conductive฀ surfaces฀or฀the฀board฀may฀be฀damaged฀and฀watch฀for฀stray฀wires฀or฀component฀leads฀as฀well.฀(I฀watched฀ my฀ISP-PRO฀board฀be฀dragged฀by฀its฀serial฀cable฀across฀the฀metal฀edge฀on฀the฀table฀and฀looked฀on฀help-lessly฀as฀sparks฀flew.฀Needless฀to฀say,฀the฀ISP-PRO฀didn’t฀work฀after฀that.)฀It฀helps฀to฀add฀small฀stick-on฀ rubber฀feet฀to฀the฀bottom฀of฀all฀boards. •฀ It฀is฀possible฀to฀damage฀the฀74HC4053฀electronic฀switch,฀as฀is฀rated฀at฀a฀maximum฀switched฀current฀of฀25฀ mA.฀The฀most฀likely฀damage฀scenario฀comes฀from฀forcing฀the฀PIC฀to฀sink฀excessive฀current.฀Additionally,฀ unlike฀a฀mechanical฀relay,฀the฀74HC4053฀introduces฀approximately฀80฀to฀100฀ohms฀of฀series฀resistance. Another฀difficulty฀beginners฀often฀have฀is฀confusing฀VDD฀and฀VSS฀when฀wiring฀circuits.฀VSS฀is฀ground฀in฀Basic฀ Micro’s฀development฀boards.฀VDD฀is฀the฀supply฀voltage฀and฀is฀+5฀volts฀in฀the฀development฀boards.฀Thus฀a฀ schematic฀reference฀to฀+5฀V฀is฀the฀same฀as฀VDD฀and฀a฀reference฀to฀ground฀corresponds฀to฀VSS .฀(This฀terminol-ogy฀comes฀from฀VDD฀as฀the฀“drain”฀voltage฀and฀VSS฀as฀the฀“source”฀voltage฀for฀a฀field฀effect฀transistor,฀the฀ basic฀building฀block฀of฀PICs.)฀

Figure฀ 2-4:฀ Basic฀ Micro’s฀ 08/18฀ prototype฀ board.

Figure฀ 2-5:฀ Basic฀ Micro’s฀ 28/40฀ prototype฀ board.

Figure฀ 2-6:฀ Simplified฀ block฀ diagram฀ of฀ 28/40฀ prototype฀board.

(30)

Programming฀Style

Every฀program฀printed฀in฀the฀text฀is฀also฀provided฀as฀a฀file฀in฀the฀accompanying฀CD-ROM฀with฀two฀versions฀ supplied—the฀originally฀developed฀programs฀compatible฀with฀MBasic฀version฀5.2.1.1.฀and฀a฀revised฀ver-sion฀compatible฀with฀5.3.0.0.฀There฀may฀be฀differences฀between฀the฀printed฀program฀and฀the฀CD-ROM฀for฀ several฀reasons: •฀ The฀CD-ROM฀is฀quicker฀to฀update฀and฀may฀have฀a฀later฀or฀corrected฀version฀of฀the฀text฀program. •฀ Page฀width฀and฀overall฀length฀restrictions฀make฀it฀necessary฀to฀limit฀the฀comments฀and฀blank฀spaces฀ used฀on฀the฀printed฀page.฀The฀data฀files฀have฀no฀similar฀restriction฀and฀hence฀may฀have฀additional฀com-ments฀and฀may฀be฀formatted฀for฀greater฀readability.฀(Although฀not฀documented฀in฀the฀User’s฀Guide,฀ MBasic฀uses฀the฀vertical฀bar฀“฀|฀”฀as฀a฀continuation฀symbol,฀thus฀allowing฀one฀logical฀line฀of฀BASIC฀ code฀to฀be฀split฀over฀two฀or฀more฀physical฀lines.) Standard฀Program฀Layout As฀an฀aid฀in฀readability฀and฀maintainability,฀I฀like฀to฀follow฀a฀standard฀layout฀when฀programming,฀as฀exem-plified฀in฀the฀following฀template:฀ ;Program฀Sample.Bas฀–฀File฀name฀ ;Version฀1.00 ;14฀September฀2003฀–฀original฀version ; ;Constants ;---Define฀constants฀here ;Variables ;---Declare฀variables฀here ;Initialization ;---Initialization฀code฀here฀–฀this฀code฀is฀executed฀only฀once Main ;---Main฀code฀segment฀here If฀something฀GoSub฀Sub1 If฀something฀else฀GoSub฀Sub2 If฀something฀totally฀different฀happens฀GoSub฀Sub3 GoTo฀Main฀;if฀appropriate฀to฀have฀a฀continuous฀loop฀฀ Sub1 ;---฀ Subroutine฀code฀here Return Sub2 ;---฀ Subroutine฀code฀here Return Sub3 ;---฀ Subroutine฀code฀here Includes฀GoSub฀SubSub1 Return SubSub1 ;---฀ Subroutine฀code฀goes฀here Return End

References

Related documents

• linking the posts, on the forums, you may specify that the owner of the website should introduce the hyperlinks, for example, when promoting ImmuOne product, you can specify

Index Terms —Structured Canonical Polyadic Decomposition, Vandermonde factors, MultiDimensional Harmonic Retrieval, modified Alternating Least Squares algorithm, Toeplitz rank-1

It is therefore being advised that IMF programmes put greater focus on institutional quality determinants so that it can perform better in terms of its objectives of

2012: NLDC received grant funding for treatment and additional surveys, chemical treatment of Island Lake and the Spider-Island channel was unsuccessful due to use of

Financial planning is a systematic approach whereby the financial planner helps the customer to maximize his existing financial resources by utilizing financial tools to

We will look at these dynamics and complexities in the case of two Soviet writers who were sent to East Asia in the mid-1920s with the aim of fulfilling both of the aims

[r]