123 PIC Microcontroller Experiments for the Evil Genius

  • 74 419 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

ISBN D 0? 1q51,'ra-0

ttnn llilXIJl

E3 PIf@

Mitrrtrcontroller Experiments fsr the Evi| Eenius

Evil6eniu5series 123Robotics Experimentsfor the Evil Genius Electrcnic Gad|etsfot the Evit Genius:28Build'it'yourself Prciec! Electonic Ci/cuits for the Eril GeniL.s:57Lesso^, with Prciects 123 PI(P Microcotltollel Eryeriments fol the Eril Geniut Mechatonics fol the Et il Genius:25 Build-it'youtself Prcject' 50AwesomeAutomoli'e Prcjectsfor the EviI Genius Sotat Energ! Proiectsfor the Evil Genius:16 Buikl'it'yoarselfThemoelectic MechdnicalPrcjects

and

25 Build'it'Yourself Prcjec1 Bionicsfol the Evit GeniLLs: MORE Etectronic Gadgetsfor the Evil Genius:28MORE Build'it'yourself Proiects

1 A 3P I E @

Mitrrotrontroller Experiments for the Evi| 6enius MYKE PREDKO

McCraly-Hill New York Chicago SanFrancisco Lisbon London Madid Mexico Cily Milan New Dcthi SanJuan Seoul Singapore SydDey Toronlo

Catiloging-in-PublicttionData is oDlilc with the Library of Congfcs!

copyrightO 2005byTheMcCraw-Hill CommpinicqInc.All rightsfcscrvedPrinledin thc UniredStolesorAnerica.Exceplaspermittedundcrthe UniledStalcsCoPvrightAct of 1976'to or dislributedin nnyforn or by anymcan$,otslored pnn of thispublicationmaybe reproduced in a dalabaseor rotricvalsyftD.vilhout theprif writLenpcrnhskn ol rhepublishcr' 3 4 5 6 7 8 9 0 Q P D / Q P D0 I 0 9 l J 7 ISBN 0-07145142-0 Thesponhtug edint lot thi\ hook trasJkd!)Bisr Ml the?roductiontttll!tui$r LLC ThcIrt dituctorJol easPMtle A. Pelton.ft wat NetibTinw Tat by l kAllistet PttblishiSService!, th! &v0 ,,a! Ahthory kndi. Ptink anl hantl bt Qu.h.cot/Dubuque.

{}Jlllbk::\,ti,''*t"nrocvored'ocid'riccraPcrconl0ini'saminiDUDor50Pcrccntrccvcred andsalesptomoto useaspr€mirims at specialquanlilydiscounts Mccraw-Hillkloks arcdvailable pleasewrileto thoDirectof tions,or tor usein corponlcLr.iningpro$am$ForInoreinformation, York,NYl0l2l-2298. O' contacr PcmPlaTa.New I'|ofessional,Two ofSpccialSales"McGraw-H

lnformatio! conlainedin this woik hasbeetrobtainedby The Mccraw-Hill Conpa_ bclieledto be reliabloHowever,neithcr nicr lno.("Mccraw-nill") lron sources Mocraw-Hill nor ih authoreSuaranteerhc accuracyor comPlebnessof any informdlbr hcrcir.andneitherMccraw-Hillnor itsauthosshallberesponsible tionpublished dnr eros, onn$iornor danagesarisingoul oi useof thisinfomation.Tbiswork h ural Mccraw Hill and its authos are suPplving with thc underst.nding Dublished informationbut arenot attempnngb renderprofe$ional senicesIi suchserricesare prolesional shoLrldbe soughl. required.tho alsislane of an aPProPnate

ft LOnIenrS gF

w€n w)E w"

w

w.

wt

t F&as i['tq ffii

ffi

asSt J6r!6 iqost

ff

{b .{.

Acknowledgments About the Author

SectionOne Underthe Coversof the PIC16F684 EXPERIMBNT1

I/OPins

EXPERIMENT 2

Configuration word

EXPERIMENT 4

PIC MCU Variable Memory,Registers, and ProgramMemory in Simulating cFlash.c MPLAB IDE

SectionTko IntroductoryC Programming

t3

18

27

C DataTypes

29

EXPERTMENT 7

ConstantFormatting

31

EXIERTMENT10 EXPERTMENT 11 EX?ERTMENT12

EX?ERTMENT13

ConditionalLooping

45

EXPERIMENT 16

TheFor Statement

46

Assignment Statements Expressions lJrtwrse uperators

33 35

LogicalExpressions ConditionalExecution Usingthe If Statement 39 NestedConditional Statements

SectionThree SimplePIC@MCU Applications 51 EXPERTMENT 17

BasicDelays

53

EXPERTMENT 18

PTCkit1 Sequencing StarterKit LEDS

55

BinaryNumberOutput UsingPICkit 1 Starter Kit LEDS

56

EXPERIMENT 20

BasicButtonInputs

58

EXPBRTMEN'r21

DebouncingButtonInputs 59

EXPBRIMENT22

MCLR Operatiorl

6I

EXPBRIMENT23

EndingApplications

63

EXPERIMENT 19

20

EX?ERIMENT6

EXPERIMENT9

EXPERTMENT 15

L4

28

EXPERIMENT8

43

x

VariableDeclaralion Statements

EXPERIMENT 5

q.f,

TheSwitchDecision Statement

EX?ERMENT14

t

Introduction

EXPERTMENT 3

ix

SectionFour C Language Features67 EXPERIMENT24

Functions andSubroutines 69

EXPERTMENT25

GlobalandLocalVariables70

EXPERIMENT26

Defi[esandMacros

7I

EXPERIMENT27

VadableAuays

73

EXPERIMENT28

Structures andUnions

75

EXPERTMEN.T 29

PointersandLists

'76

EXPERTMENT30

Character Strings

79

EXPERIMENT 31

LibraryFunctions

81

4I

SectionFlve PIC16F684Microcontroller 89 Built-in Functions ExPERrrtNT32 Brownout Reset

91

ADC Opcratiotl EXTERIMENT'33

93

34 ComparatolOperation EXpERINIENT

96

EXPER|"I|NT35 Watchdog'l'imer

99

36 ExpERlNlFrNr

Short Timer Delays

UsingTMRo 37 Exr,r,RrNrENr'

Usingl'MRt ComparingClock Oscillators

106

r'40 Pin Rcsislarce LlxPEnt\'lr-tN

t09

PwM SignirlJl t l | 41 Cunerirung F\11 RrN,rE\

Rcaction-TimeTester

140

52 ExPERltvtENT

Rokenbok@Monorail/ Tiaffic Lights

143

LED Seven-Segment Thelmometer

t46

PIC MCU "Piano"

151

Model Railway Switch Colltrol

lfJ

54 EXPDRIMENT

56 PC OperatingStatus ExPERIMENT Display

ExIERIMENT57 Thc asmTemplate.asm

SectionSix Interl'acingProjectsfor the PIC@Microcontroller ll'7

58 ExPERIMENT

ExpLRIMEN]43 Driving a Scvcn_Segmcnt

59 Loadingthe WREG and EXPERTMENT

L\"r Rr\I N"4 |

45 ExPERTMENT

LED Matrix DisPlaYS

,16 LCD Display EXPERTMENT ,lT ExpFtRrNrENl

Filc ard BasicDirectivcs 160

124 126

ProduciDg Random Numbers

128

ExpERrvrENr,l8

Two-Bit LCD Display

129

EXPERI4nNT49

Switch Matrix KeYPad MaPPing

t31

SpecifyingProgram MemoryAddresses

161

SavingIts Coitents

164

60 ExPERtMENT'

Detining Variables

165

6l EXI,ERIMENT

Bitwiselnstructions'i6'7

62 EXPLRTMENT

Additionlnstruclions

168

63 EXPERIMENT

AddLibs: Strange SimulatorResults

170

ll9

M u l t i l ' l cS c v q n - S c g m c t r t 121 LED Displays

156

SectionEisht Introductionto PIC@ MCU Assembly Language 159 Programming

I 42 StolingandRctrieving Exl,r-rRrN4BN t14 Data

DirectlY I-ED DisPlaY homthcPICl6F6U4

.

51 EXPERTMENT

EXlrinrMENT55

104

Mcasulcmcnts

LED Display 138 Pumpkin

53 EXPERIMENT

102 Usiig theTMRoPrescaler

137

50 EXPERTMENT

101

Exl,trtIMENr'38 LongTlnler Delays

f 39 ExPERTMEN

Section Seven SampleC Microcontroller Applications

64 EXPERIMEh*T

Sublractionlnstructions

65 EXPERTMENT

Bank Addressing

Bit lnstructions EXPERTMENT'66

Contents

']71 173 1'75

EXPERTMENT 67

r7'7

Bit Skip Instructions

EXPERIMEh-r 68

ConditionalExecution

178

EXfERTMENT 69

declszLooping

180

EXPERTMENT 70

Subroutines

181

EXPERTMENT 7l

Defining and ImplementingArrays

EXPERTMENT86 PIC MCU Instrument Interface

231

EXPERTMENT8T SoundDetection

235

EXPERTMENT 88 Multiple Miqoswitch Debouncing EXPERTMENT 89 EXPERIMENT 90

EXPERIMENT 72

LogicSimulation Using thePlC16F684

EXPER]MENT 73 TheC "Switch" Statcmont

188

191,

ExPEnIMENT74

Dcfines

ExPEnTNIENT 75

ConditionalAssembly 197

ExPERti\4ENr 76

Macros

194

199

EXPERIMEN].77l6-BitValues/Variables 201 ExPEnTMENT 78 EXPERIMENT 79 EXPERIMENT 80

UniversalDelayMacro 203 High-lavcl Pro$amming 205 lmplementing Read-Only 208

AIrays EXPERIMENT 8l

Data Stacks

210

EXPERTMENT 82

CircularBuffers

212

EXPERTMENT 83 Readingandw ting the

EEPROM Data Memory 214

SectionTen Sensors ExPERrMENr34PIC MCU BS2User lnterfacc

227 224

EXPERTMENT 85 PIC MCU BS2Keypad Interface 230

{",t,

:i i',1'

Light Sensors

23'7 238

I82

SectionNine PIC@Microcontroller AssemblyLanguage '187 ResourceRoutines

f"\

Infiared (IR) Surface Sensor

239

ExpERlMENr9l Intcrfacing10Shary cP2D120 Rangirg Object Sensors

242

ExPERTMENT 92 Do-lt-Yourself IR Object Sensor

243

ExPERTMENT' 93 lR Object-Ranging Sensor

24'7

ExpERTMENT 94 UltrasonicDistancoRangeSensor

249

ExPERrMENr95RoborIRTag

251

SectionDleven Motor Control

255

EXPERIMENT 96 DCMotor Driven Usi g the CCPPWM and Using a Potentiometer Control 257 E x P f R r v F \ r1 , 7 D C M o t o rC o n t r oxl i l t SimpleTMR0 PWM

261

ExpERIMENT98ControllingMultiple Motors with PWM aDd Bs2lntcdace

264

EXPERIMENT 99 Bipolar StepperMotor Control

265

EXpERIMENT 100 UnipolarStepperMotor Control 269 EXPERIMENT 101 Radio-Control Model Scrvo

Control EXPERTMENT 102 Multiple ServoControl SoftwareStructure

Contents

272 2'74

*J

?: !:"{a' iil

RobotBase 103Two-Servo EXPERTMENT 27'7 withBs2lnterface SectionTwelve SolvingProgramming Problemsin PIC@ Microcontroller AssemblyLanguage279 t ultiplicatitrn E \ p r R r M\rr 1 0 4 L i g h l - B iM with a 16-BitProduct 280 ExpERrMENl105 Division of a 16-Bitvalue by an Eight-Bil Valuc 282 a NumberUsing | lu6 Squrring Exr,r'RrMr'\ Finite DifferenceTheory 284 107 Find lhe SquareRool of a EXPERTMENT 28(t 16-BitNumber 108 Converlinga Byte into EXPERIMENT Hex, Three Decimal,T\'r'o Eight Binary ASCII or 289 Bytes 109 Produccthe Even Parity EXPERTMBNT 291 Valuesfor a Byte 1 I0 Sort a List of 10Eight-Bit EXPERTMENT ValuesUsingthe Bubble292 SortAlgo tllrn EXpERIMENT 111 Encrypt and Decrypt ull ASCIIZ SlringUsing a SimpleSubstitution 294 Algolithm E x p E R r \\4r lrl 2

Number Sequence

113 Find the LargestCommon EXPERTMENT FactorofTwo Eight-Bit

Numbers

298

SectionThirteen ZipZaps@Robot 301 EXPERIMEN T 114 Characte zing the

Zipzaps

303

PIC MCU PowerSupply 305 EXPERTMENT'115 116 EXPERTMENT

PIC MCU Electronics 307 PCB

I 17 EXPERIMF,NT

IRTV RemoteControl 310

I18 EXPERIMENT

Molor andSteering Control

311

119 EXPERTMENT

BasicThsk-Control Software

120 EXl ERTMENT

lR RemoteContlol

121 ExPERTMENT

and Lighl Sensors Followirlg

122 EXPERTMENT'

IR ObjectDetection 318 Sensors

123 ExPERTMENT

IR Linc-Followirlg Sensors

Index

C c n e r a t ce F i h o n a c c i

ii$ i--; !'1

# 4_.1 *

i)

t-: vti.r

29'7

Contents

316

320

327

F" 'tr

{'}

FcknouJledgments

!tj..:. i't!

.;:

tlt!!).:tt

;.::!:! rir:i,: :?rra,

-\ri

l't ij

q My edilor at Mccraw-Hill, JudyBass,who consistentlyrespondsto my questionsand suggestions, regardlessof how dunb they are, with good humor and thougltfulness.

This book would not have been possiblewithout t1le help, suggestions,and time from the fbllowing indi viduals: . CarolPopovich,Greg ADderson,Joc Drzewiecki,AndreNemat,and Fanic Duven$ho ha\e helpednre hageol I\4i(rochrn understandwhich productswould be besl suitedfor this book,and havebeenwilliDgto .pcndrimewith me lo undersrand my rcquircand m9nts,answermy legionsof questions, suggestavenuesto follow that hadn't occurred

supportstallfbr HT-Softandlhcir technical quicklyandholping D'ryqucstions answering to cxplailtheiDnerworkingsofthe PICC is Lirccomtiler.The PICC.linc of compilers proand tool for thebcginnct a lrcmcndous lo re.lc:)iunrlrlikc,andoneTncvcrhcsitatc onmlcDCl. andlhes!uBradNorth,RichardBonafede, Schoolilr dentsot RickHansenSccondary Onf.lrio,lbrhelpingmeto learn Missassauga, learn moreabouthowstudenls(andteachers) andthc PIC electronics, aboutprogramming, MCU BlairClarksonaDdDnvcPiloleat the Oltario pushingmeto CentreinToronlo,I'or Sciencc concepts to peoexplairbasicmicrocontroller Sumo" plethatluve built theTAB Electronics Botat a workshopandwantto do norc with ir. lhe BS2i rt'rfaccandRobotIR Tig cxpcrimentsarea direclresultofthiswork.

lo bc oneol lhd be(l lhc PIC MCU continucs .uppo cd dc! ice.on lir)e.3ndI !\ouldlike lo thalk the many individualswho havetaken the time lo put informationand projectson rhe lnlefncla\ \rcll assupporlandhclpntheN trying to better undclstandthe PIC MCU or get their applicalionsup and ruDning.

.

fbr ideas,answcrs Celeslica anditsemployccs questions, to and opPortunities to straDge expandmy lcchnicalhorizons,

.

My daughterMarya,whohasgrownup with a fatherlhatis alwaystryingout ncwprojecls on hcr.Shctricsthemout wilh cnlhusiasm needa bit of despitethclhcl theygenorolly tuningbelorctheyworkperlcctly.

.

for kecpingeverything My wilc Patience, daughler cvcnwhenour youngcst togethcr, Talithawassick,andfor havingdinneron the slove,evcnwilh measkinghc! 10do a"quick read"of a sectionor two.I couldn'tdo anyof it withoulyou.

To all ofyou,thankyoufor all youunselfishhelP expcdences, and andwillingness 1()shareyourideas, enlhusiasm for lhisbook. mgke

Acknouledgments

httpJ/rilirur.mske.Eom

ix

l.-ir (*

Nl"

{tr

s{n ;:.! f*

{,n

,')'$.iifi : ::i,ilfi !i!i:.1.-iti::i1i ii'ii#iirt ill,i:i$[ii]{g{S"*; i{w

Futhor Fbout the M ffi EGH @ W{ €w ffi

$SSg Sr$ss ffi

ffi

KS&&{ffi€

in Torontq Canada,a supplielof printedcircuitboardsto the comMyke hedko isTestArchitectat Celestica, 123Robotitr Prcjectsfor theEvil puterindustry.Aln expedenced author,Myke wroteMccraw'Hill's best-selling Progammingand CustomizingPICMirrc Microconlrolkrs, Genius;PICmicrcMictocontoller PocketRefercnce; RobotContollers;andotherbooks,andis theprincipaldesignerof bothTAB SecondEdition;P/oSramming EElecironics BuiM your Ov,nRobot kits,

h .tJ

5 {)

+-, ,.t

About the Author

Introduction When I wrote my first book on the Microchip PICP microcontroller (commonly abbreviated to "MCU") almost10 yearsago,the mostcommoncriticismI receivedaboutthe book wasthat it took too long to get to the projects.This is quite foreign to me becauseI tend to leam a new device,like a microconftoller,by first reviewingthe datasheets for the part's electrical information,workingat understandingthe architecture andhow it is programmed,and endingwith understandingwhat kind of developmenttools are availablefor the part.Looking over this list of tasks,it is quite obviousthat they cameaboutwith my backgroundand training.Being a teenagerin the 1970sand goingto universityin the early 1980qtherewasn'tthe variety of easy-to-work-with devicesthat are available today,and the sophisticated personalcomputer-based developmenttools that we take for grantedwerenot evenbeingconsidered, let alonebeingdevelopedor sold.My method for learning about a new part is effective for me and a result of the situation I found rnyself in when I first started working with electronics Today you can setup a development"lab" and createa basic applicationfrom scratchfor the PIC MCU in lessthan 20 minutes usingMicrochip's MPLAB@integrated developmentmvironmmt (IDE) andPlckitrM 1 starterkit with HT:.soft'sPICC LiterM c compiler. The purposeof this book is to introduceyou to the MicrochipPIC MCU and help you understandhow to createyour own applications. In this introduction, using the PICkit 1 sl2irterkit printed circuit board. (PCB) andfree developmenttoolsfrom Microchip andHT-Soft,I will showyou how easyit is to createa simplePIC MCU programthat will flashone of the light-emitting diodas (LEDs) on the PICkit 1 starter kit. As you work throughthe book,your understanding of the PIC devicewill increaseto the point where you shouldbe comfortablecreatingyour own complex applications in both the C programming languageaswell asassemblylanguage. The PICkit 1 starterkit (seeFigurei-1) contains everythingyou will needto leam how to createand testyour own PIC MCU applications. This includesa programmerPCB (seeFigurei-2), a universalserial bas (USB) cableto connectthe PICkit 1 starterkit to your PC,a CD-ROM containingthe sourcecodefor the applicationspresentedin this book,two PIC MCUs,an eight-pinPIC12F675,and a 14-pin PIC16F684. In this book,I will be focusingon the PIC16F684becauseits 14pins allow a greatervariety of different applicationsto be built from it, but you will also gain experiencewith the eight-pin PIC12F675.

On the back coverof the book is a web link that you canuseto order a PICkit 1 starterkit for usewith this book.If you do not buy a PICkit 1 starterkit, the sourcecodecanbe dolrinloadedfrom my web site (www.myke.com). In this book, I will be working exclusivelywith Microsoft Windows.I recorrmend that you use the latestversionavailable(at this witing it is Windows,XP SP2)whenworking with the PICC Lite compilerand MPLAB IDE tools usedin the book.Development toolsare availablefor Linux, althoughnot for Apple MacintoshOS/X (but you shouldbe ableto get the Windowssoftwareto work from an emulator).You will find that the softwareworks well underWindows. If you look at the CD-ROMSthat comewith the PICkit 1 starterkit, you will find they havethe MicrochipMPLAB IDE and HT:Soft PICC Lite compiler developmenttools that are usedin the book. Although you could load theseprogramsonto your PC,I recommendthat you downloadthe latestversionsfrom the Microchip and HT-Softweb sites.These tools are continuouslyupdated(duringthe period that this book waswritten,MPLAB IDE had five upgrades, two of them major and changedhow someof the operationsare performed)to includenew featuresand PIC MCU part numbers,and to fix any outstandingproblems.In this book. I usedMPLAB IDE version7.01

Figure il The Microchip PICkit I stsrterkit enablesyou to createyour own PIC MCU applications and to testthem out easily qnd inexpensively.

o.,

o

. i . n , ; :" 3 o . . & q < r . r . , . .

.dG9

EmbeddedSv$em Franc "o san t 0nlelence

.,"-..,,P, I:S:,"f,'i.'::.i'i.Yrf"!.'*...,"-..,

Figure i-? The PICkit I starterkit's PCB consisxof programmercircuitsalong with eightLEDI, a switch, and a potentiometerthat enableyou to easilylearn how to program and accesstheperipheralfeqturesof the PIC MCU.

and PICC Lite compilerversion8.05.With the versions that you use,you may seesomedifferencesin look or operation,but the featurespresentedin this book will all be present.If you are confusedasto how to perform someoperation,you canconsultthe T[torial sectionof MPLAB IDE, which canbe found under the Help pull-down. To start settingup the softwareneededto start go to developingyour own PIC MCU applications, www.htsoft.com, asshownin Figurei-3.PICC Lite is a free,full-featuredC compilerthat supportsquite a few of the differentPIC MCU part numbers(includingthe PIC12F675and PIC16F684includedin the PICkit 1 starterkit package).Next click "Downloads"and then select"PICC Lite (Windows)"(seeFigurei-4).To downloadPICC Lite compiler,you will haveto register with HT-Softat no charge(seeFigurei-5).To do this,follow the instructionson the pageshownin Figurei-4.It shouldgo without sayingthat the page will probablynot look exactlylike the figureshere,due to the delaybetweenwhen I havewritten this and whenyou actuallyaccessthe web site. The retail PICC compileris capableof building codefor literally all the PIC MCU part numbersand doesnot haveany of the restrictionsof the PICC Lite compiler. After registering,the PICC Lite compilerinstallation softwareshouldstart downloadingautomatically. Dependingon your secudtysettings(especiallyif you useMicrosoftWindows/XPServicePack2 or later), the downloadmay be blocked(asin Figurei-6). If this is the case,you may haveto tum off the securityto allow the downloadto take place.Oncethe program hasdownloaded,I recommendselecting"Run" instead of"Save" (seeFigurei-7).Thiswill installthe PICC

StepI - Go to www.htsoft.com

Figure i-3

I d"e:.;,.

Il;,-.

Figurei - q

94-

.J

d

&at'r!

Step2 - PICC Lire Compilerdownloa,l

!l I i;i /,i4.

.-" € rr. , a

. tj.o I

q ..^:i,i;l: d!!:@

ltfi il,f;:i#;il ;i':ii:f .ri::,:.il":ll

Fiqure i-5 customer

1 , 2 3 P I C @l ' l C [ JE x o e r i m e n t s f o r

Step3 - Registeringa an HT-Soft

the Evil

6enius

lib

Edt

r/i4

FMt6

Iode

rgp

Oo* - .,.y' sl td $ j)s..,a$r"-* € 3- + n

:, ffl im I

r,G@ I oownuas > oomos Download P.oducl Demos and Fra€ Software

@

Registratron is requrredto dowrload.lf)€u ha\€ notalreadyregisteredyou willbe redlrectedt0 a pageattowingyouto register, free 0f charge,or tog n Ityou haveprevjou9ty registefed. D6mos

Faee Software

The demoprog€ms belowa€ inbnd€d l0r prosperllve custome6 ofNI.TECHSotwarelo evaluateour prodult beforenaking a prxchasedecision.Tltedemostpically haw 53medeliberalelimilalionscom!rJedt0lhe full, purchasedprcdurt,and also ha!€ an €xpiry,usualt2l

H|T:CH Sdlwar€mak€scenarnlelsions of ils sonwaE pfoduclsavadableasrree sonwdreTh€seproduclsareiree f0rr.ykind oluse, inrludinqeducalrol]al rnd commercirl !se, bulremainlhe propeE of!1lI€c!1 sonwa€ 313l times Allcopy qhl and olhernghlsare resered and nowa*artot

Thesed.mos are provrdedenlirelydlhoul anywamnly. and arelorevalualionpueosss onv, and are notlicensed

Thenee sottvi?fedownloadsdo no!ha!€ a built,tn€xpiry

l;;--__l t"""""".,-l P'((18 I HrrErH | @ f r pr r r u . d i

r@n

,i,";,"";,";".,""

@

f -sere.1 I

t

8051

| I

. l

--,1 w

.

i

1

(Lrnua Prcc,L|TE HitachiHS/300 f 68HC05l 68H411 H - I 0 E f o iP C C 1 8 MEP43|]

z80l I C0nlactsileet0 orqanizea lime llmiledeyalualionollhis

Figure i-6

Step4 - Allowing PICC Lite Compilerfile downloadinWindows/XP SP2

Lite compilerwithout leavingyou with an .exeor .zip file to deletelater. When the PICC Lite compilerinstallationscreen appears(seeFigurei-8),click "Next" and follow the defaults.If you are promptedto load the software/ driversfor MPLAB IDE, do so,asthey will be requiredto usePICC Lite compilerwith the Microchip tools andprovideyou with a truly integrateddevelopmentenvironment,or IDE. The MPLAB IDE is a singleprogramcontainingan editor, assembler, linker,simulator,and PICkit 1 starterkit's programmerinterface,and it will be the only program you haveto run to createPIC MCU applications. PICC Lite compilerwill integratewith MPLAB IDE whenthe latter is installedso you will havea single Windowsprogramfor developingC and assemblylanguageprograms for the PIC MCU. After PICC Lite compileris installed,you will be askedif you want to restartyour computer.Click "No," then powerdown your computer,and power back up.I find that soft resets(onesin which poweris

I . aO ) , * i r *

o i.i; ! r.{.G:..

@ t;,;i

l

e-d

q

l";

Figure i-7

"

Step5 - PICC Lite downloading

not removed)may not resetall the PC'sparameters, and the softwareinstalledon boot may not work properly.Poweringdown and then backup eliminatesthese potentialproblems.

lntroduction

€d-

Step6 - PICC Lite Installer

Figure i-8

With the PC backup,go to Microchip'sweb site (www.microchip.com shownin Figurei-9) and click "MPLAB IDE" or "DevelopmentTools,"followed by "Full Install" (seeFigurei-10).TheMPLAB IDE softwareis quite large(30 MB) andwill take sometime to downloadif you usea dial-upconnection.This time click "Save"insteadof "Run" and store the .zipfile into a temporaryfolder on your PC (seeFigurei-11).You shouldbe ableto unzip the file by double-clickingon it, and the file management softwareon your PC will expandthe file into the directory of your choosing(ideallythe sameone you startedwith). After the MPLAB IDE installfiles are unzipped, double-clickon "Setup" and follow the instructionsto insrallthe MPLAB IDE (seeFigurei-12).If asked, make surethe programminginterfacefor the PICkit 1 starterkit is includedand you will not haveto look at

,

,.!

r ./ , G

n,. a'

,.

dai:l {rnc*rr

WWffi

EE-

rE-E:

"

Figure i-9

'

Elir

Step 7 - Set yotry browser to

www.microchip.com to downloadthe MPLAB IDE as well as PIC MCU Datasheets

..)

. *

.' ,a i

:t'6G

I

t-t- i

.d. .11G..

Fiqure ilO Step8 - MPLAB IDE Download Page on www,microchip.com

r

t

t ; ^ .

;.

J

--4-j9-

t/*..*,Ell

8.. .

SiAl'l.,....'

Figure i-ll Step9 - MPLAB IDE Installfiles stored in a temporaryfolder

any readmefiles (unlessyou want to). If you are promptedto reboot your computer,click "No" and then power down andpower back up,asyou did after installingthe PICC Lite compilersoftware.Do not connectthe PICkit 1 starterkit to your PC usingthe USB cableuntil you are told. That'sit;you've just installeda set of integrated developmenttoolsthat arejust aspowerful assome softwaredevelopmentproductsthat costmany thousandsof dollars.Withthe toolsinstalled,you can copy the sourcecodefiles for the applicationcodeusedin this book from the PICkit 1 starterkit's CD-ROM codefolder into a similar"code" or "Evil Genius" folder under the C drive of your PC.Another source for thesefilescan be found on my web siteat www.myke.com.

l , P 3 P I C @l ' l C UE x o e r i m e n t s f o r

the Evil

6enius

r{ q f*i: i.{

$-i*

kr. Figure i-12 Step10 - MPLAB IDE Installer window

Fisure i-13 Step11 - MPLAB IDE Start Up desktop

Now let's try to createa simpleprogramthat flashes an LED on the PICkit 1 starterkit. To do this,doubleclick on the MPLAB IDE icon that hasbeenplacedon your PC'sdesktop.Whenit first bootsup,the MPLAB IDE desktoplookslike Figurei-13,and is readyfor you to startenteringyour own application.Click "New" andenterthe following codeinto the window that comesup:

#incluale -CONEIG(FCITDIS

& IESODIS

& BORDIS & IINPROIECT

!4CIJRDIS & P!\IRTEN & WMDIS int it nain( )

& INTIO)

'

t PORTA = 0; CUCONo = ?t ANSE J = 0i TRISA{ = 0i TRISAs = 0t (1 == 1) while

Figure iJq Step12 - FirstApplicqtion enteredinto MPLAB IDE Editor wind.owand savetl

{ f o r ( i = 0 r i < R A { = A A 4 ^ 1 t

25000r i++) i

Different partsof this programwill be displayed usingdifferentcolors;don't worry if it looks a bit strange.Theprogramshouldbe savedas"c:\Evil Genius\Flash\Flash.c" (seeFigurei-14).Oncethe programis saved,closethe window that containsit. All programsshouldbe run aspart of an MPLAB IDE projectthat savesoptionsandfeaturesselections specificto the application without requiring you to reloadthem eachtime you start up MPLAB IDE.The first stepis to specifythe projectnameand whereit is goingto be stored(seeFigurei-15).

Figurei-I5 prolect

Introduction

Stepl3 - Creating an MPLAB IDE

r'1 .-i

Figure i-'f6 Step14 - Selectingthe PICC Lite C Compileras the MPLAB IDE Buiki tool

Figure i-18 Step16 - Selectingthe PIC MCU part numberto work with (notethe largenumberof PIC MCUs to choosefrom).

The Flashapplicationis written in the C programming languagefor the PICC Lite compiler.To specify the PICC Lite compiler,click "Project" and then "SelectLanguageToolsuite."Youmay haveto scroll throughthe Active Toolsuiteto find PICC Lite compiler (seeFigurei-16).When you haveselectedit, makesurethat the locationof PICL.exeis correct.If it is not, look for the CIPICCLITE folder on your PC, and point the ToolsuiteContentsto picl.exein the BIN subfolder. When you are working with assemblylanguageprograms,you may haveto perform the sameoperation thereaswell. In this case,the assemblylanguageprograms(suchasmpasmwin.exe) canbe found in the ProgramFiles\MPLAB folder or in its subfolders. Right-clickon "SourceFiles"in the Flash.mcwwindow (seeFigurei-17) and select"Flash.c"from the

folder (whereyou stored c:\Evil Genius\Flash\Flash.c the programearlier).Toload Flash.conto the desktop, double-clickon "Flash.c"in the Flash.mcwwindow. Flash.cwith the projectthat was This will associate just created.Each time you work with a new program,it shouldhavea new projectassociated with it. Next,you will haveto make surethe proper PIC MCU is selectedfor the application.CLick"Configure" and then "SelectDevice,"and find PIC16F684in the list (seeFigurei-18).I'm sureyou will be amazedat the numberof differentPIC microcontrollerpart numbersthat comeup.After working throughthis book,you will discoveryou can programand usethe The vastmajodty of thesechipsin your applications. differencein the part numbersis the numberof pins and interfacingfeaturesbuilt into the PIC MCU. Programmingand interfacingare identicalto what has beenpresentedin this book. Now you are readyto try and "build" the application.You canclick "Project" and then "Build All," or pressCtrl+F10to compilethe applicationand store the resultin a .hexfile that will be programmedinto the PIC MCU later.If any errorsoccur,go back over the codeyou keyedin and compareit to the previous Iisting.Thisis the mostlikely sourceof the problem. Oncethe programhascompiledcorrectly,you will get the summaryinformationshownin Figurei-19,listing the amountof spacerequiredto storeand run the programin a PIC MCU With the programcompiled,plug your PICkit 1 starterkit into a USB cablepluggedinto your PC. Afterwards,the MPLAB IDE screenwill look like Figure i-20with the statuswindow changingto list the Firmwareversionof the PICkit 1 starterkit. If it does

, a:": :

l

.i.,u

,:-,) :t,,1 ir.,.':

Figure i-17 Step15 - Specifyingprojectsourcelile

5

l , a 3 P I C @I ' l ( U E x o e n i m e n t s f o r

the Evil

6enius

Figure i-19 Step17 - Build informationfor "Flash.c"application

not change,then click on the programmer toolbar loIlowedby "SelectProgrammer"and then the PICkit 1 icon.A four-buttonprogrammertoolbar will alsobe displayedon the desktop.Removethe PIC12F675that cameinstalledin the PICkit 1 starterkit and put in the PIC16F684that wasin the PICkit box.Rememberto storethe PIC12F675in a safeplace(usingthe pieceof foam the PIC16F684wason is a goodchoice).Click "Programmer,""Program Device,"or the Program icon (placeyour mouseover the programmericonsto makea button legendappear)to downloadthe applicationinlo lhe PICl6F684.The programming operation will take a few moments(the operationis indicated by a growing bar on the bottom-left corner of the desktop,and a "ProgramSucceeded"message will be shownon the statuswindow,Figurei-21). Oncethe build/compileoperationqfollowedby the deviceprogrammingsteps,are complete,the D0 LED of the PICkit 1 starterkit will start to flash.If it doesn't,you shouldreviewthe sourcecodeand the processof creatingthe project.

Figure i-eO Step I8 - tnformarionprovitled when the PICkit 1 starter kit is pluggedinto the (levelopment PC's USB Port.Note:Four-buttonProgrammer toolbar at the top centerof MPLAB IDE desktop.

Introduction

(evenif it's flashingan LED, asI do in this introduction).The secondtime it will take half aslong,the third a quarter,and so on. Over a fairly short period of time, you will be ableto createapplicationsefficientlythat asthosecreatedby professionals. are assophisticated

Figure i-al Step19 - Thefirst applicationhasbeen programmedinto a PIC16F684and insertedinto the PICkit 1 startedkit connectedto the developmentPC. TheD0 LED shouldnow beflashing!

Seeingyour own PIC microcontrollerapplications working will be an amazingexperiencefor you,and it will give you a senseof pride to know you can do somethingthat only a smallpercentageof the world's populationcan do.Unfortunately,the processof getting thereis full ol frustration,confusion,and hard work.Along with teachingyou about the PIC MCU, the purposeof this book is to help you gain the skills necessary to developyour own applicationswith a minirnumof the inevitablefrustration.confusion,and hard work.

Prerequisites That'sit;you havejust set up a very sophisticated microcontrollerapplicationcodedevelopmentlab and createdyour first application.I realizevery little of the programor the processyou went throughto get to this point makessense,but asyou work throughthe different expedmentsin this book,their functionswill becomemore obviousand easierfor you to useon your own.Justasif you wereto reviewthe datasheets for the part'selectricalinformation,working at understandingthe architectureand how it is programmed will help you understandwhat kind of development tools are availablefor the part. Throughoutthe rest of this book,I will help you learn aboutand work with the PIC MCU. The experimentsrangefrom the very trivial to somevery complex intedacingapplicationsthat are really quite a bit of fun.Thereis a lot of materialin this book and a lot to learn;try to work througheachexperimentin a sectionbeforetaking a break-I would be surprisedif you were ableto get throughthe entirebook in less than a year. As part of the learningexercise,try to developyour own circuitsandcode-this will cementthe knowledge you gainfrom the book and help you build the skills neededto createyour own applications. Don't be afraid to usemy designsand codeasa baseor asa part of yours (usingcut and paste).Playing"what if" can be a lot of fun and very instructive.I believethat before somebodyis comfortableworking on a new deviceand developmentsystem,he or sheshouldhave50 or so of his own applicationsunder his or her belt. Don't be discouragedwhen,at first,your applicationsdon't work.It isn't unusualfor it to take a week or two to get a person'svery fint applicationworking

This book waswritten to be the secondin a sequence (123RoboticsExperiments for theEvil Geniusvtasthe first book) and,assuch,many of the basicelectrical, mechanical, and programmingconceptsusedin this book werepresentedin the first.To understandfully the experimentspresentedin this book aswell to be you will haveto ableto createyour own applications, be familiar with the conceptslistedbelow: .

Basicelectricallaws . Partsof a circuit . Ohm'slaw . . .

Seriesresistances Parallelresistances Kirchoff'svoltagelaw

.

Kirchoff's current law

o

Thevinin'sequivalency Resistormarkings

o .

Semiconductorbasics . Diode operation . LEDs (including7 SegmentDisplays) . Bipolar transistoroperationand pinouts . MOSFET operation

.

Binary electroniclogic . The six basicgates . Diflerentlogictechnologies . . .

l , e 3 P I C o l l C l JE x p e n i m e n t s f o r

The Booleanarithmeticlaws Typesof flip flops Commoncircuits

the Evil

Genius

. o . o

Adders Muxes/Demuxes Counters Shift registersand.linearfeedbackshift registers(LFSRs)

Oscillators . Basicrelaxationoscillator o

Reflexoscillator

. .

Crystalsandceramicresonators 555timer chip Common electronicdevices . 74L op amp . 386audioamplifier .

IR objectsensors Powersupplies . Batteries . 780x/78l0xvoltageregulators .

Switchmodepower supplies Numberingsystems . Scientificnotation . Metdc prefixes r . .

Capacitormarkings Binary numbersand conversions

Hexadecimal numbersandconversions Programmingconcepts . Data types r . .

Variabledeclaration Assignmentstatement Vadablearrays

.

Iflelse/endifstatement

.

While statement

o

Subroutines Microcontroller concepts . Memory organization . .

Input and output pins Specialpin functions

.

Powersupplies

fomments for Teachers and StudentE While the targetaudiencefor this book is Grade 11 (junior) and Grade12 (senior)high schoolstudents preparingfor postsecondaryeducationin engineering,

computerscience,mathematics, or physicalsciences, it shouldbe appropriatefor universitystudentsor technical hobbyistslooking for more informationon programmingand interfacingPIC microcontrollersinto an application.To ensurethat the materialpresented herewould be relevantto high schoolstudents,the Ontario Ministry of Educationcurriculumguidelines for Computer Engineering(found at www.edu.govon .ca/eng/document/curricuUsecondary I gradeLLl2 /tech/tech.html#engineering) havebeenusedasa referencewhen topics,experiments, and materialswere selected. This book shouldbe usefulboth asa course text and asa referencefor both teachersand students. After working throughtheseexperiments, you will not only havea good understandingof how PIC16F684 and PIC12F675microcontrollers(and indeedthe entirePIC family of microcontrollers)are programmedand interfaceto other devices,but you will alsobe well on your way to beingcapableof creating your own sophisticated PIC MCU applications. Readingthroughthe experimentsin this book will not makeyou proficientin creatingyour own PIC MCU application.I am a firm believerin doing,and I would expectthat in any course,there would be many assignments that consistof modificationsof the experimentspresentedin this book.Theseassignments shouldgive studentsthe task of creatingtheir own applicationsand,aspart of the process, planning, wiring,and debuggingthem.For the studentsto becomereasonablyproficientin developingapplications,they shouldbe given10 to 20 applicationsas assignments over the courseof a term in order to familiarizethemselves with the MPLAB IDE software developmentenvironment,the PICkit, and the PIC MCU operation. Along with providinginformationon Microchip PIC microcontrollers(the PIC16F684in particular), electronicsinterfacingprogrammingin C, and assembler programrning, this book attemptsto developand engenderthe importantthinking and problem-solving skillsthat are expectedfrom a graduateengineer. Theseskillsincludethe ability to work independently, perform basictechnicalresearch,createa development plan,and effectivelysolveproblems.Along with being usefulin professionalcareers,theseskills are criticalto your success in collegeand university. The experimentsin this book do not lend themselveswell to group activities-you'll find that it is difficult to dividesmallmicrocontrollerapplicationsinto differenttasksthat can be carriedout by different membersof a group.For this reason,I recommend assignments that are limited asone-studentprojects. Projectsgivenduring the term shouldbe solvablein lessthan 100linesof code(two printed pages)and shouldnot requirea lot of researchon the part of the

lntroduction

...:

;: ,: :.:

student.Insteadtime shouldbe spenttrying to decide the bestway to solvethe programand structurethe software.All of theseprojects,exceptfor a summative project,sholJldbeeasilycompletedin a week or less.

.

H Noteto StudentE: .

.

.

Teachersare very good at figuring out when an assignmentis copied or plagiarized,and getting caughtwill land you and any other students who are involved in trouble.Copyingcodeor circuitry from the Internet or other books will be identified quickly asit is difficult to rework them to fit into your designsystemor style of programming.In any case,cheatingwill not help you gain the necessaryskills for developing your own application(and ultimately passing the course).In short,hand in only your own work and make sure that you can explainthe It will pay how and why of your assignments. off in the long term. spenda few When given an assignrnent, momentsa day on it, no matter what.Don't leaveit until the night before it's due.By doing a little bit of work on it eachday,your subconsciouswill work on the assignment,and when it comestime to finally createit and wdte it up, it will seema lot easier. Don't be afraid to try somethingdifferent.The worst thing that can happenis that it won't work-the upsideis you will discovera way of approachingthe problem that is very efficient and easyto implement.In this book, there are severalexampleswhere I just tried different things and discovereda better solution to a problem than I would haveexpected(and cases where the alternativesolution wasworsethan the original).

|-;

ff t:'"j

.i,"j

ful

H Noteto Teachers: .

;r1

'r{

ii; !r -r''i it-J

10

For teachersconsideringusingthis book asa I would like to emphasize text for their courses, that the materialis designedfor the PIC16F684, PICkit 1,and,to a lesserextent,the PICI2F675. I realizethat investmentshavebeenmadein other PIC (and other) MCUs, in programmers, in and other equipment,but the MPLAB IDE/PICC Lite compilerand PIC16F684/PICkit combinationis an extremelyflexible and costeffectivetool for applicationdevelopment.

.

Tiy to usereal-world situationsfor your assignmentsand try to vary them,both to interest your studentsand to make it harder for them to copy from preexistingmaterials.A good way to is to keep a comeup with theseassignments notebookhandy and record the different things you seein your travels.Toys'R Us, Radio Shack,TheSharperImage,and other retailers can be wonderful sourcesof inspiration. Lead by example.You shouldwork through as many different applicationsuntil you are comfortable doing so and can debugmost of the problemsyou experience.An important tool to be familiar with is the MPLAB IDE simulator. A coupleof teachershave told me that they don't bother with the simulator when they are teachingbecausestudentsprefer to seeflashing lights or somethinghappening.Pleasetry to break this habit and encouragethe use of the simulatorasa tool for verifying the operation of the codeaswell asfor debuggingexecution problems.Using the simulator will give students the ability to seethe program working or not working,and why it is not working before making the effort to wire the application.For virtually all applicationsthat run the first time power is applied,the developershould have demonstratedthe corrgct operation of codeon the simulatorbefore he or she attemDtsto burn it into a PIC MCU.

lcons and Conventions At the start of eachsectionand experiment,you will fild one or more iconsindicating the parts and tools you needto have availableto complete the experiment(s).I havechosento do this rather than providing one central list of parts required for all the experiments in thisbook.Thereasonfor doingthis is to helpyou efficiently plan for the section'sexperimentswithout having to buy many hundredsof dollars of parts (many of which you will not needfor months).I have tried to keep the number of parts to a reasonableminimum by designingasmany experimentsaspossibleto execute within the PICkit 1 starter kit-once the PIC MCU is programmed,the applicationprogrammedinto it can executeon the PICkit 1 starter kit without modification. The iconsusedto specifypartsand operationsare asfollows: At the start of eachsection,I will list the required partsfor the sectionunder this icon (seeFigure i-22).As I indicated,this is a summationof the parts usedin all the exoerimentsof the section.

l , e 3 P I C @l ' l C UE x p e ni m e n t s f o r

the EviI

Genius

F{ !!E

.t a.* !'r,

Figure i-ea

RequiredPartsicon

Figure i-eq

.l*

PICkit I starterkit icon

l ;

':i" i't, ..:}

Figure i-a3

PC/Simulatoricon

Figure i-45

PartsBin icon

Figure i-e6

Toolboxicon

Virtually all of the experimentswill requirea PC (indicatedby Figurei-23)runningWindowsand loadedwith rhe MPLAB IDE and PICC Lite compiler.ThisPC shouldbe runningone of the following versionsof Microsoft Windows: o . . e .

Windows98 SE WindowsME WindowsNT 4.0 SP6aWorkstations(NOT Servers) Windows2000SP2 WindowsXP

And the PC shouldhave32 MB memory (128MB recommended),95MB of hard disk space(1 GB recommended),CD-ROM drive,alongwith Internet Explorer 5.0or greaterwith an Internet connection for installationand online Help alongwith one free USB port. When the PICkit 1 starterkit icon (seeFigurei-24) appearsat the start of an experiment,it meansthat the PICkit is requiredeitherasa platform for the experiment or as a PIC MCU programrner. The PartsBin icon (seeFigurei-25) specifieswhich partsare requiredfor the application.Partsmay be reusedbetweenexperiments The lasticon (seeFigurei-26) liststhe tools requiredto createthe application'scircuit.Youshould not requireany specializedtoolsfor the experiments presentedin this book.Rememberto wearsafety equipmentwhendoingany cuttingor drilling.

In terms of conventionqI will use both StstemInternationale(Sl),better known asthe metric system,and English measurementstogether where possible.I recognize that there are still a number of issueswith specifying the correct measurementsystem,so by listing both I hopetherewill be lessconfusion.Standardelectrical prefixeswill be used,andI assumethe readeris familiar with them.They include the following: k for thousands M for millions m for thousandths p for millionths p for trillionths I restrain from speciffng part numbers except whenI believethat only one manufacturer'spart shouldbe used.Often,equivalentsto variouspartscan be found in surplusand generalelectronicsstoresat a very low cost. For the screenshotsshownin the book, I haveused MPLAB IDE version7.01.I know that after this book

lntroduction

11

goesto press,later and more capableversionsof MPLAB IDE will be available for download from Microchip'sweb site.www.microchip.com The MPLAB IDE functionspresentedin this book will not changefrom version to version (except for fixesto discoveredproblems),so despitesomecosmetic changesin appearance,their operation will not change.I recommend that you alwaysuse the latest versionavailablefrom the Microchipweb sitebecause if you haveproblems,the first recommendationthat you will be givenwill be to try againwith the latest version of the software.

FindingParts When you are first starting out in electronics,it can be difficult to find retailers to provide you with the parts and tools to createyour own circuits.Over time, you will developa network of storesthat havethe parts you need,but if you are startingout or are looking for better suppliers,here are somesuggestions: .

.

Along Mouser Electronics(www.mouser.com). with Microchip products,Mouser also hasan excellentselectionof opto-electronicparts. I find Radio Shack(www.radioshack.com). Radio Shackto have a number of components that I can't find anywhereelse(thermistors, TRIACs) aswell asgood-qualitywire at a reasonableprice.Recently,Radio Shackhas includedsomeMicrochip PIC MCU chipsand the ParallaxBASIC Stampto their catalog. Incal electronicsstores.InToronto,I recommend Supremetronic(www.supremetronic.com) asit hasall the passiveand discreteparts that I need,prototypingPCBs,and other usefulparts that are nice to handleand choosefrom instead of decipheringPDFSon line. Local surplusshops.If you are in the Toronto area,I'm sureyou will recognizeActive Surplus (www.activesurplus.com) by the large stuffed gorilla out front. While having a good selection of electronicparts,surplusstoresoften have a variety of other parts and subassemblies that are perlectfor hackingor designingnew controls.

Digi-Key (www.digikey.com).Ihavenot found a sourceof parts anywherein the world that matchesthe selection,price,and serviceof Digi-Key. Jameco(wwwjameco.com).Another excellent supplierthat carriesMicrochip parts.Also carries a good selectionof robot parts (including gearsand motors).

S*!

i.,,,

o.',..:

ir,"i *;

L2

l,e3 PICo llCUExoeniments fon the Evil

6enius

Section

One

Underthe Eoversof the PlElEFEBq

1Plcl6F684

Before astronomersbeginto investigateand learn more about a star,they make surethey fully understandthe tools they are goingto use.Thetools to be usedare chosenfor their ability to investigatethe specific aspectsof the starthat is to be studied.Althoush a fewdiscoveries havebeenmadewith poorlyundelstoodequipment,the vastmajority of obseruations haveresultedin failure.Justlike astronomers, before we investigateand learn aboutthe MicrochipPICP microcontroller(the PIC16F684specifically), we want to know asmuch aspossibleaboutthe toolswe are goingto be using. The PICkitrM1 starterkit is an excellenttool for learningaboutthe PIC microcontrollerasit includes, alongwith its programmingcapability,a basictestcircuit that you can usewith a PIC MCU. As shownin Figure1-1,the PICkit starterkit providesfrom 8 up to 12individuallyaddressable LEDq alongwith a button input and a potentiometerfor variable-voltage inputs.

The builtin programmerinterfacesto the developmentPC via aUSB port, which is preferableto serial or parallelports.Overall,the PICkit 1 starterkit is almosta perfecttool for learninghow to programthe PIC MCU and interfaceto hardware. When I describedthe PICkit 1 starterkit asbeins almostperfect.it probablysetotf somealarmbellsin your head-this type of qualificationis normallyused to describethingslike a usedcar or a blind date.In this case,I am beingasliteral aspossible.The PICkit 1 starterkit is an extremelygoodproductand an excellent first tool (probablythe bestthat I know of) with which to leam to programand interfaceto the PIC MCU. The threepotentiallynegativeissuesI would like to bring to your attentionregardingthe PICkit 1 starterkit are actuallyquite minor and,to some extent,canbe exploitedto help you better understand how the PIC MCU works. The biggestissuethat will haveto be addressedin this book is the organizationof the PICkit 1 starter kit's LEDs. If you wereto follow the wiring of the eightLEDS and the four I/O pins they are connected to, you would noticethat only two of the pins canbe outputs(asshownin Figure1-2).And if all the I/O pins to which the LEDs are connected(RA5, RA4, RA2, and RA1) weremadeoutputs,you would have multiple LEDs lit if you make any of the I/O ports high.The solutionto this problemis to enableonly two pins asoutputsat a tirne (one high and one low). This allowseightindividuallyaddressedLEDs, but not an arbitrarynumberof LEDS to be on at any siventime.

--ri ., I I

Figure l-'f

EquivalentPICkit I starterkit circuit

13

=:-

--ri " I I

Fiqurel-e

PICkit I stqrterkit LED on

Arbitrary numbersof LEDs canbe turnedon by scanningthroughthe LEDs,just asa TV's electron beamscansacrossthe cathoderay tube,turning on phosphorsone at a time.Thistrick will be demonstratedlater in the book and will be usedto display eightbits of data at a giventime.The organizationof the eightLEDs (alongwith the button andpotentiometer)wasmadeto supporteight-and 14-pinparts in the PICkit 1 starterkit's socket.That organization and the choiceof pins (and how they are organizedon the PIC MCUSthat fit in the PICkir 1 starrerkit's socket)is actuallyquite inspired,asit leavesthe six pins of the 14-pinmicrocontroller'sPORTC available for other uses. The secondproblemis that it takesa few mouse clicksto turn off the power goingto the programmed

part.I seldomrememberto turn off the power,andI doubt you will either.Although the differentPIC microcontrollersare very robustdevicesfrom the elecyou shouldneverpull them trical overloadperspective, from a socketwhile power couldstill be appliedto someof thepins.Althoughlhe zeroinsertion forca (ZIF) socket,which I showyou how to installlater in the book, goesa long way in mitigatingthis problem, you shouldstill be cognizantthat you could potentially be damagingthe PIC16F684everytime you plug it into and unplugit liom the PICkit 1 starterkit while the Plckit is still connected. The final issueto be awareof is the potentialliability of the USB port usedto connectthe PICkit 1 starterkit to the developmentPC.Although most commercialand homePCshavebuilt-in USB ports and versionsof the MicrosoftWindowsoperatingsystem that canaccessthe PICkit 1 starterkit very simply, thereare a numberof PCsin educationaland institutional settingsthat do not havethe requiredports or software.Thereis no easyfix to this problemother than trying to find the fastest,biggest,and mostmodern memoryPC to be usedasa programmingstation. None of thesethreeissuesare major showstoppers -they are reallyjust speedbumps,and they canbe overcomef airly easily.

1-l/0 Pins Experiment

Arguably the mostimportantfeatureof the PIC MCU (I/O) pins.The 12 pins availis its setof inpttt/oLtlp&l ableto the applicationdeveloperallow the microcontroller to sensethe outsideworld and output in differentways.ThePIC16F684tI/O pins are capable of manydifferentfunctionsincludinganalogand difBut asI first startintroferent digitalsignalprocessing. ducingthe chip,I will treat the I/O pins assimple

T4

digitalI/O, capableof sensingor outputtingsimple binarysignals.Themore advancedfeatureswill be addressedlater in the book whenyou becomemore comfortablewith programmingand working with the PIC16F684. The basicPIC MCU digital I/O pin designis shown in Figure1-3.TheIR/S bit controlswhetheror not the pin can output the valuesavedin the PORZ bit. The term TRIS is an abbreviationofTii-State and referencesthe tri-statedriver that candrive the PIC MCU'S pin.When the TRIS bit is low (0), the valuein PORT is driven onto the pin, and the pin is saidtobe rn output mode.whentheTRlS bit is high (1),the PIC MCU pin state,and the data level at is held in a high-impedance the pin canbe read without beingaffectedby the contentsof the PORT bit.This is known asinput mode. RememberingwhichTRIS stateaccountsfor which modeis cuite easvto remember:ATRIS valueof 1

l , a 3 P I C o l ' l C l JE x p e r i m e n ts f o r

the EviI

6enius

CONFIG(INTIO & WDTDIS & PWRTEN & !,ICLRDIS IJNPROTECT \ & UNPROTECT & B O R D I S & I E S O D I S & F C M D I S ) ' int

o

co

&

j;

i,

nain( )

(5

{

o

o

PORTA = 0t C M c o N 0 = 7 r / / Turn off Comparatola A N S E I J = 0 r / / turn of,f, ADC T R I S A { = 0 r / / Make RA4/RA5 Outputs TRISAs = 0t == 1)

whil€(1

(

< 255, < I29,

f o r ( i = 0 r i f o r ( i = 0 ; i

Figirre 1-3 l/O pin

RAI = ri

puts the pin in input mode,and a TRIS valueof 0 puts the pin in output mode.TheTRIS bit valueapproximatesthe first letter of input or of output. By convention,you will seeI/O pins referredto usingthe format

//

tor

(i

RAA = li

Forev€r i.all // j1-1-1,

sinple

Delay

Loop

Si1np16 Delay

lroop

D O LED ON i j

RA{ = 0t

].oop

//

< 255r < 429,

i11) jaa),

//

D0 tED Of,f

= Ot i Ot j //

< 255, < L29,

i11) // jaL>,

Simple

Delay

loop

sinDle

Delay

r.oop

D0 LED On AEain

a,&*

wherethe ampersand(&) representsthe port, and the numbersign(#) representsthe port's pin. So PORTA, pin 4 is known asRA4.This shorthandcanbe a bit confusingbecauseit refersto both the pin and the PORT register.TheTRIS bits are usuallywdtten in the rormat TRIS&*

with & and # beingusedin the sameway asthey are in lhe PORT bil/pindefinilion.Thesenamingconventions are usedfor both C and assemblylanguageprogramming. When I first presentedyou with the cFlash.cin the Introduction,the mannerin which the LED was turned on and off is not easyto see.To help you see the operationof the I/O port and how it affectsthe PICkit 1 starterkit's D0 LED,I createdcPins.c, which turnson and off the LED usingthe PORT andTRIS bits more explicitly.

*incluale - Exalnine /* cPiaa.c

Operation

This Progran is a moalificalion wiEh nore e:.plicit writea to bits. RAA - IJED Positive RA5 - IJED tilegativ€

conn€ction cona€ction

nvke Drealko 04.09.15

SectionOne

of

PIC MCU PinE

of -cE1a6h.cthe TRIS and PORT

(i (j

= 0i = oi

i i

TRISA4

= 7i

//

for for

// LED lrrrned out

< 255, i11) < L29, itL), EltlE RA4 irto

Off

f o r ( i = 0 r i < f o r ( j = 0 r j
;

Data Processor and Register lnterface

!"j *: r1 .

X i.'i

Figure f-tl

r.8

Princetonarchitecture

Figure f-5

Harvard architecture

l , a 3 P I C @I I C U E x p e n i m e n t s f o n t h e E v i l

Genius

The programfiles loadedfor PIC MCU and other Harvard computersystemsare much simplerthan the filesloadedinto the Pdncetoncomputersystems. Theseare producedby the MPLAB IDE, end in the extension.hex,and are usuallyreferredto as,,hex files."A typicalPIC MCU hex file consistsof two or three segments, one for applicationcode(startingat the resetaddress),one for the configurationfuses,and an optional one for electricallyerasableprogrammable read-onlymemory(EEPROM) data.For the applicationspresentedin this book,the hex files will consist only of the codeandconfigurationfusesegments. Thereis no needfor definingdifferentareasin the memoryspace. This lastpoint is subtle,but extremelyimportant. Whenyotsdeclarea variablein a Princetoncomputer system,you are speciflng the label to be usedwith the variableaswell asreservinga spacein memoryfor the variable.When you are declarea variablein a Harvard computersystem(like the PIC MCU), you are simply specifyingthe label to be usedwith the variable;there is no need(or any mechanism)to reservespacefor the variable.This meansthat the locationfor variablesin a Haryard computersystemcan be chosenwith much lessrigor than in a Princetoncomputersystem,and the Harvard applicationcan still be expectedto run. The separatememoryareasalsomeanthat initial variablevaluescannotbe loadedinto the datasegmenL in the Harvard computersystemvariablesare initializedfrom programmemory.In termsof hex file space, you saveon the needfor a datasegment,but you do requiremore for the initializationspace.For microcontrollers,this is not an importantpoint becausein either computerarchitecture,variableswill haveto be initialzed by code,becausethe applicationsare not loaded []to memoryastheyarewirh a PC. Although I've probablygivenyou the perception that variables can be placed any.wherewilly-nilly in the PIC MCU, the truth is a bit more complex.As I work throughthe book,I will explainthe variablememory andregisterspacein more detail,but for now I just want to note that they sharethe samespaceand explaina bit about the functionof the registers. What I am casuallycallingthe registersin this experiment are refefied lo as the specialfunction registersby Microchip (and,similarly,what I am calling variablememoryis more properlycalledthefile registers),the specialfunctionregisters(SFRs)listedin Table1-3are usedto monitor the statusof program executionaswell asprovidean interfaceto the hardwareperipheralfunctionsof the PIC MCU. As you read throughthe book, the functionand addressingof theseregisterswill be explainedto you.

SectionOne

Table 1-3 PlEl6FEBqMicrocontroller5DeEialFunction Flegisters

! & !

&,!,

ta

Name

Flddress

INDF TMRO

0x00& 0x80 IndexDataRegister 0x01 TMRoValue

PCL

0x02& 0x82

Low 8 Bits of the ProgramCounter

STATUS

0x03 & 0x83

PIC MCU ProcessorStatus Register

FSR

0x04 & 0x84

PIC MCU Index Register

PORTA

0x05

PORTA I/O Pin Value

9.i

PORTC

0x07

PORTC I/O Pin Value

i.i

PCLATH

0x0A & 0x8A Upper 5 Bits ofthe Program Counter

INTCON

0x0B & 0x8B lnterrupt Control Register

PIR1

0x0C

PeripheralInterrupt Request Register

TMR1L

0x0E

Low Byte of TMR1 Value

Function

TMR1H

0x0F

High Byte of TMR1 Value

T1CON

0x10

TMR1 Control Register

TMR2

0x11

TMR2Value

TMR2CON 0x12

TMR2 Control Register

CCPR1L

0x13

Low Byte of CCP Register

CCPR1H

0x14

High Byte of CCP Register

ft\ $-*

1t'

I E*tl

".,.t-

PWM1CON 0x15

CCP PWM Control Register

ECCPAS

0x16

CCP Auto-ShutdownControl Registet

WDTCON

0x17

WatchdogTimer Control Register

CMCON0

0x18

ComparatorControl Register

CMCON1

0x19

ComparatorControl Register

ADRESH

0x1E

High Bits of ADC Result

ADCONo

0x1F

ADC Control Register

OPTION

va:*

gn

:x

iLJ

0x81

PIC OperationControl Register

TRISA

0x85

PORTA Data Direction Pins

TRISC

0x87

PORTC Data Direction Pins

PIE1

0x8C

Pe pheral Interupt Enable Register

PCON

0x8E

PowerControl Register

OSSCON

0x8F

OSCTUNE 0x90

L,i,

i1r F.t

OscillatorControl Register OsciilatorTLningRegister

ANSEL

0x91

ADC Pin Enable Register

PR2

0x92

TMR2 Period Register

WPUA

0x95

Weak Pull-Up Enable Register

IOCA

0x96

Interrupt on Port ChangeSelect Register

VRCON

0x99

ComparatorVref Control Register

EEDAT

0x9A

EEPROM Data Register

EEADR

0x9B

EEPROM AddressRegister

EECON1

0x9C

EEPROM Control Register

EECON2

0x9D

EEPROM Write EnableRegisrer

ADRESL

0x9E

Low Bits ofADC Result

ADCON1

0x9F

ADC Control Register

U n d e r t h e C o v e r s o f t h e P f C l t F b gq

tf': tii it;*.d

yt"

it fil

{r? 19

Exp e r im entU- S im u l a ti n g c F l a Eh .ci n MP L H BID E

The simulatorbuilt into the MPLAB IDE is probably the leastusedand understoodtool availableto you, which is unfortunatebecauseit is the mosteffective tool that you haveto find and debugproblems.The reasonswhy applicationsare nol simulatedis due to the perceivednotionsthat it is too muchwork and that the programis either very simpleor basedon simple changesto a working program.Old handswill cringe at theseexcusesand rememberhow they learnedthe hard way.PersonalIy,Ineverattemplto burn a programinto a PIC MCU without first simulatingit and makingsurethat it works properly.I alwaysdo this beforeseeingif it will work in the applicationcircuit; by simulatingit first you haveconfidencethat the programshouldrun. In this experiment,I will work throughthe basicsof settingup the simulatorfor an MPLAB IDE project.I havea few commentsregardinghow it shouldbe used but, for the mostpart,I recommendthat you useit for all the experimentsin this book. Someof the initial programming(C and assembler)experimentsare designedto work only in the simulator,but onceagain, you shouldsimulatethe experimentsthat accesshardwarebeforeburningthem so you canunderstandhow theywork and what they are expectedto do.As you gainexperiencewith the simulator,you shouldalso developpersonalstandardsfor displayingdataand what the simulatoris telling you in understanding order to find problemsand gainconfidencein your program. After you havecreateda projectand havebuilt the sourcecodeto ensurethere aren't any syntaxelrors (languageformattingerrors),you shouldenablethe "Select debuggerby clickingon "Debugger,"then "MPLAB Sim" asshownin Figure1-6. Tool,"and After enablingthe simulator,the simulatortoolbar (seeFigure1-7)will appear.The toolbar will allow you to do the following: . Run the simulatedprogramat full speed.On a 2.4 GHz Pentium runningWindowsXP,I find that 1 simulatedsecondexecutesin about 3 seconds,

.

Stop executionat any tirne.A breakpoint,which forcesthe applicationto stop at a specificlocation, can alsobe put in the applicationcodeasI will showpresently. Clicking'Animate" causesthe program to run relatively slowly,so you can watch the flow of the program.I find that this feature is bestused to illustratethe operationof basicoperating concepts. The program (subroutineor function) can be executedone step at a time by clicking |he Step In icon. If the operationof a subroutineor function is well understoodand felt to be correct or if it takesa long time to execute,clicking the S/ep Over icon will causethe simulator to execute the codein the subroutineor function at full speedand stop at the statementfollowing the call to the subroutineor function If you find yourselfin a subroutineor function that is going to executefor a while,you can StepOut of it. After clicking this icon, execution will run at full speed,stoppingat the instruction after the subroutineor function call statement. To restart the simulationfrom power up, click on the Reseticon.This icon will causethe simulated PIC MCU to return to power up conditions and resetthe StopwatchandStimulus functionsaswell.

'fo

setabreakpoifi (whichcausesexecutionto stop when it is encountered)in your program,double-click on the statementat which you wish the breakpointto be placed.Executionwill stopwhen the simulatoris runningor animatingan application,whena breakThe breakpoint'sstatementis not point is encountered. executedbut will executeif the Run.Animate,or one of the Stepiconsis clicked.Figure1-8showscFlash.c with threebreakpointsset and the execvtronrun arrow pointingto the first statementin the program. At this time,you may want to start up your cFlash project,enablethe simulator,and put breakpointsat the threelocationsI havein Figure1-8.Onceyou have donethis,click on the Reseticon and you'll seethat thereis no run arrow.If you click on one of the Stepiconsrepeatedly,the alrow won't appearand you may think that you havenot enabledthe simulator properly. If you click on the Run icon,the programwill stop and the run arrow will appearat the first breakpoint (at "PORTA : 0"). Before the C programstartEthere

l , e l P I C @l ' l C UE x p e r i m e n t s f o r

the EviI

6enius

D d k . , . i

'CIPlCCLrTE\Bll.{flCl a4' C E cfl.sh @, cF ash.c'-O'cF dsh lbf,Zg9 .O .O MA_AA ,t EFOB4 'CleLCO-rT€\all.dPlCt E€'-E cFllsh.lde,'C\vvitig\8to Eli Gei],s\Gd;\cl ash\cFt&h ob , O"cFtash cot -0..F 4i

(

s 0 0 7 0 s 0 0 3 1( s000s I

Figure l-6

{)

hef -O -MPI-IA -l6FdB1

bv!€

1) btres 5) byls !o!ar Bnd 0 RAlt

EnablingMPLAB simulator

is somecodethat setsup the executionenvironment and callsthe C program.Placinga breakpointat the first statementin the programand then clickingon the Run icon after resettingthe simulator,will provideyou with an applicationthat is readyto go from the first statement. Now would be a good time to start single-stepping throughthe program. When you get to thefor statements,you'll seethat the alrow doesn'tmove for eachclick of the Stepicons or evenseeminglyfor the Animate icons.Thesestatementsprovidea half-seconddelayfor the programyou canexecutethroughthem quickly and stop at the statementsafter them by clickingthe Run icon. To understandbetter how long an applicationis taking to execute,click "Debugger"and then,,Stopwatch"to displaythe Stopwatchwindow shownin Figure 1-9.Thiswindowwill showyou how many instructioncycleshaveexecutedand how long it would

5ection One

havetakenif you wereworking with a PIC microcontroller runningat 4 MHz (whichis the defaultexecution speedof the PIC16F684).The simulatedexecution speedcanbe changedby clickingon "Debugger"and then on "Settings;"this pop-upalsoallowsyou to changeother operatingparametersof the simulator. You canalsomonitor the valuesof resisten and variables in your programby addinga Wuchwinclow (seeFigure1-10).Toadd a register,selectit in the pulldown to the dght of 'Add SFR" and click on ,Add SFR;" to add a variable,selectthe oneyou want from the pull-downto the right of 'Add Symbol"and click on 'Add Symbol."For your initial programs,I recornmendthat you alwaysput in the I/O (TRIS and PORT) registersaswell asall the variablesin your program.When you are putting registersand variables into the Watchwindow,you may feel that the method in which they are displayedis suboptimal.Youcan changehow they are displayedby right-clickingon the

U n d e r t h e C o v er s o f t h e P I C l h F h A q

2L

l-,;;;;

l*

. '' ;

iir:

"Animate"

,,Run,, :

z1€

,*.* 91ri

"Pause/Stop" "StePln' i-1..*

Figure l-7

Simulatoractive

::d

Execution at Breakpoint (ArrowOver '8")

'irj

:

Breakpoints Set

t: '.r-i ? . ;:1:

:;;.t i.i

Figure l-B

22

Settingbreakpoints

l , a 3 P I C o I I C I JE x p e r i m e n t s f o r

the Evil

Genius

14 H tU L* 3*t

{u

t t

Slopwatch Window Active (46 Cycles)

?"t *, Figure l-9

Adding Stopwatch l 1

."{ ?";"{

C J

"Watch"Window

nr

1 d

L!4t 'd6 'r

sur**e . |

qdt rd6r

|

4

l.@f---Effi

'f Figure -10 Addine aWatchWindoh,

Section One

Under the Covers of the PICl,tFhAr+

23

r*C

Ddll

Select RegisterNari DisplayType Sizeand Other

al.t

vdPs1tAetodt !d@----

l

kl!b---l '*li;----:j rro&l:-.+

l

Parameters

41

lFlr,iiIb*l htrf

4

'GqJ f@l@-tr_-ffi t r o @

;;:i*,-.;;;'

'f Fiqure I -'f

L"t

'"t .S'r H

U'

*n{ IJJ

Changing Watchdata types

variable,selecting"Properties,"and then changingthe data format asshownin Figure1--11. With the informationI havepresentedhere,you can simulate most of the functions of the code written for the experimentsin this book.Right now,you can just monitor the operationof the code,but in a later experirnent,Iwill showyou how to setspecificinput and register stimulus,which will help you seehow your applicationruns under differentconditions. For now,you canchangeregistervaluesby doubleclicking on their value in the Watch window and putting in new valuesmanually.Addingstimulusto a simulatedprogramtakesa bit more work than the execution,breakpoint,stopwatch,and variabledisplay stepsI've shownhere,but it providesyou with a way to testyour applicationunder constantconditions,rather than dependingon rememberingto set certainvalues manually. One of the nice featuresof the MPLAB IDE simulator intedaceis that it is the sameasthe ICD 2 debuggerand ICE 2000emulatorinterfacesThis meansthat whenyou startworking with thesetools, thereshouldbe only a minimallearningcurvegoing from the simulator.The simulator features I've presentedhere are alsopresentin the debuggerand emu-

24

lator.Debuggersand emulatorsprovidehardware interfacesto the application circuit (actually replacing the PIC MCU in the circuit) that you can monitor, set breakpointsfor, and repeatover differentsectionsof code,justasyou would in the simulator. that At the start of this experiment,I suggested you usethe MPLAB IDE simulatorto test all the applicationcodein this book (and any that you encounter)beforeburningit into a PIC MCU. By doingso you will gainexperiencewith the simulator and learn to comeup with strategiesthat work bestfor you.Additionally,you can experimentwith placing your editing,watch,and stopwatchwindowson the MPLAB IDE desktopin positionsthat makethe most senseto you and that allow you to debugyour applications efficiently.

For Consideration When peoplestart investigatingMicrochip PIC microcontrollers for the first time, they are generally overwhelmedby the numberof differentpart numbers (microcontrollers with differentlealures)thatare

l , e 3 P I C @l ' l C l JE x o e r i m e n t s f o r

the EviI

6enius

available.As I write thiE there are over 250 active PIC MCU part numbersto choosefrom, not including packagingoptions.Most PIC MCUs are available with at least three different packages;eachpart number is available with two operating temperature mnges,and the older parts are availablein different voltage rangesThis means,whenall is told, there are about 2,000differentPIC MCUs and optionsto choosefrom. Over the next two pages,Iwould like to make your choice a bit simpler. There are six rnajor PIC MCU families to choose from as I have outlined in Table 1-4.The PIC16F684 that is featured in this book may seemlike one of the lower-end PIC MCUs, but it is actually a quite flexible memberof the mid-range. The low-end PIC MCU processorarchitecture is available on many of the entry-level parts.The processor is very similar to the mid-range architecture (which is usedin the PIC16F684)but doesnot havesome immediate instructions,doesnot support interrupt requestqcannotsupportlargeamountsof variable memory and doesnot have any advancedperipherals. The mid-rangePIC MCU architectureis the mostpopular architecture(andusedin the PIC16F684),asit supportsmoderateamountsof variablememory advancedperipherals,and interrupts.The PIC17 high-

end architecture is somewhatunique to this familv of microcontrollers althoughit doesiave somesimjLritiesto the low-endand mid-range.The PIC18architecture is really a supersetof the mid-range architecture and it offers a number of featuresthat allow it to accessmore program memory variable memory, and peripheral registersaswell asinstructions that will simplify and speedup traditional applications.Once you are comfortable with programming the mid-range chips and have worked through the experimentsin this book,you shouldn'thaveany problemslearninghow to program and working with the other PIC MCU architectures Most new usersand hobbyistslimit themselvesto the partslistedin Thble1-5.Thesedevicesare all easily programmedeitherby the PICkit 1 starterkir, PICSTART@Plus,ICD 2, or a homegrown programmer (of which there are many different designsavailable for dornload from the Internet). The Microchip ICD 2 is a debuggerinterface that givesyou many of the capabilities of an ln-circuit emulator (ICE) without the extremecostof an ICE. I want to make a few commentson eachof the deviceslistedin Table1-5.The PICSTART Plusis a developmentprogrammer produced by Microchip, which can be usedfor all PIC microcontroller Dart

Table1-4

Il::::!il llc yi::?:::,T1P,'-. l"'"iIi* PICMCU Pmcessor Familg FlrchleEture

Programmlng PtugEm Plgqrithms and ICD Memory Tgpe

PIC10

Low-End (12 Bit)

ICSP and ICD

PIC12

Low-End (12 Bit)

ICSP

PlC12

Memory Slze

Begt4er Size

lO Plns

Featues

Flash

256 to 512 instuctions

16 to 24 bytes

4

Comparator

EPROM (OTP)

512to\,0u instructions

25 to 41 bytes

6

Mid-Range(14 Bit) ICSP and ICD

EPROM (OTp) and Flash

512to2,048 mstructrons

25 to 128bytes

6

Compamtor and ADC, Advanced Timers

PIC14

Mid-Range(14Bit) ICSP

EPROM(OTP) 4096instructions 192bytes

20

Comparator andADC, Advanced Timefi

PIC16

Low-End (12 Bit)

EPROM (OTp) and Flash

512to 2,04a instructions

25 ro 73 bytes

121o20

PIC16

Mid-Range(14Bit) ICSP and someICD

EPROM (OTp) and Flash

512to8,192 instructions

72 to 368bytes

PIC17

"High-End" (16 Bit) Parallel

12 to 33136ADC,Se al to 52 for I/O, Advanced LCD Timerg LCD, USB

EPROM(OTP) 2k to 16k rnsrucnons

272to902bytes 33 to 66

PIC18

"PIC18" (16 bit)

Extemal Bus,Serial I/O, Advanced Timers

EPROM (OTp) and Flash

6,10to 4,096bytes 16to72

ADC,Serial I/qAdvanced Time$

Parallel,ICSP on FlashParts

ICSP and ICD

SectionBne

8k to 64k instructions

Under the Covers of

the PICIEFhAt+

25

qt \J

n

n $l !-.

p-

o

r-t 8' al F.

o t

numbers-in somecasesadaptersare requiredto proanotherPIC MCU part gramthe parts.In somecases, numberhasto be usedto implementthe ICD 2 function for a specificPIC MCU. Although the PICC Lite compileris either restrictedor not availableon some of the PIC MCUs listedin Table1-5,the full product supportseachof thesePIC MCUs and doesnot limit the programsizeor numberof variablesrequiredfor the application.Each of the listedPIC MCUs hasflash programmemory!which meansthat they canbe erased by the programmerbeforeburninga new application into them.All of the PIC MCUs exceptthe PIC16F54 havethe mid-rangeprocessorarchitecture.I included the PIC16F54becausemany earlyintroductorybooks and web sitesreferencethe PIC16C54.and the PIC16F54canbe usedin its placefor theseapplications.Another traditionalbeginner'spart is the PIC16F84A,but I would recommendthat you consider more advancedandfeature-richchiossuchasthe

more advancedchipshave PIC16F627Ainstead.These peripheral aswell asbuilt-in oscilfeatures additional for you to add oscillator the need lators,eliminating circuitryto your application. I chosethe PIC16F684for this book becauseit is a mid-rangearchitecturepart (14 bit), canbe programmedby the PICkit 1 starterkit, and hasICD 2 and PICC Lite compilersupport.This chip alsohasa built-in precisionoscillatorand is tolerant of a wide rangeof operatingvoltages.It alsohasa wide rangeof peripheralsthat make it appropriatefor usewith applicationslike robotics.Tosummarize,Ifeel the ratio PIC16F684givesthe bestcost-to-performance (whenthe PICkit 1 starterkit and PICC Lite compiler is includedin the decision)for a mid-rangepart that couldbe usedin a wide varietyof different applications and would be easyfor somebodylearningto work with the PIC microcontroller.

Table 1-5 FlecommendedFirst PIE MEIJs Pan Number

Numbs Df Plns

PIC16F675 6

PtugEm Memog

Va able Mem1rg

PeipheBls

and Debugqel

PICC Lite su1pott

1,024inslructions 64 bytes

ADC,Timers

PICkit 1 starterkit, PICSTART PIUS,ICD2

Yes

Comparator

Plckit 1 starterkir, PICSTART Plus,ICD 2

No

PIC16F630

12

1,024instructions 64 bytes

PICI6F6U4

12

2,048instructions

l2tl byles

ADC, Comparator, Timers

PICkit 1 starterkit, PICSTART Plus,ICD 2 Using PICI6F688

Yes (Limited Size)

PIC16F54

13

512instructions

25 bytes

None

PICSTART Plus

No

PIC16F84A 13

1,024instrlctions

68 bytes

None

PICSTART Plus

Yes

PICI6F627A 16

1,024instructions 224bytes

PICl6F87x

26

192to 22 to 33 4,096to 8,192instructions 368bytes

Comparator,Timers, PICSTART Plus,ICD 2 SerialI/O ADC,Timers, Seriall/O

l , P 3 P I C @f l C U E x o e r i m e n t s f o r

PICSTART Plus,ICD 2

the Evil

Yes (Limited) JustPICl6F87?A and Limited

Genius

Section

Two

Introductoru f Programming

1Prc16F584

I often seethe C programminglanguagedescribedas the UniversalAssemblyLanguageby peopletrying to put it down.Thisisn't a very fair characterization becauseC is an extremelyflexibleprogramminglanguagethat wasthe developmenttool of choicefor virtually everyoperatingsystemin usetoday,aswell as for a long list of successful businessapplicationsand games. Along with commoncomputerscienceapplications,C is usedto programmore artificial intelligence, computer-aided designsystems, aerospacecontrol systems,andsupercomputerapplicationsthan any other programminglanguage.DespiteacknowledgingC's widespreaduseandpopularity,detractorstend to focuson a few points. C is an incrediblyrich languagethat makesit suitablefor a wide varietyof differentapplications. This richnesscanbe a two-edgedsword;insteadof forcing everydeveloperinto following a commonprogram layout,C allowsa wide of programmingstyles.As I will discussat the end of this section,multiple C statementscan be combined,which canmake codeunreadableto eventhe mostexpertprogrammer.An acknowledgment of C's capabilityto be written into incomprehensible codeis the singlepoint of the yearly"InternationalObfuscatedC CodeContest" (www.us.ioccc.org/) in which the most confusingcode possibleis createdfor prizes. The richnessof the codeis often givenasa reason for why C is hard to learn.I regularlycontestthis with proponentsof BASIC,JAVA, and other programming languages. All theseprogramminglanguageshavea similarnumberof statementtypes,and I do not believe that any one hasadvanlages over anotherto make

programmingwith it more efficient.In termsof readability,poor codecanbe written in any language.Good programsare not the result of the languagedesigner; they are the resultof the programmerthinking about how to approacha problemand clearlyexpressingin their codewhat the programsare doing. Additionally,C is heavilydependenton pointerEa programmingconceptthat manypeoplefind difficult to work with, debug,and understand,especiallywhen readingother people'scode.I admit that I like working with pointers,but this is due to spendingmanyyears understandingwhat pointerscan do whenprogrammedin C and how they can be usedmost efficientlyfor most applications. In this book, I will introduceyou to the basicsof C pointersand try to emphasizeonly the thingsyou haveto know to work with the language. For most C applications, pointers are minimallyrequiredand are often quite transparent to the operationof the program. My major complaintaboutthe C programming languageis one that few peoplecommenton;in its ANSI standardform, C cannotaccessdata smaller than bytes.The versionof C usedin this book canread from and wdte to bits,but you will still requiresome very convolutedcodeto carry out somehardware interfacing. Despitetheseconcerns,C hasa numberof characteristicsthat make it well suitedfor usein this book andfor individuals learningaboutprogramming. Firstly,becausethe languageis over thirty yearsold, a plethoraof bookshasbeenwritten about learningand codingit. Secondly, it is actuallyquite easyto wdte efficientandreadablecodein C, and a good portion of

27

the text in this book is devotedto teachingyou to do just that. Lastly,I believeHT:Soft'sPICC LiterMcompiler is the besthighJeveldevelopmenttool available for the PIC16F684microcontroller,and I'm pleased that it canbe usedin this book to teachyou aboutthe PIC@microcontroller. PICC Lite compilerhasthree thingsgoingfor it that I feel are critical to its usein the book and for use by new users.First,it integratesextremelywell with MPLAB@IDE. As shownpreviously,PICC Lite compiler works not only with MPLAB IDE's basicoperations,but it producesthe necessary information neededby the MPLAB IDE simulator,MPLAB IDE debuggers, and ln-cCircuitemulators(ICEs) to allow source-code-level debugging.I feel this is criticalfor producing,testingand analyzingapplications. Second,PICC Lite compilerproducesvery efficient code.In Section12,I demonstratehow I would solvea numberof math problemsusingassemblylanguage programming.At the start of eachprogram,I have written out the operationof the programin C;you may

want to cut and pastethis examplecodeinto separate C source-code filesand seehow efficientmy assembler is to the codeproducedby PCC Lite. I would be surprisedif my codehad more than 10 percentfewer instructionsthan any application,and I wouldn't be surprisedif there are caseswherePICC Lite version createdcodewith fewerinstructionsthan I did. Finally,the priceof PICC Lite compilercannotbe beat.PICC Lite compileris not a crippledversionof the full productiit includesa1lthe capabilities, including codeoptimizationand simulator/debugger/emulator support,of the full product.Thisis an important point becausewith PICC Lite compilerintegratedinto MPLAB IDE, you havea developmentenvironment with capabilitiesthat would normally costwell over $1,000dollars.Thkentogether,the C programminglanguage,with applicationswritten with an eyetoward readabilityand efficiencyand implementedin the PICC Lite compiler,is the bestmethodof learning aboutthe PIC microcontroller,microcontrollerprogramming,and applicationdebugging.

Experiment 5-Variable Declaration Statements Declaringvariablesin PICC Lite compileris generally assimpleas: int

variableNamet

beforethe main statementof the application.This will createa 16-bitvariablewith the labelVariableName (or whatevervariablenameyou want) that canbe used anywherein your program.In this experiment,I would like to discussa few issuesregardingvariableand hardwareregisterdeclarationsto help you make surethat you cansuccessfully createapplicationseventhough you haveworked throughonly a few experirnentsin the book. VariableName is a label and canstart with any upper-or lowercaseletter or the underscorecharacter. After the startingcharacter,the restof the label canbe any letter,number,or underscorecharacter.Blank characterscannotbe usedin a label;if blanksare encountered, the compilerwill try to divide the character stringsto determinewhich of them are programming statements, directivegor defines.These character restrictionsare alsousedfor subroutineandfunction names, For standardvariables,two optionsexistthat you shouldbe awareol The first is the ability to initialize the variablewhenit is declared.By addingan equals signand a constantvalue,you can setyour variableto

28

a specificvaluefor the programwithout havingto add anotherline later in the code.To initializethe variable r to 47,you would simplykey in:

Another option that is availableto the declaration statementis the constke1'word,which convertsthe declaredvaluefrom a variableto a constant: conat

in!

xconstant

= 47t

In the declarationof xconstant,anytimethe label xConstantis encountered,the compilerreplacesit with the value47.By declaringxconstant asa constant,you canno longerwrite to it. For examplethe statement: xconstant

= 48t

will return an error. If you were to look at the pic16f684.hfile that was put on your hard file whenPICC Lite compilerwas installedin the \PICCLITE\nclude folder,you will see a numberof statementsthat look like: static

volalile

unsigneal

char

tMRlIJ

e 0x08,

Theseare hardwareregisterdeclarationsand involveadditionaloptionsthat you do not needto be

l , a l P I C @l ' l C UE x o e r i m e n t s f o n t h e E v i l

Genius

concernedwith, becauseHT-Soft hastakencareof declaringall the hardwareregistersin the PIC16F684 microcontrollerfor you. I want to saya few wordsaboutvariablenames. Pleasetry to make an effort to make them representative of what they are beingusedfor. I seemany studentscreateprogramswith variablenameslike Reg3 whenthe nameRemainderor PWMValueis much more representative of what the variableis usedfor in the application.Although I recognizethat the variable memoryis limited in the PIC MCU, pleasedo not feel you haveto useone variablefor multiple functions;try to useeachvariablefor one purpose.Usingthe same variablefor multiplepurposescan makewriting your programmore difficult and causeproblemswhen different functionschangea variablein waysthat screw up the operationof other areas.

One areathat alwaysgetsnew programmersconfusedis in the areaof counters.I recommendthat you usethe conventionalvariablenamesof i, j, k, and n. Thesevaluescameinto usewith Fortran,the first highlevelprogramminglanguage,and by usingthem for this functionconsistently, they are immediatelyunderstoodwhenreadingthe code.By namingvariables approp ately,you will find the tasksof trackinghow datais beingprocessedby the applicationand of debuggingthe codemuch easier. As I explainmore aboutthe C programminglanguageto you, I will expandthe variabledefinitionto includearrays,pointers,and local and globalvariables. For the time being,if you keepwith the simpleformat of the variabledeclarationthat I haveqivenyou here. vou will not haveanv Droblems.

ExFeriment6-C Data Tgpes The bit valueis not supportedby the C languagestandard,and I would recommendthat you do not useit for variables.I realizethat a singlebit is usefulforlag variablesrn aprogram,but becausethe bit is not availablein other C implementations, you will not be able

Table 2-1 PICELite ComEilerData Tgpes Dependingon your programmingexperience, the need to specifyvariabledata typesmight seemnew and somewhatominousto you.The restrictedvariable memoryavailableto you in the PIC microcontroller under the PICC Lite compilercanmake the decision on whatvaluesto specifyseemingly moreominous. Therereally is no needfor this apprehension;most variablescan be declaredusingthe lnt datatype wilhout problem. The data typesavailableto you are listedin Thble 2-1.For the mostpart,the data typesfollow Americon NationalStarul.ards lnslinrle(ANSI) standards, or what I call StandardC values,and are availablein C compilen for other processors.This allowsyou to import programsor partsof programs(usuallyreferredto as snippets)that havebeencreatedfor other applications, but you would like to useon the PIC microcontroller. Similarly,if you wereto comeup with somegoodprogramsor algorithms,this could be exportedto other systemsquite easily. Threedeviationsexistin the PICC Lite compiler data typesto ANSI C that I havemarkedin Table2-1.

Tqpe

Bit Size Comment5

bil

I

Booleanvalue-Note: Not a Standard C data type ASCII character/signed integer(-128to 127)

unsignedchar 8

Unsignedinleger (0 1()255)

short

Signedinteger(-32,7681o32,767)

16

unsignedshort 16

Unsignedinleger (0 to 65,536)

int

16

Signedinteger(-32,768 to 32,767). sameasshofi

unsigned int

16

UnsignediDteger(0 to 65,535);same as unsignedshort

long

32

Signedinteger(-2,147.483,648 to 2.14'7 ,483,64'7)

unsignedlong 32

Unsignedinteger(0 to 4,294.961.295)

float

Reat(0 to 1/-6.81(10r3);Assume 3 dig, its of accuracy/default floatingpoint modc;Note:Nota Stanldard C datatype

21

Real (0 to 1/ 6.81(10r3)r Assume6 digits of accuracy/specified usingPICL D32 compilationoption Note: ln StandardC, this is float, not double

5ection Tu-ro I n t n o d uc t o r y C P n o g r a m m i n g

29

to import the codedirectlyto other implementations of the C programminglanguage,and you may end up gettingin a bad habit of working with bit variables. Note that I do not make the samecomment for individual bits in a specialpurposeregister.Theregister bits are uniqueto the PIC MCU and,assuch,would not be involvedin a codeexportto anotherdevice. me float anddoubk (floating point) variabletypes are substantiallysmallerin their PICC Lite compiler forms than in their high-end processorforms.For the most practical applications,you will not seea differencebetweenhow they execute,but you may have issueswith very large numbers or numbers having many digitsof precision.

ut {! I

H-l

As I haveindicated,the C data typesare strongly typed,andwhen you are equatingvaluesbetween typeEyou may get a waming or an error from the compiler indicating that a type conversionis required. If the data is stored in a format compatible with the destination,the simplestway of resolvingthis is to use the type cast,which consistsof placing the desired data type in parenthesisbeforethe sourcevariable.For example,if the variables i and j were different tlpes but data couldbe passedbetweenthem,a simpletype cast.asshownhere.couldbe used: i

= (iTr'ee) j,

The experimentfor this applicationis quite simple; just a few linesof codedemonstratewhat happens with type conversion: ff4

*incluale - rave6tigate /* cTlDe.c PICC lJite cdE)iler

Dala

Type

in

operation

Tbie Program shosra the D€eal for variab].es wheu dlata iE tsransf€rretl.

TtrD€ Casling

ThiB plograln IDE Siltrrlator on1y.

unaler

nlrhe prealko 0{.09.15

is

tlesigrned

tso run

the

of

MPr.al

char float

it jt

main ( )

t i = 47t

)

j = i;

//

can vou Plac€

char

i = i,

//

can you Place

float

(1 == llr while // Enal cTtrp€

//

into

f,loat?

inEo

Loop Forever

When you compilethis application,you are goingto get the message: warningt000l C:\ Evil Genius \c!P!.De\cTtDe. iq)licit converEioa of float to inlege!

c 27

:

is tellingyou that the statement"i = j;" This message When this messagecomes involvesa data conversion. up,you haveto decidewhetheror not to resolveit by usinga typecastor by leavingthewarningasis Rememberthat the two datatypesstoredata in different ways(whichcanbe inferredfrom Table2-1);casting the float type to a char is a bad idea becausewith the type cast,the fkst byte of the float type couldbe passedto the char without beingmodified.In this case, you would leave it asis. For virtually all the applications in this book and for virtually all the PIC MCU applications that you will do on your own,you will only requirethe int data type. This 16-bitbit-variabledata type will handlea reasonably largerangeof valuesaswell aswork with ASCII characters.This data type is handled well natively in the PICC Lite compiler,but for long and floating-point datatypes,additionallibrarieswill haveto be addedto the final application.Theselibrariestake up quite a bit of spaceand can slow down the execution of the appliTo avoidthis spaceissueand cationsignificantly. reducedexecutionperformance,I recommendthat thesedata typesnot be usedunlessabsolutelynecessaryand the impactof their operationis well understood.Later in the book, I will give you a coupleof thoughtson how to simulatethe operationof these data t)?es using8- and 16-bitvadables.

t,,l tt!

'&&* 3C

30

char?

l , a 3 P I C @l l C U E x o e r i m e n t s f o n t h e E v i I

6enius

l-+1

Experiment 7-Constant Formatting .aJ & !{DTDIS & PWRTTN & IICIJRDIS & -CONFIG(INTIO T'NPROTECT \ & I'NPRC},IECT & BORDIS & IESODIS & FC!'DIS) t

iU i-

main( )

{ 't rt

PORTA = 0t CMCONo = 7i // Turn off Corq)arators ANSEIJ = 0r // Turn off JU,C TRISA - 0*49t // E':able PORTA IJED Oull>uts

When I introduceddeclaringvariables,I suggested stronglythat variablenamesshouldbe representative of what the data is usedfor, and if the variablesare usedfor commonfunctions(suchascounters),they shouldbe givenconventionalnames.Thispleais commonto manyprogrammingbooks,but what isn't all that comrnonis a pointer to speciq/ingconstants in the mostappropriatedatatype for the situation. The readabilityof a programcanbe enhancedor damagedby programformatting,by variablenames,or by comments.

Table 2-2 EonstantFormattingOFtionsand 5uggested Best UseE Constant Definition Fomat

Best Use

Decimal

##

Default Value

Hexadecimal

0x##

RegisterCounterValues

Binary

0bf#ffifrfrf Noncounrer RegislerValues '#' Human InterfaceValues

ASCII

while(l

== 1)

l'a-

Ijoop Forever

//

{ ( 0 = = ( P O R T A& ( 1 < < 3 ) ) ) PORTA = '3'// Tuln on Fou! LED8 eLse PORTA = 4r // Turn on Remaining Four if

//

) //

)

IJEDS

erihw Enal cconfu8e

Chancesare,you couldnot understandwhat cConfusedoesby just looking at it.You will probably haveto look at a schematicfor the PICkit 1 starterkit LEDs and applicationinput hardware.By makingfour changesto this prograrn(and turning it into cClear.c),I think you'll agreethat the functionand what is happeningis a lot easierto understand.

*incluale - *cconfuse.cz /* ccLear.c a Deobfuscator

after

pasaing

!i, i:?

through

T]hia is lh€ same program as $cconfuse. c-, but with rrore appropriately chosen constant tlata values. nyk€ predlko 0{ . 11. 1s

1= i:-;;

Thereare four waysto specifycomments,asis listed in Thble2-2 alongwith the situationswherethey are bestused.In cconfuse.c,I havecreateda simpleapplicationthat usesdatain differentformats;beforeburning it into a PIC16F684and runningit in a PICkitrM1 starterkit, try to figure out what it does.

*incluale - wri!€ /r ccoffus€.c to plcki! Ilaralware in a Confuaing Way This program Derforms function ia obfuscated t]lDes.

CONI,IC ( IIITIO & WDTDIS & PWRTEN & I{CLRDIS IJNPROTECT \ & I'IIPROTECT & BORDIS & IESODIS FCI'IDIS ) i

nain( )

{ PORTA = O' Cl.fCONo = 7; // \tr'r ofg Cornpalators ANSEIJ = 0t // I'urn off ADC TRISA = 0b001001i // EnabLe PORTA LED Outsputss

Interface

wr!ire(1

a simple task. but ils b!' poorly chosen dtata

== 1)

//

looD

Forever

t it

fiE'ke prealko 04.11.15

(0 == 8A3 ) PORTA = 0b010010t

//

Button Presseal, D4, D7 On

PORIA = 0b000100r

//

BI'rt.Eo'r Releaaeal, D5, D5 On

//

, l

5ection Tulo

& &

//

D0, D2,

D1,

D3,

eli'nw Enal cclear

I n t r o d u ct o r y

C Prognamming

31

Threeof the changeswereto convertthe TRISA andPORTA registerassignmentvaluesto binary from decimal,hex,andASCII. I think you will agreethat they wereeffectiveand helpedyou seewhat washappeningin the applicationand the output values.The fourth changewasto eliminatethe complextestof the RA3 (button input pin) and simplydo a bit compare. With the bit compare,you couldeasilylook at the PICkit 1 starterkit schematicand seethat RA3 is connectedto a pulled-uppushbutton. When decidingwhich constantformat to usein your program,you might want to follow theserules: .:t,13 .

$ .i.il

.

-i-,

Use decimalby default.For basicvariableand variablefunctions,decimalis probably most appropriateand easyto read. Use binary when you are working with a register that doesnot containcounterdata.An

.

exampleof this is loadingthe OPTION register with the value0b10011111. Use hexadecimalfor registercountingor result data,Comparinga counter'scurrent valuewith a hexadecimalconstantwould be most appropnate.

.

UseASCII data when human interfacesare involved.Thisincludestext messagingaswell as userinput.

.

Data sizesshouldbe appropriate.Use six bits for the two setsof six-bit PORT andTRIS registers.For other registers,useeight bits.

I think what I am trying to sayin this experimentis bestsummedup by the maxim:"There are 10typesof peoplein this world: thosethat understandbinaryand thosethat don'1."

it t

a::-i

Experiment 8- RssignmentStatements .rj r", il i: t_'j'

1l'-.€ .'].{

,t:; I

c;

If you are familiar with other highJevelprogramming languages, you are awareof the statementtype called the assignment statemezl, which is usedto storedata of a variableof a specified type.In thepreviousexperimentEI havedetaileddifferentdata typesaswell as how variablesare declared.In this experimentI will presenthow assignment statementsare written in C. The basicform for a C assignment statementis: ValldbleNam€

.:;".1

.... ill1.:

1::

= E = B Return True if the first value is equalto or greaterthan the second.

while(].

== 1)t

0t

t d

gj

P

Endl er,ogic

A j )

(

i f ( k < n )

(

Statsement(a)

//

Ex6cut6al

if

"i

> j"

and

\k

Executetl

if

"i

> j"

anil \k

< a/

) Somepeopleare born troublemakers. When I was16,I had to go to a two-hour defensivedriving course becauseI had too many pointsfor speeding.Everyone in this coursewasgivena Driver'sEducationMqnual with the basicrulesof the road.Severalminutesinto the course,somebodyput up his hand and notedthat if a policeofficerwasdirectingtrafficin a mannercontrary to a set of traffic lights,then you must follow the policeman'sdirections,and if a farmer herdinganimals wasdirectingtraffic in a mannercontraryto traffic lights,then you shouldfollow the farrner'sdirections. He then askedthe question:"So,what do you do if there is botlr a police officer and a farmer that are giving contrarydirectionsto the streetlight?"I'm bringing up this little storybecausein the previouse>.periment, I presentedthe idea that the next statementafter the if statement(or the following elsestatement)would be executed.Andwhen I presentedthis conceptto a setof high-schoolstudentqI wasquickly askedthe question, what happensif you havean if statementfollowing anotherif or elsestatement?

Section Tuto

else

{ // ,

s!at6m€nt(a) // fL

>= n/

fx

, // fr Another solutionis to noticethat there are only two areasin the codeabovethat executeconditionalstatements,and they couldbe accommodated by two if statements: > j)

if

((i

//

stat€ment(a)

(

) elae

if

(i

&& (k < n)) ttrecut€al

if

"i

> j"

and

*k

< n,,

Executeal

j.f

"i

> j"

and

\k

>= n/

r."i" *...!.

f*r 1{ it,

> j)

t //

a1

Statse$ent(E)

I // ti

{,'.l

This methodisn't bad,but couldbecomevery long if statementsare wdtten for both the if and the else.And, dependingon how the compilergeneratedthe code, this methodcould be very inefficientin termsof code sizeand executiontime.

1 i

I n t r o d uc t o r y C P n o g r a m m i n g

41

&{

i_ld

s)

s) 6

The generallyacceptedmethodof combiningconditional executionstatementslike this is to recognize that the if statement and the conditionally executing statement(s)following it are all one statementand can be nestedunderneaththe originalif statementasin the following:

k = ot

Execul€al

if

*i

ExecuEeal

if

"i

> j"

enal *k

< n"

{

# ",".d !s{

c.' L;

EJ lri

StalelBent fi| //

(a)

> j"

andL \h

>= nz

l els€

This PlogrEnl wif!. .tunp b€tsw€ea aliffelent alue to itiffelent coatlitiona. The LED values

*.s"*

((4 == k) && (0 == n))

if

{ PORTA = 0b000000100r TRISA = 0b011101011t

Path

//

3

k = - 2 i

) (4 == k)

e13€ if

{

poRTA = 0b000000100t // TRISA = 0b011111001t

PaEb 4

k = - 2 ,

) (

{

LEDE

PORTA = 0b000000100r TRISA = 0b011011011t

j,

PoRTA TRISA |

ll

//

& &

//

0b000010000, 0t011001U1,

//

Path

5

Ei

| // fi k = k + 2r

//

N€xt

Tin6,

co to

\e1a€/

e rihw Enal cNoNest

OnceI had the programworking,I removedthe redundantbracesand looked for placeswherethe multiple if statementscould be combined into something simpler.I cameup with the following cNest.c: *incLuale - Jurltr Betsween IEDg /* cNest.c

Inain ( )

rrilh

ifs

\rif" NeEt lhe various Ithl.6 Program rrill anal get statenenta of ocNoNeBt.c" !o lry ia eaaier to f,ollow. Drogram that

t

42

5

t

rt

PORTA = 0, C!4CONo = 7r // r'rr! .ANSEL = 0r // Turn

Path

else

]

k,

//

)

a!e:

-CONFIG ( INTIO & VIDITDIS & PWRAEN & !,ICLRDIS I'NPROTECT \ & I'NPROTECT & BORDTS & IESODIS FCUDIS) t

i,

(0 == n)

if

LED Anoale Cathoale DO NA{ RA5 Dl RA5 RA4I D2 RA4 RA2 D3 RA2 RA{ D{ A.E5 RA2 D5 RJA2 RAs D5 RJA2 RA1 D7 A:A1 RA2

int

UJ

IJaEb 2

K != 0

//

t

|

-,i

Ilat }l L

) elae

ifa

lErhe D!6alko 04.11.15

d

k)

elae

iiJ

6""*

< L29t j++)l

PORTA = 0b0001000OO, // TRISA = 0b011001111, , // f,i

I shouldpoint out that this methodof nestingdoes not apply only to the if statement.It appliesalsoto all the conditionalexecutionstatementsin the C programminglanguage. As you work throughthe book, you will seemany examplesof nestedprogramming statementsof differenttypes.Actually you'veseenone already,the 500ms delaycodethat wasusedin the cFlash.cprogramconsistsof a for statementnestedas part of anotherfor statement. To demonstratehow nesting can simplify a program,I createdcNoNest.c.This programflashesdifferent LEDS on the PICkit 1 starterkit in a somewhat randomorder,but it is hard to follow and seeimmediately what is happening in the application:

r 1

! !

j

{

*incluale - aluJnlr B€tsw€e! LEDE with /* cNoNesl.c

&5F S.-{

= 0r

PORTA = 0b0000100OOi // TRISA = 0b011101011t

(

.ii"[

Foreve!

(0 == n)

if

slat€nenl(a) elae

w

Loop

valueE

{

) *-{

(j (0 --

if

{

//

//

tests

special

for (t = 0r i < 255t i++) // siry)te Delay rJoop for

a E ( r > t , i f , ( k < a )

//

== 1)

whl.1e(1

t

k & n ale

//

off conl)alators of,f, ,rDc

l , e 3 P I C @l l C l JE x o e r i m e n t s f o n t h e E v i I

6enius

a

The r,ED valuea

ar€!

eLse

LED Anoate Catboale DO RA{ RAs Dl RAs RA1I D2 RA4 RA2 D3 NA2 RA{ D{ NAs RA2 D5 AJA2 RAs D5 RA2 RA1 D? RA1 A:A2

if,

((4

== k)

&& (0 == o.l ) // Cdlbitr€ the ,/ Teat Coaalitions

PORTA = 0b0000001OO, ll TRISA = 0b0U101011,

Patjr 3

r1\

k = - 2 t

) elEe

{

rvk€ Dledlko 0{.11.15

if

F.(

(4 == k)

PORTA= 0b0000001OO, // TRISA = 0b011u1001t

Nr"

PaE}r 4

k = - 2 , , /t fr elae Lf (0 == n)

t

& P!{RTEN & !,ICIJRDIS & _CONFIC ( INTIO & lfIllDIS T'I{PROTECIII \ & IJNPROIECT & BORDIS & IESODIS & FCMDIS ) t

til

X rd

,

;J ;

PORTA= 0b000000100r // TRISA = 0b011011011,

Patsh 5

) elEe

inr nain(

{

j,

i,

k,

PORIA = 0b000010000r TRISA = 0b011001111t

nt

b.E>

5

)

I

{ PORTI = 0t CfiCONo = 7r // 'l''rt AIISEL = 0r // Tura

k = la L 2? //

while(l

k & n ale

-=

1)

//

SD€cial

Test

ve].ues

(1. = 0r (J = 0r

i j

Loop For€ver

< 255, < L29,

i11) jLLlt

//

Sirqlle

Delay

IrooD

(0 == k) (0 == n)

t PORTA TRISA -

Tine,

co

tso *e].se/

{'d

0b000010000r 0tr011101011,

//

Path

1

) elEe

t

PoRTA= 0b00010000O, /l TRrsA = 0b011001111t I ll fr

PaE}r 2

elihlr Enal cN€st

// //

)

t for f,or

N€xt

off CdEaratola off ADC )

k = O, //

g *

fr

ll

I

if if

Path

//

I admit that cNest.cisn't a hugeimprovementto cNoNest.cin tems of readability,but by reducingthe number of unneededbraces,I did manageto reduce the amountof spacethe main loop of the application takesandmadeit easierto look throughthe ent e application.This is an example of what you will seeif your taskis to supportan applicationthat hasbeenin usefor a long time and hasbeenmodifiedto reflect new requirementsand fixesto variousproblemsthat havebeenencountered.In thesetypesof applicationq it is often impossibleto understandexactlyhow the codeis working,and you end up makingsmallchanges that perpetuateits increasingcomplexity.

m ?-' I J ;t

tlt a

(tl f'r'

ExFerimentltl-The 5ulitch DecisionStatement

L

singlevariablemustbe tested.Multiple if and else statementscanbe combinedto meet the reouirements quite simply.Thecodebelowdemonstratesihis: (4 == L') ll

Go south

If

raalex et

'

'J

m

rr

{

&,

t Dir€ction

l

= 180,

' ela€

if

15 == L,

//

Go North

ll

co East

if

Intt€x

at

a 5 o ,",{

5

{ Dir€ctioD

Casesexistwheremultiple statementsare required becauseof multiple constantvaluesagainstwhich a

= 0t

) e].e€

if,

(7 == l,

if,

rDtlex

a!

7

t

5ectionTuuo I n t r o d u c t o r y C P n o g r a m m i n g

43

= 90t

Dilection

if

else

//

Go Iqeat

for

€Ise

€verylhinq

// //

t

n!

s

.t-,'

f,f) b.{ j't

I

= 270t

Directlon ll f!

If you were writing your application in BASIC, you statementslike the would probably use the seLect/case switch/casestatemenrsusedin cswitch.c listed below:

*incLual€ - Detlonstrat€ /* cswlEch.c fhl,s Drogram alemonstratea awiEch atatdlent.

cE)eratLon

of

tsh€ op€ralion

ttsltilch" of

lbe

rq.ke prealho 0{.10.18

f 5

i n t i = { t int Di!€ction

= -1t

{t I

nain( )

{ switcb

(i)

t caBe 4. // Dir€clion bzaaki // c aBe 5t // Directioa bieaki case 7 . I / Dilection b!€aki default: // Direction ) // hcti\ta

rtr *-r ".{

{i!

!thil€(1

cio South lf = 180t r.eave switch if Go Norlh = 0t Glo Eaat = 90; co west = 2?0t

if

Ind€x

== 4

at

Even

; il

//

Slatem€ut the \Case/ Ncase Slatern€nt

the

Cour1tet

caae

7"gO / /

Goiug

aouth

Inalex

= {

caee

O .

Goirg

Norlb

Itral€t

= 3

Going

EaBt

Itrale*

= 2

Gol.ag Weat

IEdl€x

= 1

7 //

thlng

E1a€

ceee 9Or ll j - j + 1 , b!eaki tt€fault: //

== 1)t //

hcliws

E^d. crf

Obviously,you have to plan for situations where you can eliminate the break statementin your switch code,but whenyou do,you reallyhavea feelingof accomplishment-andthere'sa goodchanceyou've simplified the amount of application code required for the program.Wh€n you are startingout,you will probably use the switch/casestatementsfor situations like this one,where multiple if statementsexist, and all of them are comparing to a constantvalue and executing a break statementat the end of the case.

ai) ii{ ( a

r!1 ld54

}{{

Ls$ 44

!o

(

This application provides the samefunction of respondingto multiple possibleconditionsthat are listedin the multiple if statementsat the start of this experiment.By usingthe switchstatement,the codeis actually a lot simpler.The casestatement'sparameter combinedwith the switchstatement'sparameterforms the if statement:

"ir-' i-i

cas€Pafameler)

Di!€ctLoa // Clear (Dir€ctioD)

j = J + 1,

fo!

after nd{t

=-

The useof the casestatementis obviouslya lot easier to key and a lot lesslikely to havea syntaxerror like you had with the if statement. The "default:" conditionworks exactlythe sameas the elsestatementin the if statement;the statements after it executeonly if all parametersdon't match any of the casestatements. This is all there is to the switch/casestatements except for one point: the break statemetu.This statement causesexecution to jump out of the current switch statement and executethe statement following it. You can do someinterestingthingsif the break statement inside a switch block is not included. For example,if the switch block was being usedto record the direction of motion (keeping in the tradition of the wheel direction), it could increment the counter for each90 degreesrather than placing a hard value in the countervariable.The examplecodefor this implementation is: I = 0t awitch

stat€$eat Iaal€x aE 5

halex

Slaternentss sb!€ak" or \ // fL

] |

(SwitchPalameter

t

)

al

l , e 3 P I C @I ' l C UE x o e r i m e n t s f o n t h e E v i I

6enius

ir* i

E x per im entl5-fo n d i ti o n a l L o o p i n g

ili ,,?{

#ineLutle /* cFlaah !'lhile.c I.ED on a PIC16E584

ginE)le

to

Elash

an a-ii

|!hi€ Program is a noalif,ietl to uae ..whil€,' Loops iDsleaal of \fo!', loops RA4 - r,ED poailive ItA5 - IJED Negative

ti:: c Program

version

of

\tcFlash.c,,

:,- "

Connecti,on

i:

I'ke pretlko 0{.05.19

There are a coupleof rnethodsof implementingconditional loops.In this experimentI will look at the most common method: the bastcwhile loop. The while loop allowsyou to repeatedlyexecutea set of instructions while a test expressionis true.That is,the while loop canbe usedfor conditionallyrepeatingcode.But it can alsobe usedto implementinfinite loopsin your applications.Someprogrammingphilosophiesdo not use the basicwhile loop,but they alsodo not provideyou with the simplereadabilityof the basicwhile loop. The while loop is a programmingconstructthat testsan expressionbeforeallowingexecutionto take placewithin the loop.If the expressionis not zero,then executionwill take placewithin the while loop,and at the end of the loop,executionwill return to the expressiontest and the processwill repeat.If the expression evaluatesto zero,then executionwill skip pastthe loop andcontinueat the statementafter it. To showhow this works,the following statements canbe used:

while

(i

i

F nain( )

{

t\

PORTA = 0t CMCONo = 7t ANSEI = 0t TRISA4 = 0t TRISAs = 0t

ll // //

\E oft Corparatora Tuln of,f ADC !,take RA4./RAs OutDutg

//

Loop

l'i

it-,,'

while(1

== 1)

Foiever

!-:.

t i = 0, j = 0, whil€ ( (i

{ 1

(

a; < 2s5) ll

= t + 1t (i > 2s5)

//

(j

< 78))

Incronent

//

Ro11 Ov€!

//

ToggLe

ar!

S!fiaLL Counte! to

Larg€

i-J

Counler

J = j + 1, // fi ) // elihw

< {)

= i + 1; // elih!,r

& &

,

{ )

-CONFTG ( INTIO & WDTDIS & PWRTEN & MCLRDIS IJNPROTECT \ & T'NPRoIECT & BORDIS & IESODIS ECMDIS ) t

//

WhiL€

lJoop Coale

In thesestatements, the variablei is initializedto zero.Next,it is comparedto 4, and if it is lessthan 4 (i.e.,the expressionis true or returnsa nonzerovalue), the codeinsidethe while loop (incrementingthe variablei) is executed.Wheni is no longerlessthan 4, the while expressionbecomesfalse(and returnsa zero value),the codeinsidethe while loop is skippedover, and executioncontinuesat the statementafter the closingbraceof the while loop. To demonstratethe operationof the while statement in an application,I havemodifiedcFlash.cinto cFlashWhile.cin which the two for statement delays havebeenreplacedwith a singlewhile loop that incrementsthe two variablesi andj until they are both greaterthan 255and78,respectively:

5ection Tulo

R:A{ = RA4 ^ 1, | // eLihw , // wrd. cFlaah

LED

;-i

while

Although cFlashWhile.cis a direct copy of cFlash.c, I found if I usedthe sametestvaluesfor i andj (255 and 129,respectively), the delaywould increaseto 833 msecsrather than the standardvalueof 500ms By runningcFlashWhile.cin the MPLAB IDE simulator, I wasableto empiricallydeterminethe valuefor j that would resultin an approximately500ms delay. As you work throughthe code,you will discover that I usethe while statement while

(1 == 1)

a /ot.This is my loop-forevercode,and I useit either as the overallloop in an application(like this one) to

I n t n o d uc t o n y C P r o g r a m m i n g

45

l-,

the I/O and processingcode,or I placeit at encompass the end of the applicationto stopit from returningto the caller(and end up executingagainrepeatedly).The statementcouldbe simplifiedto: while

(1)

The PICC Lite compilercan detectstatementslike this wherethe test expressionis alwaystrue (or 1) and replacethe statementwith somethinglike:

| / / goto

insiale

Code Ex€cuteal IJooPt

vthile

loop

There is anotherform of the while loop.It is the do/while,which takesthe followingformat: alo t // Coale Ex€cutetl inside

) while

(orDreasion)

the alo/whil"e loop

t

This is a subtlemodificationof the original,whereduring the first time throughthe loop,the expressionis lo execute not lesledto be true;you areguaranteed the codeinsidethe loop at leastonce.The advantageof usingthis form of the while loop is that variablesor hardwareregistervaluesthat are testedin the while exoressiondo not haveto be initializedto force execution to work throughthe codeat leastonce.SomeC I implementationsh avethe do/untilstatement,whrch do not like becauseit forcesnegativelogic into your program(i.e.,loopinguntil a conditionis true is the logicalnegativeof loopingwhile a conditionis true). Two keywordsare alsousedin while loops:break and continue."Break" will force an exit of the while loop,and"continue"will force executionto retum to the while statementwherethe expressionis evaluated. I do not usethesestatementsbecausethey Personally, act asgotosin the program,changingexecutionwithstalements. out regardlo theslructuredProgramming And I do not recommendthat you usethem in your programs,asthey canbe difficult to debugand can leadan applicationto behaveunpredictably(especially if you are new to programming).

l5-The ForStatement Experiment PXekit*

L

The designof the for statementis actuallyquite ele"what are the gant andresultsfrom the question, requirementsof repeatingloops?"The statementformat ls: (Initializationi for Incr€nent ) Statemeat

I try to teachprogrammingasI wastaught,and that is to emphasizethe capabilitiesof the differentfunctions built into the languageand how to usethem approprF Somepeople,however,seemto atelyin applications. think they canusethe for statementin virtually any situationwhereconditionallyloopingcodeis required.I guessthe theorybehindusingthe for statementin different situationsis to reducethe numberof statement typesthat are in your programrninginventory.In this experimentand at the end to this section,I will show that the for statementis a wonderfullyflexiblestatement,but that it canmake codea lot more complexto understandand debus.

46

l.oop

'lesu

E {pressioni

Loop

and its operationis similarto the BASIC codefor a loop.Initializationis the processof initializingvariablesthat are (ideally)requiredfor the loopingoperation, but the processcan alsoincludeother assignment is an exPression. /oop testexpression statements.The similarto that usedin the while statementto test whetheror not the loop shouldrepeat.Finally,the /oop incrementstatemenlis normallyusedto incrementthe loop counterafter eachiterationof the loop. The for statemenlis typicallyusedwhenyou needa loop that repeatsa set numberof times.It might look like the following: (i = Or i < ua*Nlrrib€tt i++) for Executseal Repeatedllv // gtatenent slatementi nfor,, IJoop

l,e3 PICo llCUExperiments fon the Evil

Genius

by

and could be modeled as:

$rhi1e

(i

nvke prealko 04.11. 09

< Maxl{llmber)

{ Statementr i )

Executeal Repeateally Statenent \tfor,, Loop Equivalent to Ni++"

//

= i + 1r // elihw

//

by

CONFIG(INTIO & WDTDIS & PWRTEN & DTCIJRDIS& UNPROTECT \ & UIIPROEECT & BORDIS & IESODIS & ECMDIS) t

j..

int

In this for statement,a counteris initializedto zero and is incremented(usingthe "i+ +" statement,which is equivalentto i = i + 1) until it is equalto MaxNumber.This shouldbe quite easyto understandand usein your own applications. The useof the for statementbecomesmore complex whenyou considerthat multiple initializationand loop incrementassignment statementscanbe used (with eachseparatestatementseparatedby a comma). lf commasare not usedto separatethe assignment statements, the compilerwill becomeconfusedasto how to parse(convert)the statementscorrectly.The following for statementis completelyvalid: (i = 0, for Statetnentr

j //

= 47i i < MaJdilumberi i++, j = j Executefl Repeatedly Statement \forz Loop

lhrough

PICkit

This Program r,ri11 ro11 throuEh each PcB. rrEDs built inlo the Prckit The LED values

the

only

$for/'

PORTA = 0t ClilCONo = 7, ANSEL = 0r

// //

Tlvr'r off r'urn off

Conparators ADC

k = 0t

//

S!a!t

LED 0

for(rr)

//

r.oop Forewer

(i = 0r i < 255r i++) // for for (j = 0r j < !29t j++lt

- 2) by

I

k) && (0 == n)r (1 == k)u

n++)

for (rI = 0, (2 == { // Sinulate {if PORTA = 0b010000t TRrSA = 0b101011t | // tof for (n = 0, (3 == 'if { // sinulate PoRTA = 0b000100, TRISA = 0b101011, , // t.of f,o! (n = 0r (4 == { // simulate 'ig PORTA = 0b100000t TRISA = 0b011011t | // rof for (n = 0, (5 == trif { // simulate PORTA = 0b000100i TRISA = 0b011011t

k) && (0 == n)t (2 == k)/

n++)

k) && (0 == n)r (3 == k)/

n++)

k) && (0 == n)r 14 == k)"

n++)

k) a& (0 == n)r (s == k)/

n++)

+ 1)

% 8r

slatements.

5ection Truo

but

,

Delay

k) && (0:= (0 =- k)"

k = (k

to be 'cFor", explosive.

simple

f,or (n = 0r (0 == { // sirnuLate \if PORTA = 0b010000i TRISA = 0b00L111t j // '.of for (n = 0r (1 == t // simulate \if PORTA = 0b100000; TRISA = 0b001111t

for (n = 0r (6 == k) \if (6 { // sinulate PORTA = 0b000100, TRISA = 0b111001t j l/ rof for (n = 0r (7 == k) Nif (7 { // Simulate PORTA = 0b000010 t TRISA = 0b111001t J // rof

are:

The original nam€ was going that se€neal too potentially

at

{

r,ED Anoale Cathoale DO AA4 R.A5 D1 RA5 RA4 D2 RA4 NA2 D3 ai[2 RA4 D4 RA5 R]A2 D5 RA2 AA5 D6 RA2 RA1 D7 RA1 RA2 Using

nt

t

8 LEDE using

of

k,

nain( )

In this for statement,both i andj are initialized,and both variablesare changedin the loop incrementportion ol the for statement. To demonstratehow versatilethe for statementis,I havecreatedan applicationthat cycleseachof the PICkit 1 starterkit's eightLEDS.(It will be explained in more detail later in the book.) Ratherthan explaining how the for statementswork in the application,you shouldwork throughthem on your own (it's really not very hard-especiallywith the providedcomments).

*include /* cPKr,ED 2.c - Ro1l oII].y \for?'

j.

//

// //

rr), n++)

&& (0 == n)t == k)/

n++)

&& (0 == n), == k)/

n++)

rncrement of o-7

IJoop

k within

Erl..l CPKI,ED 2

Introductory C Programming

range

For Eonsideration At the start of this section,I noted that C is notorious for its ability to allow programmersto createvery eff! programstatecient but very difficult-to-understand ments.In the lastexperiment,I showedhow the for statementis very versatileand how it canbe usedto replaceall the traditionallyusedconditionalexecution motivationfor writing complexstatestatements.The mentsis usuallyto minimizethe amountof keying requiredfor an application,althoughsometimesit can seemlike the author of the codeis simplytrying to demonstratehis or her mentalsuperiority. For example,in looking at an exampleapplication, you might run acrossa statementlike the following: * ?. Match = 0r (Malch = (i = (j = start) for (PORTC != (PoRTA ^= sequencelj++l))) && (i++ < 25)i r, // Sequence Match confirnation?

At fint glance,it is probablyimpossibleto understandwhat this statementis intendedto accomplish, and,to makemattersworse,the commentis no help at all asit doesnot seemto relateto anythingin the statement.You may feel like givingup and looking for anotherexample,but you can do this;you candecode statementslike this surprisinglyeasily. When I presentedthe for statement,I noted that it wasin the lollowing format: for

( Initial

izationr

lJoop TeEt

E:.pressioni

Lootr)

sta!emen!

stateand eachpart (whichconsistsof a C assignment ment or expression)of the for statementcanbe broken out into piecesand rewritteninto piecesthat make more sense.For example,the initializationassignment statementof the for statementis: i = ( i = s t a r t ) * 7 i

and takesadvantageof the ability of C to savean Becausej intermediatevaluein a complexexpression. is equalto Stafi and it is a factor in the initializationof the two following lines i, the authorhascompressed into one:

.

i = S t a l t * 7 t j = startt

Similarly,the comparisonexpressionof the for statementcanbe broken out andunderstoodby recogn2ingthat comparisonvaluesare arithmeticvalues (zerofor false,and not zero for true).To take

48

advantageof this point,lhe Matchv^riable (the first part of the comparison)is loadedwith the resultof the comparisonof PORIA (whichhasbeenXORed with a valuefrom the Szquencearray) to PORTC.When an arithmeticor binary operatoris placedbeforethe statement,the line is equalssignin an assignment translatedasthe destinationvalueoperatedon by the other parameter.I might write out the comparisonpart of the for statementasfollows: PORTA = PORTA ^ Sequencetilr

//

Same aB 'PoRTA

j = j + 1r // rnclemen! *r., "." ilT#"

" "

r{atch = 0t (PoREc == PoRTA) if Match = 1t

Both partsof the comparisonusethe unaryincrement (++) operatorto incrementthe variablesi andj d/€r the expressionhasfinishedexecuting.When the unaryincrementor decrementoperatoris put to the lelt of the variable,asin the following example ++J i

the variableis incrementedbeforethe statementexecutes.Similarly,if the unaryoperatoris on the right sideof the variable,the variableis incrementedafter the statementhasexecuted.I recommendthe useof the incrementand decrementunary operatorsin your coding,asthey are a lot easierto key than the completestatement j

= j

+ 1t

and they are generallyacceptedasthe shorthandversion of thesestatements, A null statementis usedfor the incrementpa of this statement.This is a bit unusual,but the unary proexpression incremenloperatorsin lhe comparison vide this function. Justasthe null statementis usedasthe for statement'sincrementstatement,a null statementis usedas the loopingstatementor statementsthat follow the for statement.If you look at how I havebroken out the you will seethat there is an comparisonexpression, assignment statementto PORIA, which could be movedto the loopingstatementareaof the for statement. If I wereto write equivalentcodeto the for stateit would look ment givenat the stafi of this discussion, like this: something i = S t a r t * 7 t j = start, tilatch = 0, ((0 == uatch) $hile

l , a 3 P I C o I I C t JE x o e n i m e n t s f o r

&& (i

the Evi]

< 25))

Genius

t

PORTA = PORTA ^ Sequenceli] t (PORTC == POREA) if Malch = 1r // s€quedce Malch Coafifinalion? j = j + 1, i = i + 1t ) // €1ihrt i = i + 1t j = J + 1,

You should be able to relate this code to the original,and I'm surethe commentmakesmore sensenow. What might not make senseis the incrementing of i andj after the while loop;thesestatementswereput in to make surethe valuesat the end of the equivalent matchthe valuesat the end of the orisinal for statement. In terms of readability and decodability,I am sure the seriesof statementsI havecomeuDwith are vastlv

superiorto the singlefor statement.In termsof efficiency,the number of instructions created for either solutionis not substantiallydifferenqnor is the execution speedof the two solutionsdramaticallydifferent. The major difference between the two statementsis the amountof keyingrequiredfor them;the sequence of statementsrequiresmany timesthe numberof the keystrokesof the short for statement. It should be no surprise that I recommendthat whenyou program,you avoidheavilycompressing statementsunlessa strongreasonexiststo do so. Although you may savea substantiallygreaternumber of keystrokegyou shouldaskyourselfhow much time you might later losedebuggingor decodingone compressedcomplexstatement.

h{ L C

n

9--, TU

TJ 4

Section Tr.uo I n t r o d u c t o r y C P n o g r a m m i n g

49

Section

Three

S i m p l eP I C @ M C UH p p l i c a t i o n g

Prc16F684 14-pin ZlF socket (3MlTextool 2L4-333900-0602J reconmended) 0. 01 p,F capacito! length of 283-foot 30-gauqe sol j.d core

Before goingon,I would like to walk througha modificationto your PICkitrM1 starterkit. This minimizes the chancefor damagingeither the PIC microcontroller you are programmingto put into anothercircuit or the PICkit 1 starterkit you are usingto programthe PIC MCU Although the PICkit 1 starter kit is an excellenttool, the machinedreceptaclesocket that is built into the PCB is not designedfor many repeatedplug/unplugcycles.Looking at manufacturer'sdatasheets, military-gradedual inJine chip package(DIP) socketsare qualifiedfor 48 cycles. The specifiednumberof plug/unplugryclesfor industrialgradesocketsis 50 times(althoughthey are not tested to seeif they meet this specification). As you work throughthis book and your own experiments, you will easilyexceedthe maximumnumberof plug/unplug cyclesfor a military-gradesocket,and chancesare at leastone or more pin receptacles in the socketwill wearout and stop makingreliablecontact.Youwill find alsothat pluggingand unpluggingpartsin the machinedreceptacleis difficult and that it's easyto bendthe pins,havethem fall ofl or get stuckin the PICkit 1 starterkit's sockets.You canavoid these problemsby adding a zero insertionforce (ZIF) socketto the PICkit 1 starterkit. Followthe stepsoutlinedhere.To add the ZIF socketyou will needthe following: 1 PICkit 1 starterkir with snap-offPCB still attached 1 14-pinZIF socket(3M/fbxtool 214-3339-000602Jrecommended)

51

or

1 0.01pF capacitor 1 3-foot length 28- or 30-gaugesolid core wire Weldbondglue Solder The toolsyou requireare asfollows: Solderingiron DMM with audiblecontinuity tester Needle-nosepliers Clippers Wire strippers A ZIF socketis similarto the machinedreceptacle socketalreadyon the PICkit 1 starterkit. The differenceis that the pin receptacles can be openedor closedby movingthe lever on the socket.The open positionis shownin Figure3-1,and the pin receptaclesare closedwhen the leveris pusheddown.This socketwill be addedto the open 14-pinDIP socket areaon the prototypingsnap-offPCB on the right sideof the PICkit 1 starterkit (seeFigure3-2),and eachpin will be wired to the correspondingpin of the machinedreceptaclesocketalreadyon the PICkit 1 starterkit. Expectthat this taskwill take an hour.

Figure 31 14-pin3M/TextoolZIF socket

Fiqure 3-a 14-pinZIF socketaddedto the prototypingareaof the PlCkit I storterkit

The stepsfor addingthe ZIF socketare asfollows: Solderin the ZIF socketwith its lever up (i.e., pin receptaclesopen).This will ensureproper operation.If you solderthe ZIF socketin with the lever down,you will find that the receptacleswill not open properly.You may find that you haveto prop up the PCB with ZIF socket to make surethe lever staysup during soldering. If, after soldering,you find that somepins stick or don't open easily,move the ZIF socket'slever up and remelt the pin's solderto seeif that relievesthe stress. 2. Using point-to-point wiring,add the 14 connections betweenthe machinedreceptaclesocket and the ZIF socket(seeFigure 3-3).Pin 1 of the PCB socketshouldgo to Pin 1 ol the ZIF

1.

52

Figure 3-3 Point-to-pointwiring usedto connect ZIF pins to PICldt I starterkit programmingsocket ptns

socket,pin 2 of the PCB socketshouldgo to pin 2 of the ZIF socket,andso on.When I have donethis,I try to keepmy strippedpin lengths to r/:z inch (1 mm).There are two rows of holes besidethe 14-pinsocketholesin the prototyping snap-offPCB;to theseholesyou canattach one side of the wires rather than soldering them to the pins of the ZIF socket.When you are addingthe wires,it is a good idea to leave the ZIF lever up to make sure that, rt the ZIF socketpinsremelt,therewon't be problems later with any of the receptacles. 3 . l e s t y o u rw i r i n gu s i n gt h em u l t i m e t ecro n l i n u ity testerfunction.Each pin of the machined receptaclesocketshouldbe testedagainstthe correspondingpin on the ZIF socket,aswell as againstits adjacentpins to make sure no shorting exists. 4. W h e ny o ua r ec o m f o r l a b lteh a ty o u ru i r i n gi ' correct,solderthe 0.01p,Fin the two holes abovethe 14holesusedby the ZIF socket.I solderedthe 0.01 pF capacitoron the backside of the PCB becausethe ZIF socketcoveredthe holeson the topside.When solderingin the capacitor,make sure the leadsare asshort as possible,that it lies againstthe PCB,and that it doesnot extendbeyondthe rubber feet on the b o t t o mo I l h e P ] C k i t I s l a r l e rk i t . 5 . T h e f i n a ls t e pi s t o g J u ed o l rn t h ew i r i n gu s i n g the Weldbondglue.If you put on a reasonably thin bead,the glue shouldset to a hard,clear consistencyin 6 to 12 hours.To make sure the wires don't extendbeyond the rubber feet on the bottom of the PICkit 1 starter kit, you may

l , e 3 P I C o l ' l C UE x p e r i m e n t s f o r

the Evil

Genius

want to hold down the wires with a weight or tie them down while the glue hardens.I use Weldbondbecauseit can be Dulledoff later without damagingthe PCB.

1 starterkit that will standan indefinitenumberof plug/unplugcycles. And, it is still connectedto the other functionsof the PICkit 1 starterkit, which allows you to experimentwith the LEDS,buttons,and potentiometerintedacesbuilt into the PICkit 1 starterkit.

Onceyou'vecompletedthe six stepsand the glue hashardened,you havea ZlF-socket-equipped PICkit

h* * ;ii:

" ; l;t fi ''5

ExFeriment17-Basi c Delags

inh i'rt.

iEnd = 235r jRnA. = 23st

// //

.-- 1

OutBifle l,oop value Inaiale Loop value

I!

nain( )

{

The cFlash.cprogrampresentedin the introductionto this book includeda simpletwo "for" statementdelay. For the application,I wanteda delayof a half-second (500ms) so the LED would flashon and off with a period of one second.Findingthe end valuesof the for statementswasdoneempirically;I usedthe simulator, asI will showin this programto time the delayand then adjustedthe valuesuntil the delaywasapproximately500ms.In this experiment,I wantedto go back to the cFlash.capplicationand seeif therewassome way in which I couldquantifythe delayso I coulduse it in other applications. To test the application,I modifiedcFlash.cslightly asyou canseein the sourcecodebelow:

*incluale - Try /* cDlay.c

to

fluantify

Delay

VaLues

Ihia Program iB a rnodificalion of \cFlash.c,. u36al to qua.Dtify the value of th€ enat of the tlelay valiabl€a anal the time dl€1ay on the FlaBhitrg D0 I,ED. ThiB Drog!€m is !o be usetl with botb th€ prc16P684 ainulator andt the prckit x pcB with iEEtalLefl. RiA4 - I.ED Poaitive aA5 - I,ED Negative

Connecliolr Connectsion

0{ .05. 19

( INTIO & WD4IDIS & PICRTEN & UCITRDIS & _coNFIe I'NPROIIECT \ & I'NPROTECT & BORDIS & IESODIS & FCMDIS) t

anit

PORTA = 0t C M C O N o= 7 r ANSEIJ = 0r TRISA4 = 0; TRISA5 = 0t whiL€(1

(

// // //

tura off Comparalora turn off ADC Make IIA4/RA5 Outputss Jj!

== 1)

//

loop

Foreve!

//

BreakpoinE

,l 1

$oPo, i j

NOP()t aa4 = aA{ ^ 1r // €lihw ) // Entl cDLay

< iEnal; < jEndr

Eele

i++) // j++) t

//

Breakpoint

//

Toggle

Delay

roop

Eere

LED

il;

]

|-r:*

The first changeto cFlash.cfor this experimentwas to add two variables,iEnd andjEnd, that I could changeeasilyto testthe operationof the application. The secondchangewasto placetwo statementsyou haveneverseenbefore(NOP0) beforeand after the delaycode.The reasonfor the first changeshould be apparent:Thevariablesallow the loop valuesto be changedeasily without affecting the program statements. The secondmodificationaddstwo instructions that don't do anything,and I could usethem for breakpointswithout affectingthe operationof the applicationor breakingan instructionthat is usedmultiple timesin the application. The pointsmaderegardingthe NOPO;statements are probablyconfusingandmight not make a lot of senseat this time.First,the NOPO;statementsare replacedwith "nop" or no-operationassemblylanguageinstructions, which I will discussin more detail later in the book.And statedpreviously,the PICC LiterMcompilerNOPO;statementcanbe usedasa

5ectionThree S i m p I e P I C @l l C U A p p l i c a t i o n s

53

{*

breakpointwithout affectingthe operationof the C program.Second,you will find that the PICC Lite compilerhasa very efficientbuilt-in optimizer,which looks for opportunitiesto ffeate executablecodethat optirnizerwill is assmalland efficientaspossible.This try to reusecodethat performsthe samefunctionin diflerent partsof the program.Whatthe optimizer the considersto be the samefunctionis not necessarily samething you or I would considerthe samefunction. Therefore,you will find situationswhereexecution will jump aroundto differentlocationsin the applicaBy addingthe NOP0: tionswithoutapparentreason. you are putting an instructionexplicitly statements, beforethe start and after the end of the two delaytbr statements. To measurethe time of the delay,I enabledthe MPLAB@IDE simulatorandthen addedthe Stopwatchfunction (seeFigure3-4)to the project.The Stopwatchfunciion will count the numberof instructionsthat executeafter the start of the applicationor after beingreset. To measurethe delayfor differentvaluesof iEnd andjEnd,I put a breakpointat eachof the two NOP0; I did this by movingthe cursorto the line statements. wherethe NOP0;statementwasfound,right-clicking, and selecting"Set Breakpoint."I then resetand ran the program,and whenit stoppedat the first NOPO; statement,I clickedon the stopwatch'szerobutton to resetthe stopwatch,then clickedon the run button again,andwaitedfor the next breakpointto stopexecution. I wasexpectingthat the iEnd andjEnd valuescould be reversed.Thisis to saythat the delayof iEnd equal to 50 andjEnd equalto 100would be the sameasiEnd equalto 100andjEnd equalto 50.Thisturned out not to be the case;the delayvariedby severalpercent whenthe iEnd and jEnd valueswerereversed.Totry

Stopwdtch

and comeup with a simple,repeatableformula that couldbe usedfor the application,I tried makingboth valuesthe sameandcameup with the followingrough formula: Delay(seconds)

= 1.8(10-5) x iEnd

This formula is reasonablyaccuratefor the rangeof 50 ms to 2 seconds. Thinking aboutthe optimizerand thinking about the code,I realizedthat the optimizerdidn't do an obviousoptimizationand that is why I replacedthe two for statementswith the following statements: i j

= iEndtt = jEndt

Without anythinghappeningin the insidefor loop, the two loopsare not doing anythingother than exiting with i andj beingchanged.In this case,the for loop codeis still includedin the application,but you will find caseswherethe ultimateoptimizationthat I listed previouslywill be producedby the compilerand its optimizer. Delaysare criticaloperationsin microcontroller(or any real-time)programming,and I will be showingyou a numberof methodsto implementdelaysin your Although you might want to get your applications. delaysexact(in termsof time or instructioncycles), rememberthat this is often closeto impossibleand usuallynot required.As I discussdifferent applications,I will point out whetheror not the delay's absoluteaccuracyis criticalor if an approximation (usuallywithin 10percent)is acceptablefor the aDplication.

Totalsimddted

cycresf---1iDE67l-**iffii66' r -lI!.'| Inskuction l-

I

@l

q...l I a.n.nEr I r nA-n. rimeftsecsl l-----'iro-i6571-m Tiho

Requency IMHz) Processor

fllooo6''

It ClearSinulstionTirne0n Eeset

Fiqure 3-q

54

MPLAB IDE Stopwatchfunction

l , e l P I C @l ' l C l JE x p e r i m e n t s f o r

the EviI

Genius

t l t

ExperimentlB-Sequencing PlCkit1 StarterKit LEDs Table3-1 Plckit1StarterKit LEDUisplasTBISHand PoFTF Values

As you havegainedinsightinto PICC Lite compiler PIC microcontroller programming and the PICkit 1 starterkit, you haveprobablystartedaskingyourself how did I know to turn on the LED marked"D0" to make the PIC16F684's RA4 and RA5 pins outputs, and then output a 1 (or high voltage)and a 0, respectively. The processthat I went through is quite simple and only requiredlooking at the schematics for the PICkit 1 starterkit. It did not requireany probingor trial and error. When you look at the LED circuitryin the PICkit 1 starter kit's schematic(fowdin the PICkit I Flash StarterKit UserbGuide),yor seethat eachof the eight LEDs is wired aspart of a pair,like I showin Figure35.To turn on one LED, currentmustflow in one direction. and to turn on the other-currentmustflow in the oppositedirection.When I startedthis experiment,I hadhopedthat I couldmake activemore thanjust the two I/O pins connectedto the LED output.But whenI followedthe variousconnections, I discoveredthat if more than two I/O pins were activeat any time,there wasa good chancethat a secondLED would be inadvertentlylit. With this knowledge,Icameup with Table3-1, which lists the TRISA and PORIA register values neededto turn on eachLED on the PICkit 1 starter

LED

TBI5B

POETE

DO

B' 11001111.

B ' 0 0 0 1 0 0 00 ,

D1

B'11001111'

B'00100000,

D2

B'11101011'

B ' 0 0 0 1 0 0 00 '

B'1110L01"L'

B'00000L00'

D7

*s4

B' 11011011'

B'00100000,

B'11011011'

B'00000100,

B'11111001'

B'00000100'

B'111110 01'

B ' 0 0 0 00 0 1 0 '

t!t

! i

, ii

kit. To test this knowledge,Icameup with the following program, which turns on eachLED in sequence. The program usesthe samedelay that we usedfor the originalcFlash(flashingD0 LED) programto show clearlyif eachLED lit and if they lit in the correct order.Note that in the program,the informationfrom Table3-1 is part of the documentation.

#idcLutl€ - RoIL /* cPKtED.c

Through

Prckit

8 LEDg

This Prograll will ro11 through each LEDg t'uilc inlo PCBChe PICkit The LED valuea

of, the

! ; I

e"f

are:

:- -

LED Anoal€ Cathotle DO RA4 RA5 D1 RA5 R]44 D2 RA4 RA2 D3 RA2 RAd D4 RiAs RA2 D5 RJA2 RA5 D5 RA2 RA1 D7 RJA1 RJA2

**'ti f 1 1r.

nyke 9retlko 04.09.10

! :

COIII'IG(ITVIIO & VIDITDIS & PICRTEN & UCIJR.DIS & I'NPROTIICT \ & IJNPROTECT & BORDIS & IESODIS & FCUDIS) t

PIC MCU l/O Pin

int

i,

j,

fe

k;

nain( )

(

P I CM C U l/O Pin Fisure3-5

*{ . !

LED wiring

PORTA = 0t CUCONo = ?r // t\rn ANSEIT = 0t // Tuln

off cdnlraratsors off A.DC

k = Ot //

IED 0

Start

a!

5ectionThree S i m p l e P I C @l l C l JA p p l i c a t i o n s

55

while(1

=-

1)

//

loop

Eolev€r

{

{J

r-(

P.{ v

Ft

o g

s, r.L{

z

f , o r ( i = 0 r i < 255r t++) // f o r ( j = 0 r j < L29i i++),

Si{E)le

(k) | / / seLect switch caBe 0: PORIA = 0b010000t TRISA = 0b001111t bleakt caa€ 1: PORTA = 0b100000t ERISA = 0b001111t breakt cese 2: PoRTA = 0b010000, TRISA = 0b101011, breaki cas€ 33 PORTI - 0b000100t TRISA = 0b101011t breakt gAB€ 4: PORIA = 0b100000t fRIsA = 0b011011t br€akt caEe 5l PORTA = 0b000100t rRISA = 0b011011t b!€akt caae 5: PORTA = 0b000100t TRISA = 0b111001, bleekt

r,ED tso Display

$..

k = (k

+ 1)

% 8i

6 'r.l

Loop

PoRTA - 0b000010, TRISA = 0b111001, b!6aki ) // hctiw8

h L.

wblch

Delay

//

) }

//

//

Incr€ment o-7

k wilhin

range

of

elihw End cPIGED

Looking at the program,its functionshouldbe fairly easyto understand. The largeswitchblock of codepassesexecutionto the seriesofTRISA and PORTA register wdtes that are specific to the LED to be written. I could have wdtten to individual bits rather than the entirepod, but I felt that the entfue port write waseasierto follow and understand. I would considerthis applicationto be quite large, clumsy,and not particularly well written. The use of four register writes per LED took a long time to key in (evenwith cuttingandpasting),and there wasa very goodchanceof makinga data entry mistake,or typo. As you work through the experimentsin this book, (particularlythe onesin Section4), think abouthow you could improve the way a piogram is written. For example,in this preyious example I believe I could reduce the total number of lines to lessthan a quarter and eliminatethe repeateddata valuesusedto tum on and then turn off eachLED. This program could be consideredan initial version of a Cylon Eye from BattlestarGalactica,which is also called a "Knight Rider Eye" from the TV show.After trying out the program presentedin this experiment, you might want to seeif you canmake the LEDs reversedirection after reachingone extreme.To do this you will requirean additionalvariable,this one storingthe directionof the LEDs and changingits valueeachtime the on LED is either D0 or D7. Along with the direction variable,you will have to make sure that theTRISA and PORTA bits are returnedto their originalstate(i.e.,loadedwith zerosfor output and in input mode) before tuming on the next LED in the seouence.

m II

Experiment l9-Binarg NumberOutputUsingPlEkitI StarterKit LEDE

t* ()

s

.l-l t . H

s

P{

X

In the previousexperiment,I demonshatedhow each LED on the PICkit 1 starter kit could be turned on in seouence. I notedthat onlv one LED couldbe turned

l;'rl 55

on at any time,meaningthat the PICkit 1 starterkit cannotsimultaneouslydisplaymore than one bit of data at a time. In this experiment, I will show how you can display an incrementing eight-bit counter on the eightbits of the PICkit 1 starterkit. The methodusedto displaydata on all eight bits simultaneouslyis the samemethodthat I will demonstratefor multidigit LEDS and dot arrayLEDs.To give the appearancethat all the LEDs are active at the sametime, we will rotate through eachLED in sequenceand, if the bit the LED is representingis set, then the LED is turned on for a set period of time. If the bit is not set.then the LED is left off for the same

l , e 3 P I C @l ' l C l JE x o e r i m e n t s f o r

the Evil

Genius

PORTA = O' elae // DiEplay the PORTA = 0b100000t TRISA = 0b001111,

amountof time.Thisensuresthat the brightnessof eachLED will be constantregardlessof the number turned on, and that constant timing for the application is provided.Thismethodis alsothe basisfor controlling multiple rnotorsand servos(asin a robot) and shouldnot be consideredapplicableonly to the devicesshownin this book. The rule o[ thumblhat I usewhensequencing LEDs is that eachone shouldbe turned on 50 times per second.For this application,IwantedeachLED to be on 100timesa second,which meansthat eachLED is turned on over a 0.01second(10 ms) period.For eachLED to be activein the 10 ms time period,it shouldbe turned on for 1.25ms (10 ms dividedby eight).Tocreatethis delay,I useda singlefor loop with the delayvaluefound empirically. The programI cameup with incrementsa counter onceeveryhalf a second,and the currentcounter valueis displayedon the eightLEDS of the PICkit 1 starterkit:

*inclual€ - use /* cLEDDlEp.c counter Using IED at

\ePKLED.C,, 100x Der

LE'D Time

on Delay

f'J,

f,or (i = 0r i < Df,ayr i++)t ( (value & (1 x

= xPositiont

AaBigtL a VaLue to lhe "X" coordlinate

//

Pointersand pointersto strucluresare often usedas parametersto functions.By passinga pointer rather than a value,the originalvaluecanbe changedand much lessdata canbe transferred,resultingin faster programexecutionand smallervariablememory requirements. This is really everythingthereis to know about pointers-in conceptthey are quite simpleand easyto understand.Debuggingprogramsusingpointersis generallydifficult becausefew simulatorsand emulators providepointer informationin any kind of meaningful context(suchaswhich variablethey are pointingto). To effectivelydebuga programthat has pointersin it, two thingsare required:a goodunderstandingof how variablesare stored(and where,most likely,they are in the variablememory)and some imaginationin trying to understandhow the code works.In the next experiment,I will showhow pointersare usedwith charactersand strings,but for now you know all the basicsand probablymore than you will needfor the foreseeable future. Lisl.rconsistof structureswith pointers,which point to the structurebuilt into them.They allow each instanceof the structdata type to point to another instanceand to form a linearchainof structs.Thetwo lypesof listsmoslcommonlyusedin programming (seeFigure4-3) are singlylinked listsand doubly linked lists.The advantageof the doubly linked list is that it allowsmovementin eitherdirectioneasily,while the singlylinked list allowsmovementin only one direction.The end of the listsare usuallymarkedwith a null (value0) or -1. Other waysexistfor organizing structures. For this expedment,I wantedto sort the sameselection of numbersasusedin the previoustwo experiments.Here we will usea singlylinked list.The code itself,which follows,is not muchmore complexthan the codeof the previousexperiments, but I experienceda numberof challengesdebuggingand confirming the application'soperationthat would make this experimentunreasonablydifficult for an inexperiencedprogrammer.

*incluale cpstrctsort.c /* BubbLe Sor!

-

Structure

This Program is a simple pointerB 20 values using S!ructurea rryke predko 04.08.03 tlalabrare Notes: This Proglaft has

been

5ection Four C L a n g ua g e F e a t u r e s

Baaed C Progran

"Bubble to

writtelr

go!t"

a Irink€al

to

run

of tist

of

in

7'l

Curren!

l:lrffi Doublv

,

--l T--.

while(l

l-.-__-l ,

ListPointers

th€

Lists

MPITAS IDE Sinulator

ONLY.

it unsigrlefl char hitvalues I20l ={33. 54. 42, r2O' 37' 5, 41, 69, 13, 45, 99, 105, 8, 21, 4OO, 33, 4L,71, 51, 90) t atruct sErucl char

(

Datastruct Dataslruct*

N€xtDr

//

hitElementt

//

ehar vaLuet ) SortA!ray[20],

Slartr

//

gointe! to the Next Element Initial Elenent

20 Ele';nerft Structure Afray/S!art

Previousi

//

C1ftte':t,

//

Enalinqr

//

Pleviou€ Pointer vaLue Pointe! Current value Poinler to the LaEt Elemenl

main ( )

t Start.Ner{ED

// fr

Enaling = Currentt , / / eralrw

iiii""ffi Figure q-3

= current->NextsDt

= &SortArraytolr//

Point start IJist < sizeof (Initvalues) r .NextD = &SortAlrayti+1] = ii . hitELem€nt .value = hitvalueE [il

(i = 0r i for SoltAllayIi1 soltA].lay [i] soltArlaytil // '.of J SortArray t 19I . N€xtD = (structs Endling = SortArlay[19],NextDt

to of

//

//

== 1);

Move Bach

//

the

FiniBhealEooP

Pointer

Forever

EtLd. cpEtrctsott

Although buildingthe singlylinked list is quite simple and can be found in the first for loop,sortingthe list by movingpointersis not exactlyobvious.In order to sort the list,I usedthe Previouspointer asan anchor to the list and then movedthe pointersasshownin Figure4-4.When I developedthe code,I madea diagramsimilarto Figure4-4 to make surethat I understoodhow the pointersweremovingthe two list elementsasin the bubblesort swap.Despitemaking this diagram,it wasstill a challengeto get the program to work properly.I endedup makinga spreadsheet with everyelementand eachpointer and kept track of how they changedandwherethey endedup pointing. Overall,writing and debuggingthis applicationtook me aboutfour hours,whereasthe other sort applications presentedtook Iessthan an hour. The puryoseof this experimentis to demonstrate how pointerscan be easilycreatedand point to different piecesof data.Lists are generallybestsuitedfor largeapplicationswith many data elementsthat might be reordered.inserted.or deletedat random.At first glancelistsmay not seemto be appropdatefor microcontrollerapplications, but there are caseswherethey are useful,suchasallowingeasyeditingof a robot programsequence. I suggestyou considerthe list concept whenyou havea situationwhereadding,removing, andmovingdata elementsis required.

the the

i++) t

(

P.evaous

t

Datastruct*)

-1,

Previous

]tr'hiIe (start.NextD = &sEatEt Previous

!= Endins) t of, to lhe Start // Poinl the Liat = Start.NextDr Current // Point lo the First ElernenE (current.->NextD lrhile != Enaling) - >NextD- >value ) { (Current->value > culrent if = Culrent->llextDt Previoua->NextD = Culrent->NextD->NextDt Currenl->NexlD Pr€viou6- >NextD- >NextsD = Cutrent, = Previoua->NextD; Previous Nothing Changeal, Goto lilext // ) 61ae { = Currenti Previous

78

Ial

Previous Current Previous P€vious.>NonD> NenO =

Figure Q-Q

P I C @l l C l JE x p e r i m e n t s f o r

List sort

the Evil

Genius

ExFeriment 30-fharacter 5trings char*

Ne$rName1, Ne$rl{ame1

NewNane2

t

= lilyNalnei

- &M]'Nametol;" NerdNane2

Although working with structsand unionscanbe difficult,working with pointersand characterstringsreferencedby pointersis quite easyif you follow the basic conventionsoutlinedhere.Unlike other languages (suchasBASIC), C doesnot nativelyhandlestringsof data.Although it canhandleindividualASCII characters,which are treatedlike eighfbit numbers,each ASCII characteris storedin an arrayelementto create a characterstdng.Toeasethe workload causedby copyingeachcharactereachtime it is used,the starting addressof arrayscontainingASCII characterstringsis normallyusedto referencethe entire string.These methodologiescombineto make working with character stringsin C more complexthan in other languages you may haveusedwhenlearningprogramming.The sameattributesandmethodologiesmake character stringsmuchmore efficientwhenyou are comfortable working with them. When a numberofASCII charactersare combined in a string,they normallyend with a null (zero)character to form what is known asan'ASCIIZ strins."The p l a c e m e no lf t h en u l lc h a r a c t eart t h ee n do [ t h es t r i n g is automaticwhenit is enclosedin doublequoteslike this: char

Mtalame t121

= nq'ke

predlkotr r

//

Def.i')e

a

;;-;;.--*' To declareenoughspacefor the stringand the null characterat the end,you will haveto count the number of expectedcharactersof the stringand add one to it. When a stringvariableis referenced,it is referenced by the vadablenameandit is a pointer to the first characterin the variablearray.

== ilflilil:l

Eachindividualcharacterin a stringis accessed as an arrayelement.IndividualASCII characterscan eitherbe representedasdecimal,binary,or hex numbersor the actualcharactersin singlequotes.In the following codesnippet,the third characterof MyName is comparedagainstupper case'K'.

// //

(NevNar$elt2l == rK' ) if Stalements executeal if Equal to Uppelcase elae .K, Sla!€llrents ex€cuted if Not l4)pelcase

rK,

Along with decimalrepresentations ofASCII charactersand the actualASCII charactersin singlequotes, a numberof specialcharacters(precededby a backslash[\]) are alsoavailablein C.Thesecan be placed eitherin singlequotesasa singlecharacteror aspart of a stringlistedin Table4-3 and found in the "For Consideration"pafi of this section. Dependingon the circumstanceq ASCII stringscan be locatedin eitherprogrammemoryof variablememory.To allow the compilerto differentiatewhich is which,the "constkeyword" shouldbe put beforethe charin the pointer declaration: const

cha!+

ConstPointeri

The constkeywordwill make the pointer 16 bits long (insteadof the standardeightbits,which can accessonly variablememory).The extrabits are used to indicatethe locationof the pointer data aswell as the locationwithin the specifiedmemory.My rule of thumb regardingthe useof the constkeywordis to let the compilerindicatewhenit is needed;mostof the time it is not required. To demonstratehow stringsof charactersare implementedand manipulatedasarraysof singlecharacters, I havemodifiedthe sort programto sort an aray of pointersto strings.I createdthe stdngCompareand stringcopyfunctions,which perform byte-by-byte operationson the bytesin the strings,becauseC does not havethis built-in ability.

5ection Four C L a n g u a g e F e a t u r e s

79

#include cslrIlsolt.c /*

sifiD1e

Bubble

C Program

(i = 0r i < 10, t++) fo! for (j = 0r j < (10 - (t + 1))r j++) gortvaluea (atringcdE a!€ ( (char*) lf [j], (cha!r) + 1l) > 0) { soltvaluestj SortVaIu€B [jl ) t stringcoDy(TenD, + 1l)t atrinqcolDr(SorlvaLuea [J L Sor|alu€stj ( Sorlvalu€e !!eq>) t sEringcopy [J + 1]' // EL I

Solt

"Eubble Sortd of ThiB Proglam is a simple 10 Fou! chalecter Stsringa b!'moving them functions iD the solt arlay. fhe atring w€re wrilten bY Myk6 Pr€alko. nvhe pretlko 0{.08.05

!'hi1e

Earatware Not€B: Tlris Program hag b€en !h€ I.IPITAB IDE Simulator

u! St l-{

"r{ H

?n

jt unsigled i, sorlvalu€a char* char T€alI) [5] t cha! nameo [5] cha! name]. [5] char nam€2 [5] char name3 [5] cha! nam€{ [5] char names [5] char nrme6[5] char nam€? [5] cha! nameg [5] cha! nameg [5] int

F.{

| writlen ONIY.

to

run

E c = = = = = = = =

Etllngcorq)a:.e

( char*

{

slringl,

//

chat*

gt!lng2)

cd|tr)are two Strl.aga

i n t j = 0 ,

(i = 0r ('\0' l= Sttiusl.lil ) && i++) - strins2til, j - stsltnsltil // j < 0 ff, S!!ing]. // j = 0 if stringl. // ) > 0 if strins2

for

4J t! ru p"{

ru "Li F \

return ,

//

Striag].t0! for (i stlingltil ,

ll

Return

1,

he

< string2 = string2 > st':.ing2

t SorlveLuea [0] Sortvaluea [1] Sortvalues [2] sortvaluea [3I Sortvalu€s [{I sortvalu€s [5] sortvalueB [5] so!tsva1u6a [7] gollvalueB [8] sortvaluea [9]

= = = = = = = = = =

nam€ot na.nelt aame2, narne3, n€$€4t nane5 t nam€5t nam€7t nam€8t nam€gt

Errd. cEtlmgort

*incLudla cstrDsolt.c /*

sil[I,le

c Program

Bubble

So!t" llhis Proglem la a ai!E)1€ "Bubble with novlng 10 Four Cha:.actse! Stringa atrings.

lilotea: llartlwar€ Progran has b€en l|rl'!t€n fliis o$[Y. "n" ** IDE Sirmrlator

stringl

Byte

to

*,

char* charr cha! cha! cha! cha! cha! cha! char char char char

SoltvaLuea TettE)i narreo t5j = nenel [5] = n€me2 [5] = neme3 [5] = neltr€{ [5] = = nene5[5] . name6[5] Dame? [5] = '1ame8 I5l 5 name9 [5] =

[10] t "mtk6"t ntoDin' 'lolin' trcalarri njune" n lf'tlntr ' ulrar1rn' "ryra"' trsara'r "kira"

P*

&€4 80

Fo!€ver

sorl of

!vk6 Dr€alko 04.08.0{

= String2 [0] r // Cor4, E}l.e t.ilsl .\0, - 1lt i++) != Stringlli = sErins2 [i],

E!.d. stringco9y

FinisheauJoop

In the programyou'll seethat I first initialized arraysfor eachnameand then passedthe addressof eachnameto the SortValuesvariable.In standardC, you could initialize a pointer variableto a seriesof double quotedASCIIZ strings,but this is not possible in the PICC Lite compiler.I suspectthat the reason why it's not possibleis due to the memory organization of the PIC MCU; the initial valuesare storedin programmemory,which is completelyseparatefrom the variablememory.Before a variablepointer can be assignedto a value,the value must be storedin a variable. After writing cstrmsort,I noticedthat I could improve the performanceof the sort routine by simply movingthe pointersto the stringsin the sort instead This programis of copyingthe stringsthemselves. listedhere:

ConrDare ReEull

cha!* strl.ng2) stsfing1, into I / coDy stltng2

nain ( )

#

(0 == j),

E'l'd atlingcotDpale

EEringcoDy(clEr* I

iI

Cry

//

j;

//

in

[10] t trnvketr, trtonLn' trLofi" trcaren' trjulent n].r.ann' n$aryn' ilr\ylent trsalan' nkiren'

//

( 1 == 1) t

1 , 2 3 P I C @l l C U E x p e r i m e n t s f o n t h e E v i l

6enius

run

in

lnl |

(ctla!*

alringcorq)are

Stringl, char* // contr'ale two

String2) slllngs

for

i n t j = 0 , for (i = 0, ('\0, !- srrinslttl j - slrinsltll - strins2lilt // i < 0 if // j = 0 tf //j>Oi-ES r€lurn

!!aid(

t++1

strinsl < Sl!1ngl2 = String2 strinsl t r i n g 2 > String2

R€tsutn CdlDare Result

Eird. slringcon[)are

//

I

//

Jr

) && (0 == i ) ;

unaigneal

i,

Sorwalues Solwaluea Soltvaluea Soltvaluea goltvaLua3 goltvalueE gortvalueE Soltvaluea Soltvalues Sortvalu€a

jt [0] [1] [2] [3] [{l [5] [6] [7] [8] [9]

= = = = = = = = = =

= 0r i < 10r i++) (i = 0r i < (10 - (t + 1))r i++) (Btringcompa!€ ( (chalr) if Sortvalues (char*) Soltvalu€s tj + 1l) > 0) T€d|D = Sortvaluea [j I. = Sortvalueslj Sortvaluealjl + 1]t gorlvalues [j + 11 = TerD; | // fi

whiL€(1

ll

!

== 1)t

//

Finiah€alt

tJ 1, (

nam€o t nalne].t name2 t nane3t nalle4t narlesi aam65i nar€? t aameSt aame9i

. . . . .

m lt:

{*

ooD Eorev€r

t-J "

ErLd cstrpsort

The updatedversionof this sort is recommended and demonstratesthe important functions of pointers that you shouldbe comfortablewith in order to program in C.To recap,thesefunctions include the followrng:

)

t

(t for

Declaringpointers Assigningpointersto the start of variable stnngs Accessing(readingand writing) pointer values Defining ASCIIZ strings AccessingspecificcharactersinASCIIZ strings as array elements

d\

r*d

r t i

t-l

r*" 9,, ! i F

Experiment 31-LibrargFundions

tsd

t"/ with strcmp and strcopy,respectively,and added the string.hheaderto get the following:

*inclutl€

#iaclutl€ - SirE)le cstrf,Sorl.c /*

q F"t

C Program

Bubble

sort ! d

nBubbl€ Sortn of Ihis Program ia a siq)Le 10 Four Chal.acle! SlringE blr noving them in lhe soll array. CdE)arison anal litovemeDts ar€ provial€al by l"ibrary Funclions.

In the previous experiment, I showedyou how to compare and copy eachbyte in an ASCIIZ variable array. Klowing how to create thesefunctions in C is important,but it is not alwaysnecessary for mostapplications.As part of the C languagespecification,you can take advantageof a collection of functions that will take someof the load off you when you are programming.Thesefunctions (along with the PICC Lite compiler C languagestatement definition) are included in the "For Consideration"part of this sectionand are part of the "C run-time" library that is linked with your application at run time. For example,I replaced the stringcompare and the stringcopy functions in the previous experiment's code

X

llyk€ prealko 04.08.05 Harflware Noles ! ThiE Progr€m has been th€ MPLAB IDE ginulato!

j, u.Eaig!€d i, char* SortvaLu€s char TerDIr[5] t char nameo [51 char nam€1[5] = char name2 [5] = chat nam€3 [5I E char nam€4 [5I char nan€5 t5l = = char !€.ne5l5l

Fr.

wrilt€n to ONLY.

run

in

[10 ] t "!oni" nlorinr ncaran'june"t nltrnntr "nar't'".

SectionFour C L a n g ua g e F e a t u r e s

dI

- '4yran t = rrsaratr t = "kira",

narLet [51 nameS [5] name9 [5]

cha! cllar char

For Eonsideration

nain ( )

(

Sortvaluea sortvalueE sorwalu€a sortsvaluea sortsvalu€a Sorlvaluea soltvalu€s So:atvelu€s Sortvalu€a Sortvaluea

[0] I1l [2 ] [3] [{l [5] [6] [7] [8] [9]

= = = = = = = = = =

nameot 'rameli name2t naltre3t nalle4 t namest name5t !a!re7 t lr€lneSt aame9t

for (i = 0r i < 10r I++) for (j = 0t j < (10 - (i + 1))t j++) (sllclE)( (char* ) Sorlvalues ljl, if (chai*) Sorlvalues tj + 1l) > 0) ( Sortsvaluea [j I ), strcDy(T€dlp, strcDy(Sortvalu€a tj + 1] ), [j L Sortvaluea strcpy ( sortvalu€a t j + 11, Teqr) t ll fi | !thile(1 |

l, .,-t P EIJ

t+

at l.Lt

ls

"*t r,J 3*t

//

== 1),

//

Fini

ahefltJoop

Foreve!

EE.d. cal:.fsofl

The resulting program takes up just about asmuch spaceasthe previous one and runs in roughly the same amount of time, which meansthat my routines are similar in efficiency to the provided library routines.The big difference is that the first program was shorter and required lesseffort to debug.(I assumethat the strcmp and strcpy functions work properly and this assumption is extendedto all the built-in functions of the runtime library.)For the mostpart, I would recommend that the run{ime library functions are usedin your C programswhereverpossible. One caveatexiststo the previous statementand that is the linker brings in only the library functions that are calledby the application.Ifa functionis not called,then the function is not linked into the final application hex file. I'm pointing this out becausein you canvery quickly smalldevices,likethe PIC16F684, use up all of your available spacewith libraries and applicationcode.It is importantto rememberto balancethe number of built-in and user-written functions so you don't useup the programmemoryavailableto your application.

f l

H

For modern systemgC is the programming languageof choicebecauseit is availablefor a wide rangeof systems and processors(including the PIC microcontroller). This ubiquity requires anyone who is planning on developingan application for processingsystemsto have at least a passingknowledge of the language.The following information is specific to the HT-Soft PICC (including the PICC Lite) compiler: Application mainline/entry point is the main function: nain( ) // ADpltcaEton {

Coile

|

// ll

!

Codle

N)plication

E'rd ApDllcatiorr

Functions are defined in a similar forrnat to the main function: Frnctlon( R€turn_Ttz9e Parameter. . J) stsalt. { // nrdctioa .

// leEuln

|

TtT)e Paramet€r

//

zunction

T]ZI)e

Coal€

valuei

EnA l\tnction

The function can be placed before or after the mdn function(not inside),but if it is placedafterward,there must be a function prototype, like this one,before main: Retu!n_T!'D€ l,lractl.on( Paramete!..1),

llyl)e

Peraneter

[,

lPt pe

PIC MCU configurationregisterfusevaluescanbe specifiedusing the "_CONFIG(x)" define (found in the pic.h include file). The specifiedvaluesare ANDed together to form a complete configuation register value. I recommend that a value for eachbit be specified to ensureproper operation of the microcontroller. Table 4-1 lists the PIC16F684'sconfiguration fuse bits, the HT-Soft defined constant'soption values,the AND valueq aswell asthe Microchip defined assemblylanguageconstantsI havenoted alsorecommendedvalueswhen I felt it wasimDortant.

e&{ 82

[,

l , e 3 P I C @I I C U E x p e r i m e n t s f o r

the Evil

Genius

Data declarationsare very consistent,starting with the constant declaration: coaat inr tJabel = value, ., vanaDtesoeclaredInstdea Iuncfion(rncludlngthe main vaiable) are known as local variables,are accessibleonly within that function,and may be different upon entry into the function. Variables declared outside of all functions are termed,global, as they can be read and updated anywherein the application.

where the last characteris an ASCII null (0x{00). Array dimensionsmustbe specifiedin the variable declaration statement.Multidimensional arrays are defined with eachdimension separatelyidentified in squarebrackets([ and]): int rb'eeDspac€ [Din1l tDn!21t pointers are declaredwith the asterisk (*) character after the variable type: char*

tlzp€

rra.t€l

o rt

o o 5

u f-r.

glrinq'

p-

t= valu€l,

Valueis an optional initialization constant.The different types are listed in Table 4-2,with the data value rangesare in parenthesis. Single dimensional arraysare declared using the form:

qt

o

Finding the addressof the pointer in memory is accompliJred using the ampersand(&) character:

rj 9' cF

EtsringAda!= &strins,

,J.

ll'I)e Labeltsizet

[= { hitialization

o

values..}lt

5

Strings are defined assingle dimensionalASCIIZ uuTays: char

striDgl

17 I

= "I'his

is

a gtridg"t

Table 4-1 PIE'l6F58qConfiguratlonFuse Values Connguration Bit

PICEUte CompllerValue

Microchlp Value

Ualue-Functlon

Bit 11-FCME\ Fail-Safe clock Monitor Enable FCMEN (Default), FCDIS (Recommend)

_FCMEN_oN -FCMEN_oFF

0X03FFF,0X037FF

Bit 1o-IESO,Intemal Extemal switchover

_IEso_oN

0X03FFF,0r03BFF

Bit 9|8-BODEN, Blownout DerectEnable 0x03DFF,0x03CFF

IESoEN (Default),IESoDIS

(Recommend)

_IEso_oFF

BOREN (Default),BOREN_XSLP, SBOREN,BORDIS

_BOD-ON _BOD_NSLEEB _BoD_sBoDE\_BoD_oFF

0X03FFF,0X03EFF.

Bit 7-CPD, Data Memory Protect

UNPROTECI (Defaulr/Rec), CPD

_CPD_OFF,_CPD_ON

0X03FFF,0X03F?F

Bit 6- _CP, Program Memory Protect

UNPROTECT (Default), PROTECI

_CP_OFF,_CP_ON

0X03FFR0{3FBF

Bit 5-MCLRE, Extemal _MCLR Pin Enable

MCLREN (Default), MCLRDIS

_MCLRE_O\

0X03FFR0X03FDF

Bit 4-_PWRTE, Power Up Timer Enable

PWRTDIS (Default), PWRTEN (Recommend)

,PWRTE_OFF, _PWRTE-ON

0x03FFR0rt3FEF

Bit 3-WDTE,Watchdog Timer Enable

WDTEN (Default), WDTDIS (Recorffnend)

_WDT_O\

0X03FFF,0x03FF7

RCCLK (Default), RCIO, INTCLK, INTIO (Recommend), EC, HS, XT, Lp

_EXTRC, _EXTRCIO, _INTOSq _INTOSCIq _EC_OSC,_HS_OSq _XT_OSq _LP_OSC

Bit 2:0-FOSC, Oscillator Selection

_MCLRE_OFF

_WDT_OFF

S e c t i o nF o u r C L a n g u a g e F e a t u n e s

0X03FFF.0I{3FFE. 0X03FFD,0X03FFC, 0X03FFB,0X03FFA, 0x03FF9,0x03FF8

83

Table4-3 C BackElash Eharacterg

Table4-2 PICELlte EomDilerBata TgFeg Tcpe

Btt Size Comments

bit

1

BooleanValue

8

lnteger (-128to ASCII Character/Signed

char

unsignedchar 8 short

16

unsignedshort 16

rn)

Eharacter

nSC Value

FunEtion

\0

00x00

Null Character

0x007

Alert/Bell

0x008

Backspace

0x009

HorizontalTab

0x00A

New Line (Line Feed)

0x00B

Vertical Tab

0x00C

Form Feed(New Page)

0x00D

Cariage Retum

0x05C

ASCII BackslashCharacter

0x03F

ASCII QuestionMark

0x027

SingleQuote

0x022

Double Quote

\b

unsigned Integer (0 to 255) 6'7) SignedInteger ( - 32,768to 32.'7 UnsignedInteger (0 to 65,536)

int

16

SignedInteger (-32,768 to 32J67);sameas short

unsignedint

16

as UnsignedInteger (0 to 65,535);same unsignedshort

long

to SignedInteger ( 2,147,483,648 2,147483,647)

unsignedlong

UnsignedInteger (0 to 4294,96'l,295)

float

assume3 digitsof Real (0 to 1/-6.81(1033); accuracy/defaultfloating point mode

double

assume 6 digitsof Real(0 to l/-6.81(10'3): usingPICL -D32 compiaccuracy/specilied lation option

\f

\?

Mathematical operatorsinclude the ternary operator and thoseshownin Table4-4 andTable4-5. The ternary operator is the form: TestE v+ = X) // BiE 4 - Inverts corlr (RcO C2 rnpur ) // BiL 3 - RtAo C1 Inpur // Rit 2tO - CofirDarator with Cref VRCON = 0b10L00000r // Vr€f MoaluJ.e Control //Bit7-Enable // BiE 5 - Low Range (0 - 2/3vdd) // BiE 3.O - anaJ.og Lev€Ls aNSEI, = 0t // No ADC hputa while(l

{

== 1)

//

Loop

For€ver

f o r ( i = 0 r i < 8 r i + + ) | / / Loop through Each of the 8 r,EDS for (j = 0, j < Dlayr j++), "on" D€tay Loop //Display (0 == (DisDvalue & (1 > 1; VRCON = VRCON + Nextvrefval.uet (0 -= Nextvrefvalue) if ( / / Einisheat, Display New vaLue = Vrefvaluet Dispvalue = 0i Conltstate // and Restart Analog lt€asurement /l fi , break; ) // hctiws ) // elihw // Erd cconp ) //

if

To measurethe potentiometervoltage,you could haveuseda simplealgorithmthat startedwith the Vref voltageat a minimum value(VR = 0b00000)and incrementedit until the comparatorchangedstate.I usedthe binary searchalgorithm,becauseit requires the logarithmbase2 comparisonsof the numberof bits insteadof potentiallyhavingto run througheach bit. In this case,usingthe binary searchalgorithm reducedthe number of comparisonsfrom a maximum

98

of 16 to four.A binarysearchalgodthmusedin comparisonoperationslike this one continuallyhalvesthe comparisonincrementuntil nothing is left.As I show in the followingpseudocode, a test valueis used,and if the valueis greater,it is left asis,else,the valueis removed.After the comparisonis complete,the test valueis dividedby two againand addedto the comparisonvalue.Thisis repeateduntil the comparison valueis zero. = Maxinu! / 2; // Stalt at 1/2 Maximum = Conparisonvalue; ConDarevalue // add to the Cofiurarison value u'hiL€ (0 != Coq)arisonvalue) // Repeat while a value to Add { I / Deternine if vaLue to b€ Saneal (Conparevalue if > InputvaLue) = Compalevalu€ - Conparisonvalu€i Cornpar€Value = cdll)arisonvalue comparisonvalu€ / 2t // R€peat O!|eration = Comparevalu€ Conparevalue + ConDarisonvalu€, | / / eli})w ComparisonvaLue

The binarysearchis ideal for digital systems becauseit works nativelywith binary bits.The previous codeis not significantlymore complexthan codethat doesan incrementingcompare,and the operational speedgainedis significantand growsin significancefor eachadditionalbit to compare. As I alludedin the introductionto this exDeriment. whenI firstrurnedon the originalcodenothinghappened.I wasvery surethat the hardwarewasgood (it worked fine for cADC.c),so the problem wasclearlya programmingproblem.This is a very difficult program to debugbecausethe MPLABo IDE simulatorwill not simulatethe operationof the comparatorandVref modulewith an analoginput.To find the problem I reviewedthe operationof all the program,sstatements (fortunatelyit's a shortprogram)and found the following one line if

( 1 = = ( C M C O N o& ( 1 < < 5 ) ) )

that neverbehavedasif the expressionwastrue.This instructionANDS the contentsof CMCON with bit 6 set and returnstrue if it is equalto 1.My initial responsewasto rereadthe comparatorsectionalong with previouslywritten applicationsto figure out if I wasdoing anythingwrong.The codelooked great.I then changedthe programso everyhardwareregister wasrenamedto a variable(for example,CMCONO became CMCONO)and went on to try different valuesof _CMCONOto find out if therewassomethinsI wasn'lseeing. Usinglhis merhodI discovered there wasn'ta valuefor _CMCON0 that would causethe expressionto be true. I looked at the expression"1 == (CMCONO& (1 > 5, & 0x0F1 & 0x03r',

Increm€nt lhe counler another 1/4 geconal //

= 0t // rte3el // 1A // elihw // E^d. c2x?Seg

for

The basicapplication,incrementingthe displayonce every 250ms to count from 0x00to OXFF,is quite simple and is calledc2xTSeg.c.There really shouldbe no surpdsesin the codeother than the fact that segment valuesfor eachbit are reversedfrom cTSegment.c becausethe PIC MCU are connectedto the display's cathodesinsteadof anodes. The two NOP0;instructions aroundthe final for loop wereplacedthere to giveme somewhereto break and to ensurethe applicationdelayed10ms,givinga 50 timesper second update speedfor the two displays. After creating c2x7Seg.c, I thought it would be fun to comeup with a czxTSegPc in which the lightedsegment seemsto run aroundthe two displays. These typesof displaysare quite easyto write and a lot of fun to watch.After watching this, I can't help but think theremustbe a simplegraphicalgamethat couldbe createdusingseven-segment LED displays.

!

's l 1

p'

ffi 41X

r*,t

) eLse

tsr€

-.{: tt*

S e c t i o n5 i x

Intenf acing Projects

f or

t h e P I C @l l C U

L23

r-15-LEDMatrixDisplags Experiment

1 Plc16F68 4 1 74SI383to8decoder t

Seven-!ow, LED display

five-col,umn

4?OO tes istols 0.0I pF capacito! tYpe )

B!eadboa!d-mountable (E-Switch SPD! switch 8G1903 reconmended)

Needle-nose plie!s

thlee-cel1 pack

B!eadboard Wiring

kit

Seven-segment LED displaysare excellentfor displaying numericdata,but whenyou havecharacterdata, their usefulness reachesthe end very quickly.A number of differenta/phanumericLED displzysexist.The mostbasictype is the 14-segment LED display,which is similarto the seven-segment displaythat you just experimentedwith. However,due to the specificposilions of differentsegments, it's difficult to make all the differentcharactersrecognizableon a 14-segment display.T\e dot-matrix LED display allows arbitrary charactersto be displayedwhile usingfewer I/O pins than the 14-segment LED display,but it requiresmore work to useand plan how to build your characters. The dot-matrixLED display(whichI calljust an LED matrix) consistsof a numberof LEDs arranged with their anodesand cathodesasshownin Figure611.By pulling a columnconnectionto positivevoltage, individualLEDs canbe lit by connectingthe appropriate row anodesto ground.For this experiment,I useda 5-columnby 7-rowLED matrix,but if you look around,you will discovera numberof differentsizes are available.Regardlessof their size,they will be driven usingthe samemethodologyasshownhere. Using a 4700 resistor,the batteryclip,and a couple of wires,you canfigure out the wiring of your display.I startedby connectingpin 1 (the top left corner)to negative voltageand then scanningthe pins to try and identify the sevenanodes.I wasnot successful at light ing any LEDs, so I repeatedthe procedurewith pin 1 connectedto positivevoltage.OnceI startedgetting LEDS to light,it wasa simpletask of identiflng the functionsof the variouspins andmappingthem out on

L24

(any

AA alkaline

AA battely batteries

a sheetof paper.In doing this,I discoveredthat the rowsconsistedof the LED'S anodesand the columns werethe LED'S cathodes. The 5X7-LED matrix displaythat I usedrequires 12pins to operate;althoughthe PIC16F684is quoted ashaving12 I/O pins,it actuallyhasonly 11 I/O pins and oneinput pin. RA3 can only be usedasan input, so to take advantageof all 35 LEDs of the matrix that I used,I had to comeup with someway to multiply the

Row 0 Row 1 Row 2 Row3 Row4 Row 5 Row 6

Col

Col

Col

Col

Col

Figure 6-11 LED matrLr

l , E 3 P I C @I ' l C l JE x p e r i m e n t s f o r

the EviI

Genius

umn of LEDs is wdtten at the sametime and eachcolumn is scanned.This is similarto the dual seven-segment LED displayexperimentand givesthe appearancethat eachcolumnis activeat the same time.cMatrix 2.cruns throughthe first four characters of tlie alphabet.

5x7LED

-: - --r-

#includle 2.c - Display cMatrix /* r,ED DiEplay

Soine Letlera

on 5x7

l

I

74L5174 Figure 6-l?

MatrLrcircuit

numberof operationalpins.In this application,I used a 74S138decoderto providethe columnnegative power supplyselect.The selectionof the S technology decoderwasselectedover the more commonLS technologyto sink the mostcunent (LEDS fully lit) aspossible.This allowedme to usethreepins to select betweenfive columns,requidnga total of l0 pins to control the LED matrix.Thecircuit that I cameup with for this applicationis shownin Figure6-12.Note that I tried to arrangethe wiring in sucha way that the anodes(rows)are all driven by PORTC (with the seventh bit beingpassedto RA4), while the cathode's (columns)pull-downdecoderis selectedby the three leastsignificantbits of PORTA.

8A0-Decoale!0gelect RA1 - Decoale! 1 Select Rt2-Decoaler2Select aA{ - Anoale 5 Rco - Anoale 0 Rc1 - A$oale 1 Rc2 - Alotl€ 2 Rc3 - Atroale 3 Rc{ - Alotl€ 4 Rc5 - Anoit€ 5 nyke predko 04 . 12. 04

& WDTDIS & PWRTEN & MCLRDIS & -CONFIG(INTIO UNPROTECT \ & UNPROTECT & BORDIS & IESODIS & FCfiDIS), int

i,

const

j, char

k,

n,

Dlay,

lJ€ttsers[]

CurLettert = {0b1111110,

Start

//

0b0010001, 0b0010001. 0b1111110, 0b0000000. 0b1111111. //

To testout my mappingof the LED matrix and how presumed I to wire the application(seeFigure6-13),I createdcMatrix 1.c,which turns on eachLED in the matrix in sequence. With the LED matrix and driving circuit behaving asI expected,Ithen createdcMatdx 2.c in which a col-

0b100L001, 0b1001001, 0b0110110, 0b0000000, obo1,141,1,o, 0 b 1 00 0 0 0 1 , 0 b 1 00 0 0 0 1 , 0b0100010, 0b0000000, 0b1111111,

with

Next r,etter

//

nct

//

Einal

0b1000001. 0b1000001, 0b0111110, 0b00000001t

nain( )

{ PORTA = 0i PoRTc = 0b000001t CMCOIiIo = 7t a.NsEt = 0 t TRrSA = 0b101000t TRrSC = 0t Curletter

= 0t

// // ll // //

ALl Bits are Low stalt with Top lreft altrrL off compalatols off rrDc $rrn na5/RA3 Input8

//

start

wit'h

'A"

Fisure 613 5x7 LED matrix driven by a PICl6F684and 745126

S e c t i o n5 i x

Interfacing

Proj ects

for

the PICo llClJ

125

while(J.

== al

ll

Iroop tlorever

t (Dlay = 0r Dlay < 50r Dlay++) for for (i = 0r i < 4, i++) Character t // 20 mB to Diaplay * 5) + ilt j = Leteera[(CurlJett€r k = (j >> 2) e 0b010000, PORTC=j&0b1U111, PORITA=k+it for (n = 0t n < 259r n++)r / / 4 mE Delay // tof | Curlfette!

) |

= (Curletter

// elihw / / ED,d cMatrix

+ 1,, e.4,

ll

Increnent IJettser

It is interestingto seethat the characterdisplayis not substantiallylongeror more complexthan the first setof codethat movesthe lit LED acrossthe LED matrix.Most of the work wasspenttuning the 4 ms delayin the displayloop (the conceptusedwasto cycle through eachcharacter over 20 ms,requiring 50 repetitionsto displaythe characterfor 1 second).After mappingthe LED matrix pins and beforewiring the application,I spenta few momentsthinking how to most efficiently wire the application and wfiting out somecodeto figure out the wiring arrangementthat would keepthe codeto asfew instructionsaspossible.

2

E x F er im e ntlt 6 -L C D D i s p l a g

r. Prcr.5F68 4

tri

1 Two-line,

16-column

LCD

1 10k breadboard-mountable potentiometer , . 1

1 0.01 pF capacitor tYpe)

(any

Breadboa!d-mountable (E-Switch SPDT switch 8G1903 recomrnended) Needle-nose pliers

Breadboard-mountable momeotary On push button

B!eadboard

;'-':,:

Wiring

kit

Three-ceLI pack

AA battely

3 AA alka1i.ne

t.a

-;-i

:;i.a r,:,f

Creatingapplicationswith LCDS hasa reputationfor beingdifficulr.I would disagreewith this statement, noting that if you follow a simplesetof guidelinesyou really won't haveany troubles.The circuitpresentedin this applicationcan be built andprogrammedin aslittle as5 minutes.And, if you would like to enhance(or change)the output messageor add more functionality to the application,you will find it to be quite easy(asI will demonstratein the next experiment). In 123RoboticsEtcperiments for theEvil Genius,I includeda fairly comprehensive explanationof how to interfaceto Hitachi 44780controlledLCDs.I am not goingto repeatthat informationherebut will point you to my web site (www.myke.com) whereyou'll find informationregardinginterfacingto theseLCDs and

L26

batt eries

programmingthem.Along with thesetwo references, you'll find additionalreferenceson 44780-based LCDs. I shouldpoint out that 44780-based LCDS are text basedonly and havea 14- or 16-pininterface,andyou canprobablyfind somesurplusor old productsforjust a dollar or two. Graphic LCDs have a different interface and require a completely different interface. The circuit that I usedfor this experimentis shown in Figure6-14,and there are two thingsI want to bring to your attention.Most LCDs require4.5to 5.5volts, not the 3 volts I haveusedup to this point on breadboard circuits.To provide the LCD with the correct voltagelevel,I usedthree alkalineradio batteriesin a seriesclip to provide4.5volts.If rechargeable batteries are to be used(whichproduce1.2volts per cell,instead

- JE x p e r i m e n t s f o r l , e 3 P I C @l ' 1lC

the Evil

6enius

tlitachi 44780 Baaedl LcD This Ptoglaln rnitializes to in 4 Bit Moate anat then vrriues a sfurD1e atring nras useal to tir€ alelay values. it. The simulator can be founal

'.cD write Infomalion hltp: / /www. rnyk€. con

RC3:RCo - I . c D I / O D 7 : D 4 ( P i n s Pin RC4 I.cD E clocking RC5 LCD R/S PiN

at

1{:11)

rnyke prealko 04.11.08

-: I I

Figute6-lq

CONFIG(INEIO & WDTDIS & PITRTEN & I'ICLRDIS & IINPROTECT \ & UNPROTEC!! & BORDIS & IESODIS & FCIiIDIS) i

LCD circuit

int

j,

i,

k,

ni

Use clobal Debug

//

for

variables

Display 12s) ll (k < 7s))) Flashi'lg // Start/conlinu€

'Len Tfain Signal

Figure 7-B

"Righl'Tmin Signal

Traincrossingcircuit

S e c t i o n5 e v e n S a m p1 e C l l i c n o c o n t r o l l e r

ftl v/!

tld

Chans€ Flaahing Ligh!s = 10r FlaEhTirn€r // Res€t F1aah Tiner PORTC = PORTC ^ 0b001100t , /t fL | // fJ\ / / elilLv ] // Ena cRok 2

{

//

if

10 gecoDtla

{

i < flft!'nEr

,

= lO + 2Oi // Flash f,or (PoRTc & 0b001100)) Start | // If Not Flashing, PoRTc = 0b001000t = 10, FLasbTinler | // ti ll Ei l IJighlTiner (0 :E if

)

Applications

145

rl

tn ry *r.a

n

?r L

r",

To find the problem,I first simulatedthe application to make sureit worked asI expectedit to.You will find that the simulatorexecutesstraightthroughthe ADC operationswith the GODONE bit beingimmediatelyresetafter it is set.Using the Watchwindow registervalueupdatefeature(double-clickon the register'svalueto bdng up an edit window),I testedthe applicationfor differentADC return valuesand made surelhat the codeworked properly.Next,I commentedout all the codefor one of the two channels and I found the applicationstartedworking,until I returnedthe commentedout code.Next,by flashing the LEDS,I output the valuereadversusthe value averageand discoveredthey were different.Finally,I

tried to do a dummy readof the ADC channelbefore the live read,which wascheckedagainstthe averaged value. The previousparagraphdoesnot conveythe frustration and annoyanceI felt in working throughthe problem.But, by working stepby step,ensudngthe programworkedin the simulatorasI expected,checking my assumptions rigorously,and not presumingthe root causeof the problembeforehand,I wasableto debugthe application.This is a usefulsequenceyou canusewhenyou are facedwith an applicationthat doesn'twork or behavesin a mannerthat is totallv unexDected.

ExFeriment 53-5even-5egmentLEDThermometer

P I C 1 6 F 6 84 ?4HCT138 three decoder 1 14-pin

to

e j,ght

socket

2N3906 bipolar transi stors

NPN

(Radio Shack Thelmistor 271-110 recor nended) 0.01 pF capacitols tYpe)

DMM Scientific

100O res istors

calculator

330f,) res istors

Needle-nose pliers I

So lde r 9lire-yr3p

10k, 1-percent ance resLstor

tole!-

SPST switch 9i36

fwo-cell c1ip

Weldbond adhesive

Probablythe mostpopularPIC MCU projectI have ever designedwasa three-digitdigital thermometer for the PIC16C84that waswdtten in assernblylanguage.For somereasonthis circuit and softwarewas extremelypopularandwasusedasthe basisfor a wide vadetyof differentapplicationqincludingmonitoring the temperatureof a chickenincubatorlThe original digital thermometeruseda timed I/O pin resistance measurementof an RC network.Two input pinswere usedto calibratethe thermometeroutput,andthe calibration valuewassavedin the data EEPROM of the PIC MCU. The originalcircuit works quite well,but I wantedto eliminatethe needfor the calibrationvalue

L46

(any

AA battery

aswell aswrite the applicationcodein a high-levellanguagethat did not obfuscatethe operationof the thermometer.Tomeet theseobjectives,Iusedthe PIC16F684's ADC alongwith the PICC Lite compiler languageand its floating-pointnumbercapabilities. The circuit (seeFigure7-9),althoughfairly complex,is quite straightforward.One commonanode, seven-segment LED displayis activeat any one time, and the applicationsequences througheachLED display 50 to 64 timesper second,givingthe appearance that all four are activeat the sametime.The seven-segment LED displaysare poweredby PNP transistors

l , P 3 P I C @l ' l C UE x p e r i m e n t s f o r

the Evil

6enius

G1 Vdd

Ptc1 6F 684

B

C

.G2A/B Y3 \2 Y1 YO

RA5 RC5 RC4 RC3

-l-

RCI RA4 RCO

I

-L ,: Figure 7-9

"o

2x Dual7-Segment Common Display

Digital thermometer circuit

that are,in turn, drivenby an eight-bitdecoder.This may seemlike an ungainlymethodof driving the seven-segment displays,but it is requireddue to the lack of availablepinsfrom the PIC16F684to ddve the segmentcommonpns. I built my prototypeon a fairly smallPCB (seeFigure 7-10)and usedpoinlto-point wiring to createthe circuit asshownin Figure7-11.Thereare a lot of wires to solder,and whenyou havecompletedthe circuit,I suggestyou testit with cThermo1.asm.This will initialize the PIC16F684and displaythe ADC valuefor the resistor/thermistor voltagedivider.I usedthe Radio Shackthermistorbecauseit is reasonablypriced and the backof the packageliststhe thermistor'sresistance for differenttemperatures, which is nice to havewhen debuggingsoftware.

*inclutle cTherno 1.c /. This program will ADC Value on four Displays.

Display

the

ADC Value

alisplay the curlent PrC16F584 7 gegnent Conmon Anoile LED

The Codle/wiring is baseal on 'c4x7s€9.c" Noting that Negative Aetive LED Wiiirlg iB useal. Eartlware AA5 RC5 RC4 RC3

-

Fiqure 7-10 Digital thermometertop with thermistorby the on/off switch RC2 - Segment RC1 - Segment RC0 - segment RAO - Right - Left

e f g

7 Segtnent 7 Segment

Display DispLay

lryke prealko 04.12.23

Not€s:

Segment Segment Segment Segment

a b c al

-CONFIG(INTIO UNPROTECT \ & ITNPROTECT

& WDTDIS & BORDIS

S e c t i o n5 e v e n S a m p1 e C l "ilc n o c o n t n o l l e r

& PWRTEN & ITCIJRDIS &

IESODIS

& !'C!IDIS)

Applications

& t

L47

o g br

tl (l

s, E

tn 0, w I

l-N

CI,

F

{l rn I

rn !J'

\v

H

--l $..1 \u P{

= (DtEplayIJED

NoPOr (1. = 0r fo!

Figure 7-'f'f Digital thermometerbacksideshowing pointto-point wiring

Eotevei

j

E j

for

/ / veed

fot

+ 1'

(s0 =- i)

if

(

+ !,

// uged L < 32"1, i++r,

NOP O T

Fl l{

l2oo!,

* 4)) = (DisDleyvalue >> (DiEplat'!@ & 0x0Ft PORTA = ( (LEDDigit lDisp1a]'Digitl >> 1) & 0x020) + (1 *inclual€

190

Program

outputs

a "1"

if

rlro

In!,uta

a?e

1 0 : myke pretlko 11: 04.11.19 L2. 13: L4l CONFTG(TNTIO & W!||IDIS & PYIRTEN & IICIJRDIS UNPROTE & TTNPROTECT & BORDIS & IESODIS & ECMDIS) t

(

231 24: PORTC = 0t 0003F3 1283 BCE 0x3, 0x5 00038{ 187 CLRF 0x7 25: CllCONo = 7r // 000315 3007 !4OvLw 0x7 0003F5 099 uovuF 0x19 26: iNSEL = 0r // 000387 1683 BsE 0*3, 0'r5 0003F8 191 CLRF 0x11 = Oi 27t a&lsc2 // 0003F9 L10? BCF 0x7, 0x2

29: 30:

w h i L e ( 1 -= 1) (

//

rurn

off

coltll)ara

Ilurn

of,f

,rDC

Make RC2 Oulput

Loop

Forever

== Rc{ ) e& (1 == Rc3)) BCF 0x3, 0x5 BrFSC 0x7, 0r.4 BrFSS 0x7, 0x3 GOTO 0x3f9 ll Both Hlgh, BSF 0x7, 0x2

j;

while(l

8:

RCA - Iq)ut RC3 - Iry)ut RC2 - o|rtput

a

!,-!l

i,

This

1 9 : ints i, j, 20l 2 L l rnain ( )

RC{ - Input RC3 - rnput RC2 - Output

int

{:

L7z -

ml'k€ grealko 04,11.19

L t_:

an AllD Ciate on

Simulate

Nolrlatch3

tti

Thia

c -

2, /+ cLogj.ac ale. PIC15F6

Evil \cIJogiccate.

c

0003FA 1283 0003F8 1A07 0003Fc 1D87 OOO3FD 2BF9 32t RC2 = a, 0003F8 1507

OutsDut

You can make a couple of conclusionsfrom the resultingassemblylanguagecode.Thefirst being,the disassemblylbting is not complete.The closingbrace for the "while(1 - - 1)" statementand a"goto 0x3fa" instruction.which returnsexecutionto the start of the comparison,are both missing.Tosimulatethis applica"pin stimulus"featureof tion, I usedthe asynchronous the debuggerto changethe statesof the input pins. The secondconclusionyou canmake is that the PICC Lite compilerisn't stupid.It wasableto convert the two-termif statementinto just three assemblylanguageinstructions. And at the sametime,it wasableto note that resettingthe TRIS bit RC2 in bank 1 is the sameasresettingpin RCZ in bank 0. Later in this section, I will provideyou with a set of macrosthat will allow you to virtually write your own highJevelcode in PIC MCU assemblylanguagewithout havingto understandhow the differentinstructionsoperate.But the codeproducedby thesemacroswill in no way be asefficientasthe previoussnippetof code.

l , a 3 P I C @l ' l C UE x o e r i m e n t s f o r

the Evil

Genius

how The three-instructioncodedemonstrates in the PIC operationscan be created sophisticated just MCU using a few instructions. Two-bitinput AND statementsand OR statementscanbe producedusing the following templates: i

AND Code 3 Instruclion btfsc Regislerl, bitl

btf,ss goto

Register2,

biL2

SlitDNotTrue

r Coale E:.ecutedl Both Bits are

ANDTrue:

;

3 Instruction OR Coale btfss Registerl, bitl btfsc goto

oRNot True:

Bit Input r If First is 0, Jump to not True lnput i rf Seconil Bit iB 1, .runp to True r One or Both Bit hpurs is 0, nnotn

Register2. oRTrue:

bil2

if 1

Bit ia 1, r If First iIump to "True" r If seconal Bit is 0, Jrrnp to "Fafse" are t one or Bolh Bits

I r coale Execut€al if Both Bits are 0

Thesetwo snippetsassumethat Tiue is high.They canbe easilymodifiedifTiue is low for one or both bits;simplychangethe btfssto btlic, and visaversa.

If you are a student,I shouldremind you that it is not a good ideato w te your applicationin C and then usethe PICC Lite compilerto generateassemblylanguagecodefor your applicationfor a numberof reasons.First,the displaydoesnot labelregistenlif you window wereto hand in the codefrom the disassembly I suspectthat eventhe mostdim-witted unchanged, teacherwould noticethat somethingwasamiss.Second,the codedisplayedin the DisassemblyListing window doesnot haveany goto labels,and you will find it to be a significantchallengeto correctlymove the locationof the codeproducedby the PICC Lite compilerto the locationsrequiredfor your application. Chancesare it will take more time and effort than if wereto do it yoursell Finally,the worst thing that couldhappenis that you correctlylabel the registers and addcorrectgoto labels,and then your teacheris so impressedby your work that you are askedto explain to the whole classhow the codeworks.If you're having createa problemssolvingan applicationin assembler, program the PICC Lite compiler C and lun it through to seehow a professionalwould codethe application. But remember,it's in your bestinterestto take what you learnfrom the DisassemblyListing and applyit to your application,rather than trying to usethe code that'sdisDlaved.

" 5 u ri tc h " ExFer im ent 73- lm pl e m e n ti n gth e f S t at em entin H s s e m b l gL a n g u a g e In this experiment,I would like to showhow the C switchstatementcanbe implementedin assembler: ( (variable) switch // value lo be testeal case 2: nvaliable == 2n to E.ecute if // Statenents the "switeh" Statenent breakr // Exit cas€ 23: == 23 if, 'variable to Execule // statenents br€akt

If askedwhen I first learnedto program,I like to reply that it hasbeengoingon for 25 yearsnow.Theimplication is not that I am somewhatdim, but insteadthat thereare so many differentwaysto solveprogramming problems.I bring this up becausethe codefor this expedment,which waswdtten after the codefor the followingexperiment,is a lot mole efficient(i.e., smaller)than the codein the following experiment. Logically,the programsshouldbecomemore sophisticatedandefficientasyou progressthroughthe book.I wantedto illustratea particularpoint, so I placedthis more efficientprogrambeforethe lessefficientone that followsit.

//

//

Statenenls b!eaki default: statenents "variabl€ ]

//

Uo Execute

if

nvariable

== 47

l= 0 oi 47 // "variabl€" != 0" and to Execute if, "variable t= 47" hctiws

There are several ways in which the switch and case statementscould be implemented in this block of code to execute specific code if the contents of a variable match a specific value, but for this experiment, I will focus on the general casecode. One way to implement this function is with multiple comparisons.lf this were to be written in a language

S e c t i o nN i n e A s s e m bI y L a n g u a g e R e s o u r c e R o u t i n e s

191

with an "if then goto" statement,the switchstatement could be implemented as: (2 == Variabl€) if th€n == ?LraE gwitch CaEe StaEenelts to tlr.ecute lf goto gwitcbDone

{J

// //

ct

Not2: (23 == Vrriabl€) if th€! // == s€coaal Snitch Case // Staldl€rta to Execute if goto swilchlrone Not2 3: ({7 == Variable) tf th€n // == Thirit S:nitch Caa€ // gtaldlellta to Execute if goto switclDone Not{7: - Stat€arenlB // trD€faulli to rrvari.rbl€ != 2' anal

c

E o

.p

(s

.tJ

q

"q ()

{J ''.1

B

q

U

c,

rrvariabl€

!= 23'

nvariable

!= 47tr

gotso Nots2 "Variable

goto

| I

golo

c

trvariabl€

Executse

movf, r.or1!r

variab1e, 2

9{

X

rd

{t

if

,

"tf

(2 == variable) goto Not2r

btfaa sTAlt'os, z gtoto Not2 'varia.b16 ^ 2n equal to Ex€cute // Staletrents tf z€lo switchDone "o"o Not2: xorlrc 23^2 , W R E G= ( W R E G ^ 2 l ^ ( 2 3

^ 2)

t = W R E G ^ 2 3 ^ 2 ^ 2

t gTATUg, Z btfaa goto Not23 // gtelen€nts to tir.ecute

goto switchDon€ Not23: r.orLlr 47 ^ 23

L9z

l-

23n atd

^ Hnoi

23 (B€cauae2

"Valiabl€

if

-=

23

, wREc = (WREG ^ 23) ^ lA7 ^ 23) , = W R E G ^ 4 7 ^ 2 3 ^ 2 3 , = vlR.EG ^ 47 (Becatnae 23 ^ 2 3 = 0 )

bbfa8 STATUS, Z golo !bt{? // Stalen€nla to E lecule grditclrDone goto No!{t: - Stsatenenls // "Defaultrr "Vari.rbl€ t= 2" aDil "valiable

{?tr

naamPKIJED 2 LED"

PICkit

1 starEer

Lf

lo

"variabf,e

Execute

== {7

if,

hit

copies r This program allrectly the op€ratio! CPKLED.c antl Elaaheg the I LEDa oa the PICkil , 1 alarter LLt in Sequenco. i Hartlwar€ llotses: PIC16F68{ rutlDing at 4 llllz Usltrg t Clock Exterttal Resel ia Ua€fl t Circuit Runa oa PICkit 1 Etart€r r

!t

.Fl (,

=-

!=

This method of implementing the switch statement in assembleris quite efficient and easyto code,asI show in the code for this experiment, asmPKLED Z.asm.In this codeI havetried, asmuch aspossible,to dfuectlyconvert the highJevel statementsof cPKLEN.c to assemblylanguagestatements: lit].e Ruaning

Not{?

This isn't a terrible baseto work from, but we can do better.The secretto doingbetter is to load the vari, able into the WREG and then compare its contents usingthe xorlw instruction repeatedly.Take advantage of the MPASMTMassemblerbuilt-in calculatorand XOR the valueto be testedwith the Dreviousvalue.Bv doingthis.the previousXORed valui in theWREG is returned to the original state and then XORed with the new value.To showyou what I mean,I canconsider the if equivalent to the switch statement as:

c

t{

23

SwitchDoE€:

t-

c,

-=

anat

1Y1

1J

No!23

,'variable

thea

IJ

== 2'

rvaliabl€ SwitchDone:

t Original r inl

i,

the

rnternal

Lits

Coale: J, kt

r nain ( ) t t r

t PORTA

= 0t

t

ANSEIJ

,

k -

=

0i

0;

// //

Turn Tufi

//

starts

t //

== 1) wbile(l IrooD Eorever

r //

sirnDle

, ll t t t t t r r i i i t t i r , t

(k) t alritch geLecL Whicb LED to DieDlay caae 0: PORTA = 0b010000, TRISA = 0b001111, breah, caa€ 13 PORTA = 0b100000t TRrgA - 0b001111t breakt caae 2r PORTA = 0b010000t TRISA = 0b101011t braakt caae 3: PORTA = 0b000100t TRISA - 0b101011, breaki

t t r t r r i i

PORIA = TRISA = breakt case 5: PORTA = TRISA = breekt cese 5:

off off a!

Cdrlraratoig A.Dc LED 0

f,or (i = 0r t < 255r i++) Etelay IJooD for (j - 0r j < 129r j++)t

0b100000, 0b011011;

0b000100; 0b011011t

l , e 3 P I C @l l C U E x p e n i m e n t s f o n t h e E v i l

6enius

of

= 0b000100t = 0b111001t

r r i ; i , i r

PORTA TRISA br€aki caa6 7t PORTA TRISA breelr // l

, //

k E ( k + 1 ) ? 8 t IDcr@ent k lrithi!

, ,

l )

//

= 0b000010, = 0b111001,

of

& -INTOSCIO

Varlabl€g CBI,oCK 0:.2 0 Dlay Actj.veBj.t E![DC r

r Recoril

Bil

tsb6 Activ€

Turrr

,

SetuD TRIS for Nd.t Disglay

mov$f goto

II{DF LEDIron€

o

EoD

For

t

Cl.lCONo gTAfUS, RPo tt{SEL ^ 0x80 srATus, RPo

t All

I/O

Blts

to

Trr/D2: xorl$ btfss

2 ^ L gTAtgg,

golo movltp novfif rnovlw

Trt'D3 8'010000, PORTA 8,101011,

rtrovwf goto

INDF IJEDltole

BitB

Digital

a!€

AcElv€BLt

r gtalt

novlw

TRISA

novl "f

FSR

r PoLnl FgR to TRISA fo! raat TRrsa try)alating t

Loo93

i

D].ay

btfss goto tlecfaz goto clrf

sT ms, S-2 Dlay, t S-{ Dlay

, Dlsplay

i i

i

-1

rdith

Returl Delay High

8 Bits

D€cr€eeat conlenta 256'* t i

z

clrr' edld].w

-1

btfaa goto al€cfaz groto

sTATus, z $-2 Dley, f. S-4

Dovf

ActivcBit,

t R€Deal i

o

th€

Ft F.

Rep€at

Tuln

on D1

getuD

fnIS for Dj.aDlay

, R€D€a!

for

D6Lay

tlre of VIREG

R€p6al Again 400 ms total

f,or Delay

255x

Bit

movlrf grolo T4fD5: xorlv btfa8 golo lrovlrd lrovwf movlw movwf goto

th€ of WREG

t Loadl the

FiniEhed,

t DisDlay

i

the

tr.|

R€9eat

(^,

D2?

Turn

I

on D2

t S€luD TRIS for Next Dis9Lay

8'000100' PORIIA 8'101011'

r DIaDIay

r Tuln t

getup

MDF LEDDone

the

4

ro

ReDeet

r Finiahedl,

3 ^ 2 Sf[lTUg, z Trl'D{

{ ^ 3 grATItg,

5 rt

Z

NdrE

TryD{: ,ror1w btfaa goto novlw ovnf rovltr

3 o

Dl?

D3?

o

on D3 IRIS f,or Dl.aDlay

t Finishedl, i

DigPlay

i

Turn

,

getuD

th€

Q

ReDeat

{

D{?

F.

rf

z

TrtDs B'100O00' PORTA 8'011011' INDF IiEDDoue

o

on D{

TRIS for NexE Diaplay

t Finlahetl,

the

Rep€at

efter

255x

r Decra€nl coDt€rrta , 256x

rr

D0

ll6re

t

Off lovwf goto

clrf

clrf clrrl' aaLllw

8'100000' PORITA 8,001111, MDF IiEDDoa€

lrov1w movrrf, lrovlw

ICD Delntg

r hitialize , Turn off

PORTI 7

16

on D0

, Einisb€al,

1 ^ 0 STATUS, z TrfrD2

movlrf goto

Tr,yD3 ! xorlw btsfaa goto

PAGE uailline

clrf novlv coqraralors novn"f baf clrf bcf

i

Next

_cowrrc _ltllEN_oFF & *IEgo_oFF & _BoD_oFF & _CPD_OF! & _CP-O!F & -!|CLRE_ON & _PIIRTE_ON &

or9

8'010000' PORTA 8,001111,

novlw rovlrf, movlw

'IST R=DEC INCLI'DE nD15f 58{. iDc"

t

movlw novwf fiovlw

TryDl: r 0x0Fl' (Carry Set) wlltten for low ; Not€: a|lal uial-Rang€ r PIC UCUg so no rradlallet i

1tr

+ 1

w

r Deslinalion Deatsination

STAEUS, C

,

j

rd

i

sultrf

Destinalion

+ 1,

sulrwf novf btf6a l n c f autwf btfss golo

j, w i+1,rt sTATItg, c i + 1 , n j+1,!t STATUS, C Errolloop

Sinuleto! vl'a

+ 1.

IJISE R=DEC INCII'DE'rp15f

ElrolLooD:

j:2, Destiaatlon:2 ENDC

Declaration t vari.abl€ (2 Byle) r 16 Bil Couater

PAGE 0 i R€quir€dl TCD2

fo:. l.tPI|IB

"i" with r rnitialize 123{5 al€cinal t High Byte Fl.fat

movlw riovwf lrovlw lrovwf

IIIGE 1234 i + 1 L@l L23{ i

llovlw movwf movlw nowf

IIIGII 56789 j + 1 rJow 56?89 j

,

lncfsz d e c f

i, f i + 1 , f

i i

i n c f

i + 1 , f

t

blfsc

STATUS, Z

alecf flecf

202

i+1,f t.

f

(i

,

if

i

i is ifump

> j)

hitialize

"J.

ui" lDcr€ment Incremen! High Byte if Reault of Increnenl ing lJolr Byle

rifl fiecreneat D€c!€neat Eigh ByEe if IJow Btte is equal to r initsielly i i

goto

E!!or

Less than j, Tak6rr, Error

if

Everytshing

Codrpare ODeration diftn't lrork cofiectly

i

684. incn gtolo

ors noD

(Lo!t If Cerry R€set By!€ Negative) Tak€ ole ltore Alray from th€ High Byte f

t Finish€d, okay

CBIToCK0x20

= - j

f

!A[ke Prealko 0{.09.21

lj.1

'*

blfac

lacf

l.{

s

i, w J, w D€stination i+1,w

15 Bit

D€alonalrate 15 Bit Varieble Declaration, Initializatioo, additio[, Eublraction, incr€rr€nting alecren€nt ing anat cod[)erisod operations.

! f*

Ddlotrstlat€

movf atLlwf rnofitrf movf

ErrorlooD

€Dd

Note that the conditionaljump hasa bit-skip instruction that follows the samerules as the eight-bit conditionaljump codepresentedin Section8.This meansthat the tablesusedto definewhich valuewill be subtracted(andfrom where),and the tablesusedto define the flag state for the bit-skip instruction are exactlythe same.The conditionaljumpcodefor a valuebeingequalor not equalto zero is a specialcase that canbe testedby simply ORing eachof the two bytesof a 16-bitvariabletogetherand checkingthe zero flap:. inovf

Lab€I,

ct + 1,

,

iolrrf

Lab€I

blf,ac gotso

STATUS, Z gL:rteenBilzero

w

i

i

OR Low Btrte rrith Eigh Bytse (bolb Z SaE if, reEult lrytea ) ia zero iturll) value

ia sixteen ia zero

bit

This exampleillustrates that although the different operationspresentedin asml6Bit are usefuland can usuallybe usedwithout modification,somecasesexist wherebetter codecanbe producedwithout the same number of instructions.For exarnple,think about how to implementaddingan eight-bitvalueto a 16-bit

l , e 3 P I C @l l C U E x p e r i m e n t s f o n t h e E v i l

6enius

variableand what would be changedfrom the previous codeto do so, When you have worked through the different operationslistedin asm16Bit,you shouldhavea good idea how to performbitwiseoperations. You shouldalso recognizethat the bitwiseoperationsare somewhat simplerthan additionand subtractionoperations

becauseyou do not haveto carry or bonow. Multiplication and division are somewhatdifferent than bitwiseoperationsor additionand subtraction,but you shouldbe ableto comeup with someideason how they canbe implemented.(Section12will examinedifferent methodsof multiplicationand divisionthat can be usedasa basisof these16-bitoperations.)

78-Universal DelagMacro Experiment

fsl {lt L'

6$ rlt

:'il

t''i' movlr movlrf alocfaz g o t o

When I discussed delaysin C programming,I probably seemedpretty nonchalantaboutit; basicdelayscould be implementedusingfor loops,and more precise delaysusingbuilt-in timersand critical signalswould be output from customhardwarebuilt into the PIC MCU. For C programmingand relativelyfeature-rich deviceslike the PIC16F684,this is a pretty good attitude to take.But whenyou are working with simpler PIC microcontrollers, which do not havemultiple timersor CCP rnodules,you are goingto haveto programa delayyoursell Programmingdelaysin PIC MCU canbe donesurprisinglyefficientlyasthe macro in this experimentdemonstrates. The PIC's clockis dividedinto four parts,eachof whichprovidesa differentfunctionin the instruction rycle.By knowing the clockspeed,you can easily period asthe reciprocalof derivethe instruction-cycle the PIC MCU's clockspeeddividedby four. Every instruction,exceptthe onesthat changethe program counterexecutein one instructioncycle,and this knowledgecan be usedto createsimpledelays.As you might expectthe nop instructioncanbe usedto delay one instructioncycleand the following instruction: goto

$+1

r Delay tswo iaBtruction cycles

will delaytwo instructioncyclesusingonly one instruction. If you needto delaymore instructioncycles,you canusea smallloon like this one:

i i t

Loatl

Countser

Declenent Loop Back

oremmino

,i

''

the count€r to count€r

Eachloop executesin three cycles,and a maximum of 769instructioncyclescanbe delayedusingthis method.Note that I am usingthe relativeaddressfor looping:Multiple delayloopscan quickly run out of your list of stocklabelslike BitDelayloop, and you'll end up with labelslike BitDelayloopT.Theseare not recommendedbecausethe loopsare probablycopied and pastedfrom other partsof the program,andyou may not alwaysrememberto updateall the label references.A jump to an incorrectlabel can be very difficult to seein a program.By avoidingtheir usein delay loops,you will eliminatea possiblesourcefor pro-

i,i" aa:a

*

:.n

crrnrc

For longerdelayqyou will probablyseethe needfor a 16-bitcounterand might want to usea loop like this one: rnovlw morwrf movlw novwf alecfsz goEo al€cfsz goto

HIGH Count Dlay + 1 LOW Courlt Dlay D1ay, f t-1 Dlay + 1, f S-3

r lJoaal 15 Bil

Counte!

* :l-.'i

Decrement

r Decr€menu t I,ooI) Back By!e

IJow Byte Bigh Byte to Low

{:i3

This codeworkswell, but it is extremelydifficult to comeup with a 16-bitvaluethat will delaya specified numberof cycles.I usethis codeto give me a quickand-dirtyapproximate200ms delayby resettingthe 16-bitDlay variablebeforeenteringthe loop:It's fast, easyto code,and doesn'trequireany thinking on my part. The following codeis the recornmendedway to use a 16-bitdelaycounter: novko monwf movlw

5ection Nine

Cyclea Dlay f Dlay, $ - 1

EIGE

(count

DIay row (Count

+ 255)

r

Loafl

15 Bits

+ 256)

A s s e m b l y L a n g u a g eR es o u r c e R o u t i n e s

203

!.;}i,

i!!

{r ;t

a;

aatal].w

-1

t

Decldment Byt.e

btfsc a€cfsz

sTATus, z Dlay, f

,

Decremen! High ByUe IJooD Back

goto S-3

.H t t rlt

br

This codehasa constantloop delayof five instruction cyclesand requires one fewer instruction and file registerthan the more obvioussolution.The 256is addedto the cycle count to ensurethe correct number of loops(otherwise,if the high byte of cyclesis zero, the codewill loop 255extra times,and whenit is not zero,it will loop one time lessthan you want it to). I shouldpoint out that the lastpassthough the loop executesin four cycles,and with the three overhead cyclesbefore the loop, the cycle delay can be written out asthe followingsimpleformula: Cycl.eaDe1ay

&,1

{tl

&q G} L'

! ,

P-

=

(Counl

* 5)

+ 2

In the program asmDlay,I have taken advantageof this delay and the one- and two-instruction-cycle delaysshown previously to create a macro that providesa precisedelayto a specifiedcyclecount.In the program, I have listed the macro and put in a number of teststo make surethat it executesfor the specified number of cycles.The nops after eachmacro invocation are usedto set breakpoints before and after in order to time the code'sexecutionusingMPLAB IDE'S Stopwatchto test the operationof the macro. tit.I€

.d

i

lJolt

"aamDlay

-

Creatse

a ltnivelsa1

The nDlay'r rnaclo Ln lhis loutine t dlelays ranglng fron 1 to 1.000,000 cycl€a. r Haralware Notes: r PIC15A584 runrring at { MHz in t ReE€t ia tied alirectly lo Vcc i Pullup/PlogrEll[ling llalibrare i r ,

Delay

r3i1l

Droviale

Slmulato! via

l4yke Prealko 04.09.22

r 1

$,,.

,"{ IJ

\t

P.{

X

sd

_coNFIG _FCI{EN_OFF & IESO_OFF & _BOD_OFF & _CPD_OFF & _CP_OFF & _!,ICLRE_ON & _PWR!!E_ON & & _rNTosclo _vrm_oFF CBIrOCK 0x20

t Varialcle Declalation 24 Bit r Requilea (w/ WREG) Count.e!

D1a]rya1u€:2 ENDC Dlay Dlacro CycL€s variable C'yclesl,eft varlaltle largeNum = CycleE CycleslJefl locaL IJongloop

204

i

r Can't Hanall€ > 93 SeconflE (e { !'lllz)

> 0*0{FFFF00

Cycles

"R€quireal error Delay is longer than rDlay, !{acro caa suDDort,l 6ndif if Cyclea > 327581 lJoop? t Ne€dl Large IJargelnrm = cycfe6l€ft / 327 6A! novlw IJarg€Iilum + 2 novwf DLat4lalue Number of t Calculate t oopa LouglooD I t R€DeaE for Each IJoop D1at4ra1u6 + 1 clrf t Do Ma:rimrm Poasible IJoo9 Coun! clrf Dlayvalue dlecf Dlayvalue, f blfsc z s!!a!!us, tlecfsz Dlayvalue + 1, f goto 9-3 dlecfaz Dla]^lalua + 2, f , ReD€at IJooD goto Longloop - ( (IJarge!tum * 327581) + qlcleal,ef,t = Cycleal€ft 1+(LargeNul[*3)) ontlif r Neeal Irarg€ lJoolt if eyclea > 1{ a IJooD be r WilI required? - 3) / 5) + 256) rovllr IIIGH ( ( (Cycle6lefE novwf + 1 Dlat^Ialue - 3)/ 5) + 256) novhr l,ow ( ( (Cyclesl,eft nonrdf Dlay'Va1ue tlecf Dlayvalue, f t 5 C]'cle Conslanl D€Iay tooD btfac STATUS, z + 1, f, alecfsz Dlat4ta1ue goto $-3 - (3 + (5 * ((Cyclesl€ft = Cl'cleaI,eft CycLeglef!

- 3)/ s))) e|talif vhiLe

C'yclesl.efl

goto Cyclealef,l if

; >= 2 t

$+1 = Cyclealeft

Cycl€slefl

== 1

Fini8hed lrith I,ooD Cotle Pltt- in 2 hatruction cyclo D€Iay6 - 2

i

Put ia tslr€ IJast Requileal Cycle

nop €ntlif €ndn

LIST R=DEC INCIjTDE rD16f 68{. inc"

{t

lf

PAGE olg no9

0 r Requireil

Dlatz nop

3

r shoulal

DIay nop

4{

r Dolay

for be goto

4{

t

DIay no9

r Delay

0.1s

Dlay 5000000 noD

t Delay

5a

Dlay

t Delay

50s

50000000

K€eD Tlack of Rdlalnlng C?clea

1 na De1ay

; Finish€it. Okay

Eve4.thiDg

ead

l , e 3 P I C @l ' l C UE x o e r i m e n t s f o n t h e E v i l

& nop

CycL€a

Dlay 1000 noD 100000

!@LAB ICD2

Genius

I would considerthis macro to be a combinationof If you wereto read showboatingand overachieving. throughthe application,you would seethat it is limited to 83 million cycles(83 secondsin a PIC MCU running at 4 MHz), andit providesaccuratedelaysto the instruction cycle.This accuracyis usually required for long delays,but for short delays,the accuracythis macroprovidesis not required.You might want to cut the macro down for your own use and avoid the initial loop, which is required for delayslon ger than 327,681. cycles(a third of a secondwhenrunning at 4 MHz).

If you comparethis applicationto othersin the book that are designedto run only in the MPLAB IDE simulator,you might think that my inclusionof the _CONFIG directiveis an oversight.Generallyit's not requiredin the simulator.However,in this casethe _CONFIG directiveis required.In the testingof the simulated delaysthat are longerthan 2.9seconds, PIC MCU would resetitself due to the Watchdog Timer resettingthe processor.Toavoidthis problem,I addedthe standard-CONFIG statementto make surethe WatchdosTimer is disabled.

133 :r!::

:,i,, tr:

il: . ,.n

ExFeriment79-High-Level Programmingin Fssembler andwould producethe following statements: ,l

tbsf novf tbef IbEf rnovwf tbcf

STATUS, RP01 i, w STATUS. RP0l STATUS, RPOI j sTATus, RP0l

: . f.-.1 !_-

for the macro invocation: ASSIGN J,

When I first startedworking after university,I waspart of a team developinga high-performance functional testerfor electroniccircuits.One of the taskswe had wasto comeup with a programminglanguagefor the tester,and I havealwaysbeenintriguedby the solution that waschosen.Ratherthan comeup with a custom compilerfor the programminglanguage,a standard compilerwasselectedandits macroprocessorwould be usedto convertprogrammingstatementsinto the data requiredby the tester.Tosomeextent,this effort wassuccessful asit did producedata,unfortunatelythe compiler'smacroprocessorcould not prodtce enough of it. Much of the data wasin the form of tables,and the maximumtable sizethe supportedby the macro processorwas32 KB. So far in this section,Ihavegiven you a macrofor conditionalexecution(or if) statementsand a macrofor precisiondelays.And, I wanted to seeif I couldcomeup with a macrothat would support assignment statements, becausewith sucha macro,I felt I couldclaim that I had developeda highlevel languagethat ran under the MPLAB IDE assembler. Unfortunately,I couldn'tdo it in ore macro.It requiredtwo.Thefirst macro,ASSIG\ wasusedto assignthe contentsof a variableor a constant(literal) valueinto a variable.Its format was: ASSIGN DeB!,

Equalsign,

alTtt)e,

SectionNine

Soulc€

or

:iiiu

novlw tbsf novwf Ibcf

47 STATUS. RP01 i sTATus, RPol

for the macroinvocation: ASSIGN

i.

=,

n.t.

47

The settingand resettingof the RPObit of STATUS is dependanton the positionof the variables.I could haveoptimizedthe macroto eliminateone set of bcf/bsfstatementsif both variableswerein bank 1,but I felt this wasgood enough.The blank ( ) or period (.) dTypeparameterbeforethe sourcemacroparameter indicateswhetherthe sourceis a variableor a literal. Thereis no way the macroprocessorcan determine which is which. I originallywantedthe ASSIGN macroto be ableto handlenot only straightassignment statementsto vari ables,but alsotwo-parameteroperations(like add or subtract).Unfortunately,I wasn'tableto discovera methodto determinewhetheror not a parameter waspresentand,in any case,if any parameterswere missingthe macroprocessorwould flag an error.So,I addedthe secondmacro:

A ss e m bI y L a n g u ag e R e s o u rc e R o u t i n e s

205

.:.tLr.

l -.i

OPERjATE Dest, EquaLSigm, alTtrpe2, Soulc€2

alTl.pe1,

Sourcel.

Ope!,

and would produce statements like: movl$ lbsf Bubwf tbcf tbsf movwf tbcf

47 STATus, RPol j, w sTATus, RPol sTATus. RPol i STATUS, RPol

for the macro invocation: OPERATE i.

"

-,

",

j,

-.

n.n,

47

The OPERATE macroputs in the secondparameter wasfirst to load WREG with the subtractorif the subtractionoperationis selected. To test the ASSIGN and OPERATE macros,I created asmAssign.asm: "aslBssign ti!1e Assignment llacro "

-

Experiment

with

an

This Drogran tl€monslralea hosr an assigfirent Inacro f,or the PIC MCU coulal be createal. Earahrare Notes: PIC16E68{ running

at

4 r!trIz in

Sinulator

Myke Predlko 04.09 .24 LIST R=DEC INCLUDE ip16f584.

cBr,ocK 0x20

inc" r variable

Declaration

ENDC ASSIGN macro D€st, Equalsign. variabl€ _aai = 7, _aa) aaj Equalsign _aai (_aai != _aai ) if rrNo Equala ('=,) elror in else r (dr!|I)e != ".") it , ( Source > 0x7F) if bsf, STATUS, RPo , endif movf Source & 0x7!'. nr ( source > 0x7F) if bcf, STAAUS, RPo r endif r movlw Sourc€ endif (Dest > 0x7F) if bsf stATus, RPo t enalif mowqrf D€st & 0x7E (Des! > 0x7E) if bcf STATUS, RPo t endlif endif

206

flTl.pe,

Souree

Assignnent Slalement Equals Present variabl"e Direc! Aank

1 Variable

Bank

1 Variable

Liceral

Bank

1 VariabLe

Banh l

variable

OPERATE nacro Dest, Equalsigm, alBrpel, Sourcel, Oper, ilTtpe2, Source2 = 7, variable aaj ,aai _aaj Equalsisn -aai (_aai != ,aai ) if "No Equal8 ('=,) elror in A6signment Statement t Equala P!€sent " . " ) (dr5/pe2 != if Dilect r variable (Source2 > 0x7E) if bsf STATUS, RPo r Bank 1 ValiabLe enali f nowf Souree2 & 0x7F, w (source2 > 0x7F) if bcf STATUS. RPo r Bank 1 Valiabl"e endif elae r Lit€ra] movlw Soulce2 enalif (alTtrpel != n.r) if Dilect t variabLe ( sourcel > 0x7F) if bsf STATUS, RPo t Bank 1 variable €nalif ((1 oper 1) == 2) if Operato! r Aaldlilion addwf, Sourcel & 0x7F, !r' eLse ((47 oper 25\ == 22') ; sublraction if Operator subwf, Sourcel 1' & oxtF, etse ((3 Oper 2) == 3) if t OR Operator ionrf Sourcel & 0x7F, w elae ' AND op€rator ((3 Oper 2) == 2l if anahrf soulce1 & 0x7E, w ((3 ope! 2) -= !) ' xoR operator xorwf Sou!c61 & 0x7F. w else "Unknolen Operatorrr 6!!0r enalif €ntlif endtif entli f endif if, (Sourcel > 0x7F) bcf STAIIUS, RPo t Bank 1 variabl€ endif el"se t Litelal ((1 oper 1) == 2) if Op€rator r Aalalition aaldlnr Sourcel else ((47 ope! 25) == 22) r subtraction if Operator subLw sourcel eLse ((3 ope! 2) == 3) if r oR operator iorlw soulce1 el6e ' .aND operator ((3 ope! 2l == 2\ if anallw Source]. else ((3 oI)€! 2, == r, i xoR OPerator if xorlw sourcel if

rrunknown Operatsor" error endif endif endif endif endif, endif, if, (Dest > 0x7F) baf STATUS, RPo t Bank endif

l , e l P I C @l l C l - JE x o e n i n e n t s f o r

1 variabl€

the Evil Genius

novwf Dest & ox?F (Deal > ox?F) bcf STATUS, RPo endif, endlif eadm

Prc15F584

t

ruDning

in

Prckit

1 stalter

kit

tql X

if,

t Ba.Dk 1 Varia.ble

i t

!'lyke

i

04.o9.28

Pledlko

b

r Reguireal rcD2 =' =, =,

ASSIGN i, ASSIGN 1, ASSIGI{ J,

for

UPIll|l

" "' j t.d, 47 tr n, TRISC

o P E R A T EI , = , " " , ) , + , O P E R A T Ei , = , " " , ) , & , O P E a A T Ei ' = , " " , ) ' - , OPERATE OPTIOII_REC, =, n

$

t

ry

FCIIEN OFF & -rESO_OFF e _BOD_OFF & collFrc CPD OEF & CP OFF & MCLRE-ON & _P!|RTE_ON & _lsDn_oFF & TNTOSCTO

cBrocK ox2o

Declaralion

i variable

".", 47 ".", 47 ".n' 47 tI OPIION_REC,

Fiuishetl,

Everything

d (*

2{ Bit r Dlay Requirea (w/ WREG) Counter

Dlayvalue:2

}J. ;-a 5

fD

t, J ENIrc

&,

..n,

AggIGll nacro Deat, Equalslga, ASSIGN necro sroulce ttot rr

OPEIIAIIErracro Dest, goto

t9

I.IST R=DEC INCI,UDE "p16f584.inc"

PAGE

Okay

ODer, tlTtzge2, oPERAIEE t;

ilFtzpe, ahown

Equalsign,

Source

{

ttTyDel,

gourcel,

\S

Source2 not

gholoa

ea(t DLay llacfo

When you test this application in the simulator, you are going to discover it is extremely difficult to follow the progress ofthe code;execution will bounce between the macro invocations and the lines inside the code.You might want to look at the Disassembly Listing, but common statements are listed one after the

other in the macro definitiong making the code even more difficult to follow. Your best resourcefor seeins how the macrosproducethe necessary codeis in the sourcecodelisting,asmAssign.lst, which providesthe insertedmacrosand indicatorsfor activestatements. Unfortunately,the MPLAB IDE simulatordoesnot stepthroughthe listingfile. From my perspective, this inability to simulate effectively the application is the biggest drawback to

Cl.cL€a

Dlav tnacro Eource not Ehoffn

;t

onceper secondand is written injust the ASSIGN, OPEI{ATE, Dlay, and ifgoto macros (the body of^ which I have deleted from the folloritititJ,iiJl,

l-,,

PAGE ors

€ Fd

o

nor)

t R€quir€tt ICD2

assreN poRTl, =, PoRrA' =. 1::I9I ff:]:il ff:;y'=;' AssreNrRrsa, =,

for

n.s, 0 i hirialize ""' oxrr

!!Pr.At

variableg

r R€lurn trele when Done

rJoop:

"o*ro,toT]1Y,",1'?."ii""u

ifsoto i, ==, j, setDo Assrc!{ PoRrA' =' ""' 0 o""o "3i13,

assrcN poR![A, soto

rJooD

t Turn off the LED

256? Yes, Pot nt lo N€rrt 255 Atldr€sa BLock ituJqr to TabLe Elenent Readt only Table Sentsence Subj ects Cryptic verb

_ReattEabl€: tll alts alts dtr tll d r retLw retlw r€t1ra retLw retlw retlw

nMyk€ tr ra' + rE, 29*4 tw' - 4 L 6 , 2 0x77 0x5F 0x72 0x5D oxt 3 0

-

:i..:t:

Save Offset PCLATH Calculate

rA,

i

!".*

f.a i*r " ifl 'l.;.j sl !.!:

l'i

F } i,"*i

Noun

h'g

rR t Zero At St!iug

Endl of

&-d

n;

enal

SectionNine

:

A s s e m 1b y L a n g u a g e R e s ou r c e R o u t i n e s

es 209

When you read through and test asmTable,you will seethat it loads a file register array with the contents of the read-onlyarray.And, if you displaythe file registers,you will seea messagethat hasbeen(simplistically) encryptedin the read-onlyanay. As an exercise, I would suggestthat you try to come up with your own subroutine code that implements the generalread-onlyarray (i.e.,it canbe any'lvhere in the PIC16F684's memory;it is greaterthan 32 bytes,and its contentscanstraddlea 256-instruction address block). When you try to come up with your own routine,rememberthat you havea numberof directiveq suchasthe if directivethat can be usedasI havein the followins routine:

E!I6rdtabl6: clrf ((

if bBf

i PCIJATII i EenTable & 0x0100) PCLATH, 0

Relurn llex ASCII for Lslvbbl€ in WIIEG != 0)

Char

enalif (( Iler(lable & 0x0200) ! = 0 ) if baf PCLAAH, 1 endtf ( (_Ir$c!able if & 0x0400) != 0) bsf PCITATH, 2 endif antllw 0x00F ; rtust want low€r { Bits adatln l,ow _H€xTabIe lhe Correct t Calculate

off6et btfsc STATUS, C iacf PCITATE. f noverf PCI. _Ile*Tablel '0 L23{ 5578 gABCDEF"

Experiment 8l-Bata Stacks cationsandprinter control,and network-enabled printersallow datato be sentdirectlywithout the need of the printer switchand data spooler.But the data storagemethodsusedin this applicationare important to know as they will be required from time to time in your applications.

# /l

frt

u"t d

l\l

3 I

c-d

,&r *'{

,it

One of the first applications I designedfor microcontrollerswasa Multi-PC printer switchand data spooler.Thisdevicewould monitor the printer ports of multiple PCs,and when one startedsendingprint commandsto a printer,the devicewould passthem along to the printer.And if therewere other PCssending datato the printer later,their datawould be held in a memorybuffer until the first PC had finishedsending data to the printer.When the first PC had finished(I waited15secondswithout a new charactercomingin), the circuitwould then assignone and then passthe data of the other PC'sdata to the printer.Along with savingdatafrom other PCs,the spoolingfunction allowedthe PCsto passdatato the device,which "appeared"to be a printer,at full speed.Real directconnectprintershold the PC from sendingmore data until it hasfinishedthe operationit hasbeengiven. Thesefunctions allowed all the PCs connectedto the printer to work at full speedand have a minimal programdelaywhensendingdata to the printer.Today, modernmultitaskingoperatingsystemssuchasWindowsand Linux provide separateprocesses for appli-

2L0

Thereare two basicmethodsof storingdata temporarily. The first is known as the s/ack and can be modeledlike the stackof papersin an executive'sinbasket.When datais storedon the stack,other pieces can be stored on top of it (seeFigure 9-4) with the interestingresultthat the first pieceof data on the stack will alwaysbe the last piece removed from the stack.For this reason,stacksare knowr aslnstin frrst oLl (LIFO) memory To demonstratethe operationof the stack,I created asmStack.asm, in which I haveset up a 16-bytedata arrayin bank 1 for the storageof data.Tostoreand retrievedata on the stack,I createdthe Pushsubroutine:

/-------\ /

"Stack"

Fieure9-q

l , a 3 P I C o I ' l C UE x o e r i m e n t s f o r

Stack

the Evil

Genius

Outgoing

ht |

trstackn affay // Put Deta i! | / Al E1€dl€n! nstackPtrn

Pueh(Data)

(StackPtr == SlackEop) leturn ctv€rFlowE:afoli Pointer at €ndl of // The stack els6 // if

stack 5ti11

SDace Left

t = Datai SlacklstackPlrl = StackPtr + 1, SlackPtr retuln //

]

Save Data Poidt to Nexts E1€n6at

// //

PuEhcoodl,

€nil

Puah

And the Pop subroutine, which operates as follows: int

Po9()

//

Relurrl Data the gtack

a!

lhe

Top of

{ - 1) = stackBolton) ( (stackPtr r€tum ltnal€rrlowErroli Nothtng ltr gtack ela€ // "PoD' alatsa off Stack t stackPtr = stackPlr - 1r // ltov€ to r,ast gtack Eleneat

if //

r€luln

StackIStackPtrl

,rop

#*:

3;i.

novlr

20

novrdf, cllf

i i

PuahlJoop: novf call caLl incf tl€cfsz

goto

//

I feelthatlimit checking is important.evenifit is ignoredby thecallingcode;itensures thatno memory

j, !t T€Jr1€Read Push j, f i, f

outside the allocated stack array area is accessed. To simplify the limit testing in asmstack.asm,I took advantageof the bit a;ngement of a t6-iit uutter

golo

$

,

Finiahetl,

i

Push Value

t subroutineg Puah. bsf, STATUS, C

leturn

"olo.rr,

4

il}l"", rs", r

"

oroo

dl6cf

FgR, r

whenyou simulateasmstack.asm.

novf bcf

INDF, w STATUS, C

Eabl€Reeil movwf movlw novwf novf aalfllrd btfac lncf nofirf Table:

I -T€dE) HIGH ,Tabl€ PCIATE w _T€rrp, r,ow _Table STATITS, C PCIATII, f PCL

i t r

program rhis slack coulil Irqrl€oenteal

t r

llarahcare Not€a: PIC16F684 ruELng

i , ,

lryk€ Pr€tlko 04.L2.29

tl€monsllat€s be La the Prc

at

Si

holt

ple

Mcu.

{ MEz in

simulator

t Variable

_Tellp E![DC

olg

0

S e c t i o nN i n e

p

(-f

on glack

El€rlen!

Loop

onlo

have t A].leafly Elem€nta? t 16 El€m€nta, Error

,

Pop ToD of

Forever

Stack

16

Declaratiorr

OO

I

u

g, rF p,

Retuln

Q

Stack

i Notshhs nlt€r€?

rt g,

i Y€s,

ta

Return

Erlor

i Erse, Return toD Brrle

r Randlom Tab].e

r PORTA nnd IBISA Tabl€ valu€B

Bit

dt 47, 23, 33, 2, L90, 4t, 37, Ar, 42, 2r, 94, 74, 79 dr a, 6a, 54, 29, 37, 9!, t-0O

t IgT R=DEC INCIJTTDEn915f 58{. inctr CBLOCK 0x20

Va1ue

slack

F.

3 o

value

gtacki

a 15-Eleme!t

o

P

It also means that data can be lost, as you will observe

Denonstlate

gee what,s

r PoI) rop

a one,then I know I havegoneoutsidethe stackarray and can stop trying to accessinvalid memory locations.

-

t Gloto Next

Pop i, f, PoDLoop

srArus' c :::,, ollii"rr"t"tu"' "

"agnslack

llanalon

i

rsR, w

ti!1€

Get

r\ghTJooD

xonrf

with bit 4 ofthe addressalways being zero.If it is ever

i

Data onto gtack

20 i

Llj*, li"i-

X

Ft r Itah

FSR,

tql

t

leEuln

6nal PoD

!4PLAB ICD2

, stalt stack al 0x20 in Bank1

] )

f,or

novlw novflf PoDLoo9 ! call tlecf,az goto

blfBc

t

r Requi.r€al

6ad

When you run asmStack.asm and executethe Poploop, you will seethat the data is retrieved in oppositeorder in which it wasinput.This makesa stacka poor choicefor spoolingdata in the application mentioned at the start of this experiment.However, it

Assembly Language Resource Routines

2LL

}f U'

makesa stackan excellentchoicefor savingdatathat may be temporarilyoverwrittenand in needof retrievaloncethe operationthat overwrotethe data hasfinishedexecuting.Stacksare usedin the PIC MCU for savingthe programcounterwhen calling

The originalprograrncounteris saved subroutines. beforejumping to the subroutine.It is then retrieved when the subroutinehascompletedand the original programcountervalueis required

Experiment 82-Eircular Buffers Although I've drawnthe circularbuffer asa linked list,it can be easilyimplementedasa simplearray.For examDle. the Put functioncanbe modeledas: i,n

Put (Data)

int

Put Data Circular

//

Into th€ Buffer

t j.f ( ( (PutElement GetElenent) relurn

In this experiment,I will introduceyou to the circular buffer,which retrievesdatain the sameorder asit was written and is ideallysuitedfor the printer switchand buffer applicationI mentionedin the previousexperiment.

:-

.1

t , '

The circularbuffer consistsof a circular,singly linked list of data elements(seeFigure9-5).The buffer hastwo pointers,one pointingto the next location wheredata will be storedand one pointingto the next locationto be readfrom. If both the next storepointer andnext readpointer are pointingto the sameelement,the buffer is empty.Conversely, if the next store pointer is pointingto the elementjust pdor to the next read element,the buffer is full. Justa few instructions are reouiredto imDlementthesebasicrules.

==

+ 1) % Buffersize)

PutNexl + 1== (FuLL ) ReadNext the Dala El€menl / / Can Slore

Buffer!\rl1t

//

€lE6

t Buffe! [ PutElernent I = Datat = (I'utEl€menl + 1) PutElen€n! retuln Putcooali // fi I // End Put

)

e. Buffelsize,

And the Get functionis: int

Cet()

//

( int

the Next Buffef

Get uhe

value

in

T€dr!)t (GetELernent

if

r€tuln eLse

--= PutEl€nents)

Buf felEnutty, //

Elemenl

NoEhing

//

to

to

Retrieve

t T€trll) = Buffer

,1',,ti

Next Element )

j,t,.r.

Get lhe Eleneat = (GetE!.€ulent + 1) % Buffersizet GetElement relurn Tetngi // Retuln the ELenent // fi I // Enal cet lcetElement

],

//

To demonstratethe circularbuffer function.I createdasmCBuffer.asm, which usesthe same16-byte (0xA0 to data areathat wasusedby asmstack.asm OxAF).Thisallowedme to usethe sametrick to make surethat data outsidethe allocatedmemoryis not allocated(if bit 4 is set,the pointer is outsideof the circularbuffer).

i,.i

NextElement to Writeto

tit.le Buffert

"aEmCBuffer

t t ,

This proglam Circular Buffer coulal

i

Earahfar€

circuLa!

how a 16-ELement be Implement€dl

Figure 9-5

Circular buffer

212

t r e l P I C @l l ( U E x o e r i m e n t s f o n t h e E v i l

in

the

NoleE:

6enius

PIC MCU.

PIC15F684

running

at

4 UEz in

Simulato!

t novf

u!'k€ Pr€tlko 0r.12.29 TJIST R=DEC I N C T T T D En D 1 5 f 5 8 4 . i n c n CBIJOCK 0x20 _!!dE) _CBuf,f,€lPul, ENI'C

i

variable

Declaratiof

_CBuff€rG€!

0 Requireal tcD2

for

Stalt Buf,f,er in Bank 1

MPr.aB

al

0:{2 0

_CBufferPut _CBuff€rcet nrovlv lrofitrf clrf I\tIJooD 3 rnovf caLl cal,I incf tlecfaz golo movlw movnf GetlooD: caLl

r

nonwf xorwf baf blfac

FSR CBuffelPut, STAT['S, C STAT['S, z

movf incf bcf bcf,

INDF, w _CBuff,ercel, _CBuf,feleel, STATIIS, C

rc i

i|t

C.

thea

w

t4 X tn

t Nothing t Get the

h{

to Get Valu€

*.J.

f, 4

EF{

:U F"t

PAGE org no9

cBuffercet,

GeE Nex! value Buffer rF Put == Get, Notshtngl

20

dr 47, 23, 33, 2, 94, 74, 79 atr a, 64, 54, 29,

J

i

Put' Dala

i

G€E Rantldr

i

Goto

20 t

i

gee !l?rat,s

cet

i

Gel Next E].ement

j, w TableRead Put j, t i, f IttIJoo9

al€cfaz goto

i, f Getl.oop

goto

s

Next

t Finiaheal,

Into

auffer

value vaLue

on stack

Buffer

IJoop Eorever

subroutia€s Pul Value inlo cilcu1ar Buffe! movlrf movf movwf

_T€tq) CBuffelPut, FgR

aaltllrd andlvr

1 b'11101111,

r.olwf bsf, btfac

CBuffercet, STArsS, C STAtgS, Z

novf novlrf Lncf bcf bcf

w _Teq), IIIDE _cBuff€rht, _CBufferPlrl, STATI'S, C

TabIeReaal: novvrf _TerE) movhr AIGH ,Table movwf PCI|ATE movf Te!E), rd ailillw I,ow _Tab1e blfac STATgg, C incf PCIATE, f movwf PCIr _T6b1e:

w t Can,l bave Put i rf Itnreadl Dala Buffer t IIBEG haa VaIue

8ina1

== Gel in

eet

rt

i

No llore Space Slole Value

f t fncr€Erenl Pointe! 4 t Keep within Range i No Erlor

SectionNine

r Rarlalom Table

s*

Rr , PORTA andt TRISA Bit TabL€ valu€s 190. {{, 31, 43. 42, 2L, 37,

9L,

t I

1-OO

C J 5-i.

6ntl

you When you havesirnulatedasmStack.asm, shouldhavenoticedthat datais retrievedin exactlythe sameorder it wasplacedin the stack.You shouldalso havenoticedthat the maximumstorageof data in the circular buffer is one byte lessthan its total size.This canbe a concernto new programmerswho expectthe data structuresto storeeverythingavailableto the device,but in practicalterms,it really isn't a problem. The important considerationwhen using a circular buffer is that the averagerate of data removalmust be greaterthan the averagerate of datainsertion.For the printer switchand spoolercircuit,I found I had to increasethe sizeof the buffer to 256kilobytesof DRAM beforeI couldreasonablyexpectto avoid a conditionwherethe incomingdatawould fill the entire circular buffer. At 256 KB, the printer switch buffer could storeabout 100pagesof text,which wasusually more than threepeopleworking on PCscould generate beforethe printer finishedprinting.In most cases, it is not practicalto keepincreasingthe sizeof a circular buffer to meet the expandingneedsof an application; you will probablyhaveto put in somekind of"handshaking" to tell the sendingdevicethe circular buffer is full and enabledata sendingonly whenspaceis availablefor more data.

A s s e m b l y L a n g u ag e R e s o u rc e R o u t i n e s

2t3

[,4

t--t H IAJ

h& Hr fft Fx€

rn

Experiment83-Heading and [Jriting the EEPHOMOata Memorg

F\

rrovf

*-l

Addleaa,

lh€ AfLlleag r c€l to b6 Reaal

vr

STA!!I'S, RPO EEA.DR ^ 0x80 bsf novf

tlt

bcf

rr{ IU

et

FEr t4{ Fll

lei

L*{ $ tqJ

!

!

ar) ffi

Earlier in the book,I introducedyou to the PIC MCU's built-in EEPROM datamemory or in other words a number of bytes built from electrically erasableprogrammableread-onlymemoryandcan be usedfor data storagebetweenpower on or resetcycles of the PIC MCU Accessingthe EEPROM in assembler is a bit trickier than in C whereHT-Softhasprovided you with macrosthat will read andwrite the EEPROM for you.In this experiment,I will present you with the informationand codeyou needto access the EEPROM aswell asdemonstrateits operation usingthe sameapplication,but I'11do so in assembler, asI usedearlier. The EEPROM control hardwareis accessed by four registerslocatedin bank 1 of the PIC16F684. In other PIC MCUs, you will find these registerslocated in multiple banks,which makes the coding of the accesses more difficult.Two of the registersare usedfor passing a databyte at a specificEEPROM addressand are given the labels EEDAT and EEADR, respectively. Thereare two control registers:EECON1 (described in Table9-1)and EECON2,which is a key sequence register usedto ensurethat data is written correctly. Thesefour registersare usedto read and wdte the EEPROM datamemory. To perform an EEPROM rcad,the following instructionsequenceis used: Table9-1 EEC0NIFesister Bils

UJ F € r

Bits

Label

Unused(Return 0 when Read)

7:4 3

Function

WRERR

d!!

Setwhen write operationdid not complete coffectly write enablebit, set beforestart ofwrite operation

(f,"

2t4

WR

Wdte start bit,set to initiate wdte operation

RD

Read start bit

r g€l lhe aaltlr€Fa to Reatl

EECON1 ^ 0:.80, RD EEDAT ^ 0x80, !r

r Loatl in Byte dadalleaa n

at

STAEUS, RPo

Performing an EEPROM write is abit more involved asit requires the two-byte write to EECON2 to allow the wdte to take place.In the sequencebelow, notice that I store the addressin a temporary register at an addressthat is shadowedbetweenthe two banks before changingexecution to bank 1. novf

AaLlleaa,

!t

novwf

0x70

movf bsf noverf novwf

.tg Data, STATug, RFo EEDAT ^ 0t 80 0x070, w

novwf baf movf,

i

Get the Aaldresg lo be R€afl in t Store ahailowetl regisler

0x070 ia aame in Bank regiEter o/L EEA.DR ^ 0x80 t get the aaldreBg to lllile EECON1 ^ 0x80, WREN r gtsarl writso oDeratl.on EEDAT ^ 0x80, w r lJoaal Ln Btrte al "Adlttr€aa. r

bcf, DOp movlw

STA|!US, RPo

movwf movlw mofidf bsf

EECON2 ^ 0x80 0rrAA EECON2 ^ 0x80 EECON1 ^ 0x80,

WR

btfac

EECON1 ^ 0x90,

WR

golo bcf

S-1 STATUS, RPo

0x55

t

gtart of Requileat Sequ€nce

Enal of R€quiled S€qu€nc€ fo! wflte t wait to Cdll)L€ts€ i

This write sequenceis different from what you would seein a datasheet.I wrote it asa generalcase, and it will not return until the write hascompleted. In most applications (like the code usedfor this experiment),I will not poll the WR bit for completion at the end of the write;insteadI will checkit before startinganotherEEPROM access, asshownin asmEEPROM.asm:

l , e l P I C @l l C l JE x p e r i m e n t s f o r

the Evil

Genius

t'asmEEPRoM - Display/Save a value using till"e the EEPRoMtr ; a saveal EEPRoM This program DispLays , on vaLue Unlese lhe Button t RA3 is presseal antl th€n the AI)c is Displayeal. r is ; When t'ha Button ReLeas€dl, the A.DC Value is Saveil in EEPROM. ; This coale is baseit on ; ' t i ; ' r , ,

rlalahrare Notes: PrC16F584 runningr

in

PIckit

1 starter

kit

Myke Pretlko O4.72.2a IJIST R=DEC INCLUDE np16f,684. inc"

t

incf cal-L xotlw btfsc goto InitEEPRoM: clrf movlw catl incf movlvt call incf mowlvt call incf movlw call iIICf movll' call

== otAA?

t EEPRoM(3)

Gootl t Yes' Pattern the EEPRoM t hitialize

!'SR 0 EEPROMWTite FsR, f oxEF EEPRO!4Write FsR. f 0x55 EEPRoIqwrile ESR, f 0r.AA EEPRO!4Wfite FSR' f 0 EEPROMWTiIe

with

t Start

Nothing

Registers

FCr{EN-oEF & rESO OFF & -BoD oFF & -coNFrG & l{cr'RE oFF & -PWRTE oN -cPD oaF & cP-oF!' -wDT oFF & rNtoscro Variables cBLocK 0r{2 0 r, j Display, OldDisplay Buttonstat€ ADcstate. Dlay, TemP ENDC

HArIEEEPROM: 4 novlw mowf ea11 mowwf

;

;

FSR, f EEPROMRead oxaa z sTAlus. IIaveEEPROll

lJoop: movlw movwf novf novwf Displayloop: novf -

PAGE I'tainline org clrf novlw novwf novLr'

O PORTA 7 CMCONO b'00000001'

mov f bsf movlw nltrovwf

ADCONO STATUS. RPO 1 ANsEr, a Ox80

novl$r

b,00010000'

novwf bcf clrf clrf

ADCON1 ^ OxgO STATus. RPO ADcstate Bultonstat€

clrf call xorlw btfss EoIo incf call xollw btfss groto incf call xorlw btfss Eolo

FSR EEPRolrReaaI 0 STATUS, z IniIEEPROM FSR. f EEFRO!4ReaaI oxFF STATus, z hitEEPRoM FSR, f, EEPROMReaaI 0x55 sTAlus, z rnitEEPROliI

i

Turn

of,f

comParators

r rurn on the ADC with: ,Iuslif,ieal : Left ; vdll inB€ead of, vr€f 0 ; channel t Do Bank

1 Inits

ilust RAO as Analos InPut the clock as r select Fosc/8 i

r Init

state

variables

t

; EEPRO!I(2)

-=

0x55?

value

FsR EEPROMReaaI DisPlaY Loop t Displav i ],oop through the 8 t'EDs

7 i Dia!)lay. TemP

Each

of

w

t Get

1:-*

PORTA value

the

calt rlf btfss novll' novwf novlw adalwf, ca1l. bsf movwf bcf novl"w mov$tf, movll' adalt$ btfsc decfsz qoto clrf Inovlw

PORrATabl€Reaal Ternp, f t STATus, c 0 t PORTA 8 t i' w PoRTATableRead STATUS, RPo TRISA ^ 0x80 STATUS, RPo IIIGII ((2000 I 5l DlaY Low ((2000 / 5\ -1 t STATUS, z D1aY. f $ - 3 PoRTA t ! t

Eubvtf btfsc goto

f i' STATUS, C DisplavLoop

sanalle btf6s grolo

Press Button PORTA, 3 ButtonPreaa

or

ctrf btfss goto clrf movlw mo\twf

ADcstate Butlonstate' l,oop Buttonstale 4 !'sR

Releasetl t Button 0 t Ilave Nevt A.Dc value? t No' ,Just Return

t E E P R O T ' I ( 0 )= = 0 ?

r EEPROU(1) == O*FF?

Diaplav Initial EEPRo!'I( 4 )

,

up Temp to

Shif,t Bit

Reset'

Get

TRIS

Test

No write Pattern

+ 256) + 2551 Displav

LED f,or 2 ms

Turn Off r,ED Take 1 AwaY from DisPlaY

, l'oop

Until

i

the

== O*FF

Ignore t RA3 P?esseal?

i Wlite

New value

S e c t i o nN i n e A s s e m b l y L a n g u a g e R e s o u r c e R o u t i n e s

2L5

nlovf cal]. novf novwf goto

Display, w EEPROUWTiIe r' OtaLDiBpfay, Diap1ay loop

movf

ADCStsate,

btfBa goto baf Lncf

SAATUS, z BPADgtalel ADCONo, GO ADCState, f

r Roslore

Display

rnovlw tnovwf bsf bcf letuln

Value

oniAA EECON2 ^ 0x80 EECON1 ^ 0x80, STATITS, RPo

WR

EEPROUREA'I :

goto BPADStAt6l: xollw btfaa golo btfss iEcf

\r

i Wbat La the state?

.eDC Operation r Start to lhe Ne*t ; Increnenl Stale

IJooP t R€aal the

ADCState,

fol r wai! FiniEh

noverf

movr.!'

3iiilll;"I",

o

' saveDiaplavvalue?

7. Butlonslate ADmSH,

mov!'r

Diaplav

clrf

iu)Cstate

soto

rJoop

rd

0 .-{

t! 0) 1d .*{

st6t€

Machine

'r

Values

Contenta ;;;-";-;;;;,-;" Wrile

of,

FSR

EEcolt1 ^ 0x80, wR i waits for EEDAT ^ 0:.80

novr

FsR, w

l*

novwf bsf nop novlw

EEADR ^ 0x80 EECON1 ^ 0x80,

novwf

2L6

i

Stote

EECON2^ 0x80

Woral tso Write

.

37 instructions that the PIC16F684's (and all midrange PIC MCU's) processor recognizes. The instructions are listed in Table 9-3.I recommend that vou trv

The MPASM assemblerdirectivesare listedin Table9-4.Many of the directivesare listedfor completenessand will not be requiredduringnormal programming;thesecasesare markedin the table.Also note that the list directivehasa numberof arsuments: theseare listedin Thble9-5. "Use of this dfuectiveis In Table9-4,the message not recommendeddue to unknownooeration"indicates the directive is carrying out its function by insert-

ing instructions.The instructions usedare unlnown, may affect the contents of other registerq and may

$REN

r stalr

0x55

previous

w'i!e ro cdE)lere

solo s - 1 novwf

:

to memorizetheseinstructions, a brief explanation of what they do,andthe registersthey affect).Youwill be ableto programmore efficientlyand be ableto think aboutwhat instructionand the algorithmto be usedto solvea programmingproblem.

RPO

L) ts{

t PoRTAan'l TRrsA Bit

b , 0 0 0 1 00 , ilt b , 1 0 0 0 0 0 , , b , 0 0 0 1 0 0 , , b , 0 0 0 1 0 0" b'000010' alt b,001111,, b,001111,, b,101011,, b'101011' dt b,011011,, b,011011" b,111001., b'111001'

btfsc

.

the originalvalueis displayedand the lastADC read valueis storedin EEPROM, to be displayedthe next time the PIC MCU is resetor poweredup.

Table 9-2 lists the different parameter options for the

b,010000,,o,tooooo,,JTJtooooi,

w

displayed on the LEDS. when the button is released, '

For Eonsideration

C

EEPROI'twlit€:

The asmEEPROM,asm code is a direct translation

of cEEpRoM.candperformsidenticallyto it (the

f,or

PCIATH ErcH _PoRTAtabLe PCIATH, f PCIATH, !t PCIATE, f

STATOS,

r,f

Dotentiometeron RAO is readbv an ADC and then

ADC vaLue

Table

bEf

AaldreaE Reaal Byt€ at AaLlroaB

, Get

r Ciet Table vaLueE Irigbt Display

inc! pcr.Ar*, t novrdf pcl. I -PoREATable

.rt

EEDAT ^ 0x80, gTATuS, RPo

glore

starter kit,s button (on RA3) is pressed.when the PICkit 1 starter kit's button is pressed,the value of the

a'lfllw r'ow PoREArabre l-t

movf bcf !€EUrn

Previous cor$)let€

t !{ark Diaplay value gav€al

r ReEet

r subroutLn€a PORTATabIeR€aal:

ST.nmS,

S - 1 ASR, rd EEA.DR ^ 0x80 i EECON1 ^ 0t> {) & 0x0F, Rsvalu€) (LCDDala & 0x0F. RSValue), // Shifl out ByEe

LCDWrite ( 0b1100000 0,

= llwollunalrealus

ai

r*

(i

= 0r

< ir

i

Delay

//

i++)r

for

character

(i

fo!

0,

//

!= 0, 1)t

i++)

!{ove CurBor to lhe Seconal lriae i++)

== 1)

//

-

0r

i

I.oop Through PoLliltg GPIO3 anal AN2

< Tw€nlt'mB, i++)t // BaEic Delay

fo!

ADc

main( )

int

i,

GPIO = 0b110011t Inlerface // gtsart with A.DCONo = 0b00001001i

ANSEI = 0b00010100,

CMCON = 0b00000111r oPTION = 0b011111!!t

Bits High // ADc Turneal on: ADI'U - left .tualifieal // VCEG - Valal Reference / / clls - aN2 // QO - Off // A.DoN - On // // ANSEI Specifieal as Tosc = Toac*8 // AN2 - Analog I/ laDut // Disable CofiE)araEor Uodlule // E':abLe pu11 uDs on RAo & RA1

wPU = 0b000011t TRISIO= 0b001100t // Everything Buts cPIo3/aN2 //

CiODoNE = 1t // Stsart ADC operatior == GODONE)t whiLe(1 j = (A.DRESH >> 4) & 0x0F, // Ploc€ss Usins ALgolithm Above j = ((j & 0x08) a L,-{

!

fq

discoveredthat the remotecontrol can"bounce"its output signaloff differentobjectsin the room (e.g.,the wall behindyou) and the TV will respondasif you werepointingthe remotecontrol directly at it. This is the theorybehindthis experiment;asyou canseein Figure10-19,lightis bouncedfrorn an IR LED to aTV remotecontrol via someother obiect.Earlier in this section,an IR white/blacksensorperformeda similar function,but the distanceat which it works is limited to a half-inch(1 cm) or so.The circuit presentedhere can detectobjectsasfar asseveralfeet awayand will evengive you someidea of its distance,asyou will see in the next experiment. Consideringit costsjust a few cents,the TV remotecontrolreceivers(suchasthe SharpGP1UD series) are amazinglycomplex devices.They continually monitor the incomingIR light andrespondto a signalthat is modulated(turned on and off) at 38 kHz. Insidethe TV remote-controlreceiver,the input signalis processed, the and if a 38 kHz signalis encountered, open collector output is pulled low, which allows multiple receiverson the samecircuit. Amazingly it will filter out any constantsignalsand continuallyadaptto its environment.What I discoveredwith the TAB Electronicsrobot is important:It will learnto filter out any continuous38 kHz signalasnoiseif it is left activefor more than a few millisecondsThis is why in Figure1019,I havedrawn a PWM signalthat allowsthe 38 kHz signalto be passedto the IR LED only periodically. Figure10-20showsthe signalsentto an IR LED and the responsefrom a TV remote-controlreceiver;the in the output is delayof severalhundredmicroseconds simplytheTV remote-controlreceiverrecognizingthe incomingsignaland then recognizingthe signalhas stopped. To demonstrateusing an IR LED and TV remotecontrol receiverfor objectdetection,Icameup with the circuit shownin Figure10-21.It'swired with the IR LED bent over and pointingin the samedirectionas the TV remote-controlreceiver(seeFigure10-22).The

IR LED hasa f-inch (2.5cm) pieceof black heatshrink tubing placed over it to direct the IR waveform away frorn the TV remote control. The circuitry shown in Figure10-22is actuallythe circuihy for the next experirnent.The four LEDS are usedto determine the rangefrom the circuitto anotherobject.Only one LED is required for this application. The 1k resistorandpotentiometerusedin the applicationis to limit the amountof currentbeingpassedto the IR LED and thereforeto limit the light output from the LED which will help set the point where objectswill be detected.Thisschemeis often usedin robotsto limit the detectionto 1 foot (30 cm) or so, eventhoughit is not really a recommendedmethodto A better way will be limit the detectiondistance. shownin the next experiment. The first application for testing this circuit is asmlR.asmand simplydelaysfor 50 ms the sendingof 10pulsesto the IR LED. As the signalis beingsent, the TV remote-controloutput is beingpolled,and if the signal is active for two cycles,then the application acknowledges an objectis in front of the circuit. tits1e

"asmlR

Your

own IR

Obj€ct

Thi6 Progr€m Outpul6 a 38 KHz Sigrnal (25 |rs Periotl) aigual a for 10 Cyclea anaMonilolE IR Tv Rdlole Contsrol Receiv€! for reflectionE.

r t t ; r i

Haltlware Notea: PIC15F68{ running at { MHz using the Inlernal Clock Input RC{ - IR R€ceLve! Rco - rnaicator rJED RC5 - IR IJED OulDuts

t i

Myk€

Pretlko

[nnnlnnnt ltltL|il1i1;*__ -IR LED

! 1

a lR LED

0.5%OutyCycle PWMRunninsal20 kHz

OpaqueBarrier

,rd { r

Contrcl Signal 10k?

te{ !c.c

RoIl

i r , ; r

m irt

-

#

ig

**:*:T

5

Reflected SignalReception



p u H lJ.oDelctll

Figure l0-19

244

IR detectortheory

i*

5 v

Figufe l0-40

l , P 3 P I C o l l C U E x o e ri m e n t s f o r

100.tr9

IR operation

the Evi I

6enius

Iroop: novlw novwf movLw atltllw blfac flecfaz goto

Fisure l0-al

clrf

IR detectcircuit

HIGH ((50000 / 5) + 256, Dlay Low ((50000 / 5l I 256) -1 tbe ; Wait to Repeat , Test STATUS, Z Dlay, f S-3 ; 5 C-ycle Deley I.ooD i for 20 ma l 20

movrdf IRLoop: goro goto btfaa

PORTC, 4

.T; l l

xo!!rf

Figure 10-22 Breadboardcircuit usedto detect objectsand measurethe distanceto them ,

05.01.03 IJISI R=DEC I N C L TD B n p 1 6 f 6 8 4 . i a c n

1 < < 5 PORTShaalolr, PORTShailow PORTC

fl€cfaz goto

IRIJooIt

novlw aubwf

4 j,

novf aEallw btfsc lorhr lrovwf novwf

PoRTghatlow, or= {

2 -

Ro11 Your

F

o v

OleD IR

o n

r+

a

o

3

a

o

r-J

Obj€ct

llhia Progrem Oulputs a 38 KHz Siqlal (26 Ea 9erioal) .ignal for 16 CalcIeE uEing the ECCP Ptt!{ a!41 Monitora a IR fV Remole Codtrol Recel,v€r for refleclions. Ealalwale !{otes3 PIC16E584 rurr.oing al 4 llHz Using the Internal Clock RC{ - IR Receiver Input RCo - htlicator LED

S e c t i o nT e n S e n s o r s

o

H

w

I'aEnIR tille Detectorn

C!{CONo STATUS, RPO INSEI. ^ 0x80 TRrAC ^ 0x80,

clrf

ICD Debug

PORIA PORTC 7 i

novtrf

For

3

rF

Although asmlR.asmworks perfectly,Iwantedto come up with a way of performing the sameoperation using the CCP's PWM c cuitry. Instead of having to come up with the 38 kIIz signal for the IR LED in software,the PWM can do it and tell me when it is complete by using the TMMIF flag and the 16-times postscaler.This has some advantagesthat will become apparent presently.The changedapplication is called asmlR 2.asm.

uainlirre

l-r.

anfl

€nd

vari€lc1€s CBLOCK 0x020 i, j DIay PORTShadlolt ENDC

Ff

X

& _BOD_OFF & _colltr'lo _ECME![_oFF & _IESO_OrF _CPD_OAF & _CP_OFF & _!'ICLRE_O!F & _plfR!!E_ON & _}IDT,OFF & _INTOSCIO t

(D

A/2

w

t LooD

rrl X ,rd

245

RC5 -

15

Rua 1'MR2 for cycLes

b,01111100'

IR LED Outpu!

!!2CON $yk€ Prealko 05 . 01. 03 LIST B=DEC rNcrJr[rE rp16f 684. incn -CONFIG _FCIIEN OFF & _IESO_OFF & _BOD_OEF & _CPD_OFF & _CP OEF & -!,!CLRE_OF!' & _PVIRTE_ON & _WDT_OFF & _INTOSCIO

*"4

t

Variabl€s CBI,oCK 0x020

DLay PORTShadlow ENDC

ni

v*

t

STATUS, RPO TRISC ^ 0x80, STATUS, RPO

cl!f

i

IRIJOOP

b!fsB

PORTC,4

incf btf,Ba

PIR1,

bBf bsf

0 nrovwf r For

:--

,'.-Jl

rlovlrtr

rci,tt !

PORTC 7 cltcoN0

b'00001100'

mowf movl$r

ccPlcoll 13

Enable ECCP

P!0tiI Modle of

CCPR].L

b..:i

RPo sTAllrgs, ANSEL ^ 0x80

mov\tf bcf

PR2 ^ 0x80 TRISC ^ 0x80,

bcf

STATUS, RPO PORTShaalow

0

i visible r Output

r,ight

r,ED

clea! lhe PoRTc OulDuts Value

Repeat tbe

i T€EE

i; i:.j

s

STATUS, D1ay, f

cLrf bcf

TMR2 PIRI,

t:1

Z 5 Cyc].e DeLay for 20 ns

TIIR2IF

fo! out

IRlooD STAITUS, RPO TRISC ^ 0x80. S|!ATUS, RPo

5

i

IR tED Output

r Tllrn

b,01111000' T2CON

off

TMR2 Off

30

movf anatlw btfac iorLw

PORTShatlow, o:tFF ^ 1

goto

IJooD

ahilty Carry

PolLs if

Low? Four

Sel

rs TUIN

ON LED?

sraTus, c 1 PORTShaalow POBTC

Carry

Set,

j >= {

CI€ar EMR2 Reaet Inte:arup! Requeat Flagl

Loop

By usingthe PIC MCU's built-in PWM, I have come up with a method of performing this task that doesnot needcountedassemblylanguagestatements to createthe 26ps-periodwavesentto the IR LED. Becausetheseassemblylanguagestatementsare not required,the applicationcould be written in C (as dernonstrated in cIR.c). In cIR.c,you may havenoticedthat I checkthe poll count for only a valueof 15 insteadof 30,asin the asmlR 2.asmroutine. The reason for this wasmy useof the 16-bitvariablei asthe counterinsteadof an eightbit variable.I assumedthat the codewould take approximately twice as long to executewith a 16-bit counter as it would with an eight-bit counter.The asthe codeworks assumptionmustbe reasonable, without anv issues.

.ft ** *1

X i *--1i

246

TUR2 to

end

Hrcn ( (50000 / 5) + 2 5 6 ) Dlay I , o v r ( ( 5 0 0 0 0 / 5 ) + 256, -t r Waits to

btf,ac flecfEz goco

Wai! Time

, get I'uR2 Perioal

IrooD:

adatlw

ON

"j" eount8 the Active IR 8 cycle6 antl Output Count IJow Rx Don,t. If RC4 Eigh, Irlc!€dr€nt coudt

TIIR2IE

lrovlw aub$f

get th€ Pvt!! output Reael Conlrol

baf clrf

I,ED OulpuI

ICD Detrg

Turn off Cof,E)arators

movlrd

IR

!

go!o

PAGE Mairline org

bsf bcf bcf

l , e 3 P I C @l l C U E x p e r i m e n t s f o r

the EviI

6enius

t{ tt

ExFeriment 93-lH Obiect-Flanging Sensor

tn

ils

tv

t1 Fr.

P r c 1 6 F 5 84

H {D x

38 KHz IR TV remote!eceive! control 1 lR LED 4 LEDS I0k

resi sto!

1k res istor 4? of,) resistors

(A)

100O !es isto! 0. 01 pF capacitor

DMM Needle-oose

CapaCl't,Ol

Breadboa!d Breadboard

wiring

!,

4? /rF electrolytic

pliers

H

Smn bLack heat-shrink tubing I to 1.25 inches (2.5 to 3 cm) in length

kit

w o tf

1 Breadboatd-mountable SPST snitch I

Thlee-cell c1ip

AA battely

LJ,

3 AA batteries

In the previousexperiment,I useda 1k potentiometer to limit the amount of curent that passedthe IR LED. By limiting the current,I dimmedthe LED's output and shortenedthe detectiondistance. The problem with this methodis that it is not very reliableand would be difficult to reproduce in a manufacturing setting.If you look at aTV remote-confol receiver's datasheet, you would discoverthat asthe IR LED's modulatingfrequencychangeqso doesthe sensitivity of the TV remote-controlreceiver.If you assumethat a brighter signal is required for the TV remote-control receiver to recognizeit (when the signal was different than 38 kHz), then you would alsoassumethat a closer objectwould producea brighterreflectedsignaland would only be detectedat a closer distance.The purposeof this experiment is to test this hypothesis. To indicatethe basicdistanceto an object,I modified the circuitry from the previous experiment (see Figure10-23).Theactualdifferencesare minor;the 1k potentiometerhasbeenremovedand three additional visible light LEDs and three 470O currenflimiting resistorshavebeenadded.TheseadditionalLEDs are usedto indicatewhetheran objectis detectedat different modulatinsfrequencies.

o

t! The basefor this experiment's code (asmlRDist.asm)wastakenfrom asmlR 2.asm.One of the reasonsfor using the PWM to generatethe modulating IR signalis that it allowschangesto the values passedto it conditionally.By taking the delayand IR objectdetectcodeof the previousexperimentand placingit in a macro,I wasableto comeup with a code

ff

F

11! !"t (A'{

dJ,

r{

'R LEO Sending38 Kh2 -

\\\

tlt

l|l

,/,/l

///

q

({r=

'A

/n

D

4

Lighl

m

l/R 38 Khz

t{ Figure l0-?3

5 e c t i o nT e n S e n s o n s

IR rangingcircuit

247

novhr novlrf bcf

pelioal Plt2 ^ 0x80 gTATuS, RPo

t

S€l

clrf bcf

1'![R2 PIR1,

nrovlw

b,01111100,

I'MR2 Clear Reget Int€lru9t Requeal FIag Run 1l{R2 f,or 15 cycl€a

fhiE Program outputE a drltl-perLotl (atarling at 38 kxz sigmal, 26 lra Perlotl antl golng to a 31 llEz signal, 32 Ea P6!Lot[) aLgaal fo! 16 cyclea uging tsh€ EccP Ptgll aad l{onitsors a IR w R€trole Control R€ce:iver for reflecliong. D€penallng on whLch gig|ral Proaluced a reflectioa, e gpecific LED !ri1L be lit.

t i i r r

novwf

T2CoN

bsf bcf bcf

STAIIUS, RPo TRISC ^ 0x80, SIIATUS, R"0

t

IR

cIrf,

i

btfsE

PORTC, {

![he aclual frequeacy, Pelc€nt off Pelc€Dtag€ andl Rec€Dliv€ ia lidted in the ltaclo invocalionB

ndBinal

blfaa

PIRI,

lilacroa ar€ Nola! this apDlicabion

ir1

basethat could test different modulating frequencies easily aswell as ouFut object detectionsat thesefrequencieson different LEDs.

${

o

"aBnIRDist tille IR Objecl Detectori

n c o

a

Ut g .F{

ttt

c

-

lteasure

ua€tl

Distance

ext€nsively

with

bhe

goto

Eeralware Not€s: PIC15F58{ ruDniDg at { MHz Uaing the Int€rnal Clock - IR R€ceiver RC{ Input RC3:RCo - Inilicato! LEDa - rR r.ED Output RCs

d

€,

I

fl{R2IF

{J

5

uyk€ Preatko 05.01.07

.a

o & H

I 0a O\ {J

c

(,

g

.l-l

f{

IJIST R=DEC rNcrrtDE .D15f 584. iacn

t

VariableB CBIOCK 0x020

,

Dlay PORTShatio!. E}IDC

11{R2IF

t Wail t fime

STATUS, RPo TRISC ^ 0x80, gTATug, RPo

movLv nrovwf

b'01111000' T2CON

r Turtt

nov].w auM

30 i, !t

r Thirty t carry

mov! a'1tllrr btf,sc iorlw

PORTShatlorr, w oxFF ^ (1 CycLe, th€n

w

Reaal the

t OriginaL i Proceaa i

Fofirarala

i

teaa

vt 0

r TRISC Foll'arats t Fot-walals or Revefse? r TRISC Reverse

C r Turn

off

lilotorB?

R.Po 0xg0 RPo tshe Plgtt t Increnent i CycLe Count , M6ximrll of 32 Stat.eg i i

Finiahetl, Again

IJoop Alountl

*iaclual€ cuot-ot 2 - Control /* Pot€ntsidretet

Statse A.DC Value

than

Reverse?

0x80

go in

Eh€ VaLue r Conv€r! r from 7 BitsE

using

a

Thi6 Proglam llonitols a Pot at RA3 (!rA3) aad moves a DC Motor Accordingly. Values leEs than 0x80 nove the motor in leverse gl6at,e! whil€ valu€6 than 0x80 nov€ the rnotol fonsarals. A 1 ma looD (proaluceit by I'MR0 ) is u8eal to Droviale a 30 Uz PWU for toy nrotors

A.DC Value

ol

a DC Uotor

Harahrate t{otes 3 PIC16F6g{ running at { MBz Ugilrg the Int€rDal clock nA4 - Pot Cdmand RC5/P1A - Motsor Forwatalg Elgh RC{/P1B - MoCor Revera€ P}ru (on Low) RC3/P1C - lilotor Reverae Eigb RC2IP1D - lloto! Forwartls PWI,I (on lJolr)

rryke Drealko 05.01.10

fi

t*i 262

Value

I createda C versionof this applicationcalledcMotor Z.c.

Reaal ADC?

llotorRev€rse

PwltDuly, f, STATUS, C PtCDlDuly, f

!h€ t Convelt ; f rdr 7 Bita ; to5

enal

ADC Readl , Stalt t Ne:{ts State i

A.DRESE, w Alcvalue A.DCState

lfotorForwarale: novwf PWuDuty bcf STATUS, C rrf bcf, rrf

,

, Cheesy 7 Bi! Negatio! , of the CCPR1IJ Value i Revers€

Timer

gfoEo bcf

Plirllurtsy, f STATUS, C PIgUDuty, f 1 Directiou

Uotorlrpdale:

TMRo aa a Plitlt

monwf bcf

r r r i

ADCValue, w PI{!4Duty SltATItS, C

Fonraitlg

AIrc on AA{

STATUS, RPo b,11010001, , 1:{ prescaler to TMITO OPTION_REG ^ 0x80t 1 > 2t

fn

)

nain ( )

t

F.{ // PORTC = 0b010u1t valueg // PORTC to Conlrol CI{CONo = 7, otf ConDalatsors // \Jt]r ANSEIT=1 2t

, // fr

".fl

if, (0 == Dilection) TRISC = 0b011011; // Enable Forwaral €1Be rRISC = 0b100111; // Enable R€vers€

A.DcoNo = 0b00001101t on lhe nDC // firln .tuslifieit // Bi-E 7 - teft Sanpl€ 5 - use vDD // Bit ll BLE 4r2 - rrA4 / / BLE ! - Do not Slarl oa ADc // BiE O - Tuln aDCONI = 0b000X0000t aa Foac/g // selecE the clock

if

t Bits

F

Bite

H

( Pll!,lcyc1e >= Ir9{!'Duty} TRISC = 0b111111t // StsoP !4oto!s PflUcycle

= (Pwt'tcycle

+ al

! { } % 32,

1.uR0 = 0 i // use T!{Ro for oPTIoN = 0b11010001, // 1:4 Proacal€r

a 1 ma D€lay ) to

uae

rnt€rrupts

EIag

for

1 ms

whl.1€ (1 == 1)

t whiLe ( !TOI!) t r!|Ro to overflow // Wait fo! ToIF = 0t // Reaet f,or Nex! 1 lla D€Iay (0 == .aDcstate) or / / sEart

if

R€adI aDc?

t

GODONE= 1t // gtart ADcgtate = 1,

ADc

// elihw Enal cMotor

5,,i;tr 2

T!!Ro

ToIF = 0t //

) //

This code is much easierto follow than the assembly codeversion.But if you look closely,you will discover that the C code specifyingthe on/off of the motor works differently, and even if the PWM has a duty cycle of 0 percent (fully off), the motor driver will be on for a very short time (just a few g,s)before the softwaredeterminesit shouldbe turned off cornpletely.The reasonfor the difference is primarily an oversighton my part. I discoveredit only whenI comparedthe operationof the two programsline by line after seeingthe very shortpulseson my oscilloscope whenI looked at the operationof the two applications.

S e c t i o nE l e v e n l l o t o r C o n t n o l

263

L; ...-: r."1.

:t

n

Experiment 98-ControllingMultipleMotors urith PIIJMand 852 lnterface

[.1

o

i=r

A

I fil

1 L293D moto!

.-f

&

DC motols 1 Fou!-celI clip

F{

DMM

tn

Needle-nose pliers Wire clippers B!eadboard Wiring

F{

kit

,-|

o

U I

@

AA battely

One of the pointsI madein the previousexperiment wasthat multiple motors and complexcontrol softwarecouldbe addedto the 30 Hz motor controlPWM software.This is due to the relatively large number of rycles the code spendswaiting for TMRO to overflow beforeupdatingthe motor PWM code.In this experiment,a secondmotor is addedto the control codethat executeswhile waiting for TMR0 to overflow and that polls for the BS2instrumentinterfacethat wascreated in the previoussection. Insteadof repeatingthe build of the four-transistor H-bridgeusedin the previoustwo experiments,I

decidedto usethe L293D chip,which consistsof four two-outputlevelmotor driversasshownin Figure 11-8.Thechip includeskickbackdiodeson the outputq so you do not haveto add them to your circuit,and is designedfor driving two motors with an extra PWM output control (pins1 and 9).But for most applications,I simplytie theseinputshigh and control the operationof the motor by changingthe level of the control outputs.The chip runs on a 4.5-to 6.0-volt powerinput (Vcc on pin 16) and canswitcha higheror lower voltage i4put on Mot Powr pin (pin 8) .This makesthe L293D usefulin a lot of applications. There are three things you should be aware of with the chip (1) The 0.7-voltdifferencebetweenVcc and Gnd and the selectedoutput level is due to the bipolar componentsusedin the manufactureof the chip;(2) The chip will dissipatea fair amount of heat when a

ot

nl 'Fl H

P.{ f\

t*i

batteries

P I C 1 2 F 6 ? 5 o r P I C 1 5 F 5 84 - b a s e d BS2 cotunand simulato! inte!face

s

c

chip

(see text )

AA alkaline

p

i{

dliver

1 B!eadboa!d-mountable SPST svritch

"-l {J

o

P 1 C 1 6 F 6 84

Figure l'f-9

Figure ll-8

Dual BS2 motor

264

l , e 3 P I C o l ' l C l JE x p e r i m e n t s f o n t h e E v i l

Dual DC motor H-bridse

Genius

motor is running(causedby the 1.4-voltdrop through the high and low pinsmultiplied by the currentpassing throughthe motors).Thetotal amountof power the chip can dissipatesafelyis 3 watts.It is a good idea to provide copperPCB heatsinkingto the chip via large fill or f'lood areason your PCB connectedto the groundpins to wick awayasmuch heat aspossible.(3) The L293D hashad spottyavailabilityover the past five years,with somemanufacturersdropping shipmentsof the chip while othershavebeenrampingup. The internal kickback diodes make the chip a lot more (alsoknown asthe L293), usefulthan doesthe 75,1410 which is pinout cornpatiblebut doesnot havethe diodeson the outputs. Onceyou havewired the circuit shownin Figure1108 on a breadboard(seeFigure11-9)and connectedit to a BS2 or simulatorapplication,you canbum asmBszMotor.asm into a PIC16F684and test out the oDerationof the two motors.

The BS2 comrnandand responseoperation is quite simple with values0x00to 0x3F being usedfor Motor 1 and values0x40to 0x7Fusedfor Motor 2. Like the previous applications,the middle values (0x20 and 0x60for Motor 1 andMotor 2, respectively)stop the motors,valuesgreaterthan the middle will causethem to turn forward,and valueslessthan the middle will causethe motorsto turn in the reversedirection.Like the previousexperiment'scode,the PWMs built into this application will have higher duty cycleswhen the motor valuesare further awayfrom the middle point. The motor software shown here could be usedasis for a BS2-controlled differentially driven (two motor, one on eachside)robot or be the basisof a differentially driven robot that is controlled using the single PIC16F684.The motor control code and PWM cyclecount incrementtake only Z cycles,leaving a thousandcyclesfor sensorand controloperationsof the robot.

14

X

U

o

,>

H.

:t

tU H l E

\s

taa ! I

w f-, "

ry (J

Experiment 99-Bipolar 5tepperMotorEontrol

H

frr

t$

m

,*&

P I C 1 5 F 6 84 1 L293D moto!

dlive.

chip

1 10k breadboard-mountable potentiometer I

}15 m

0.01 F!. eapacitor

1 B!eadboa!d-mountable Bipola! steppe! (see text)

DMM Solderiog

j.!oo

Four-cell crip

solder

AA alkaline

Needle-oose pliers

Klazy

,3

moto!

K

AA battely

LJ

battelies

1 Four-pj,n 0.1oo-inch header (see text )

Sci sso!s Glue

Ft

Ca!dboa!d

Wire clippers

( ,

o r"t

B!eadboard Wi,ring

kit

Steppermotorsare very populardevicesfor a variety of applicationsbecausethey canbe turned a specified amount,do not requireextemalgearing,and are generally very simple for which to designdriver circuitry.

lu

In this experimentand the next,I will introduceyou to the two mostcorrmon typesof steppermotors,unipolar and bipolar, and then to the driver circuitry and PIC MCU software that will first simply turn them and then

SectionEleven I ' l o t o n C o n t n o l

265

$

tst

l'! t r i !

{:

!J

s3,i rl t!

fft

tr-{ j-: &*i

.r,i

a I

control them usinga potentiometer,asI did with the DC motorsat the start of the section.To help you understandhow the softwareworks,I will first present you with C codefor driving the steppermotorsfollowed by assemblercode.

one or two coils be energizedat any time. Full stepping movesthe shaftby 90 degreesat a time,and only one setof coilsis energizedat any time.

Steppermotorshavea few notablecharacteristics: They consistof four (or more) coilsarrangedperpendicularlyto eachother (seeFigure11-10).Thesefour coilssurrounda magnetizedshaft,whichwill be either attractedor repelledwhen the coilsare energized. To turn the steppermotor, lhe coilsare energizedin a pattern that will causeit to turn in one directionor another.Becauseof the time requiredto energizethe coilsand becauseof the inertia (aswell asany load resistance)of the shaftand reductiongearingplaced on the shaftoutput,the speedof the steppermotor is much more limited than that of the DC motor.The reduction gearing reducesthe movement of the motor output ftom 45 or 90 degreesfor eachchangein position of the shaftto just a coupleof degreesor so to maximizethe torque output of the motor.Along with the slowerspeedof the steppermotor,the needto keepat leastone coil energizedat any one time will draw more currentthan doesthe DC motor. On the plus side,steppermotorscanbe moveda precise amount,and they producemuch more torque than doesa DC motor. There are two types of stepper motors commonly in use.The bipolar steppermotor will be presentedhere and consistsof four coils around the magnetizedshaft (seeFigure11-10).Thebipolar steppermotor canmost easilybe identifiedby four wirescomingout of the body of the notor with pairsof wiresconnected togetherthrougha smallresistance(whichis causedby the coils).The high-currentpush-pulldriversof the Hbridgeare usedto alternativelyturn the two setsof coilson and off aswell asturn them to differentpolarities.In Table11-1,I havelistedthe poladtiesfor the different coils to move the shaft by 45 degreesat a time.This is known ashalf stepping andrequires that

Table 11-1 Half-stepEoil EnBrsizationPatternfor a Bipolar 5tepFer Motor Step

Up-DouJnEoll

East-Wesl Eails

South

otr

South

South

otr

South

North

South

North

off

North

North

otr

North

South

North

InTable 11-1,the North and Southspecifications are arbitrary and are usedto indicatethat the polarity of the coils'magneticfieldschangesover the courseof the sequence. Also note that in Table11-1,I have emphasizedthe text of changingcoil (one coil changes in eachstep). To test out the informationin Table11-1,I createda circuit (seeFigure11-11)to drive a bipolar stepper motor andwired it on a breadboard(seeFigure11-12). With the steppermotor that I used,the connector attachedit to a doubleinline connectorthat could be pluggedinto the breadboard(similarto the onesused in the servoexperirnentselsewherein the book). Chancesare you will not be so lucky and you will have to solderthe individualpins to a singlepin inJine headerthat canbe pluggedinto the breadboard. Before burningthe PIC16F684with the following software,I suggestyou cut a sliverof cardboardasa pointer and Krazy Glue it to the end of the stepper

i.,r i

StepperMotor

:.-r **

ivj

-.:

{ii

j l

il . -oxi i1i

Figure ll-10

Bipolqr steppermotor control

266

l , A l P I C @l l C U E x p e r i m e n t s f o n t h e E v i I

Figure ll-'fl

Bipolar steppercircuit

6enius

PORTC = 0t CMCONo = ?t of,.E codrparalora / / $ri'r ANSEI = 0t // Turn off ADC TRIsc = 0b000011, // RCs,RC2 OutDulB

14 t

t NOP()t for (j = 0, NOP()t

j

< 21000r j++)i

PORTC = StepperTalrle i

Figure lf-le

Bipolar steppermotor testcircuit |

motor'soutput shaft(seeFigure11-12)so that you can clearly observethe movement of the stepper motor. When the circuitis built, you canburn a PIC16F684 with cstepper.c,which takesthe informationfrom Thble11-1and usesit to createa simpletablefor halfstepdriving the bipolar steppermotor. In between steps,thereis a quarterseconddelay,and if your applicationis wired correctly,you will seethe pointer you gluedto the steppermotor shafttuning through360 degrees(a degreeor so at a time).If you do not see this pattern,you will haveto rearrangethe wireson the breadboarduntil the motor startsworking correctly.(Do not desolderand resolderthe leadson the 0.100-inchheader.)

*incluale - Tula cstepper.c /* ilhiE

Plogj.aln

is

a slel)pe!

Earakcar€ l{otea 3 PIC15E58{ Rurrdinq at { ltHz wilh oEclllator RC5:RC2 - St.eDDer Uotor Outl,uls

t

+ 1) % It

\o

// efrnw End. cstepper

\CI

The assemblylanguageversion of cStepper.cis asrnStepper.asmasfollows, Knowing that the code would neverbe longerthan 255instructions,I decidedto use a basic,not the general-case,table for the stepperpositions to simplify the coding. tiEle llotor

"aarsteDDe!

-

Prc15F684

Bipolar

steDlrer

Program OuEDutsa a a€w Bigolar g€qu€nc€ once every 250 ns.

b

t{

m (}

asmn. IJIST R=DEC rNcritDE .p15f 584. Lacn

s

Irternal

_coNFrc _EqlEN_oEF & _rEso_oFF & _BoD_og! & _CPD_OTE & _CP_OFF & _!{CI;RE_OFF & _PWRTE_ON& variabl€s CBLOCK 0x20 DIay, i E![DC t

-CONFIG ( INTIO & IIIDTDIS & PVIRIIEN & !{CI,R.DIS & I'WPROTECT \ & InIPROTECT & BORDIS & IESODIS & FCMDIS) t

= {0b011100, 0b010100. 0b000100,0b100100, 0b100000,0b101000, 0b111000,0b011000)t

r

PAGE uainLia€

uop

For

movlw

|

movwf

PORTC

CoilContfols

r*

e.j

Figure 1'f-13 Unipolar steppermotor control

Section Eleven l ' l o t o r C o n t r o l

269

can seethe six-pin header to which the six unipolar stepper motol wires are soldered, with the common wires (found with a DMM resistancecheck)being placedin the middle of the connector.Along with soldeling the steppermotor wires to a headcr,you should also Krazy Glue ir cardboard pointer to the shaft of the steppermotol to observeits motion when you tcst it. Testirg the unipolar stepper motor is accomplished in exactlvthe same way as testingthe bipolar stepper motor: cStepper3.c will sequencethrough the coils, hopefully moving the caldboard pointer continuously. Again, if it doesn'1,n]ove the wircs to tltc control transistorsuntil it cloes.A simpler way of testingand decodingthe wiring is to touch the baseconnection (through thc l00O rcsistor)of cach transistor1()the Vcld rail of the breadboard.This will tell you which cir'cuit is wir.edto which pin. And thcn you can begin attachingthen in sequence,startingat:

lnain

*include cslepper 3.c /* This

Progra$

is

Figure lli5 Unipolor stepper motor circltit using NI'N tt ttnsistors as motot' contrel drivers

- turn based

Earalware Noles: PIC15F584 Running Oscillator RC5:RC2 - StePPer

at

a unipolar

Stepper

on " asmsteppe!

4 MIIZ with

Moto!

Mo€or

()

t POR4C = 0; CMCON0 = 7, ANSEIJ - 0, TRISC = 0b000011t

. asm" .

lrrternal

while(l Outputs

== 1)

// // //

r\rrn off conparators ?urfl off ADC RC5:RC2 Outputs

//

Irootrt Folever

{ NOP()i NOP()i

CONFIG(INTIO & WDTDIS & PMTEN UNPROTECT \ & UNPROTECT & AORDIS & IESODIS

= (Outputval Outputval" & 0x3C) ((1 Ilele for Each t LSB of, Value S€t?

Bit

f t R€p6aE for

andlw

1

r Only

goto

S

i

eacb

IJSB iE

Finigbeal,

bit

Parity

Haff adder

Bil

Iroop Forever

enfl

This methodworks acceptablywell,but is another casewherea bit of knowledgeor binary numbersand

In asmParity2.asm,this function of the half adderis exploited (along with shifting bits) to produce a parity bit in the samenumber of instructions,executingin one-sevenththe numberof instructionsand usingone lessfile register variable than the original. Despite improving how the parity calculation softwareworks,asa tool,it's pretty limited.Theparity bit will detectonly onebit in error in the packet.With two bits,the parity bit will indicatethe data is correct. Additionally,when an error is detected,the incorrect bit is not indicated,nor is there any correctioninformation included.Thesefunctions are critical features of modern error detection/coffectcodes(ECC) used in high-speedcommunicationsand peripheral interfacins.

?

,

Experiment ll0-5ort a List of l0 Eight-BitValues Usingthe Bubble-Sort Fllgorithm

# +-'

After the time and effort spent in Section4 describing different waysto implement a bubble sort in C, you may be hopingthat I won't havea lot to sayaboutcreatinga sort in assemblylanguage. You'll be happyto hear this is true:Theassembly-language bubblesort (shown as follows) is a quite simple and faithful translation of the C languagealgorithm

{,

tr H

!d!". a-6

laq

292

l , e 3 P I C @l l C U E x p e n i m e n t s f o r

the EviI

6enius

,'aanbsort

lit].e

t ThiB program Sort 10 values t

1 -

ua€s

"Cn Equivalent

r for r

(i = 0r SoltliBt

solt

!h€

10 8-Blt

Bubble

sort

gotso

valu€5" Algorlthm

INDF,

tlecf addwf

FSR. f INDF, f

FsR, incf outsaitlel,oop_skiD

i < 10r i++) = Reaflvaluei [i] // R€ad in lhe

Een Byte

, i t

f,

i ELrats = Eirat r - Filat)

Heldwa!€ Notea: PIC15F584 rururiag

t Finisheal,

I

t ats th€

4

uHz

ia

+ (Seconal tl,

Increlrents

i, rtr goltl,iat

Enal of

novf aaLllw aubwf btfss

ESR, rt STATUS, Z

golo tlecfEz goto

OutELtl€IJoop i, f InsialelJoop

t Don€ 9x?

goto

$

; fo!€ve!

r Return Gelvalue3 at

ffi ld

-

f,

i t

llfr

,

sortlJt8t [j + 1l Seco al = g€confl (secoaal - EiIEE)

H

'jn

VaLueg

, f o r ( l = 0 r i < 9 i i + + ) // Outaial€ Sort LooP , f or (j = 0, J < (9 - i ) t j++) i l/ IttBiale Sorl LooP t (SorEr,iBt [i] > SortLial if ti + 1l) t // l|ave to SrtraD valuea r { + 11, T€mD = Sorll,islli r gortli8t r [i + 1l = Sortl,ist [i] = l[edl!), SoltlJist t lil

; t

subwf

to

Coale 3

Outsial€loop_skip

the

VaLue

for

lDtlex Liat?

at

bh€ rJiats?

tbe

End of

LooD

- Done

the

"i"



( (_Gelvalue & 0x0100) != 0) baf PCIJAIIH, 0 edttif ( (_cetsvaIue & 0x0200) l= 0) if PCIATII, 1 baf, enflif ( (_cetvalue if & 0x0{00) != 0) PCIATII, 2 baf, entlif aalallw 10$ _GetvaLu€ r GeE Offsets in 255 i AaLlr€aa Block btfac STATUS, C Pcr,alrfl, 0 incf mon$f, PCI. the ilumD inlo r Pelform r the Ta51e Getvalue: 10, 100, {, 15, 75, 150, 47, 2, 25O, ilr 475

!8ike

Prealko

r 0{.0{.05 IIST R=DEC rNctnDE np15f 58rl.incn , valiabl€s CBLOCK 0x20 Sortl.iBt | 10

; IJist Sort€dI

of

valu€s

!o

be

ENDC PAGE , Mainline

orlt

of

Bubbl€

Sort

- I"bfk€

I t t

(*;

tf,

a \ r

s

0 enal

clrf novlw novwf loaalloop: novf ca].l movwf incf, incf movf sublw btfsa goto

i

by loaaling i Starl , SortsLial , Setup FSR f,or IDdir€ct t Aaltlreaaingl

gortT,ist

I'd like to note a couple of discussionpoints. If you code,you'llsee work throughthe assemblyJanguage that it is essentiallya straight translation from the C languageprototype with one difference.When I coded the application, I discoveredthat when subtracting the current array value from the next, I could use the value in the WREG (Sortlist[i + 1] - Sortlist[i]) to simplify swappingthe data between the two array elements.

ESR r Retsurn H€re UntiL Loaaledl r Valuea i, w qelvalu€ INDF FSR, f i, f i, rt 10 STATUS. Z Loaalloop

movlw 9 novwf i IDsiflel.ooD ! tlovlro Sortlisl

r t i i

"i' cets the valu€ for gave ia LLat Point to Nexts EIemenC rncrenent counte!

SorEr,iatli t !Io,

Do n€xE

t Wants to

aubwf

INDF,

blf,Bc

STATI'S,

looD

one Thlough

tso tsh€ start i Poiat t th€ Liat i Use FSR as Inal€x

FSR movwf out a ial€LooD : rNDF, w novf FSR, f incf w C

aLl

t i r r i r

Point to the Next El€[l€nt + 1l sortlJiststj sortlJists Ijl If cerry Re6€1, sortl.ist Ii I >

9x

of

lJiat

+ 1l'

= SoruJiststi + 11 = sortlJiat [i + 1l + 1l - Soltti8tlil - gortliaEli + 1l + 1l + SoltIJiEt = sorrlLBr Iil

- $REG - (Sorllist -

tv

w L'

F fn

ti

] Solll,iststi

\34

lil

H

Similarly,with Sortlist[i], the value in Sortlist[i + 1] couldbe substitutedinto it by simpleaddition: sorrl,tatsf1l,

t9 {

9-.r

ginulaEo!

if

i r

'-t

= SortliBt[il + WREC = sortlistlil + (sortr,Lstli Sort'I.isl tiI ) = Sortlistli + 1l

5ectionTuelve S o l v i n g P r o g n a m m i n gP r o b l e m s

+ 1l -

293

fr

s "*.4 1.4 rft

.3J

3.,; ai

Thisoptimizationallowsfor swappingthe contents of the two array elementsin four instructions.When I originallywrote the code,I used10instructionsto swapthe valuesin the elementsusingtranslatedC code. Yearsagoon the PICList,the questioncameup regardingthe bestway to sort a short array.My immediateresponsewasto comeup with a loopingbubblesort routine like the one above.Somebodvcameun with a simplemacrothatembodiedlh..orpu,. und swapcodetogetherand createda simplebubble-sort program,which ran througheverypossiblecompare and swap,asI havedone in "asmbSort2.asm,,, which usesthe "CompareAndSwitch"macro: conpare.Bnalswilch locat CASship novf, Firstvalue, sub\of btfsc

=f:

geconalvalu€, STATUS. C

rnaclo

Firstvalue,

Secondvalue

i Is g€condvalue Value?

w

> Eilst

w ; If Carry Secontl >=

Set. then Firsts

gotso CASSkip r WREG = Seconalvatue - Eitstvalu€ = ELrEtsva1ue aaldkrf Firstvalue, f t FirstvaLue + (WREC) = sulndf Secontlvalue, f i Seconalvalue - (WREG) Seconalvalue cASSkip: €ndm

The secondprogramruns somewhatfasterthan the originalandis goodasa demonstrationtool for the squaredorder of operationsof the algorithm.For an arrayof two elements,one compare/swap is required; for threeelementqthreecompares/swaps are required; for four elements,six compares/swaps are required; and for five elements,10compare/swaps are required. Unfortunately,the numberof instructionsrequiredby this methodsoonbecomesthe limiting factorin its adoptionin usingit. For sortinga maximumof threeor four elements,this couldbe a very efficientbubble-sort method,but its usefulness diminishesaslarAerarravs mustbe sorted.

,': {Ji

Experiment111-Encrgpt and Decrgptan FSCIIZString Using a Simple SubstitutionFlgorithm

.t; t*{ :"el

traamEdcrypt

titLe Decrtzpt

;-!

anal ASCII

i l

1 - Ulrke Stringn

-

Encr!'p!

alal

Thia aDDlicalion coaverta a String of Characters sloleal ia th€ Eile Reglsters anal then al€cr]Dtss lh€sr using a glannetrical (Ror13). convelEioa table

?,r!

rrcr' Equivalent

!

Codt€:

DataSt,ring

Data encryptionis a fascinatingtopic and a numberof programsare availablefor the PIC microcontroller that implement data encryption standard (DES) and other modernencryptionalgorithms.Simplesubstitution algorithmscanbe implementedsurprisinglyeasily in PIC MCU assemblylanguage,asI will showin this experiment. iii

.r*

& ,tt

The basicoperationof a substitutionencryption algodthmis that one characteris substitutedfor anotherfrom a tablein which the charactersubstitutrons are symmetricql When the substitutedcharacter is placedbackin the table,then the originalcharacter is returned.Thisoperationis demonstratedin the following application:

!34

for

(i

= 0,

i

< at!1ea(Datastllng) t i++) / I E^ctyT,E th€ String = Encrt pt (Darastring Datastring[i] til ),

for

(i

= 0r

i

(Datagrrins) r i++) // DecrlT,t th€ Slring = Eacrypt (Datastlins Ii1 ) t

< srrlen

Datastlinslil ( 1 == 1)t

!.hil€

//

Harahtra!€ Notes: PIC15E68{ ruDning

at

Done,

4 ltlz

in

IJoop Eoreve!

Simulatsor

r.lyk€ Pledko 04.0{.05 LIST

R=DEC

tui f!

r .'r 294

l , E 3 P I C @l l C l JE x p e r i m e n t s f o r

the Evil

6enius

INCUTDE "D15f68{.

enalif aaltllw btfBc incf monwf,

inc"

i Variables CBI,OCK 0x20 DataString:12 ENDC

TIITKEPREDKOtr,

.It

novhc movDrf Inj'tIJoo9: incf

of

EncryDtioa

0 -

Datastling

1

r getup FSR lo ; tlre Datastring

Poj.nt

tv

O

UPPERCASE ' EETIE}I charectser i ascrr PCLAIIH clrf , getsuP PCLIITE for r Ta51e R€aal ( (_Encrl,I)t' if & 0x0100) != 0) bsf PCLAAA, 0 eadif

f

PoLnt

,

lo

lh€

i

novl,w

Dat.agtring

t Now, EncttE i string

rnovwf FsR lldcfl.!)t Iroop : novf INDE,

w

the i sto!€ t At Enal of

i t t t t r t r r

call Or'Wf

Eucrtz9t INDF

incf

FSR,

movf

INDF,

btfBa goto

z STllrUs, Eacrtz9tlJooD

novllr

Dataslring

f w

novwf FsR Decrl.9tlJoop : movf MDF,

€€ts rnalex

Nenl

Inlo

charac!€r gEling?

l

i i t t t r r

w

r ReD€at , gtring?

novwf

IND!

incf,

FSR,

movf

INDS'

btfaa goto

SfaTUS, z D€crylrtlooD

goto

I

f

the

to Gel charact€! Encrypt llo the sylln€tlical Decrygt Put back lh€ EncrlTrtedl Character Poinl to the N€xt

\t

Encril'l

the

to Get chalacts€r Encryl)l llo th€ Encryptsi.otr Itt back lhe Encryplefl Charecter Point to th€ Nex! Charac!€r Regeal to End of Slring?

, Now, D6cri'pl , StrLlg

call

string

to

r Finiaheal,

Enal of

Loop

t Subroutides

bEf

( (_Ercr!,Dt & b6f PCIATH, eualif ( (_EacryDr if & bEf PCLATH, enall.f ( (_Encr!'Dl if & bgf PCIJATII,

0x0100) O

r setup i TabLe l= 0)

0x0200) 1

l= 0)

0x0{00) 2

I = 0)

PcIJAlrIl Reaal

for

qv

,-t

PCI,AIIH,

1

enalif if ( (_Encrl.pt & 0x0400) != 0) bsf PCIATH, 2 endlif adtllw adaLw btfsc incf movwf _thcfy9t: alt

(0 -

r 9--!

\A')

_Efct:zpt

i i

w

Zelo Baa€ the charactera

sTATuS. C PCITATE, f PC! trNoPQRsruwqxYzABcDEEGlrriIK!!,l"

g I tRct'!13

eaal

(character

Character else Character

rrl 5.{

This applicationfirst loadsan arraybuilt with 11 file registerswith the stringof uppercasecharactersto encryptanddecrypt.Thecode,which is representedas "C" Equivathe Datastringinitializationin the header lent Code,is the standardmethodfor initializingan ASCIIZ-stringvariablearray.Using the ASCIIZ format,the lengthof the stringcanbe ignored;it is simply readuntil a byte with the valuezero is encountered. This feature minimizes the number of file registers requiredto initialize,read,and write aray elements. Next,eachcharacteris passedto a subroutinethat finds the characteroffset to the characterrelative to A, and the character in the Encrypt table at this offset is returned. The characterretumed from the Encrypt table is in the ROT 13 format in which A is changedto \ B is changedto O, and so on.This is a very basicand popularsubstitutionalgorithmusedmostlyfor demonstrationpurposes. wasgivento a high schoolstudent This assignment who cameup with the following algorithmicversionof the Encryptsubroutine: if

PCLATH

f.r.

(( Encr,lT)ts& 0x0200) != 0)

if FSR,

Dalaslringl FSR, it hitstring INDF 0 STATug, Z IaitsIJoop

if

!o

FsR

novhr aubwf call NOVWf io!1w btfss goto

clrf

X

l'rt

Lt

PAGE r ltainline org

14

_Inilstring STAfrus. C PCLATB, f PCL

>= 'N') // cha:.ectser ld-Z, change to A-u = Charaqler A-U, Change to N-Z // Chalacler - rA')' = charact€r + (\!I'

This code either subtractsor addsthe difference betweenthe ASCII charactercodefor N and the ASCII charactercodefor A. You are probablyaware that the difference between the two is 13 (assuming

1 6

K Ld4

ffi

q

t&

{ d |d

a.4 e?'{ r.&

*3

&*

r-&' t-d

Fr. !n lr.{

SectionTurelve

S o l v i n g P n o g r a m imn g P r o b l e m s

295

that thereare 26lettersin the alphabetand the break is halfwaythroughthe alphabet).But by keyingin the differenceasan arithmeticstatementthat the compiler/assembler hasto processmakesit easierto see what'shappeningand minimizesthe chancefor keying the wrongvalueor changingit inadvertently. It isn't difficult to convertto PIC MCU assembler, but the codeis somewhatcumbersome. not to mention quite hard to follow.In the followingcode,I haveindicatedthe valuewithin WREG to make the oDeration of the functioneasierto follow: Erlcrypt: sublw btfsc goto

\ ltl' STATUS, C

, WREG = rM, - WREG r Skip if Carry Re6et , (WBEG> rM') r Carry Se!, WREG is

ROTl3TJess 'M'

subht

-

('N'

-

'a')

goto ROTl3Done ROTl3LeEa: 'M'

sublw

+ ('N'

r $ I R E G- \ M ' - ( ' N ' . rA,) _ (.r{, - I,IREG) r W R E G= W R E G - ( ' N , , -'A' ) t WREG \M' .a')

subfw addtw

'N, -2 *

sublw

'A,

(rN'

-

\A')

if

$IREG

, carry Se!, , Doi\'n rN' -

lrove ro .A' Values

r Retsurn

Eneoaledl

the

r r r t t t , ' , t t

WREG = r!I, - WREG W R E G= - 2 , N . + 2 , A , + ( . N , - W R E q ) W R E G= - ' N ' + 2 ' A ' $IREG $IREG = rA' - (-'N' + 2 'A' - WREG) WREG= \A' + lN' + WREG 2'A' !\IREG = WREG + ('N' - '4' )

For the casein whichWREG contains'N'to'2,' the codeis asfollows: sublw sublw

\N, \A'

ROT13 (VIREG)t

Interesting,if the sublwis executedtwice,you end up with the originalvalue: Bublw

I replacedthe originalEncrypt subroutinein the asmEncryptl.c routine,with the new versionabove and calledthe applicationasmEncrypt2.c.Thiscode, althoughobviouslyvery efficient,is hard to follow and understand.Thebestway to examinewhat is happening is to considereachcasein which WREG contains a valuefrom A to'M' or a valuefrom'N' to'2.' For a valuebetweenA and'M,' the codeexecutesas follows:

WREG = r!r' - WREG , t IIREG = \At - ( 'N' r ViIREG) i IIREG = WREG + 'A' t WREG = !"REG t 'A' )

{'N'

-

If you comparethe two methodsof computingthe ROT13value,you'll seethat the resultsare the same.I don't think I would get a lot of argumentthat the second versionis more efficientat computingthe ROT13 value,althoughit is not obvioushow I derivedit. I would like to saythat I havea simpleprocessfor coming up with this type of optimization,but I mustconfessthat eachcaseis different.To comeup with the optimizationfor the ROT13 Encrypt function,I stafiedwith the desiredend points (the final valueof WREG whenthe contentsof WREG are lessthan or equalto 'M' and whenthey are greaterthan 'M'). Next,I went to my teststate(the sublw'N' instruction) andworked at finding an addlwinstructionthat would negatethe'N' throughA operation. Pleasedo not assumethat optimizationsare beyond your graspor that they cometo me becauseI have more experienceprogrammingand working with the PIC MCU than you do.In explainingthe operationof the instructionsof optimizedcode,I havepedantically listedthe registercontents,listingtheir valueasI algebraicallysimplily them.If you follow this methodology, you will find that you too will comeup with very efficient codethat meetsvour requirements.

l , a 3 P I C o l l C U E x p e n i m e n t sf o r

the EviI

Genius

tql

112-Eeneratea FibonacciNumberSequence X Experiment rd

o

Eight hundredyearsago,the Italian konardo Pisano Fibonaccilooked at the problemof how rabbitsreproducedand how the growthof their populationcould be plotted.Postulatingthat rabbitscouldproducea litter oncea month,after they weretwo monthsold, Fibonacci cameup with a sequenceof numbers that is the baneof computersciencestudentsmore than three-quartersof a millenniumlater. Startingwith onepair of new born rabbits,the population of rabbitsin the first year canbe listedas: L,

This sequenceis normallydescribedasthe resultof a recursivefitnction As I mentioned previously,recursive functions perform part of a specified operation and then call themselves to completethe operation.I discouragethe useof recursivefunctions in PIC MCU programming(of all types,not just assemblyor C) becauseof the lack of an arbitrary-lengthprogram counterstack. Without the possibilityof recursion,the problem becomesquite simple,with eachvaluein the sequence beingdefinedby the statement: = Eibti

"asmribonacci titte 24 TermE" Sequence for

the

cleat€

15 bil wolhLng with i D€noastrate lhe Nunb€r Arraya to calculate i for 2{ E€rms. Fibonacci E€riea ; t

"C"

tinl , ,

EquivaLenl

Coale:

Fibt241;

Fiblol

= Fibtll

//

Sequenc€

49 Byle-Fibonacci

//

].t Eirat

Two Valuea

-

1l

+ Fibti

-

2I

that can be checkedby applyingit to the sequenceof the previousnumbers, The requirement for this application is to produce the Fibonacci rabbit population growth valuesfor two years.Looking at the examplesequenceabove,you canseethat the valueof the thirteenthmonth will be approaching255,which meansthat in order to store the populationvalues,you will haveto use16-bitvariables.The general form of the addition is: . .

Add high bytes Add low bytes

.

If low-byte sumis greaterthan 255 (carry bit set),incrementthe high-bytesum

(1 == alt

//

i llaralware Not€a: PIC15E584 rutraing ;

Finisbeal'

at

{ uEz

$

ts

I F

r

x 0,

IJoop For€ver

in

3 o

|-...

, for (L = 2i i < 24t i++) - 21t - 1l + Eistt = Fibli Fiblil r / / cet Each value , r whl.l€

f-r.

;fl

1

are

rt

H l\)

Elbanacci

1 3 , 2 1 - ,3 4 , 5 5 , 4 9 , L 4 4

t-,2,3,5,8,

Fibtil

In my solution,I havesetup a 24-element,16-bit array,in which eachmonth's element value is stored and usedto produce the value for later elements.The solution to the problem usesthe FSR index pointer register to keep track of which element (and which To byte of the element) is currently being accessed. changethe FSR,I useincf and decf (incrementand decrementinstructions,respectively),and I add explicit valuesto the FSR.

SimulaEor

o o

i , !,l].ke P!€ilko ,04.08.31

}l. LIST R=DEC INCL(rDE'915f

584. inc'

, variableE cBrJocK 0x20 Erbr2*24 i,

Sloring t Array values i Fibonacci

TerD:2 ENDC PAGE org

0

c l r f

E l b + ( 0 r 2 ) + 1

clrf mov].w movwf mo'\tttf,

Fib+ (1 *2)+1 1 Fib + (0 * 2) Fib + (1 * 2)

t Inltlalize t Two A.rray

5ectionTurelve S o l v i n g P r o g n a nmi n g P r o b l e m s

1El El€dlenta

z s 5

tt (D ry V2

o

to .a

o o o

297

ql f.t

(u

.a ts r..l

x F

f-1 r{-{

novlw

2A - 2

movlrf

i

novlw

Fib

novrdf IrooD ! alecf mov! movwf tl6cf movf movwf alecf movf

lilumber of Eldlents to

+ (2 * 2)

t Point t Start

r*{

"}J

ASR. MDE, T€a\D ESR, INDF, E€[rI, FSR, INDF,

- 1l , Point ro Fibti i anal Save in Tettp

f w + L f

I,J

.r{

fr"{ & I

aatalwf movf movwf Lncf &ovf novwf incf

FSR, Tetnp, MDF FSR, TqE IIIDF FSR,

alecf,sz goto

i, f lJoop I

= 0x03DB (ln ReverseByteOrder) Figure la-5

f w

r Aatdl Fibli t Telqt

-

2l

Fibonaccivalue

to

T€aI) + 1, f FsR, f INDF, rt Tetnp, f sTtTUs, c Tetry + 1, f

atldlrf tlecf novf aalabrf btsfEc incf

goto

6n

to the of tbe Arlay

FSR

In Figure 12-5,I havecircledthe sixteenthelement of the Fib array.The element'sfirst addressin the zerobasedFib arrayis found usingthe formula: i Store r Fibtil

!s{

AiLI

TerE) in

Address = Array (Elenent - 1)l

f

SEa-rL Address

r

12 x

w f + 1,

w

f ,

Repeat

i

Finiaheal,

(24 -

2)x

trooD

€ndl

When you run this application,one of the biggest problemsyou will encounteris trying to determine whetheror not the answersare correct.Lookins at the File Registerwindowin MPLAB-IDE (seeFigure l25), you may havea difficult time pickingout the highervaluesand translatingthem into their decimal eouivalent.

So,to find the first byte of the sixteenthelement,the first addressis 0x03E(assumingFib startsat file register address0x020).This value is savedin Little Endian format (low byte at low address,high byte at high address),and the valueis 0x03DB-even thoughit lookslike 0x0DB03.Convertingto decimal,this value is 987(whichwasfound by evaluatingthe expression[3 x 256] + [13x 16] + 11),exactlywhat is expected. I'm goingthroughthis calculationbecause, like C pointer values,MPLAB IDE doesnot showthe contentsof an array (especiallywith elernentsthat are 16 bits and greater),and to seewhat they are,you will haveto employa bit of ingenuity.For the mostpart,I recommendyou avoiddata structureswith operating valuesthat requirethis amountof work to understand, but in somecasescalculatingan addressand convertins the valuemanuallvcannotbe avoided.

rr] r"{

Experiment 113-Findthe LargestEommonFactor of Tr.uoEieht-BitNumbers

F-l

c) g

To round out the section,Iwant to leaveyou with an application that can be expressedreasonablycrisply in the C language.However, translating the function to assemblycanhavea numberof uniquechallengesthat will needto be addressed. Finding the largestcommon factor of two eight-bit numbers is very difficult when translatingbetweenthe two programminglanguages directly.Substitutingdirect translationsof the C assembly languagestatementswould havepotentiallymade

".1

t"{

q, g{

X td 298

l , e 3 P I C @f l C l JE x p e r i m e n t s f o n t h e E v i I

Genius

the application more difficult to write and executeefficiently. The task of finding the largest common factor of two numbersis not particularlydifficult,althoughit requires a number of steps.The ftst is to find all the factors of each of the numbers.This is done by repeatedly dividing eachvaluewith incrementingdivisors until a divisorthat producesa remainderof zero is found. When the remainder of zero is found, the quotient of the value and the divisor (which is a factor of the value)must be savedasthe new valueand the processrepeatedto find other factors.In highJevel languageprogramming,this is quite easyto do,but in programming,codingthe division assembly-language routine requiresextrawork. However,this extrawork canmake it is possibleto perform the routine only once,simplifythe program,make it shorter,and make it executefaster. Becauseof the needfor divisionroutinesand storageof the differentfactorsof eachvalue,I usedtwo uniquevariablearraysthat are indexedusingcounters. Using a high-levellanguage, this is easyto accomplish, but in the mid-rangePIC MCU, it is more difficult becauseonly one indexregisteris availableand becausethe addressfor eachvalueneedsto be recalculated.Tosimplilf workingwith the index registerwith multiple arrayqsomeplanningis required-planning to make surethat the index you are both readhg from andwriting to is the lastindex accessed so multiple recalculationsare not required.

t

i = 2 r i - L i ((1 != value) while && (valu€ // Final the Factors if

llaralware Noles: PIC15F58{ running et { MEz in Resel is tied tlirectly !o vcc PulLup/ Progra.lring Haralware

int int lnt int

//

,

5ectionTurelve

r+

== 0

}J

s tJ

Proce€s of

trin

I Save?

3::l F"r.

Faclor

enal GetFactota

nain( )

(

int

i,

Jt

rt

= 195t = 22{t

Nunber1 Nurbe!2

v

(llulrbell, FacEorlliat) // Get FactorB GetFactors(lilurber2, raclor2Lists) cetl'actols

t

ftl

i

r

= 1t IJargeslFactor lJargeEt Factor // CaLculat€ (i - 1, (i < 15) && (0 != Faclorll,ist for i++) for (j = 1r (j < 15) && (0 != Factsorllist tiI ); j ++) (Factsorll"ist til == Facto!2]Ji8ttjl if ) // Ha''e a colEron Factsor | * = LargestFactor LalgeatFactor Factorllist IiI t = Faclo!2l.isl racto!1liEt Ij I =0t [il // fi ,

lbe

simulatsor via

N\rnib6r1, lirufiber2 t Eactorlliat [15] t Eactor2r,l.at I16l t large6!!actort

o

H

lthile

Iil!'ke Pr€flko 0{.09.01

Faclo!

// fi' l i++, // !,ook at Nexl vaLue // elihw ] (1 l= value) if to // RemaiDing Prille = value; Factorr,ial [jl ia Final // Re$ainate!

cdnron

Filst for two 8-bil s€l the factors nuribers adtl uae th€[l to findl the lalge5t factor. Thia is a atirect usage of cdrron rncn Equivalent' Coale' of the aEs€[lbly lansnras€ v€rsioD

Restalt

o

Fr, 5

!= i))

(0 == (vaLue % i)) o! value/i // rf Renainate! tE is a Eactor | // value=va1u€/i; // Eake Awey Factor = it FactorlislIj++l to the IJists // Add Factor //

FI X r-t

t

in the assemblyJanguagecode that cannot be representedin the C languageversionof the application.

the largeat ![t,I!be!s',

) a lillrniber

aactorliat [0] = 1t for (i = 1r i < 16r i++) tshe Faclorl,iat I / I'litlalLze = 0t FactorlJiats lil

The assernblylanguageresult for this experiment is "asmlcFactor.asm"and its operationis bestillustrated using"clCFactor.c".Obviously,many subtletiesexist

#lncluale - Get cr,cBactor /* Factor of 2 8 Bit

(irlt Faclor',ist[] va1ue, int of / I FLatl AlrJ lhe Factora

cetFactora |

,

//

E^d

(1 == 1), // Finished,

til

);

t'rJ

Hr

H {

c

IrooD Eorev€!

cr,cFactor

The efficienciesof the assembly-languageversion over the C versionbecamevery apparentwhen some measurementswere taken of the two applications.The versionexecutedin one quarterof assemblyJanguage the numberof cyclesof the C version(4,800to 1.5,275 accordingto the MPLAB IDE Stopwatch) and also took up one quarterof the amountof spacetaken by the C languageversion(92 instructionsto 367instructions).The improvementin the numberof file registers

S o l v i n g P r o g n a m m i n gP n o b l e m s

T J

299

x

s

3 ff o

Ft

(f)

usedwasnot asdramatic(the assemblyJanguage versionrequires72 file registersversusthe 88 requiredfor the C languageversion).From theseparameters, you might arguethat the assembly-language versionis four timesbetter than the C languageversionand is clearly the "best" of the two. The assembly-language versionwould be the bestif speedand sizewere the criticalrequirementsfor the application.If developmentcostwasan important requirement,the C languageversionwould win, hands down.Rememberthat the designof the algorithm, which I coded in the C languagebefore attempting to write it out in assembler, hasto be donein eithercase, and if a highJevellanguageis used,the codecansubstitutefor documentation. In this case,you are not developingcodethat implementsthe samefunction twice.And, in the caseof makingchangesto the code, if the sourcecodeitself is usedasdocumentation,the documentationfor the applicationis changedautomatically.Finally,the C languageversioncanbe transferred to otherprocessors with a minimum of conversioneffort.If the applicationwascodedin assemblylanguage, it would haveto be rewrittenin the appropriateassembler. This lastpoint is known asthe portabiw of the code.By definition assemblylanguagehasvery poor portability acrossdifferentplatforms.

.

Find the eight-bitmean and median of a list of 10 numbers.

.

Producean eight-bitrandom number usingan LFSR and an eight-bitseed.

.

Use structuresto add two real numbers together.

.

Sort a list of numbersusinga bit-map.

.

Reversethe charactersin a string. Find the Y:0 crossingpoint for an equationin the format Y = Mx3 + B. Find the day of the week for a given date.

. . . .

Compressan ASCII string (i.e.,replacerepeating valueswith a singlecharacter). Implement a simpletranspositioncipher.

Along with keepinga list of possiblequestions,a quick Googlesearchfor "ProgrammingProblems"is a usefulway to find additionalquestionsto work tbrough. In July of 2004,this searchyielded over six million hits.The following selection camefrom the top of the list and offers many useful and appropriate questlons: ACM InternationalCollegiateProgramming Contest/EuropeanDivision www.acm.inf.ethz.ch/ProblemSetArchive.html o

For Eonsideration tbt

iJ . t'i

*..i ,l:

li {.ii

':*

.r* lt-: ,f1

tt f)

Teacherqmore than anybodyelse,understandthe importanceof creatingdifferentassignmentand exam problems.If a setof problems(suchasthe 10in this section)wereusedfor morethanoneyearin programming course,chancesare goodthat solutionswould becomeavailableover the Internet in time for start of the next course.Thisis unfortunatebecausethe purposeof thesequestionsis to help the studentunderstandproblemrequirements,developan algorithmfor the solution,andfinally presentthe resultaspart of the project.Themarksare really secondary. The problem:It's difficult to comeup with new questions. I find it usefulto keep a runninglog of questionsthat I think of during the year.I write down all sortsof thingsthat I comeup in my day-to-daywork. Someof the problemsin my currentlist that could havebeenconsideredfor this sectionincludethe following: .

For a set of X,Y points,find the equationfor the trend line.

.

Multiply two numberstogetherusingBoothe's alsorithm.

:"r,,

300

Varioussampleproblems American ComputerScienceLeague www.acsl.org/samples.htm High schoollevel programmingproblems Department of Mathematicsand Computer Sciences, WesternCarolina University problems.pdf www.cs.wcu.edu/cscontest/2@/zou ProgrammingProblems,15th Annual Computer ScienceProgamming Contest Problem SetArchive http://acm.uva.es/problemset/ Severalhundredproblems,advertisedasbeing typical to exan/project problems Universily of California.1999Programming Contest www.cs.berkeley.edu/-'hi lfingr/programmi ngcontest/f99-contest.pdf Somebasicprogrammingproblems

l , e 3 P I C @l l C U E x p e r i m e n t s f o r

the EviI

6enius

Sect ion Thirteen

TipZaps@Flobot (rated tols Ieast 10V)

at

47 /.rF electrolytic caPacr.to!s 0.1 pF capacitor (aoy type) 0.01 pF capacitor Radio Shack Zipzaps remote control ca!

DUM

1 ZipZaps Pe!fo!mance Booster Uplrade Kit

Breadboa!d

1 Plcr6F68 4

Solde!ing

.I

ilon

solde r

38 KHz IR TV lenote control !ece j.ve!s

RoLarY hand. tool (Dremel tool) with carbide disk cutsl a^+,r

^

r,r

MAx?55

I

I I

Tvro-J.ine, 16column LCD

1 1N5817 Schottky diode

high-speed 9/64-inch bit dlill

2 LEDs

Needle-nose

5 IR LEDS

20- to Black

pliers

24-gauge

rrile

Light-dependent resi.stors

wire

10k resi,stors

Red wire Wile-wlap Permaneot Ili!e

rrile

1k j.esistols

malkel

4.7k lesistors 470f,) lesistols

clippels

Breadboard

100O lesistols

Wiling

10k breadboa!dmountable potent iometer

kit

Scisso!s Xrazy Glue Five-minute

100 pF electrolytic capaci-

epoxy

Over the yearg I have gotten at least two dozen emails from people who want to come up 'ri/iththeir own copiesof establishedproducts becausewhat's already out there is so expensiveand they feel they could produceproducts at a fraction of the establishedproduct's

14-pin, machined pin DtP socket Tvro-piD, tightangrle stlaightthlough connecto! rightFour-pin, angle straightth!ougtr coanecto! Tero-pin, straightsocket thlough Four-pj.n, straight-throu9h socket Prototyping

PCB

Breadboardmountable SPDT switch (E-Slritch EGl903 Recormeoded) Breadboardnountable mcrnentaly On push button sfin heat-sh!ink tubing Thlee-cell AA battety pack AA al,kaline batte!ies 6-32, nylon

2-inch bolts

5-32 oylon

long nuts

price. My only experienceswith retail products are with the Tab Electronics Build Your Own Robot Kit and the Tab Electronics Sumo-Bot Kit, and they have beeneye-openingexperiences. In comingup with theseproductqwe agonizedover featuresand partsto

301

try and meetthe requiled pricepoint. I very quickly learnedthat the productcostshouldideallybe onesixth,and no more than one-fifthof the retail price. The costdifferentialis due to shipping,warehousing, packaging,advertising, warranty,nonrecurring expenses costs,and profits for both the manulacturer and retailer.Using this rule of thumb,I am amazed that Radio Shackcan sellthe ZipZapsorremote-control carsfor $20.00. The ZipZapsremote-controlcarsare a lot of fun to play with. If you haven'thad the chanceto at leasttry one out in the store,you shoulddo so immediately.Figure 13-l showsthe completepackage,a car and remote-controlunit that doublesasa charger.The car hasa 1l.2V NiMH batterythat canbe chargedin about a minuteand allowsthe car to run for threeor four minutesat full speed.The car is capableof runningforward and backwardand canchangedirection;some earlierremote-controlcarscould only movelbrward and backwards, and turn in only one directionwhile in reverse.Radio Shackoffersa wide rangeof different bodiesfor the kits,alongwith variousaccessories, includingperformanceupgradesAs you play with the ZipZaps,you shouldbe impressedwith its robustness. It handlescrashinginto objectsvery well and evensurvivesthe mostextremeactivities(performingjumps, runningotTtables,and so on) without complaint.It's a pretty amazingpackagewhenyou considerthat they cost$3 to $4 to manufacture.

Fiqure l3-l The Radio ShackZipZaps@car with remotecontrol and charger

302

I am actuallysurprisedto discoverthat nobodyhas exploredthe idea of wing the ZipZaps asthe basisfor a mobilerobot chassis, whenusingother larger remote-controlcar chassisasthe basisfor robots, whichis very common.Looking at the ZipZaps,I can comeup with a few concems,namely: .

1.2-voltoperation

.

Limited operatingtime

. .

Smallsize Minimal payloadcapability

.

Surprisinglysophisticatedmotor and steering driver circuitry (and complexcircuitry overall) Ternarysteering(three states:left, right, or straight)

.

Takingolf the body,the Zipzaps is very compactly designed.Youcanpop off the circuit board'splastic coverand get a better idea of the electronicsthat act as the radio receiverandthe motor/steeringdrivers.You may haveneverseen,and haveprobablyneverworked with, the surface-mount componentsusedin the ZipZaps' electronicboard (seeFigure 13-2),which may minimizeyour confidencein modifyingthe circuitry.All theseconcernsare legitimate,and usingthe ZipZaps asthe basisfor a PIC MCU controlledmobile robot may seemunlikely,but asI will showin the experimentsthat follow,the ZipZaps is designedusing traditionalmethodologies. You can hack one into a robot over the courseof a weekendfor suryrisingly low cost. An importantaspectof hackingthe ZipZaps into a robot hasbeenthe work that hasbeenperformedin the previouschaptersand the skills and knowledge that work hasdeveloped.As I work throughthis sec-

'f3-? Figure TheZipZaps electronicsseemsmall and difficult to work with, but can be usedas a basis for a surprisinglysophisticatedrobot.

l , a 3 P I C o l ' l C UE x o e r i m e n t s f o r

the Evi I

Genius

tion, I will be addingrobot featuresto the ZipZaps chassisthat havebeenpresentedand prototypedearlier in the book;in fact much of the incrementalcode that is addedwith new featureswascut and pasted With a simdirectly from thesepreviousexperiments. ple basestructurefor the software,usingthe establishedcodebaseI wasableto createa very set of robot functionsfor the ZrpZapsrn sophisticated just a few days. I really had a lot of fun working throughthe experimentsin this chapterand I think you will too.But beforeyou start cuttingrp a Zrpzaps for your own robot, I suggestthat you spendsometime reading

throughthis sectionand rereading(and redoing)the and presentedthe variousexperimentsthat discussed control,software,and interfacefeaturesrequiredfor the robot.You may not be ableto find all the partsI used(althoughthey are pretty generic)or, due to you may find that they work difassemblydifferences, ferentlyor haveto be tuned in a differentway.By readingthroughthe sectionfirst and makingsureyou understandhow everythingis supposedto work, you will minimizethe amountof work neededto complete iirst the robot and maximizethe chancesfor success on timeat everystep.Havinga differentperspeclive the robot,you will likely comeup with somethingeven better than what'spresentedhere!

- Char acterizing the ZipZaps Experiment 1I t-.1 Tool

Box

Radio Shack ZipZaps lemote-control ca!

DMM

ZipZaps Booster

Soldering

Pe!formance Upgrade Kit

Needle-nose pliers

20- to

Before we canstart designingrobot hardwarefor a Zrpzaps remote-controlcar,it's important to underof the motor driversand the standthe characteristics informationthat we want to steeringactuators.The collectincludeswhich circuitsare usedto ddve the motorsandthe steering,what voltageis appliedto them,and how muchcurrentthey consumeduring operation.Todo this,you will haveto probe the ZipZapsvery carefully,asyou will be desolderingand resolderingsomewires.This is somewhatfine work, and you may want to take my resultson faith. Although you will be doingsimilarwork in the next now is a goodtime to get coupleof experiments, startedand get somepractice. I want you to know Justasa note of reassurance, that duringthis step(and the following steps),I broke a numberof wiresfrom the car chassisto the PCB.I wasableto reattachthem,andthe ZipZapsran fine afterward. The first order of businesswasto solderon 2- to 3inch lengthsof wire onto the red wire and black wire connectionsof the PCB.It is difficult to showin a photographwhat wasdone becauseI attachedthe wire to the backsideof the PCB (assumingthat the sidethat hasthe wires,shownin Figure13-3,is calledthe front).

iron

So lde r 24-gauge wire

Thesewireswill be requiredfor the next experiment. While holdingthe driving wheelsof the ZrpZapsoff the table (no load condition)and with the wheels stalledagainsta tabletop (worstcasewith the motor essentiallyshort circuited),I measuredthe voltage (whichis the batteryoutput) acrossthe two wiresas givenin Thble13-1.The minimal voltagedrop seen from nothingrunningto runningand stalledis an indicationthat the motor driversare well designedfor the motor and that the batteryhassomeexcesscapacity. Next. I wantedto look at the motor driversthemselves. With a bit of searchingwith a DMM, I wasable to infer that the motor driver circuit looks something like Figure13-4.Thisis an innovativemotor driver to us, designwith two control linesand is advantageous asI will showin the next coupleof experiments.Totest the motor driver,I first measuredthe voltageacross the motor (at the yellow and blue wires)for the conditionslistedin Table13-2.I then measuredthe current passingthroughthe motor for the sameconditionsby desolderingone of the motor wiresfuomthe PCB. WhenI wasfinished.I solderedthe Inoloru ire backon. The motor ddver shouldbe fairly easyto interface to a PIC MCU. The only problemyou shouldbe aware

5ection Thirteen Z i p Z a p s o R o b o t

303

Table 13-2 Motor Parameters Valtage FEross Mator

Cuffent

Condition No Load.Motor Running

V 1.20

25nA

Motor Stallcd

0.65V

l{10+mA

Throush Motor

Table 13-3 steering Drivef5olenoid Parameters

Figure l3-3 side"

Close-upviewof ZipZaps PCB's "front

of is the needto maintaina constantcurrentfrom the PIC MCU to the transistor's base.This will requirethe changingthe driver transistor-base currentlimit resistorto a valuethat will providethe sameamount of basecurrentaswasavailableto the transistors originally. Again, the motol driver seemsto be well designed with dre ability to handlethe currentproducedby a stalledmotorcondition. After seeingtheseresults,I decidedI wantedto usethe motor driver circuit built onto the ZipzapsPCB. Next.I wantedto characterize the stceringactuator electronics. After a few minutesol probingthe PCB,I cameup with thc circuitsltownin Figure13--5. EachoI the two solenoidcoilsof the steedngmechanismare drivenby the singlebipolarNPN transistors with the

Condition

Voltage Rcross solenoid

Current Through Solenoid

No turn

0V

omA

Turning/Solenoid Active

1.20V

60 mA

parameterslistedin Thble13-3.Like the motor driver, the steeringgearshouldeasilydrive a PIC MCU I/O pin.The currentJimitingresistanceshouldbe changed againto makesurethe sameamountof currentpasses throughthe baseof the steeringdriver transiston. While doingthis characterization, I ran into two problems.First,the PCB is smalland usessurfacemount technology(SMT) components, which are harderto rework than the pin-through-holecomponentsthat you are usedto. Patience,planning,and a small-tipsolderingiron are all that is required.Second, it wasdifficult workingwith the steeringgearsolenoid wires,asthey are extremelythin magneticwires and will pull themselves from the PCB whenyou are solderingadjacentcomponents.They can be resoldered easily,but you will haveto noticewhen they are missing,andyou will find that they canbe difficult to manipulate.

Table 13-1 BattergVoltageat DifferentDrivingMotor states trondition

Voltage

Motors.SleeringOff

I . 3 6V

MolorsRunning. No Load

1 . 3v0

StallcdMotors

1 . 2V 6

So l o noi d Coil

st\48T3906

Control (s2A) from Radio

st\,4 8T3904 (s1A)

SMBT39O6 (S2A)

Control from Radio Sl\ilBT3904 (s1A)

Control from Radio

Figufe l3-q

Mobr driver

304

l , P 3 P I C @l l C U E x p e r i m e n t s f o r

Figurel3-5

SMBT39O4

(s1A)

Steering driver

the Evil

Genius

Experiment ll5-Plf

ryl

MCUPor-uer 5upplg

h,

,TT Radio Shack Zipzaps !emote-cont!oI car

**, a-{ -*) fTr

P I C 1 6 A 6 84 MAx756 1N581? Schottky

diode

I,EDS

t! i r

10of,) resistor 100 pF electrolyt ic (lated at capacito!s Ieast l0v)

DMM Needle-oose pliers

0.1 pF capacj.tor tYpe)

Breadboa!d Wiring

kit

The f st thing I had to do in the processof gettingthe ZipZapsrcbottogether was come up with a schemeto power the PIC16F684that would control the robot. This actually tumed out to be a fairly painlessprocess, althougha coupleof operationalissuesregardingthe selectedchip andits usewith the PIC16F684had to be investigated.To summarizethe experiment'sresults, the addition of a PIC6F684power supply went very smoothly,and I learned a bit more about the PIC MCU andhad somethingsto think aboutin later robot projects like this one. If I were to look at the Zipzaps Robot from a perspectivethat otherpeoplecouldeasilyunderstand,I would note that it is poweredby a single1.2-volt rechargeable battery.If you look at the PIC16F684(or, indeed,any PIC MCU) datasheet, you will discover that the minimum voltage on which they run is 2.0 volts.For the Zipzaps robot PIC MCU to run at that speed,either I neededto add an additionalbattery or a step-uppower supply.The suggestionof addinga stepup power supplymay seemlike overkill,but adding anotherbattery had nothinggoingfor it; not only did it require spacethat was unavailablein the chassis,but the battery usedin the Zipzaps is heary (which will decreasethe performanceof the robot),and I would have to come up with a specialchargingc cuit for the two additional batteries. To go with the step-uppower supply,I had a number of requirementsthat it had to meet beforeI was comfortable using it. The requirements were: .

Noiseimmunity-I didn't want motor/steering electricalnoiseto affect the operationof the PICMCU.

*.-"&

(anY

I ,

0.0L pF capacitor

.

.

.

Smallsize-The control chip could be no larger than an eight-pinDIP, and the number of support componentshad to be very minimal. Reasonablerun time-The robot would run for a reasonableamount of time before requiring recharging. Able to survivePICkit programmingbackdriving. Backdrivingis the term usedto describe the forcing of an overvoltagecondition on a chip's output. Backdrivingtypically becomes very wastefulin terms of power and can damagesomecircuits.

ffi LI {

I

w

'L; g

Using a switchmodestep-uppower supplyeliminatesmany of theseconcerns.The capacitor filters and inductor energy storagevery effectively filters out electrical noisebetweenthe motor and the circuitrypoweredby the step-uppowersupply.There are a plethora of differentstep-uppower suppliesto choosefrom, which madethe task of choosinga small,efficient deviceeasier,althoughit still took quite a bit of time for me to settle on the Maxim MAX756. This chip is designedfor poweringelectronicsusinga single1.5V or 1.2V battery which is exactlythe situationwe have here. To validatethe operationof the MAX756 and make sure it would be appropriate for the Zipzaps robot, I built the circuit shovmin Figure13-6.Thisis a fairly standardapplicationof the MAX756, usingvaluesand componentsrecommendedby the manufacturer. The first test wasto seeiI the ZipZaps baltery could run this chip and haveit power a reasonablesizeload.The circuitin Figure13-6providesa simpleLED (requiring

5ection Thirteen Z i p Z a p s o Ro b o t

305

Elj

ht

*?"€

:x ldi-t

about 10mA) and the cFlash.capplication,which flashesan LED betweenRA4 and RA5. The application ran the first time, but did not run after that.Using my DMM,I discoveredthat the voltageoutput of the MAX756, with all its heavycapacitance,took a very long time to go below500mV-just enoughto put the PIC microcontrollerinto an invalid state.To fix this problem,I enabledthe brownoutreset circuitry in the cFlash.cprogram's configuration fuses andrenamedthe applicationZipFlash.c.Asa side note,all the programsin this sectionstartwith "Zip" for easyfinding,rather than the traditionalc or asm.If I didn't want to enablethe brownoutresetcircuitry,I could have put a bleedresistorof 4-7k or more across the PIC16F684's Vdd andVsspins,which would drain the capacitorswhen powerwasremoved.

*incluale - 9irp1€ zl.pFLash.c /* an IJED on ZipZaDa, Prc15E584

c program

llhia Program ia a motllfi€dl 2. crr . witb Brolrn Ou! Delect Active RlA4 - IJED Poaitive RA5 - IJED lil€gative

F X

i

i

r---!

a q

vergiofl

to

of

FIaBh

',cFlash

Cofilection connection

royke Dr€alko 04.11.25

coNFIG ( MTIO & lvE||tDIS & PWRTEN & UCI,RI'IS IJNPROTECT \ & I'IIPRCXIECT & BOREN & IESODIS & FCMDIS) t

&

nain( )

(

PORTA = 0, Cl'lCONo = 7, ANSEIJ - 0; TRISM = 0t TRISAs = 0i

Lll

e*€

== 7l

!rhi1€(1

// // //

Turn off ComparatorB Tulr off Alc Make RA4/8A5 outsputs

ll

Loop Eorever

{ for

t'*

for

f I I

) I

(i (j

= 0r

i

< 255r i++) // Si,ryrle 500ms Delay = 0r i < L29? )++lt

RAo = RAo ^ Lt // elihw // E'l.d. zipFLash

/l

T oggle

rJED state

"}{ The applicationwasbuilt on a breadboard(seeFigure 13-7),and after enablingbrownoutrest,it ran fine and did not seemto affectthe operationof the ZipZapschassisand PCB (which,other than the addition of wiresfrom the battery'spositiveand negative connectionEwasunmodified).The batterycouldbe

306

Zipzaps

Figure 13-6 ZipZapspower supply testcircuit

chargedfrom a remotecontrol while pluggedinto the power supplycircuit.And when the batterywas charged,it not only chargedthe ZipZaps' batterybut alsopoweredthe MAX756 and the PIC16F684to which it wasconnected. With the MAX756 power supplyand PIC16F684 circuit addedto the ZipZaps chassis, I found that the PIC16F684's LED would flashfor 10 to 11 minutes (abouthalf the time the unmodifiedZipZapswould sit on the tablebeforeloosingenoughchargeto the radio receiverto stopbeingableto move).With the radio receiverand wiring still present,I found that the motor would run, without any dragon the wheelqfor about4 minutes,about the sameasthe unmodifiedZipZaps. The conclusionI reachedhereis that any task the robot is performingshouldbe completedin 2 minutes or so,to make surethat it doesnot run out of power.If you werein somekind of contest,you would haveto make surethat you won in 2 minutesor less.Otherwise you would loosefrom lack of batterypower. Essentially,all the requirements outlined here are met with the MAX756 step-uppower supply.Looking at the voltageoutput from the MAX756 usingan oscilloscope,no indicationwasgiventhat motor noisewas beingpassedto the PIC16F684.TheMAX756 is an eight-pinpart with five supportcomponents, which madeit appropriatefor usein this application.The runningtime of the robot did not seemto be negatively affectedby the additionof the MAX756, PIC16F684, and LEDs to the load poweredby the ZrpZaps rechargeablebattery The only issuenot addressedin this experimentis whetheror not the MAX756 cantoleratethe 5 volts appliedto program the PIC16F684in circuit. This is an important requirement for the application,asI will discussbelow,andit is not mentionedin the MAX756 datasheet. To be on the safeside,a silicondiodewill be usedto isolatethe PIC16F684's power supplyfrom the MAX756's output.The following experimentswill determineif this will affectthe operationof the other componentson the Robot'sPCB.

l , e 3 P I C o l ' l C UE x p e n i m e n t s f o r

the Evil

Genius

14 X i-s

Experiment 115-Plf MCUElectronicsPfB

o

tt

F..

1 Radio Shack ZipZaps remote-COntrOl Ca!

3 o

1PrC16F684 1 MAX756 1 1N5817 Schottky

di,ode

1 LED i.00 pF electlolytic (rated capacitors least 10v)

DMM Needle-nose

0.1 p,F capacito! tYpe)

pliers

(Dremel Rotary hand tool tool) with carbide disk cutter Electlic

dliI1

e/er-inch

high-speed

14-pin, machined pin DfP socket (see text ) Tno-pin, right-ang,Ie st!aight-through conltec-

dlill

6^

Four-pin, !igbt-ang'le straight-throug'h connectol

i '^6

Solder Black

fwo-pin, througrh

wire

Red wile Y[ire-wrap

hd

Prototyping text )

KrazY GIue

6-32, bolts

epoxy

With the powersupplytested,you are now readyto start hacking into your ZipZaps car and turning it into a robot. In this experiment,you will be putting in nylon bolt attachment points for the PIC MCU PCB aswell ascreatingthe basicPCB cfucuitwith the power supply andPIC MCU socket.Thistaskis not goingto be easy, andyou will haveto plan for the future experimentsto make surethat enoughspace(and holes)is available for mounting additional hardware.As I work through this experiment, I will try to indicate the issuesthat you shouldbe mostawareol The prototypingPCB usedfor this experimentcan reallybe of any type,but I usedone with predefined power and ground tracesand horizontally connected holes.This simplified my wiring somewhatalthough it restdcted me in other areasfor which I had to comDensatewhenI addedadditionalhardware. Youdo noi

f)

PCB (see

2-inch-long

6-32 nylon

nylon

L'J *-, ,,

nuts

rr

haveto usea prototypingsystemlike I used,but you shouldread the restof this sectionto understandwhat prototypingPCB you want to useand how it is going to be mountedto your ZipZapschassis. Positionthe prototypingPCB over the Zipzaps chassisin sucha way that the PCB will hang over the chassisevenly.Do not positionthe PCB so it is above the empty spacein the chassisbetween the battery and the steednggear.Themountingbolts for the PCB will be gluedinto this areaand ascloseto the steeringgear aspossible.When the bolts are gluedin, it is important that the chargingcontactsand hold-downholesare not affectedin any way.If they are hindered,you will not be able to chargethe Zipzaps battery using the remote-controlunit. With the marksin place,find the centerof the PCB at this Doint and drill two e/er-inch holes 0.250-inch

5ection Thirteen Z i p Z a p s@ Ro b o t

L J

K

straightsocket

Four-pin, straig'htthrough socket

wi!e

PermaneDt malke!

Five-minute

I

H

Wire clippers ra'i

(anY

H P Ol

0. 01 pF capacitor

bir

c^l

at

5

*

307

*

Ff

n

,-t

P, {a li

ry

R lrr{

t ,

m

U A

w U

,Fl

g

o t{

{J

U

(} FI

rd p U

x

U H

A

I \o rl r{ $

c 0, E .Fl t{

o

9{

X

frl

away from the c€nter.ff the prototyping PCB is fully drilled (as mine was),drill out two holeq four holes apart. Rememberto keep the PCB centered over the ZipZaps chassiswhen you are doing this. Once the holes are drilled, run the nylon bolts through and screwthem down with some of the nuts. At the end of the boltg loosely screwon a nut and put Krazy Glue on the open face of the nuts at the end of the bolts.Carefully (making sure you do not trap any of the small steeringwires) push the open face of the nuts againstthe bottom of the chassisThe bolts should be solid to the touch in 30 secondsor so.The Krazy Glue actsasa mild solvent to the plastic usedin the ZipZapqso leave the assemblyin place for an hour or so for the Krazy Glue and chassisplastic to harden. When the Krazy Glue has hardened,remove the bolts and cut the bolts down to 1.25inch (3 cm). (You will probably have to loosen the nuts holding the bolts to the PCB first.) Insert the bolts into nuts,cover the nuts ald the baseof the bolts with 5-minute epory, and wait for another hour for the epoxy to harden. When the epory has hardened,run two nuts down the shaft 1A inch (1/zcm) and Krazy Glue them in place.Once the Krazy Glue hashardened,you can place the prototype PCB on the bolts and tighten it down when you want to test the robot. With the bolts in place,you will now have to cut down the original ZipZaps PCB.This is being done for two reasons.Fhst, you want to avoid any kind of contention betweenthe ZipZaps driver and the PIC MCU. And second,you want to make spacefor the two bolts you just installed. Before cutting the PCB with a Dremeltool and carbidewheel,I locatedthe four resistorsusedfor current limiting on the PCB and drew an indicator line on the PCB above them (see Figure 13-7)using an indelible marker. Cut the PCB along this line carefully,making sure you do not damageany of the marked resistors. Once the ZipZaps PCB has been cut, solder wires to the positive (red) and negative (black) wires leading from the battery to the PCB.Thesewires will go to a right-angle connector to be soldered to the bottom of the prototyping PCB and will need the straightthrough connector soldered to the other end. Next, solder wire-wrap wires to the endsof the four-motor driver and steering driver resistors(seeFigure 13-8). Thesefour wires will be soldered to another straighttbrough connector,which will go through another right angle connector and the bottom of the prototyping PCB.The ZipZaps chassisis now ready to be controlled by a PIC MCU! When I attached the PCB to the robot, I did it with the solder tracesup.This make soldering components quite a bit more difficult, but it ensuresthe PIC MCU power and motor/steering driver connectorsare easy

308

Figure l3-7 Mark cuttingline abovecuruentlimiting resistorsand belowblack blob

to solder and their whes do not snagor rub against sharp wires on the bottom of the PCB.The machined pin socketthat wasspecifiedfor the PIC MCU was identified becauseit can be soldered to the PCB from the topside (instead of only from the bottom side,asin a regularDIP socket). The circuit that you will be wiring to the PCB is the basicpower supply with PIC16F684(without the two LEDs) in Figure 13-9with the bottom right-angle connector usedto supply the 1.3volts from the Zipzaps battery.Again, it's important to plan ahead;due to my wiring placement,I put the MAX756 and its related discretecomponentsat the front of the robot, which left the rear of the PCB for the PIC16F684. When you have completed the assemblywork, your robot chassiswith PCB should look something like Figure13-10.You shouldstill be ableto put the ZipZaps chassison the Zipzaps charger,and it should work normally (i.e., the charger'sLED should start at red and changeto green when the charging operation hascompleted).Ifeverythingis correct,after taking

Fisure 13-8 PCB connector

l , e 3 P I C @l t C L JE x p e r i m e n t s f o r

the Evil

Genius

Zipzaps

' iI .

Fieure 13-9 ZipZapspower

the ZipZaps chassisandPCB off the charger,an LED placedagainstRA4 andRA5 (cathode)shouldstart flashing.Thereis no on andoff switch,the MAX756 will stop providing3.3-voltpower whenthe input voltagehasdroppedbelowits threshold.This shouldgive your robot a minuteor two of basicoperation,which shouldbe goodfor testingmostapplications. I found that althoughthe connectorswere quite reliable,the solderjoints and wiresfrom the battery and motor to the ZipZapsPCB would easilybreak. Ideally,the prototypingPCB would be permanently attachedto the ZipZapschassis(asis the original product PCB),but this is very difficult to do and still be ableto work throughadding(and debugging)the differentperipherals. Make sureyou plan how to con-

:-11 Figuref 3l0 wirhpnrrinlty ZipZnps chassi.: assembled PCB attached

nect and disconnectthe prototypePCB to the ZipZaps chassisso that the wire solderjoints receivea minimum of stress.If you don't,you will find that the wires will breakperiodically.You'll be left scramblingto figure out the causeof the problemand then fix it, with the wiresbecomingshorterover time. This is probablythe mostdiflicult assemblyexperiment in the whole book.Rememberto think about whereeverythingis goingnext,and try to keep everything aswell centeredasyou can.A few momentsforethoughtwill saveyou a lot of grief later.

5ection Thirteen Z i p Z a p s " R o b o t

309

Experiment ll7-lH TV FlemoteControl

Prc16F684

r{

38 KHz tR TV lemotereceivers coltrol

$'1

1 fno-1ine,

4r

1 10k lesistor I

16-column

LCD

100C) lesisto! 10k b!eadboard-mountable potentiom€ter

t'l

4? pF electlolytic capacrto!

Needle-nose pliers

al

B!eadboard

+J

Wiring

0.01 pF capacito! type)

kit

(anY

B!eadboard-mountable (E-Switch SPDT switch 8G1903 leconmended)

G.}

Breadboard-mountable momentaty on pushbutton

F.

Thlee-cell, pack

AA battery

AA alkaline

battelies

f-r

ffi H

I like controllingrobotsusingan infraredTV remote control;they are cheapand the coderequiredto read the data is surprisinglysimple.Other optionsinclude RF or soundcontrol,but thesetend to be expensive and"fiddly" comparedto IR control,especiallyconsidering that IR control is generallyhandledby the robot'scontrollingPIC MCU. In this experirnent,Iwill demonstratethe code and circuitry required to read an incoming IR signal and display it on an LCD. The IR signalproducedbyTV remotecontrol comesout of the receivermodulelooking (seeFigure 13-11).TheIR signalfrom the remote-controlcodeis

modulated with a 38 kHz signal that is only active whena low is desiredin the receiver'soutput.For hobby robots, I like to use the SonyTV codes,which consistof a PacketSrat followed by 12 bits in the format shownin Figure13-11.Thetiming of the different featuresof the waveform is basedon a 550rr,sT clock and consistsof differentmultiDlesof this time base.

"1"Bitl "0"Bit

0)

-:-T;.5

.r-l

ti &r".

Start(4T) Figufe

'13-'f'f

Synch "i'

(1r)

"0'

I

(1r) (2r)

IR remote-control codes

Figure l3-la

IR Rx testcircuit

{d I F{

310

l , A 3 P I C o l ' l C UE x o e n i m e n t s f o r

the Evil

6enius

Table 13-1 Codes LJEedbg the 5ong TV lFl Flemote-Control ZigZaDs Robol

B.ut!* 0x010

.PIP"

oxDB0

"2"

0x810

"Enter"

0xD10

"3"

0x410

"Display"

0x5D0

0xC10

"Mute"

0x290

0x210

"Recall"

0xDD0

OxA10

'Alrow Up"

0x2F0

"1"

'5" "6"

0xAFO

0x610 "8"

Ox2D0

0x110

'Arrow Left" 'Arrow Right"

oxE10

"9" '0"

0x910

"Menu"

0x070

"Power"

0x490

"Guide"

0x764

0x490

'oK"

0xA70

OXCD0

To read and displaythe IR incomingsignals,I came up with the circuit shownin Figure13-12.This is a modificationof the originalLCD displayexperiment's circuit,whichnow includesan IRTV receivermodule with the requiredfilter capacitorand resistor.The C programming languagecode for driving the LCD was translatedinto assemblerasasmlCD.asm,which can be found on the PICkit's CD-ROM.The actualcode that readsthe incomingIRTV remote-controlsignalis calledasmLCDIR.asm.Note that to simplifythe time delayqI createdthe Dlay macro,which will delaythe operationfor somesetnumberof microseconds. When I ran asmLCDIR.asmon the circuit shownin Figure13-12,I cameup with the codesin Thble13-1 for the different buttons on a universal remote programmedto output SonyTV codes.These different codeswill be usedfor controllingthe ZipZaps robot in the following experiments.

m t?t h+

J

g# *"€

r'*

i..*

# i-Et

0xC90 "Ch+"

0x090

'Ch-"

0x890

! ht qr.'

llB-Motor and SteeringEontrol Experiment

ry fli ,"{

Radio Shack ZipZaps remote-control ca! chass is Ptc16F584 PrototYpe Previous

PCB flon the exPeriment

4. ?k les istors 470O lesistors

{ !.s

ru

DMM Needle-nose Ilire

pliels

clippers

Soldeling

ilon

solder Wire-rirap

wj,re

With connectionsfrom the original Zipzaps PCB to the prototypePCB in place,the next stepis to make the motor and steeringconnectionsso that the PIC MCU cancontrol the motion of the ZipZaps chassis.

The connectionsrequirecurrent-limitingresistorsto ensurethe motorsand solenoidsare driven with approximatelythe sameamountof curent, that they perform asdesigned,and that an excessive amountof currentisn't flowing.Thisis an importantpoint because the PIC MCU will be driving high signalsat 3.3volts insteadof the 1.3V measuredon the oiginal ZipZaps. To calculatethe new currentJirniting resistorg I voltagedrop was assumedthat the base-to-collector 0.7V, and usingthe known basecurrent-limitingresistors.I couldcalculatethe exDectedbasecurrents.

5ectionThirteen Z i p Z a p s @R o b o t

311

*"3.

5C I

q , e*

.}

For the steeringdrivers,the basecunent is:

Zipzaps

t':r\ t,:,

v=lateering*3.3k = (1.3 - 0.7) isteeling

/ 3.3k A = 0.18 nA

To maintainthis basecurrentwith a 3.3-voltoutput:

i.{

t'1 f

v = isteering x Rsteeling Bst€€ring = (3.3 - 0.7) / 0.18 rnA = 14.{k

i

iJ, f'j

.; -{

With 3.3kalreadyin seriegan additional11.1kof resistanceis requiredfor the steedngsolenoiddrivers. Similarlyfor the motor drivers,the basecurrentis:

Figure l3-13 Zipzaps motor

a ! v = r l . f t t x z z u

,i:

i.".., = {1.3 - 0.'7) / 220 A

+-,)

= 2.'73 nl+

if]

To maintainthis basecuffent with a 3.3-voltoutput: - {

?

t-; ltl

R . . " " , r " n= ( 3 . 3 - 0 . 7 ) = 950O

F.!

,.:r l

':. : l rl,,.i

/ 2.'73 rnA

With 2200 alreadyin seriegan additionalresistance of 7300 is requiredfor the motor driversto be controlled by the PIC MCU With thesevaluesin mind,I testedan additional 10kfor the steeringdriversand 680Oresistorsfor the motor driversaswell asno additionalresistanceat all points.Totestthe actionof thesecircuits,Iconnected the transistorbases(with seriesresistors)directlyto 3.3volts.When the calculatedadditionalresistorswere in place,the motors and steeringsolenoidsseernedto work exactlythe sameasthey did with an unmodified ZipZaps.With no resistances in place,both the motors and the steeringsolenoidshad a lot more power.In an effort to find somebalancebetweenmore power and

Figure l3-fq ZipZaps robot prototyping PCB with motor and steeringtransistorbasecuffentJimiting resistorsconnectedfrom the PIC MCU socketto the backsideZipZaps PCB connector

burningout the motors,driver transistors,or depleting the batterytoo quickly,I found that 4.7k and 470f) werethe optimal additionalseries-base current-limiting resistorsfor the steeringand motor drivers,respecIively.T\e ZipZapsrobot prototypingPCB (seeFigure 13-13)wasupdatedappropriatelyand wired according to Figure13-14,and it ran usingthe softwarepresented in the next expedment.

i'i':

. !.t

i].

il

3L2

l , e 3 P I C @I I C U E x o e ri m e n ts f o r

the EviI

6enius

Experiment 119-BasicTaEk-Eontrol Softurare Pc/

Prckit"" 1 Radio Shack Zipzaps remote-control ca! chas sis P I C 1 6 F 5 84 Prototype

starier

Tool

Box

#def,ine Rc2 r #define RC3 t *define RC4 r RC5 r i r r

Earlier in the book,I demonstratedhow to run a motor with a fairly low frequencyPWM. I'm goingto takeadvantage of thatwork with the ZipZapsrobot because I wouldlike to usethe PlC16F6lJ4's builfin PWM lor modulatingI/R output.Theresultis ZipBase.asm, which hasa 30 Hz PWM lrequency,and provides32 PWM duty cycles,aswell asup to 1 ms betweenPWM stepsfor implementingsensorand control functions. To test out the connectionsto the ZipZaps chassis aswell asto get an idea of how long the batterywould lastwith a PIC MCU runninga stepped-upbattery voltage,I addeda numberof actionsthat take place over a 25-secondrepeating/l/e to demonstraterunning forward,forwardwhile turning right and left, stopping, and goingbackward. "zipBase

title t r i , r

-

zi.pzags

Robot

Ease

coale"

This Program Iroops orce every ns anil upalates the zipzarls Motor PI'iM (30,I{2 P!'Dt). The application also keeps lrack of the current time for the application.

.

FLia

t

heavily

.nn1,ida+i^n

ia

ba6eal on "asmMotor

2.asm".

Haralware No!€s: t PIC15F584 rurrnirrg at 4 MIIZ Using , Intelnal Cl,ock t RiA0 - Lef,t Light Senso! r RA1 - Rigbt Light Sensor r RA3 - Rear IR sensor t RA4 - Reverse Motor Control t PORTA, 4 #alefine ReversePin AA5 - Forwardls Motor Corrlrol t

PCB

kit

['orwardsPin PoRTA, 5 - Rigrht steeringr solenoid TurnRisht PoRTc, 2 - Left Steering Solenoid liurnl,eft PoRTc, 3 - Line FoLl.owing IR PWM - Object Detection IR PI4U

li{yke Predko 0s.01.25 LIST R=DEC rNcr,uDE "p16f 58{. inc rr CONFIG FCITEN OFF & IESO OFF & -BOD.OFF & CPD OFF & CP OFF & MC',RE OFF & PWRTE.ON & IIIDE OFF & ITiITOSCIO

variables cBr,ocK 0x020 RTC:2 r 55 Second RTC Directionr PWMDuty, PWMCycle t PWM Movemen! r variables Flag ENDC ;

Flag r #alefine

Bit Definitions F1ag, AppRun

#alefine

A)pReset

Tinecheck novl.w xornf btfss goto tnovlw xorwf btfss goto enalln

the r

alag,

Macro TvaLue, fiIGE Tval"ue RTC + 1. rt STATUS, Z lilotv€cto! LOW Tvalue RTC, w sTATus, z Notvector

if

Application

0

i

Set

1

i

Set if application Reset Coale to Run

Notvector

FAGE Mainline org

0

nop clrf,

r For PORTA

5ection Thirteen T i p Z a ps o Ro b o t

ICD D€bug

t Assune is Low

Everything

5r5

ft! !

'>

novwf, Ilovllr ovwf, clrf

cMcoNo b,00000001, a.DcoNo TuRo

H c,-J

F,t-l 3 I

I

EnabLe

STATI'S, RPo b,11010001,

movwf lrovlw

novwf bcf

OPTION_REG ^ 0x80t b,00000011, r t ANSEL ^ 0x80 b,00010000, r t A.DCONI ^ 0x80 b,001111, r TRISA ^ 0x80 b,110011' , i ERISC ^ 0x80 STATUS, RPo

bcf

MTCON,

cllf cltf clrf

Flag Pl[{Duty PWUCycLe

cLrf

Dilection

movwf novlv lrofisf novLw

Loop: btfss goto bcf

ToIF

INTCON, r0lr $-1 MTCON,

llDc

1'!lR0 aa a P!l!l

ADC Clock

Enable

l{otor

Enabl€ Bita

gtse€ring

as

tfufler

anal Wait

for

Check to Upflate llotor If Duty > Cycle, then Off,

sublrf movf iorftd btfac

PWUCycle, PORTA, w 1