EXPERIMENTING

withthe
PICBASICPRO
Y
ByLesJohnson
ACOLLECTIONOFBWLINGBLOCKS
ANDWORKINGAPPLICATIONS
USINGMELABS
SIMPLETOUSE
YETPOWERFULCOMPILER
IfRosettaTechnologies
ACROWNHILLPUBLICATION

~a
c2
~~~~~~~~~~~~~
~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~

~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~

· ·
· · · · · ·
- · · · - ·
· · · · ·
· ·
· · · - · · - ·
- · · · · · · · · · · ·
· ·
· · · - · · -
· · · · - ·
- · · - - ·
· - ·
· · - - - - - · · · ·
·
· ·
· - ·
- · · · · - · · · · - · ·
·
· · ·
- · · · · · · ·
- · · ·
· · -· ~· · - · · · · -·
· ~ ~· · - · · ·
· - - · ·
· -· - · · ·
· ·
· · - · - ·
· - · - · · ~···· - - · - ·
- · · · · - - - - ·
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
In t r o duc t i o n
Th e B ASIC
l a n g ua g e h a s be e n p o p ul a r
s i n c e i t ' s c o n c e p t i o n
i n t h e
1970' s
. O n e o f t h e m a i n r e a s o n s f o r t h i s i s i t s e a s e o f us e a n d a bi l i t y t o
m a ke a p r o je c t w o r k w i t h i n a m a t t e r o f h o ur s , i n s t e a d o f da y s o r w e e ks
.
B ut t o h a ve t h e a bi l i t y t o p r o g r a m a m i c r o c o n t r o l l e r i n B ASIC , i s a dr e a m
c o m e t r ue
. Mo r e o ve r , w h e n t h e B ASIC l a n g ua g e i s i n t h e f o r m o f a
c o m p i l e r
; i t c o m bi n e s bo t h s p e e d a n d e a s e o f us e
. Mi c r o E n g i n e e r i n g ,
La bs In c h a ve c o m e up w i t h t h e p e r f e c t m e di um f o r p r o g r a m m i n g t h e
P l C m i c r o r a n g e o f m i c r o c o n t r o l l e r s
. Th e P i c B a s i c P r o C o m p i l e r a l l o w s
t o t a l c o n t r o l o ve r t h e f ul l r a n g e o f 14 - bi t a n d 16 - bi t c o r e P IC ' s a va i l a bl e
.
Th i s bo o k t a ke s o ve r f r o m w h e r e t h e c o m p i l e r ' s us e r m a n ua l l e f t o f f , a n d
i s i n t e n de d f o r us e by t h e m o r e a dve n t ur o us p r o g r a m m e r
. It i l l us t r a t e s
h o w t o c o n t r o l r e a di l y a va i l a bl e de vi c e s s uc h a s An a l o g ue t o Di g i t a l
C o n ve r t e r s , Di g i t a l t o An a l o g ue C o n ve r t e r s , Te m p e r a t ur e s e n s o r s e t c ,
t h a t m a y be i n c o r p o r a t e d i n t o y o ur o w n p r o je c t s , a s w e l l a s s o m e
c o m p l e t e p r o je c t s
. In a ddi t i o n , t i p s a n d t e c h n i que s a r e di s c us s e d w h i c h
a l l o w e ve n m o r e c o n t r o l o ve r t h e P IC
. E a c h e x p e r i m e n t i n t h e bo o k h a s
a n a c c o m p a n y i n g p r o g r a m t h a t s h o w s e x a c t l y w h a t i s h a p p e n i n g , o r
s up p o s e d t o h a p p e n
. Mo s t a r e i n t h e f o r m o f s ubr o ut i n e s , r e a dy t o dr o p
i n t o y o ur o w n p r o g r a m .
Th e m a jo r i t y o f t h e p r o je c t s w i l l w o r k o n a n y o f t h e 14 - bi t c o r e de vi c e s ,
h o w e ve r , un l e s s o t h e r w i s e s t a t e d, t h e P IC us e d i s t h e e ve r p o p ul a r
P IC 16 F84 us i n g a 4 m Hz c r y s t a l .
Th e a c c o m p a n y i n g C DRO M h a s a l l t h e s o ur c e l i s t i n g s f o r t h e
e x p e r i m e n t s , a s w e l l a s t h e m a n uf a c t ur e r s da t a s h e e t s a n d a p p l i c a t i o n
n o t e s f o r t h e s e m i c o n duc t o r de vi c e s us e d
.
My t h a n ks g o t o Je f f Sh m o y e r , n o t o n l y f o r c o - w r i t i n g t h e c o m p i l e r s , but
a l s o f o r h i s a dvi c e i n t h e c o n s t r uc t i o n o f t h i s bo o k
. I w o ul d a l s o l i ke t o
t h a n k y o u f o r p ur c h a s i n g t h i s bo o k a n d I w i s h y o u e ve r y s uc c e s s i n y o ur
f ut ur e p r o je c t s
.
Le s Jo h n s o n
.
ExperimentingwiththePicBasicProCompiler
Contents .
Section1 .
DisplayControllerExperiments .
Page
SimpleSerialL CDcontroller . 1- 1
M u ltipleb au d SerialL CDcontroller . 1- 3
Contrastcontrolf oranL CDmod u le
. 1- 6
Driv ingmu ltiplexed 7 - segmentL EDd isplays . 1- 7
Su b stitu tingcommonAnod eL EDd isplays 1 . 11
I nterf acingtotheM AX 7 2 19 L EDcontroller . 1- 14
Section2 .
I nterf acingwithKeypad s .
Keypad interf acingprincipals 2 - 1
12 - b u ttonKeypad interf ace . 2 - 2
16 - b u ttonKeypad interf ace .
2 - 4
SerialKeypad controller .
2 - 6
R eceiv ingd ataf romtheSerialKeypad controller. 2 - 9
Assemb lercod ed Keypad d ecod er . 2 - 10
Section3 .
ExperimentingwithSerialEeproms .
Giv ingthePI Camemory . 3 - 1
M icrowireI nterf aceprincipals . 3 - 3
SPI I nterf aceprincipals .
3 - 4
12 CI nterf aceprincipals . 3 - 6
12 CserialeepromI nterf aceprincipals . 3 - 8
I nterf acingtothe2 4C3 2 ,
12 C
serialeeprom .
3 - 10
I nterf acingtothe2 4C3 2 u singtheM SSPmod u le
. 3 - 13
I nterf acingtothe9 3 C6 6 , M icrowireserialeeprom . 3 - 18
I nterf acingtothe2 5 L C6 40, SPI serialeeprom . 3 - 2 0
ExperimentingwiththePicBasicProCompiler
Contents . ( continu ed )
S ection4 .
ExperimentingwithA nalogu etoD igitalConv erters . Page
I nterf acingwiththeM A X 1 8 6 A nalogu etoD igitalConv erter . 4 - 1
U singa3 - wireinterf acetotheM A X 1 8 6 . 4 - 4
U singanexternalref erencev oltagef ortheM A X 1 8 6
.
4 - 5
Q u antasiz ingtheresu lt .
4 - 6
U singtheM A X 1 8 7 A nalogu etoD igitalConv erter . 4 - 8
I nterf acingtotheM A X 1 2 7 A nalogu etoD igitalConv erter . 4 - 9
U singtheon- b oard A nalogu etoD igitalConv erter . 4 - 1 2
A chiev inggreateraccu racy throu ghS L EEP
.
4 - 1 5
U singtheA D CI N command . 4 - 1 6
A nalternativ eq u antasiz ingf ormu la . 4 - 1 8
I roningou tnoisy resu lts . 4 - 1 9
S ection5 .
ExperimentingwithD igitaltoA nalogu eConv erters .
U singthePW M command asaD igitaltoA nalogu eConv erter . 5 - 1
Controllingthehard warePW M mod u les .
5 - 5
Bu ild inganR - 2 R D igitaltoA nalogu eConv erter . 5 - 9
I nterf acingtotheM A X 5 3 5 2 D igitaltoA nalogu eConv erter . 5 - 1 1
I nterf acingtotheA D 8 4 0 2 d igitalpotentiometer . 5 - 1 4
S ection6 .
ExperimentingwithR emoteControl .
S ony inf rared remotecontrolR eceiv er . 6 - 1
A ssemb lercod ed S ony inf rared R eceiv er 6 - 3
S ony inf rared remotecontrolT ransmitter .
6 - 4
A ssemb lercod ed S ony inf rared T ransmitter 6 - 7
I nf rared T ransmitter/ R eceiv er . 6 - 8
T ransmittingand R eceiv ingserialinf rared .
6 - 1 0
4 1 8 mH z , A . M . rad ioT ransmitter
. 6 - 1 3
4 1 8 mH z , A . M . rad ioR eceiv er . 6 - 1 6
ExperimentingwiththePicBasicProCompiler
Contents . ( continu ed )
S ection7 .
Temperatu reMeasu rementExperiments .
Page
D allas1 - wireinterf aceprincipals
. 7 - 1
I nterf acingwiththeD S 1 8 2 0 , 1 - wiretemperatu resensor . 7 - 5
I nterf acingwiththeL M3 5 temperatu resensor .
7 - 8
S ection8 .
ExperimentingwithR ob otics .
Proximity d etectionprincipals
. 8 - 1
S ingled irectioninf rared proximity d etector .
8 - 2
I nf rared proximity d etectorwithd istancegau ge .
8 - 4
D irectionalinf rared proximity d etector .
8 - 5
U ltrasonicproximity d etector .
8 - 7
D riv ingaD Cmotoru singanH - Brid ge .
8 - 1 0
D riv ingaD Cmotoru singtheL 2 9 3 D .
8 - 1 2
S ection9 .
ExperimentingwithA u d ioControlD ev ices
.
A d d ingav oicetothePI CwiththeI S D 1 4 1 6 chipcord er . 9 - 1
R ecord ingand play ingb ack mu ltiplemessages .
9 - 2
A llowingthePI Ctoau d ib ly cou nt .
9 - 5
D igitalV olu mecontrolu singtheA D 8 4 0 X
. 9 - 7
Controllingthegainof anop- amp
. 9 - 9
D igitalactiv eBassand Treb lecontrols .
9 - 1 0
S ection1 0 .
Programmingtechniqu es
I ntegratingA ssemb ly langu ageintoy ou rprograms .
1 0 - 1
D eclaringv ariab lesf oru sewithassemb ler .
1 0 - 2
Passingparametersu singtheD EF I N Ecommand
. 1 0 - 3
U singI N CL U D Ef ilestotid y u py ou rcod e
. 1 0 - 5
W ak ingthePI Cf romS L EEP .
1 0 - 7
A b rief introd u ctiontoH ard wareinterru pts .
1 0 - 9
U singtheO N I N TER R U PTcommand
. 1 0 - 1 7
ExperimentingwiththePicBasicProCompiler
Contents . ( continu ed )
Section11 .
Poweringu pthePIC
.
G ettingthemostou tof b atteries .
Page
11- 1
Theperf ectPower- u p . 11- 4
Append ix .
Componentsou rces .
Devicepinou ts .
CDROMContents .
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
ExperimentingwiththePicBasicProCompiler
Section-1
Display
Controller
Experiments
SimpleserialLCDcontroller .
MultiplebaudserialLCDcontroller .
Drivingmultiplexed7-segmentdisplays
.
SubstitutingcommonAnodeLEDdisplays .
InterfacingtotheMAX7219LEDdisplaydriver
.
ExperimentingwiththePicBasicProCompiler
Program - SERLCD
.BAS
SimpleserialLCDcontroller
I ntelligentLCDmod u lesacceptd ataand command instru ctionsov era
f ou roreightb itparallelinterf ace
.Command instru ctionsinclu d e : cu rsor
control, clearing, scrolling, etc
.T hesecommand sared escrib ed inthe
LCDmod u led atasheetand inthecompiler' smanu al
.T hank stothe
LCDO U T command , thesed isplay sarenotd if f icu lttou se, b u tstill
req u ireatleastsixpreciou spinsf romthePI Ctob eu sed
.T heserialLCD
controllerd escrib ed here, simplif iestheu seof thesed isplay sev enmore,
b y enab lingcontrolof theLCDwithasinglewire
.T hisisinv alu ab lein
d eb u ggingy ou rlatestmasterpiece, asitopensu pawind owintoy ou r
cod e
.By connectingittoanu nu sed pinand u singtheDEBU G command
atspecif icareaswithintheprogram, v ariab lesand registerscanb e
v iewed .
T heprogram SER_LCD.BAS
implementsasimpleserialLCDcontroller
f oru sewiththePI C1 6 F 8 4 .
W henthePI Cispowered u p, thef irstthingI td oesistu rnontheLCD
and waittheappropriatetimef orthed isplay tob ef u lly initialised , this
u su ally tak esapproximately 1 0 0 ms
.I tthenlook satthepolarity switch,
and j u mpstotheappropriatesectionof cod eand d isplay s : -
"T 96 0 0 Bau d O K!"

f ortru einpu t
or
"N96 0 0 Bau d O K!"

f orinv erted inpu t
I tthenwaitsf ora96 0 0 - b au d serialcharacterof whichev erpolarity was
chosen
.I f thecharacterisaspecialescapecharacter
( 2 5 4 ) , thenext
characterisassu med tob eacommand
.T hePI Cwilltheref orepassthe
f ollowingb y tetotheLCDasacommand
.O therwise, thed atawillpass
d irectly totheLCD.T hisallowsthed isplay tob ecleared scrolled , etc
.
simply b y send ingd atawithanescapecharacterinf rontof thecontrol
b y te : -
Serou tPortB .0 , N96 0 0 , [ 2 5 4 , 1 1
Pau se30
T hiswillcleartheLCD
.NotethePAU SEcommand , thisgiv estheLCD
mod u letimetorecov erf romtheCLScommand b ef oresend inganother
character .
Section- 1 - 1
ExperimentingwiththePicBasicProCompiler
9 V olts
0
V
78L05
I N OUT
GN D
R egu lated 5V olts
R ESET
R I
1 0k
4nHo
Cry stal
C3
2 2 pf 2 2 pf ass

ass
V

R 07
MCLR R BB
R B5
R B4
R B3
OSC1 R 82
R BI
R BO
PI C1 8F84
R A4
0502 R A3
R A2
R A1
V SSR AO
R 4
2 2 0k
Section- 1 -2
I N TELLI GEN TLCD
MODULE
R 1 1 A9 1 So51 ai >
R S
1 k
SWI
R 3
7
Figu res . 1 . SimpleserialLCDcontroller .
SimpleserialLCDcontroller
I f ad isplay withmoreorlessthan2 linesisu sed thenalterthelastlineof
theLCDd ef ines : -
Def ineLCD_ LI N ES 2

' Setnu mb erof linesonDisplay
Figu rel . 1 showsthecircu itof theSimpleserialLCDcontroller . Serial
d ataentersthrou ghR 5, thisgiv essomeprotectiontothe PI Cinthe
ev entof ashortcircu it, itisalsoconnected tooneterminalof the DI L
switch( SW1 ) .
TheDI Lswitchserv estwopu rposes, f irstitconf igu restheserialpolarity
mod e ( inv erted ortru e) b y pu llingPortB . 4togrou nd throu ghR 3, j u st
enou ghtoregisterasalowread ing ( 0) ,
b u tnotenou ghtointerf erewith
theou tpu ttotheLCD . Sharingapinlik ethisisacommonpracticewhen
sparepinsarenotav ailab le .
Second ly , itstopstheinpu tf romf loating,
( f loatingmeansthatthepinis
neithersethighorlow) . Thisisachiev ed b y resistorsR 2 and R 4 . When
thepolarity isconf igu red f orinv erted mod e, thelef tswitch intheDI L
pack ageisclosed , whichmeansthattherightswitch isopen, thu s
allowingonly R 4tob econnected totheinpu t, thispu llstheserialinpu t
pinslightly toward sgrou nd . And whentru epolarity isselected , the lef t
switchintheDI Lpack ageisopenand therightswitchisclosed , b ringing
R 2 intocircu it, b u tasR 2 hasalowerresistancethanR 4theserialinpu t
pinispu lled moretothesu pply line . Withou ttheseresistors, rand om
characterswou ld b ed isplay ed whentheinpu twasnotconnected to
any thing .
+5v
R 2
look
0
Seri5I
Date
I n
O
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
P r o g r a m s - MULTILC D2.B AS &MULTILC D4.B AS
Mul t i p l e B a ud Se r i a l LC D c o n t r o l l e r
If , l i k e m e , y o u a r e f a s c i n a t e d b y s e r i a l (RS232) c o m m un i c a t i o n , t h e n t h i s
p r o j e c t i s a m us t
. Th e b a ud r a t e s a r e s e l e c t a b l e f r o m 300 t o 19200 a n d
b o t h i n ve r t e d a n d n o n -i n ve r t e d s e r i a l da t a i s a c c e p t e d
. Th e c i r c ui t i s , i n
e s s e n c e t h e s a m e a s t h e Si m p l e c o n t r o l l e r , b ut w i t h t h e e x c e p t i o n o f a
c l e ve r l i t t l e s w i t c h c a l l e d a De c i m a l Ro t a r y DIL, f i g ur e l .2 s h o w s t h e
p i n o ut o f o n e o f t h e s e de vi c e s
. It h a s t e n r o t a r y p o s i t i o n s , n um b e r e d 0 t o
9, a n d t h e s e n um b e r s a r e r e p r e s e n t e d a s B C D

1 , 9 o
y 8
o ut p ut s o n p i n s , 1, 2, 4 a n d 8.

C c o

N c
4 1

C.
2
Se c t i o n -1 - 3
9
9
V
F i g ur e 1 .2.
Th e o ut p ut s o f t h e s w i t c h a r e c o n n e c t e d t o RB O
- RB 4,
a n d b y l o o k i n g a t t h e s e i n p ut s , t h e p r o g r a m i s a b l e t o
de t e r m i n e w h i c h b a ud r a t e i s r e q ui r e d i .e .
3 f o r 300 b a ud, 9 f o r 96 00, 8 f o r 19200 (p o s i t i o n 1 i s a l r e a dy us e d) e t c .
F i g ur e l .3 s h o w s t h e c i r c ui t f o r t h e m ul t i -b a ud c o n t r o l l e r
. B e c a us e o f t h e
h i g h e r b a ud r a t e s i n vo l ve d, a 16 F 873 r un n i n g a t 12m Hz i s us e d .
Yo u m a y h a ve n o t i c e d t h a t t h e Vdd p i n o f t h e LC D i s c o n n e c t e d t o
P o r t B .5 i n s t e a d o f t h e s up p l y l i n e , t h i s i s s o t h a t w h e n t h e P IC i s r e s e t , a l l
p o r t s a r e i n i t i a l i s e d a s i n p ut s b y de f a ul t , t h us , a l s o t ur n i n g o f f t h e LC D,
a n d e f f e c t i ve l y r e s e t t i n g i t . Th e r e f o r e , t h e f i r s t t h i n g t h e p r o g r a m do e s i s
m a k e P o r t B .5 a n o ut p ut , a n d t ur n t h e LC D o n
.
In o r de r t o r e a d t h e r o t a r y di l s w i t c h , t h e i n t e r n a l p ul l up r e s i s t o r s a r e
e n a b l e d o n P o r t B , a n d t h e l o w e r 4-b i t s a r e m a de i n p ut s , w e a r e o n l y
i n t e r e s t e d i n t h e p i n s t h a t t h e s w i t c h i s c o n n e c t e d t o , s o t h e
p o r t i s r e a d
a n d t h e up p e r 4-b i t s a r e m a s k e d o ut b y ANDi n g t h e r e s ul t w i t h
%00001111, t h e va l ue h e l d i n B -TE ST n o w h o l ds t h e B C D o ut p ut o f t h e
s w i t c h
. A l o o k up t a b l e i s s e t up b y us i n g t h e LOOKUP c o m m a n d w h i c h
h o l ds a l l t h e b a ud r a t e s t h a t w i l l b e s e l e c t e d (0-9),
t h e y h a ve a l r e a dy
b e e n di vi de d b y 100 (12 i n s t e a d o f 1200, 96 i n s t e a d o f 96 00), t h i s i s
b e c a us e t h e c a l c ul a t i o n t o s e t t h e b a ud r a t e f o r SE RIN2 i s , (1000000 /
b a ud )- 20,
h o w e ve r , t h i s i s t o o l a r g e a n um b e r f o r t h e c o m p i l e r t o
h a n dl e , t h e r e f o r e , i t h a s t o b e s c a l e d do w n , t h i s i s a c h i e ve d b y di vi di n g
b y 100 i
.e . (10000/ (b a ud/ 100)) -20. Af t e r t h e LOOKUP c o m m a n d, t h e
va r i a b l e
B AUD h o l ds t h e s e l e c t e d b a ud r a t e / 100, t h e n t h e a b o ve
c a l c ul a t i o n i s c a r r i e d o ut , a n d B AUD
n o w h o l ds t h e va l ue t o b e p l a c e d i n
t h e SE RIN2 c o m m a n d .
ExperimentingwiththePicBasicProCompiler
T risB . 4 = 1
I f PT est= 1 then
Baud. 1 4 = 0
M ode= " T "
Else
Baud. 1 4 = 1
M ode= " N"
Endif
T risB . 4 = 0
S ection- 1 - 4
M ultipleb audserialL CD controller
T oreadthepolarity switch, PortB
. 4 ismadeaninputandb it- 1 4 of BAUD
issetorclearedaccordingtotheresult
. Bit- 1 4 isthemodesetting, ( 1 =
inv erted, 0 = noninv erted) : -
' S etPortB . 4 toI nput
' I f P_ T estishighthenS etf orT ruePolarity
' R esetb it- 1 4 ( M odeb it, clearf orT rue)
' V ariab leusedf orthedisplay
' ElseS etf orI nv ertedPolarity
' S etb it- 1 4 ( M odeb it, setf orI nv erted)
' V ariab leusedf orthedisplay
' T urnPortB . 4 b ack toanoutput
T heincomingserialdataisthenreadinusingtheS ER I N2 command, as
thiscanachiev ehigherb audratesthanS ER I N
. T heprogramnowsitsin
aloop, receiv ingdataandoutputtingittotheL CD
. I f thecontrolb y teis
detected ( 2 5 4 ) theprogramisre- directedtoaroutinethatinput' sanother
serialcharacter, thiswillb etheb y tethatinf ormstheL CD astowhat
actionshouldb etak en, scroll, clearscreenetc
: -
L oop :
S erin2 S I , Baud, ( R cv By te]

' R eceiv etheserialb y te
I f R cv By te= 2 5 4 thenControl

' T rapthecontrolb y te
L cdoutR cv By te

'
Elsedisplay itontheL CD
G otoL oop

' K eeponlook ing
Control :
S erin2 S l, Baud, [ R cv By te2 ]

' R eceiv ethesecondserialb y te
I f R cv By te2 = 2 5 3 thengotoBar ' T raptheBargraphb y te
L cdoutR cv By te, R cv b y te2

' O rsendoutthetwob y tes
G otoL oop

' L ook again
Bar :
' R eceiv etheT hirdandf ourthserialb y te
S erin2 S I , Baud, [ BarPos, Bar_ V al]
L cdout

l, BarPos

' Positionof b argraph
G osub Bargraph

' D isplay theb argraph
G otoL oop

' L ook again
Y
E x p e r i m e n t i n g w i t h t h e P I c B a s i c P r o C o m p i l e r
5 Vo l t s
C l
C 2

0 . 1 u 1
1 O u 1
b~
a s s a s s
RE SE T
R1
1 0 k
i
1 2 m Hz
C r y s t a l
s
1 0
C 3

C 4
1 51 0 1

1 51 31 1
s a
VDD
RC 7
RC 6
RC 5
RC 4
MC LR RC 3
RC 2
RC 1
RC O
RB 7
RB A
RB 5
RB 4
RB 3
RB 2
O SC 1

RB 1
RB O
P I C 1 5F873
RA5
RA4
O SC 2 RA3
RA2
RA1
VSSVSS
RAO
i e
1 7
8
e
e
5
a
i
I NTE LLI GE NT LC D
MO DULE
0 0 0 0 0 o
S w 3¢ >° >
R3
1 k
SWI
Fi g u r e l . 3
. Mu l t i - ba u d s e r i a l c o n t r o l l e r .
B a r g r a p h o p t i o n
Th e B a r g r a p h d i s p l a y i s i n i t i a t e d by s e n d i n g t h e c o n t r o l by t e 2 53 a l o n g
w i t h t h e p o s i t i o n t o s t a r t d i s p l a y i n g f r o m , a n d t h e n t h e l e n g t h o f t h e ba r
: -
Se c t i o n - 1 - 5
Mu l t i p l e ba u d s e r i a l LC D c o n t r o l l e r
x
e
0
e
SW2
+ SV
VR1
50 k
R2
1 0 0 k
0
De bu g l ,
B a r , Li n e 1 , Le n g t h o f _B a r : P a u s e 1
`Le n g t h o f B a r ' m a y be a va l u e o f 0 t o 59 i f a 4 x 2 0 d i s p l a y i s u s e d , o r a
va l u e o f 0 t o 4 7 f o r a 2 x 1 6 d i s p l a y . Th e P AUSE c o m m a n d a l l o w s t h e
s e r i a l c o n t r o l l e r t i m e t o d o t h e ba r g r a p h s u br o u t i n e
.
Th e B a r g r a p h s u br o u t i n e i s i n t h e f o r m o f a n i n c l u d e f i l e , w h i c h i s l o a d e d
i n a f t e r t h e LC D h a s i n i t i a l i s e d . Th e i n c l u d e f i l e B ARGRAF2
. I NC i s f o r
u s e w i t h a 2 x 1 6 LC D, a n d
B ARGRAF4 . I NC i s f o r a 4 x 2 0 LC D. Th e c o d e
i s f u l l y c o m m e n t e d
. Th e s e r i a l c o n t r o l l e r p r o g r a m MULTI LC D2 . B AS i s
f o r u s e w i t h 2 x 1 6 LC D m o d u l e s , a n d p r o g r a m
MULTI LC D4 . B AS i s f o r
u s e w i t h 4 x 2 0
LC D m o d u l e s . Th e p r o g r a m SE R TE ST
. B AS
d e m o n s t r a t e s t h e u s e o f t h e ba r g r a p h o p t i o n
.
I C o n
2 54 `C o n t r o l B y t e
B a r C o n 2 53
`B a r d i s p l a y i n i t i a t e 3 . 6 Vo l t s
Li n e 1 C o n 1 2 8 `Di s p l a y l i n e 1 I I I I I I I I I I I I I I I I I I I I I I I I I I I I I I I I I I I I
ExperimentingwiththePicBasicProCompiler
Contrastcontrolf oranL CD mod u le
I f acontrastcontrolisneed ed , itissimpleenou ghtoad d asmallpreset
potentiometerconnected tothe V o
pinof theL CD , asinf igu rel . 4 .
Contrastincreasesasthepotistu rned toward sgrou nd and thev oltage
onPin V o d ecreases . A lternately , af ixed resistorwithav alu eof af ew
hu nd red ohmscanb econnected f rom V o togrou nd .
+5V
V R I I
50k
I NTEL L I GENTL CD
MOD UL E
mmmm °m 2y o
00

aaaa W
j r
>>>
11111111111
GND F igu rel
. 4
. L CD Contrastcontrol
.
Extend ed - temperatu reL CD mod u lesontheotherhand , req u irea
negativ ev oltageapplied topinV o, thiscanb eachiev ed withaswitch-
mod enegativ ev oltageconv erter, su chastheMA X I M1CL 7 6 6 0 . A s
showninf igu rel . 5 .
+5V I N
V D D
I CL 7 6 6 0
CA P-
V OUT
CA P+
GND
C2
10u f
Section- 1 - 6
Mu ltipleb au d serialL CD controller
F igu rel . 5 . Switch- mod enegativ ev oltagegenerator .
ExperimentingwiththePicBasicProComp
il er
Programs - 5CC_DISP.BAS
Drivingmul tipl exed7- segmentLEDdispl ays
ThemainconsiderationwhendesigninganinterfacetoanLEDdispl ayis
thenumberofpinsavail abl eonthePIC .Todriveafivedigitnon-
mul tipl exeddispl aywoul drequireaPICwith45I/Opins,oneforeach
segment
.Thisisofcourseimpractical therefore,mul tipl exingisal most
universal l yadopted .W hichwil l stil l tak e1 3 pins,butonthel argerPICs
with3 3 I/Osthisisnotusual l yaprobl em
.
Asmostofyouwil l al readyk now,mul tipl exingisaccompl ishedbydriving
eachdispl ayinsequence .Aseachdispl ayisturnedon,thesegment
datafromthePICissettothecorrectpatternforthatdigit .Thepatterns
foreachdigitareshownintabl e1
.1 .
Tabl e1
.1 .Binarypatternfor7- segmentdigits .
Toil l ustratehowasingl edigitisdispl ayed,wewil l l ook atdigits4and5
.
Thebinarypatternfordigit4is%01 1 001 1 0,andfordigit5itis
%01 1 01 1 01 .F igurel
.6showshowthesebinarypatternsrel atetothe
segmentstoil l uminate .
OF F 0
ON1 ON,
OF F 0
OF F 0
ON1
ON1
A
D
0

A

0

EC
7)
ON1
OF F 0
ON1
OF F 0
ON1
ON1
ON1
A
0
G
es> •
IF

e
E

C I
F igurel
.6
.Binaryrel ationshiptoil l uminatedsegments .
Remember,thatthe'A'segmentisattachedtotheLSBofthebinary
number
.
Section- 1
- 7
DigitDispl ayed
Binary val ueon A- Gsegments Decimal
F E C B
A
0 0
1 1 1 1 1 1 63
1 0 0 0 0
1 1 0 6
2
1 0 1 1 0 1 1 9 1
3 1 0 0
1 1 1 1 79
4 1 1 0 0 1 1 0 1 02
5 1 1 0
1 1 0 1 1 09
6 1 1 1 1 1 0 0 1 2 4
7 0 0
0 1 1 1 7
8 1 1 1 1
1 1 1 1 2 7
9
1 1 0 0 1 1 1 1 03
ExperimentingwiththePicBasicProCompiler
Common
F igu rel . 7 . I nd iv id u alL ED swithinacommoncathod ed isplay .
By examiningf igu rel
. 7 wecanseethatapply ingapprox2 V totheanod e
of aparticu larsegmentL ED , whilethecommonlineisconnected to
grou nd anind iv id u alsegmentmay b eillu minated .
T omu ltiplexmorethanoned isplay , req u iresu stotak econtrolof their
ind iv id u alcathod es . T hisisachiev ed b y atransistoractingasaswitch,
asshowninf igu rel . 8 .
A

B

C

D

E

F

a

D P
F igu rel . 8 . T ransistorswitch .
Alogichighontheb aseof thetransistorwillswitchiton, thu spu llingthe
commoncathod estogrou nd . R 2 limitsthecu rrentthatcanf lowb etween
theind iv id u alsegmentL ED s . R 1 limitsthev oltagesu pplied totheb ase
of thetransistor .
W enowhav ethemeanstoswitcheachd isplay onintu rn, aswellasthe
inf ormationreq u ired toillu minateaspecif icd igit
. W hat' sreq u ired nowis
ameansof tu rningonad isplay , illu minatethecorrectd igitand d othe
samethingf orthenextones, q u ick ly enou ghtof ooltheey eintothink ing
itisseeingallthed isplay sillu minated atonce .
D riv ingmu ltiplexed 7 -segmentd isplay s
Connectingthed isplay tothePI Cisu ncomplicated . T heA-segment
connectstoPortCb it-0 , and theG -segmentconnectstoPortC b it-6 .
S egmentsB . . F connecttothepinsinb etween . T hed ecimal pointis
connected tob it-7 of thesameport .
I nthisd emonstration, weshallb eu singcommoncathod ed isplay s . As
thenamesu ggests, allthecathod esf ortheind iv id u alsegmentL ED sare
connected togetherinternally , asshownb elowinf igu rel . 7 .
A

B

C

D

E

F

G

D P
S ection-1 -
8
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
Dr i vi n g m ul t i p l e x e d 7-s e g m e n t di s p l a ys
An d a l l w i t h i n t h e ba c kg r o un d, t o a l l o w t h e p r o g r a m t o p r o c e s s t h e a c t ua l
i n fo r m a t i o n t o be di s p l a ye d
. Th i s i s a p e r fe c t a p p l i c a t i o n fo r a TMRO
i n t e r r up t us i n g t h e c o m p i l e r 's ON INTE RRUP T c o m m a n d
.
P r o g r a m 5C C _DISP .B AS
s h o w s a w a y o f di s p l a yi n g a fi ve -di g i t n um be r
o n fi ve , 7-s e g m e n t di s p l a ys
. B e c a us e t h e fi ve di s p l a ys r e qui r e 13 I/O
p i n s , t h e p r o g r a m i s i n t e n de d t o be us e d o n o n e o f t h e n e w e r 16F87X
r a n g e o f P IC s , a n d a l s o a s s um e s a 20m Hz o s c i l l a t o r i s be i n g us e d .
Fi g ur e l
.10 s h o w s t h e c i r c ui t l a yo ut fo r t h e de m o n s t r a t i o n .
Th e fi r s t t h i n g t h e p r o g r a m do e s i s i n i t i a t e a TMRO i n t e r r up t ( a s
s h o w n i n
t h e p r o g r a m m i n g t e c h n i que s s e c t i o n ) t o g e n e r a t e a n i n t e r r up t e ve r y
1 .6384m s , by s e t t i n g t h e p r e s c a l e r t o 1
: 32 . To c a l c ul a t e t h e r e p e t i t i ve
r a t e o f t h e i n t e r r up t us e t h e fo l l o w i n g fo r m ul a
: -
In t e r r up t r a t e ( i n us ) = ( ( OSC / 4) * 256) * p r e s c a l e r r a t i o
Wi t h i n t h e i n t e r r up t h a n dl e r r o ut i n e , t h e di g i t o f i n t e r e s t s p a t t e r n i s
e x t r a c t e d by us i n g t h e LOOKUP c o m m a n d, w h e r e a s p e c i fi c p a t t e r n
c o r r e s p o n ds t o a c e r t a i n n um be r h e l d i n t h e a r r a y NUM[O_C ]. Th e
p a t t e r n e x t r a c t e d fr o m t h e l o o kup t a bl e i s p l a c e d i n t o t h e va r i a bl e
DISP P ATT . Th e va r i a bl e 0_C h a s a dua l p ur p o s e
; i t s m a i n p ur p o s e i s
t o fo r m a s o r t o f
t i m e -s h a r e fo r t h e i n di vi dua l di s p l a ys
. On e a c h i n t e r r up t ,
t h e va r i a bl e O C i s i n c r e m e n t e d, a n d e a c h di s p l a y w a i t s fo r i t s p a r t i c ul a r
t i m e -s l o t be fo r e i t i s t ur n e d o n
. Th i s w a y e a c h di s p l a y i s t ur n e d o n fo r
a p p r o x 1 .6m s s p r e a d o ve r fi ve i n t e r r up t s , c a us i n g a n o ve r a l l s c a n r a t e o f
a bo ut 125Hz
.
Wi t h i n e a c h di s p l a y's t i m e -s l o t , t h e p r e vi o us di s p l a y i s t ur n e d o ff a n d t h e
va l ue h e l d i n DISP P ATT i s p l a c e d o n t o P o r t C
. A c h e c k i s t h e n m a de o f
t h e va r i a bl e
DP w h i c h h o l ds t h e de c i m a l p o i n t p l a c e m e n t . If DP
h o l ds t h e
va l ue o f t h e di s p l a y w e a r e c ur r e n t l y us i n g , t h e de c i m a l p o i n t i s t ur n e d o n
by s e t t i n g bi t -7 o f P o r t C
. Th e di s p l a y i t s e l f i s t h e n t ur n e d o n by s e t t i n g
t h e p a r t i c ul a r bi t o f P o r t B h i g h . No t e . DP
m a y h o l d a va l ue be t w e e n 0 . .5
w h e r e 1 i s t h e fa r t h e s t r i g h t di s p l a y, a n d ze r o di s a bl e s t h e de c i m a l p o i n t
.
Wh i l e t h e i n t e r r up t g i ve s us a m e a n s o f di s p l a yi n g fi ve di g i t s , t h e
s ubr o ut i n e DISP LAY do e s t h e p r o c e s s i n g o f t h e a c t ua l n um be r t o
di s p l a y
. Th e s ubr o ut i n e fi r s t di s a bl e s t h e i n t e r r up t t o e l i m i n a t e a n y
g l i t c h e s t h a t m a y be vi s i bl e w h i l e p r o c e s s i n g t h e n um be r s , t h e n i t s p l i t s
Se c t i o n -1 -
9
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
P r o g r a m s -5,4,3,2C C _DISP .INC , MULT TST .B AS

Dr i v i n g m u l t i p l e x e d 7 -s e g m e n t d i s p l a y s
t h e i n d i v i d u a l d i g i t s f r o m t h e 1 6 -b i t n u m b e r h e l d i n D_NUMB E Ru s i n g t h e
DIG o p e r a n d . E a c h d i g i t i s p l a c e d i n t o t h e f i v e e l e m e n t a r r a y
NUM, a n d a
s e r i e s o f i f -t h e n ' s ze r o s u p p r e s s t h e u n u s e d d i g i t s . Af t e r a l l t h e d i g i t s
h a v e b e e n p r o c e s s e d , t h e i n t e r r u p t i s r e -e n a b l e d a n d t h e s u b r o u t i n e i s
e x i t e d .
To a i d i n t h e u s e o f m u l t i p l e x i n g t h e d i s p l a y s , s e v e r a l i n c l u d e f i l e s h a v e
b e e n d e v e l o p e d f o r u s e w i t h 2 t o 5 d i s p l a y s . Th e i n c l u d e f i l e o f c h o i c e
s h o u l d b e p l a c e d a t t h e t o p o f t h e p r o g r a m a f t e r t h e MODE DE FS .B AS
f i l e h a s b e e n i n c l u d e d .
Th e i n c l u d e f i l e 5C C _DISP .INC i s f o r u s e w h e n 5 d i s p l a y s a r e r e qu i r e d
Th e TMRO i n t e r r u p t w i l l a u t o m a t i c a l l y b e e n a b l e d u p o n t h e p r o g r a m ' s
s t a r t . It a l s o c o n t a i n s t h e s u b r o u t i n e DISP LAY w h i c h e x p e c t s t w o
v a r i a b l e s t o b e p r e -l o a d e d b e f o r e i t i s c a l l e d . Th e f i r s t v a r i a b l e ,
D -NUMB E Rh o l d s t h e 1 6 -b i t v a l u e t o b e d i s p l a y e d . Th e s e c o n d v a r i a b l e ,
DP h o l d s t h e p o s i t i o n o f t h e d e c i m a l p o i n t (0. .5) : -
D_NUMB E R = 1 2345

`Di s p l a y t h e n u m b e r 1 2345
DP = 0

' Do n o t p l a c e t h e d e c i m a l p o i n t
Go s u b Di s p l a y

`Di s p l a y t h e n u m b e r
Th e i n c l u d e f i l e 4C C _DISP .INC i s f o r u s e w h e n 4 d i s p l a y s a r e r e qu i r e d .
Ag a i n , t h e TMRO i n t e r r u p t i s e n a b l e d o n t h e p r o g r a m ' s s t a r t . Th e s a m e
t w o v a r i a b l e s n e e d t o b e p r e -l o a d e d b e f o r e t h e DISP LAY s u b r o u t i n e i s
c a l l e d
. Ho w e v e r ,
DP
n o w h a s t h e r a n g e 0
. .4
.
Th e i n c l u d e f i l e s 3C C _DISP .INC a n d 2C C _DISP .INC a r e f o r u s e w i t h 3
a n d 2 d i s p l a y s r e s p e c t i v e l y .
Th e v a r i a b l e s , D NUMB E Ra n d DP a r e a l r e a d y p r e -d e c l a r e d w i t h i n t h e
i n c l u d e f i l e , t h e r e f o r e , t h e r e i s n o n e e d t o d e c l a r e t h e m i n y o u r p r o g r a m .
Th e p r o g r a m DISP TST.B AS d e m o n s t r a t e s t h e u s e o f 2 t o 5 m u l t i p l e x e d
d i s p l a y s , b y u n c o m m e n t i n g t h e r e qu i r e d i n c l u d e f i l e . Th e p r o g r a m
i n c r e m e n t s a 1 6 -b i t v a r i a b l e , w h i c h i s d i s p l a y e d o n t h e 7 -s e g m e n t LE Ds .
Ho w e v e r , t h i s l o o p c o u l d e a s i l y b e r e p l a c e d b y t h e ADC IN c o m m a n d f o r
d i s p l a y i n g t h e v o l t a g e c o n v e r t e d . Or a t e m p e r a t u r e r e a d i n g r o u t i n e .
Se c t i o n -l - 1
0
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
P r o g r a m s -5,4,3,2C A_DISP .INC , MULT TST.B AS

Dr i v i n g m ul t i p l e x e d 7-s e g m e n t di s p l a y s
Subs t i t ut i n g C o m m o n An o de di s p l a y s
If c o m m o n a n o de di s p l a y s a r e s ubs t i t ut e d fo r t h e c o m m o n c a t h o de t y p e s
t h e n a s l i g h t r e -a r r a n g e m e n t o f t h e s w i t c h i n g t r a n s i s t o r s i s r e qui r e d, a s
s h o w n i n fi g ur e l .9 .
To P o r t B
To P o r t C
+5V
A
-I
0
D
E

C
D
-DP ,
B
C
D
E
F
a
DP
C o m m o n
n o a o
Fi g ur e l
.9
. C o m m o n An o de di s p l a y .
A s l i g h t di ffe r e n c e i n t h e c o de i s a l s o r e qui r e d . Th e m a i n di ffe r e n c e i s t h e
p a t t e r n s t h a t m a ke up t h e di g i t s . Wh e n c o m m o n c a t h o de s w e r e us e d, a
h i g h o n t h e s e g m e n t s i l l um i n a t e d t h e m , but fo r c o m m o n a n o de s , a l o w
o n t h e s e g m e n t s i s r e qui r e d
. Th e r e fo r e , t h e p a t t e r n s s h o w n i n t a bl e 1 .1
n e e d t o be i n v e r t e d i .e . %11111100 be c o m e s %00000011
. Th i s c a n
e a s i l y be a c h i e v e d by p l a c i n g n e w v a l ue s i n t o t h e LOOKUP c o m m a n d
w i t h i n t h e i n t e r r up t h a n dl e r . Th e n e w p a t t e r n s a r e s h o w n be l o w : -
[19 2,249 ,164,176,153,146,131,248,128,152,255]
On e o t h e r t h i n g t h a t r e qui r e s a l t e r i n g i s t h e de c i m a l p o i n t p l a c e m e n t .
P r e v i o us l y P o r t C .7 w a s s e t h i g h t o t ur n o n t h e p o i n t , but n o w i t n e e ds t o
be p ul l e d l o w . Th i s a g a i n i s e a s i l y r e m e di e d s i m p l y by c h a n g i n g t h e l i n e s
c o r r e s p o n di n g t o P o r t C .7 i n t h e i n t e r r up t h a n dl e r .
Al l t h e p r e v i o us p r o g r a m s a n d i n c l ude fi l e s di s c us s e d h a v e a l r e a dy be e n
a l t e r e d fo r us e w i t h c o m m o n a n o de di s p l a y s a n d m a y be fo un d i n t h e
C OM ANOD fo l de r .
Se c t i o n -l - 1 1
ExperimentingwiththePicBasicProCompiler
S ection- l- 1 2
Drivingmultiplexed7- segmentdisplays
F igurel . 1 0 . 5 - digitmultiplexed
commoncathodedisplay .
ExperimentingwiththePicBasicProCompiler
PSO=0
PS1 =1
PS2=0
Drivingmultiplexed7-segmentdisplays
W henusingthemultiplexerinyourownprogram, youmustrememb er
thatitisusingthecompiler' sON I N T ER R U PT command
. A ndassuch
theprecautionsandwork -arounds
explainedinthe programming
techniq uessection shouldb eob served .
I f anoscillatorof lessthan20mH z isreq uired, thentheprescalevalueof
theinterruptshouldb edecreased
. Especiallyif morethanf ourdigitsare
b eingutiliz ed, otherwiseaslightf lick eringof thedisplaywillb enoticed
.
T hisiseasilyaccomplishedb ychangingthethreelinesinthe
include
f ilesthatcontrolthePSO, PS1 , andPS2b itsof OPT I ON -R EG . F or
example, tousea4 mH z oscillatorwithaf ivedigitdisplay, thef ollowing
changesshouldb emade : -
` A ssigna1
: 8 prescalertoT M R O
Byexaminingtheincludef ilesf orthedif f erentamountof multiplexed
displays, youwillnoticethatastheamountof displaysisreducedthen
theinterruptrateisalsodecreased . T hemainreasonf orthisisthat, as
theinterrupthandlerisprocessingitsmultiplexingcode, themain
programishalteduntiltheinterruptisover, thusultimatelyslowing it
down
. T helesstimesaninterrupthandlerneedstob ecalledtheq uick er
themainprogramb ecomes .
A f inalnoteonmultiplexing
: W henreducingtheamountof displaysused,
alwaysremovethemostsignif icantdigits
. F orexample, if 4 displaysare
usedinsteadof 5 thenremovedisplaynumb er4 , whichisthelef tmost
digit .
Section-l- 1 3
ExperimentingwiththePicBasicProCompiler
Program- MAX_CNT.BAS
I nterf acingtotheMAX7 2 1 9
TheMAX7 2 1 9 iscapab leof d riv ingu ptoeightcommon- cathod esev en-
segmentL ED d isplay su singathreewire
( sy nchronou sserialinterf ace)
.
I tcanalsoconv ertb inary - cod ed
d ecimal ( BCD ) v alu esintotheir
appropriate patterns of segments
.And hasb u ilt- in pu lse- wid th
mod u lationand cu rrent- limitingcircu itstocontroltheb rightness
of the
d isplay swithonly asingleexternalresistor .
W itheightL ED d isplay sattached , theMAX7 2 1 9 isab letoscanthemat
ov er1 2 0 0 H z , thu sprev entingany d isplay f lick er
.I f ad isplay of lessthan
eightL ED sisu sed , thechipmay b econf igu red toscanonly theone' s
connected , increasingtheb rightnessand scanningf req u ency
of the
d isplay .W ithallof itscomplexity onewou ld expecttheMAX7 2 1 9 tob e
d if f icu lttocontrol, b u tq u itetheoppositeistru e
.W ithj u staf ewlinesof
cod eav ersatileL ED d isplay canb erealiz ed and withonly threepins
( d atain, clock , and load )
req u ired onthePI C, ev enthe8 - pind ev ices
may b eu sed .
ConnectiontotheL ED d isplay sisstraightf orward , pinsSEG - Athrou gh
SEG - G and SEG D P- connecttosegmentsAthrou ghG and thed ecimal
pointof allof thecommon- cathod ed isplay s
. PinsD I G I T- 0 throu gh
D I G I T- 7 connecttothecathod esof eachof thed isplay s
. F igu rel .1 0
showsaty picalsetu pu singf ou rL ED d isplay s, interf aced inthis
case
withaPI C1 6 F 8 4 .
R esistorR 2 setsthecu rrentthrou gheachL ED d isplay
.Thesmallerthis
resistoris, thegreaterthecu rrentthrou gheachsegment ( minimu mv alu e
= 9 .5 3 k o) , av alu eof 1 0 k osetsthecu rrentto4 0 mAperd isplay
.R 3 isa
pu lld ownresistorontheinterf aceb etweenthePI Cand theMAX7 2 1 9
L O AD pin, thisisreq u ired b ecau sewhenaPI Cresets, itsportsare
initialised asinpu ts
.They areef f ectiv ely d isconnected , theref ore,
any thingconnected tothemisalsod isconnected , and aref loating
.Su ch
inpu tsf req u ently f loathigh, howev er, electricalnoisecancau sethemto
changestatesatrand om, thiswillnormally cau setheMAX7 2 1 9 togo
intotestmod ewithallsegmentslit
.Theref ore, R 3 prev entsthisb y
pu llingtheload pinmoretogrou nd whennotinu se .
Section- 1 - 1 4
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
-AN
O
0
f O/
9

m

$
N

r
O!

p
$

00
I >
0 ;
7Zv
4
V
N
i
O

f 0
00000000
i 7 c ~ i 7 c ~ c ~ c ~ G
O+N(JAUI 6 J
~ul e l ul m
1 1
1 1
1 1
A
m l °I °I -I NI
0
O
e
m m m m m m m m
60000000
0 0000000
0
0
1 91 01 01 I I I
I ' 3 1 .
id
. T m
m o
Fi g ur e 1 .1 0. MAX721 9 LE D di s p l a y c o n t r o l l e r .
S e c t i o n -1 -1 5
a
n
O
N
x
D
D
D
I n t e r f a c i n g t o t h e MAX721 9
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
Ta bl e 2 . 1
. Re g i s t e r s w i t h i n t h e MAX721 9
D i g i t - 0 -
D i g i t - 7 p o i n t t o t h e r e l e v a n t d i s p l a y s a t t a c h e d , d i g i t - 0 i s t h e f a r -
r i g h t d i s p l a y .
D e c o d e
e n a bl e s o r d i s a bl e s B C D d e c o d i n g f o r e a c h i n d i v i d ua l d i s p l a y
(%1 0 0 0 0 0 0 1 w o ul d e n a bl e B C D o n d i s p l a y s 0 a n d 7)
.
In t e n s i t y
s e t s t h e o v e r a l l br i g h t n e s s o f t h e d i s p l a y s (0 t o 1 5 ) .
Sc a n Li m i t
i n f o r m s t h e MAX721 9 a s t o h o w m a n y d i s p l a y s a r e a t t a c h e d
(0 - 7) .
Sh ut d o w n , p l a c e s t h e MAX721 9 i n s t a n d by m o d e w h e n c l e a r e d
.
Te s t , p l a c e s t h e MAX721 9 i n t e s t m o d e w h e n s e t t o 1
(m a x i m um
br i g h t n e s s a n d a l l s e g m e n t s o n )
.
Wh e n s e n d i n g d a t a t o t h e MAX721 9 i t e x p e c t s a p a c ke t c o n s i s t i n g o f a
1 6- bi t w o r d c o n t a i n i n g t h e r e g i s t e r n um be r a n d t h e n t h e v a l ue t o be
p l a c e d w i t h i n t h e r e g i s t e r : -
F i r s t by t e 1 1

p o i n t s t o t h e s c a n l i m i t r e g i s t e r
Se c o n d by t e 3

i n f o r m s t h e MAX t h a t 4 LE D s a r e be i n g us e d
Th e 1 6- bi t s a r e c l o c ke d i n t o t h e MAX721 9, r e g a r d l e s s o f t h e s t a t e o n t h e
LOAD p i n
. Ho w e v e r , t h e y a r e o n l y a c t e d up o n w h e n t h e LOAD p i n i s
c l o c ke d h i g h t o l o w , w h i c h h a s t h e s e c o n d a r y e f f e c t o f d i s a bl i n g t h e
d e v i c e a f t e r t h e d a t a i s s e n t .
In t e r f a c i n g t o t h e MAX721 9
Th e r e a r e 1 4 a d d r e s s a bl e r e g i s t e r s w i t h i n t h e MAX721 9, t a bl e 1
. 1 s h o w s
a l i s t o f t h e m
.
Se c t i o n - l - 1 6
Re g i s t e r Ad d r e s s
D e s c r i p t i o n
NOP 0 No Op e r a t i o n
D i g i t 0 1 Th e f i r s t LE D D i s p l a y
D i g i t 1 2 Th e s e c o n d LE D D i s p l a y
D i g i t 2 3 Th e t h i r d LE D D i s p l a y
D i g i t 3 4 Th e f o ur t h LE D D i s p l a y
D i g i t 4
5 Th e f i f t h LE D D i s p l a y
D i g i t 5
6 Th e s i x t h LE D D i s p l a y
D i g i t 6
7 Th e s e v e n t h LE D D i s p l a y
D i g i t 7 8
Th e e i g h t h LE D D i s p l a y
D e c o d e Mo d e 9
B C D d e c o d i n g On /Of f
In t e n s i t y 1 0
B r i g h t n e s s o f D i s p l a y s
Sc a n Li m i t 1 1
Am o un t o f D i s p l a y s a t t a c h e d
Sh ut d o w n 1 2 P l a c e c h i p i n t o St a n d by
Te s t 1 5 Te s t m o d e On /Of f
ExperimentingwiththePicBasicProCompiler
I nterf acingtotheM A X 7 2 1 9
Program M A X -CNT . BA S
showsasimpleapplicationof theM A X 7 2 1 9 . I n
theprogram, a1 6 -b itintegerheld
inthev ariab le COUNTER is
incremented and thend ecremented , thisisd isplay ed onthef ou r7 -
segmentL ED s . F irsttheM A X 7 2 1 9 isinitialised b y load ingthe S can
registerwith 3 (4d isplay s
attached ) , theL u minanceregisterwith3,
D ecod eregisterwith% 0 0 0 0 1 1 1 1 , thiswillconf igu rethef irst4d isplay sto
BCD d ecod ing, thentheS witchregisterissettoone, whichwillwak eu p
theM A X 7 2 1 9 and f inally theTestregisteriscleared .
Thecou ntu p-d ownrou tinethenplacesthepositionof thed ecimalpoint
in M A X _ D P, (M A X D Pmay containav alu eb etween0 . .
. 7 , z erob eing
theright-mostd isplay ) , and thev alu eof COUNTER into M A X _ D I S P . The
su b rou tine D I S PL A Y
isthencalled , thisextractstheseparated igitsf rom
thev ariab le M A X _ D I S P, u singtheD I G operand , and d isplay sthemon
theappropriateL ED s . Notethez erosu ppression, thisissimply aseries
of if then' s
thatb lank thed igitsb y send ingthev alu e1 5 whenthed isplay
isnotb eingu sed
. Thissu b rou tineitself calls anothernamed
TRA NS F ER,
whichshif tsou tthetwob y testhenstrob estheL OA D pin
lowthenhigh, thistransf ersthed ataintotheinternalregisters
of the
M A X 7 2 1 9 .
I f moreorlessd isplay sare
u sed , changethev alu eplaced in
S CA N_ REG (thisislocated intheinitialisationsectionof cod e) ,
tothe
appropriateamou ntof L ED sattached (0 -7 ) .
A lsowithinthesu b rou tineD I S PL A Y, changethelines
:
F orPosition= 4 to1 step-1
into
F orPosition= nto1 step-1
Where nis thenu mb erof L ED sattached
(1 -8 )
I f D igit> = 3thenD igit= 0
I nto
I f D igit> = nthenD igit= 0
Where nis thenu mb erof d igitsinthev ariab le M A X _ D I S P(0 -4) ,
in
PicBasicProthemaximu mamou ntof d igitsisf iv e (0 to6 5 5 35 )
S ection-l- 1 7
ExperimentingwiththePicBasicProCompiler
Section-2
Interfacing
with
Keypads
Keypadinterfacingprincipals .
Interfacingwitha12-buttonkeypad
.
Interfacingwitha16-buttonkeypad .
Serialkeypadcontroller .
Receivingdatafromthekeypadcontroller .
Assemblercoded,keypaddecoder .
Usingthepseudocommand,INKEYS .
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
P r o g r a m s - KE YP AD12
.B AS, KE YTST12.B AS a n d INKE YS12.INC
P r o g r a m s
- KE YP AD16 .B AS, KE YTST16.B AS a n d INKE YSI6.INC
Ke yp a d i n t e r fa c i n g p r i n c i p a l s
In t e r fa c i n g t o a fe w but t o n s i s s i m p l e , but w h e n m o r e a r e r e qui r e d, a
ke yp a d i s a l m o s t e s s e n t i a l . In t h i s e x p e r i m e n t , w e s h a l l l o o k a t t h e
p r i n c i p a l s o f h o w a ke yp a d w o r ks a n d w r i t e a s ubr o ut i n e t o a c c e s s i t
.
Fi g ur e 2
.1 s h o w s t h e a r r a n g e m e n t o f a 12-but t o n a n d 16-but t o n ke yp a d .
As c a n be s e e n t h e y a r e a r r a n g e d a s a m a t r i x , t h i s m i n i m i ze s t h e a m o un t
o f I/O l i n e s n e e de d, o t h e r w i s e 12 o r 16 i n p ut s w o ul d h a ve t o be us e d t o
i n t e r fa c e t o t h e s a m e a m o un t o f ke ys
. B y a r r a n g i n g t h e ke ys i n t o Ro w s
a n d C o l um n s w e o n l y r e qui r e 7 o r 8 i n p ut s t o o p e r a t e i t , h o w e ve r , t h e
p r i c e t o p a y i s t h a t a ke yp a d s c a n n i n g r o ut i n e m us t be e m p l o ye d
.
12-B ut t o n ke yp a d m a t r i x
C o l um n s
5

6

7
Fi g ur e 2.1 .
Th e ke yp a d s c a n n i n g r o ut i n e s ys t e m a t i c a l l y s e a r c h e s fo r a ke y p r e s s
. It
s t a r t s by s e t t i n g t h e c o n n e c t i o n s t o t h e c o l um n p i n s a s i n p ut s a n d t h e
c o n n e c t i o n s t o t h e r o w p i n s a s o ut p ut s
. Th e i n p ut s a r e h e l d h i g h by t h e
i n t e r n a l p ul l up r e s i s t o r s . Th e o bje c t o f t h e s e a r c h i s t o fi n d o ut w h e t h e r
o n e o f t h e r o w s o f t h e ke yp a d i s c o n n e c t e d t o o n e o f t h e c o l um n s , a n d i f
s o w h i c h o n e
. Th e s c a n r o ut i n e p ul l s o n e o f t h e r o w l i n e s l o w , t h e n l o o ks
a t t h e c o l um n s i n p ut t o s e e w h e t h e r a 0 i s de t e c t e d
. If n o t , i t t h e n t r i e s
t h e n e x t r o w ; t h i s i s c o n t i n ue d un t i l a l l t h e r o w l i n e s h a ve be e n s c a n n e d .
Th e r e a r e a s m a n y ke yp a d s c a n n i n g r o ut i n e s a s t h e r e a r e p r o g r a m m e r s .
E a c h p r o g r a m m e r h a s h i s /h e r w a y o f do i n g t h i n g s
. Ho w e ve r , w h i c h e ve r
w a y g e t s t h e jo b do n e e ffe c t i ve l y i s OK.
Se c t i o n -2 - 1
16-B ut t o n ke yp a d m a t r i x
C o l um n s
6

7
5 8
E x p e r i m e n t i n g w i t h t h e P I c B a s i c P r o C o m p i l e r
I n t e r fa c i n g w i t h a 12-but t o n ke yp a d
Th e p r o g r a m KE YP AD12 .B AS, a n d t h e c i r c ui t s h o w n i n fi g ur e 2
.2
de m o n s t r a t e t h e us e o f a 12-but t o n ke yp a d . Th e p r o g r a m s c a n s t h e
ke yp a d a n d di s p l a ys t h e va l ue o f t h e ke y p r e s s e s o n a s e r i a l LC D
m o dul e c o n n e c t e d t o P o r t B .7
. I t i s ba s e d a r o un d t h e ke yp a d s c a n n i n g
s ubr o ut i n e I NKE YS . Wh e n t h i s s ubr o ut i n e i s c a l l e d, t w o va r i a bl e s a r e
r e t ur n e d . Th e fi r s t va r i a bl e i s KE Y, w h i c h h o l ds t h e va l ue o f t h e ke y
p r e s s e d
(128 i f n o ke y p r e s s e d), t h e s e c o n d va r i a bl e r e t ur n e d i s
DE B OUNC E , w h i c h (a s yo u m i g h t h a ve g ue s s e d) i s
a de bo un c e fl a g .
Th i s r e t ur n s h o l di n g a ze r o i f a ke y h a s be e n p r e s s e d, h o w e ve r , w h e n
t h e I NKE YS s ubr o ut i n e i s c a l l e d a s e c o n d t i m e a n d a ke y i s s t i l l i n us e , a
va l ue o f o n e i s r e t ur n e d
. On e p o s s i bl e us e o f t h i s fe a t ur e c o ul d be : -
Ma i n :
Go s ub I n ke ys

'Go s c a n t h e ke yp a d
i f De bo un c e = 1 t h e n g o t o Ma i n

'Go ba c k i f but t o n i s s t i l l h e l d
Wi t h i n t h e I NKE YS s ubr o ut i n e t h e va r i a bl e DE B OUNC E i s i n i t i a l l y s e t t o
1, t h e n t h e fi r s t fo ur bi t s o f P o r t A a r e c o n fi g ur e d a s o ut p ut s (r o w s ), a n d
t h e fi r s t t h r e e bi t s o f P o r t B a r e s e t up a s i n p ut s
(c o l um n s ) . C a r e h a s be e n
t a ke n t o c o n fi g ur e o n l y t h e r e l e va n t bi t s t h a t t h e ke yp a d i s a t t a c h e d t o
.
Th e i n t e r n a l w e a k p ul l up r e s i s t o r s a r e e n a bl e d a n d t h e fi r s t r o w i s p ul l e d
l o w (P o r t A.3 ), t h e s ubr o ut i n e SC ANC OL i s t h e n c a l l e d, t h i s e x a m i n e s t h e
c o l um n l i n e s i n t ur n a n d i n c r e m e n t s t h e va r i a bl e KE Y w h e n a ke yp r e s s
h a s n o t be e n de t e c t e d, t h i s w i l l bui l d up 13 n um be r s c o r r e s p o n di n g t h e a
c e r t a i n ke yp r e s s o r n o ke yp r e s s (a l be i t i n t h e w r o n g o r de r ) . On r e t ur n i n g ,
t h e va r i a bl e K_ FLAG w i l l h o l d 1 i f a ke yp r e s s w a s de t e c t e d o t h e r w i s e i t
h o l ds 0
. Th e va r i a bl e K_FLAG i s e x a m i n e d a ft e r i t s r e t ur n , t o a s c e r t a i n
w h e t h e r t o s c a n t h e n e x t r o w o r t o p r o c e s s t h e va l ue h e l d i n KE Y
. I f
K FLAG r e t ur n e d 0, t h e n t h e s a m e p r o c e dur e i s c a r r i e d o ut fo r a l l fo ur
r o w s
. Ho w e ve r , i f K FLAG r e t ur n e d a 1 t h e n t h e de bo un c e fl a g s a r e s e t
o r c l e a r e d a c c o r di n g l y t o t h e va l ue h e l d i n D -FLAG .
Th e va r i a bl e KE Y i s
r e -a r r a n g e d t o c o r r e s p o n d t o t h e ke yp a d l e g e n ds , by us i n g t h e LOOKUP
c o m m a n d
: -
Ma p o f t h e ke yp a d l e g e n ds fo r n um e r i c o ut p ut
Lo o kup Ke y,[1,2,3 ,4,5,6,7 ,8,9,10,0,11,128J,Ke y
Se c t i o n -2 - 2
I n t e r fa c i n g w i t h a ke yp a d
5 V o l t s
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
In t e r fa c i n g w i t h a ke yp a d
F o r e x a m p l e , i n i t s r a w s t a t e , K E Y w i l l h o l d t h e v a l u e 0 i f t h e o n e ke y h a s
be e n p r e s s e d, 10 i f t h e ze r o ke y h a s be e n p r e s s e d, a n d 12 i f n o
ke yp r e s s h a s be e n de t e c t e d, t h e r e fo r e , t h e t h i r t e e n v a l u e s w i t h i n t h e
br a c e s o f t h e LOOK UP c o m m a n d c o r r e s p o n d t o t h e r a w K E Y v a l u e s a n d
t h e e x p e c t e d ke yp a d l e g e n d v a l u e s
.
Th e p r o g r a m K E Y TST12. B AS do e s t h e s a m e a s
K E Y P AD12. B AS, bu t
t h e INK E Y S
s u br o u t i n e i s l o a de d i n a s a n i n c l u de fi l e : -
In c l u de "INK E Y S12. INC "
` P l a c e t h i s a t t h e be g i n n i n g o f t h e p r o g r a m
.
V DD RB 7
MC LR RB 6
RB 5
RB 4
RB 3
0 SC 1 RB 2
RB 1
RB O
P IC 16F 84
RA4
OSC 2 RA3
RA2
RA1
v s s
RAO
To
Se r i a l LC D
N960 0 ba u d
F i g u r e 2
. 2 . 12- bu t t o n K e yp a d C i r c u i t .
Se c t i o n - 2 - 3
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
In t e r fa c i n g w i t h a 16-but t o n ke yp a d
Us i n g a 16-but t o n ke yp a d i s e s s e n t i a l l y t h e s a m e a s us i n g t h e 12-but t o n
ve r s i o n , h o w e ve r , m i n o r di ffe r e n c e s i n t h e INKE YS s ubr o ut i n e h a ve t o
be m a de . Fi g ur e 2 .3 s h o w s t h e s l i g h t l y di ffe r e n t c i r c ui t l a yo ut a n d
p r o g r a m KE YP AD16 .B AS de m o n s t r a t e s i t s us e . Th e ke yp a d i s a g a i n
a r r a n g e d a s a m a t r i x , but t h i s t i m e i t i s 4 x 4 , (fo ur c o l um n s a n d fo ur r o w s ) .
Wi t h i n t h e INKE YS s ubr o ut i n e m o s t o f t h e c o de s t a ys t h e s a m e , i t s t i l l
s c a n s t h e fo ur r o w s , but t h i s t i m e t h e r e a r e fo ur c o l um n s i n s t e a d o f t h r e e .
Th e r e fo r e , o n e e x t r a i n p ut i s r e qui r e d w h i c h m e a n s t h e TRIS va l ue h a s
t o t a ke t h i s i n t o a c c o un t . As w i t h t h e 12-but t o n p r o g r a m , t h e va l ue
r e t ur n e d i n KE Y fr o m t h e s ubr o ut i n e SC ANC OL do e s n o t m a t c h up w i t h
t h e l e g e n ds p r i n t e d o n t h e ke yp a d's but t o n s . Th e r e fo r e , t h e LOOKUP
c o m m a n d i s us e d a g a i n t o c h a n g e t h e va l ue r e t ur n e d i n KE Y t o t h e
c o r r e c t n um be r . Ho w e ve r , t h i s t i m e t h e r e a r e 17 di ffe r e n t c o m bi n a t i o n s .
'Ma p o f t h e ke yp a d l e g e n ds fo r n um e r i c o ut p ut
Lo o kup Ke y,[15,7,4 ,1,0,8,5,2,14 ,9,6,3,13,12,11,10,128],Ke y
Th e p r o g r a m KE YTST16 .B AS do e s t h e s a m e a s KE YP AD16.B AS, but
t h e INKE YS s ubr o ut i n e i s l o a de d i n a s a n i n c l ude fi l e : -
In c l ude "INKE YS16 .INC " 'P l a c e t h i s a t t h e be g i n n i n g o f t h e p r o g r a m .
To
Se r i a l LC D
N9600 ba ud
RB 7
RB 6
RB 5
RB 4
RB 3
RB 2
RB 1
R B O
RA4
RA3
RA2
RA1
RAO
B
Fi g ur e 2 .3 . 16-but t o n Ke yp a d C i r c ui t .
Se c t i o n - 2 - 4
a
In t e r fa c i n g w i t h a ke yp a d
5 V o l t s
R1
4 .7k
V DD
MC LR
4 m Hz
C r ys t a l
1 16
OSC 1
P IC 18F84
C 1
L' 10uf OSC 2
C 3 C 4
C 2 s e s M
22p 1 22p t
Sl ut
V SS
Dv
I
I
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
I n t e r f a c i n g w i t h a k e y p a d
I n bo t h t h e 12 a n d 16 but t o n d e m o n s t r a t i o n p r o g r a m s , t h e va l ue r e t ur n e d
i n t h e va r i a bl e KE Y i s a n um e r i c r e p r e s e n t a t i o n o f t h e k e y p r e s s e d
( i . e .
k e y o n e r e t ur n s t h e va l ue
1) . Ho w e ve r , i f t h e AC SI I r e p r e s e n t a t i o n i s
d e s i r e d ( i . e .
k e y o n e r e t ur n s t h e va l ue 49) , t h e c o m m e n t e d LOOKUP
c o m m a n d n e e d s t o be un c o m m e n t e d a n d t h e i n i t i a l LOOKUP c o m m a n d
n e e d s t o be c o m m e n t e d .
'Ma p o f t h e 12-but t o n k e y p a d l e g e n d s f o r ASC I I
o ut p ut
Lo o k up Ke y , j " 11 , 2" , 113 11," 4" , 115" 11611," 7 " , " 8 " ,

" 0 " , 11#11, 3 21, Ke y
'Ma p o f t h e 16-but t o n k e y p a d l e g e n d s f o r ASC I I o ut p ut
Lo o k up Ke y ,[ " F ' " 7 1,114
" ,'l „ " 0 " „ 8 „ '5" ," 2" ,
„ E n
11911,116" 1" 3 " ,
„ p „ „ C , . 8 , .
A''

Ke y
I f y o ur p a r t i c ul a r k e y p a d d o e s n o t m a t c h up w i t h t h e va l ue s d i s p l a y e d ,
s i m p l y r e -a r r a n g e t h e va l ue s w i t h i n t h e br a c e s o f t h e LOOKUP
c o m m a n d .
To d e t e r m i n e w h i c h k e y s a r e w h i c h , c o m m e n t t h e LOOKUP c o m m a n d
a n d p l a c e a SE ROUT o r DE B UG c o m m a n d j us t a f t e r i t
. Th i s w i l l d i s p l a y
t h e va l ue h e l d i n t h e va r i a bl e KE Y
. Wh i c h e ve r va l ue i s r e t ur n e d f o r t h e 0
but t o n w i l l be t h e f i r s t va l ue w i t h i n t h e br a c e s o f t h e LOOKUP c o m m a n d
.
Se c t i o n - 2 - 5
ExperimentingwiththePicBasicProCompiler
Program - SERKEY .BAS
Serialk ey pad controller
T heu seof ak ey pad isof tenessentialb u titstilltak esu ppreciou spins
onthemicrocontrollerthatcou ld hav eotherf u nctions,
theref ore, the
logicalsolu tionistosend ou tthed ataf romthek ey pad serially .
T his
meansthatonly oneortwopinsareu sed u ponthePI C
. F igu re2 .4
showsthecircu itf orsu chacontroller
.T hek ey pad controllersend sou t
asy ncseriald ataateitherT 1 2 0 0 b au d orT 9 6 0 0 b au d .
T hethreeL I N KSconnected toPortAand
PortB ; conf igu resev eral
d if f erentpropertieswithinthecontrollercod e .
L I N K1 conf igu restheserialou tpu tb au d rate
.W henconnected , 9 6 0 0 is
transmitted , and whenlef tu nconnected , 1 2 0 0 b au d istransmitted
. T he
lowerb au d hasb eenchosensothataserialI Rtransmitterorarad io
transmittermay b econnected
.
L I N K2 selectstheou tpu tty pe .W henconnected ,
ASCI I v alu esare
transmitted , wherethev alu esentref lectstheASCI I v alu eof the
k ey
( b u ttonAwillsend thev alu e
6 5 ) . W henu nconnected , nu mericv alu es
aretransmitted , wheretheactu alk ey v alu esaresent
( b u tton3 willsend
thev alu e3 ) .
L I N K3 selectsthenu mb erof b u ttonsontheparticu lark ey pad u sed .
Connected willinterf acetoa1 6 - b u ttonk ey pad , and u nconnected
will
interf acewitha1 2 - b u ttonty pe .
T he ST ROBE pin( PortB
.6 ) , willb ehigh5 0 msb ef oretheseriald atais
transmitted , and lowj u staf tertheend of transmission
. T hismay b e
u sed asanind icatororasad atav alid ationlinetothereceiv ingPI Cthat
ak ey hasb eenpressed and seriald ataisonitsway
.By u singtheN AP
command withinthewaitingloopof themainprogram, thecontrollers
cu rrentconsu mptionisonly 0 .4 mA .
Section- 2 - 6
ExperimentingwiththePicBasicProCompiler
S erialk ey pad controller
T heprogram,
S ERKEY.BAS isb ased arou nd thek ey pad scanning
su b rou tine I N KEYS ,
thisisamod if ied v ersion of thestand ard
su b rou tinesexplained intheprev iou sexperiments
.T hemainloopof the
programexaminesthepinswherethelink sareattached ,
and places
theirv alu eintothreef lags,
BUT T ON S , N UMERI C,
and BAUDRAT E,
thesenowcontain1 or0 accord ingtowhetherthepinsareconnected or
not
.T heinternalpu llu psand R2 ensu rethatwhenalink isnotconnected
thepinwillalway sremainhigh .
T helink k f lagsareu sed toconstru ctthed if f erentconf igu rationsb y simple
if - then
command slocated atplaceswithinthecod ethatreq u irea
d if f erentprod u ctf orthespecif ied link connectionord isconnection
.
T hef ormatf ortheseriald atatransmitted is : -
S y ncb y te " l7 " , Key V alu e, Deb ou ncef lag
T hisissentasT ru epolarity 9 6 0 0 or1 2 0 0 b au d .
L owcu rrentconsu mptionisachiev ed b y continu ou sly u sing
theN AP
command whennok ey ispressed .T hismeansthatthePI Cisof f more
thanitison
.T heN APcommand placesthePI Cintolowpowermod ef or
1 8 ms, whichmeansthereisan1 8 msd elay b ef orethek ey press
is
respond ed to, howev er, thisisnotnoticed asthek ey pad isnotatime
criticalcomponent .
Again :
G osu b I nk ey s

' G oand scanthek ey pad
I f Key = 1 2 8 orKey = 3 2 then' I f nok ey pressed d othef ollowing
: -
N ap0

' G ointolowpowermod ef or1 8 ms
G otoAgain

' And look againwhenwok enu p .
End if
T hecircu itshowsa1 6 - b u ttonk ey pad connected , howev er, if a1 2 - b u tton
ty peisu sed instead , connectionsareasf igu re2
.2 .
S ection- 2 - 7
L
d
E
0
U
+5Volts
Li n k 1
Con n ected . . . 9 6 00b a u d
Open 1 200b a u d
Li n k 2
Con n ected . . . A S CI I ou tpu t
Open N u m er i cou tpu t
JJJ
Li n k 3
Con n ected . . . 1 6 - b u tton
k eypa d
Open 1 2- b u tton k eypa d
0
C:
0
U
a
a )
Q
a
Y
a )
u )
N
a ) )
0)
LL
0
a
R1
4 . 7 k 1 4
V
a s
a
a )
L
a +
. C
VDD
RB7
RB6
RB5
RB4
RB3
RB2
RB1
RBO
MCLR
OS C1
P1 C1 6 F84
4m Hz
Cr ysta l
3
RA 4
0)
C 00
b C1
1 0u f C3
4
OS C2

RA 3
RA 2
C4
C2
-
r 22pf 22pf
C
RA 1
a )
E
VS S
RA O
0
"
u " T T T
s
a )
ay
W
- V
ExperimentingwiththePicBasicProCompiler
Program - KEYIN .BAS
R eceiv ingd ataf romtheserialk ey pad controller
T heprogram KEYIN .BAS
d emonstrateshowtoreceiv etheseriald ata
f romtheserialk ey pad controllerand d isplay theresu ltsonaserialL CD
d isplay , connected
toPortA
.1 , conf igu red f orN9 6 0 0 b au d .T he
su b rou tine KEVIN
continu ally look sf orthesy ncb y te" @ " and when
f ou nd , read sinthenexttwob y teswhichcontainthev alu eof thek ey
pressed , and thed eb ou ncef lag
.Itthenretu rnswiththesev alu esinthe
v ariab les
KEYand D EBOUNCE : -
Key

Var

By te

` Bu ttonpressed v ariab le
D eb ou nceVar

By te

' D eb ou nceF lag
Key in :
SerinPortA
.0 , T 9 6 0 0 , Key

` L ook f orthesy ncb y te
If Key < > " @ "
thengotoKey in

` L ook againif notf ou nd
SerinPortA
.0 , T 9 6 0 0 , Key , D eb ou nce
R etu rn
Alternativ ely , theSER IN2 orD EBUG INcommand smay b eu sed
.T hese
hav etheab ility towaitf oraspecif icseq u enceof characters
b ef ore
receiv ingtheKey and D eb ou nced ata, and notsu rprisingly , thisoperand
iscalled W AIT
.T hesu b rou tineab ov ecanb echanged to
: -
Key

Var

By te

` Bu ttonpressed v ariab le
D eb ou nceVar

By te

' D eb ou nceF lag
B9 6 0 0

Con 8 4

7 9 6 0 0 b au d
Key in :
Serin2 PortA .0 , B9 6 0 0 , [
wait ( " @ " ) , Key , D eb ou nce]
R etu rn
T imeou tv alu esmay b ead d ed , sothatif ak ey isnotpressed withina
certaintimef ramethesu b rou tineisexited
.T hef lexib ility of the
compiler' sseriald atacommand saretoonu merou stoexplain, thePBP
manu alshou ld nev erb ef araway .
Alternativ ely theST R OBEpin
may b econnected and period ically
examined , if itishighthenthek ey pad istransmitting, and lowmeansthe
k ey pad isu ntou ched .
Section- 2
- 9
ExperimentingwiththePIcBasicProCompiler
Programs - ASM_KEY
.BAS &ASMKEY.INC
Assemb lercod ed , Key pad d ecod er
T heassemb lercod ed , k ey pad d ecod erisinthef ormof aninclu d ef ile
ASM KEY.INC, itsu seisessentially thesameastheBASICcod ed
v ersions, exceptthereisnod eb ou ncef lagretu rned i .e . DEBOUNCE .
T herearehowev er, twonewDef inesad d ed f orthek ey pad , thef irst,
inf ormsthesu b rou tinewhethera1 2 or1 6 -b u ttonk ey pad isb eingu sed ,
theseare : -
Def ine KEYPAD_BUT T ONS1 2

`Usea1 2 -b u ttonk ey pad
or
Def ine KEYPADBUT T ONS1 6

`Usea1 6 -b u ttonk ey pad
T hewiringof thek ey pad sare* showninf igu res4
.2 and 4 .3 .
T hesecond Def ineinf ormsthesu b rou tine, whethertoretu rnthev ariab le
KEYwiththeASCIIv alu eof thek ey pressed orthenu mericv alu e : -
Def ine KEYPADRET URN 0

`Retu rnthenu mericv alu e
or
Def ine
KEYPADRET URN 1

`Retu rntheASCIIv alu e
If theNUMERICv alu eischosen, thev ariab le, KEYwillb eretu rned f rom
thesu b rou tinehold ingthenu mericeq u iv alentof thelegend sprinted on
thek ey pad b u ttons ( 0will retu rnav alu eof 0, Awillretu rnav alu eof 1 0
etc.) , and 1 2 8 if nob u ttonpressed
.If theASCIIv alu eischosen, KEYwill
retu rnhold ingtheASCIIeq u iv alentof thelegend sprinted onthek ey pad
b u ttons ( 0will retu rnav alu eof 4 8 , Awillretu rnav alu eof 6 5etc .) , and
3 2 ( space)
if nob u ttonpressed .
If noDef inesaread d ed toy ou rprogram, thed ef au ltsettingsare,
1 2 -
b u ttonk ey pad , retu rningtheNUMERICv alu es .
T heportsonwhichthe
k ey pad isconnected , areau tomatically
conf igu red f orthecorrectinpu t/ ou tpu tconf igu rationeachtime acallis
mad etothesu b rou tine INKEYS. And thev ariab le, KEYisalread y pre-
d eclared withintheinclu d ef ile .Mak esu rethattheinclu d ef ileisplaced
attheb eginningof y ou rprogram, inord ertominimiz etherisk of page
b ou nd ary conf licts .T heprogram ASM_KEY.BAS
isad emonstrationf or
u singtheassemb lercod ed k ey pad d ecod er .
Section-2 - 1 0
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
INKE YS, p s e udo c o m m a n d
W i t h i n t h e i n c l ude f i l e s
INKE YS12.INC a n d INKE YS16.INC ,
a m a c r o h a s
be e n de f i n e d w h i c h a l l o w s t h e us e o f a p s e udo c o m m a n d c a l l e d
INKE YS
. In s t e a d o f c a l l i n g t h e s ubr o ut i n e INKE YS a n d h a vi n g t h e va l ue
o f t h e ke y p r e s s e d r e t ur n e d i n KE Y, a n d t h e de bo un c e f l a g i n
DE B OUNC E ,
w e c a n p l a c e t h e s e va l ue s i n t o a n y va r i a bl e w e c h o o s e
.
Th e us e o f t h e INKE YS
c o m m a n d i s : -
Va r i a bl e l

Va r B yt e B ANKO SYSTE M
Va r i a bl e 2

Va r B yt e B ANKO SYSTE M
@

INKE YS Va r i a bl e l , Va r i a bl e 2
Va r i a bl e 1 w i l l
h o l d t h e ke y p r e s s e d a n d Va r i a bl e 2 w i l l h o l d t h e de bo un c e
f l a g
. Th e r e a r e t h r e e t h i n g s t o r e m e m be r w h e n us i n g t h e p s e udo
c o m m a n d
. Al w a ys p l a c e t h e @ s ym bo l a t t h e be g i n n i n g o f t h e l i n e , a l s o
a n y va r i a bl e s us e d w i t h i n t h e c o m m a n d s h o ul d be de c l a r e d a s B ANKO
va r i a bl e s .
Al s o , do n 't f o r g e t t o de c l a r e t h e va r i a bl e s a s SYSTE M t yp e s , o r a n
un de r s c o r e m us t p r e c e de t h e m .
B o t h va r i a bl e s a r e o p t i o n a l , i f
Va r i a bl e 2 i s n o t us e d t h e de bo un c e f l a g
w i l l be p l a c e d i n t o DE B OUNC E .
An d i f Va r i a bl e l i s n o t us e d t h e ke y
va l ue w i l l be p l a c e d i n t o KE Y.
Se c t i o n - 2 -
1 1
ExperimentingwiththePicBasicProCompiler
Section-3
Experimenting
with
SerialEeproms
GivingthePICamemory .
MicrowireInterfaceprincipals
.
SPIInterfaceprincipals .
1 2 CInterfaceprincipals .
1 2 Ceeprominterfacingprincipals
Interfacingtothe2 4C32 ,1 2 Cserialeeprom .
Interfacingtothe2 4C32 usingtheMSSPmodule .
Interfacingtothe93C66,Microwireserialeeprom .
Interfacingtothe2 5LC640,SPIserialeeprom .
ExperimentingwiththePicBasicProCompiler
G iv ingthePI Camemory .
I f y ou hav eaproj ectthatreq u ireslong- termmemory storage (u pto200
y ears) thatwillnotf itintothePI C' sinternaleeprom, anexternalserial
eeprom (SEEPROM)
may b etheanswer . T hesesmalland inexpensiv e
d ev icesareeasily interf aced toany of thePI Crange . T hissection isa
gu id etochoosingand u singseeproms . W ewillcomparethethreemaj or
interf acety pes : Microwire, SPI , and 1 2C, alsothead v antagesand
d isad v antagesof u singeachty pe .
A llserialeepromsu seasy nchronou sserialinterf ace (SSI ), thismeans
thatb oththeeepromand themicrocontrolleru seacommonclock and a
clock transitionsignaltoind icatewhentosend orread eachb it . Some
sy nchronou sseriald ev icesreq u ireminimu mclock f req u encies, theclock
f orseepromscanb easslowasreq u ired , orasf astasaf ewmH z ' s . T he
microcontrollercanstrob etheclock at itsconv enience, u ptothe
maximu mspeed of thed ev ice .
Serialeepromsnormally hav ej u steightpins, power, grou nd , oneortwo
d ata/ ad d resslines, and aclock inpu t, plu su ptothreeothercontrol
signals
. H owev er, u nlik eparalleleeproms, whichreq u ireextrapinstob e
ad d ed asthenu mb erof ad d ressand d atalinesgrow, aseeprom' s
phy sicalsiz ed oesnothav etoincreasewithitsmemory capacity .
Eepromsu seCMOStechnology ; theref ore, they consu me minu te
amou ntsof power, withcu rrentsaslowasaf ew u A instand b y mod eand
amA orsowhenactiv e .
D epend ingonthed ev ice, themaximu mclock speed f oraccessingserial
eepromsmay b eov er2mH z . H owev er, b ecau seittak eseight clock
cy clestotransf erab y te, and themasteralsoneed stosend instru ctions
and ad d resses, themaximu mrateof d atatransf erisu su ally nomore
than4 msperb y te . W riteoperationsactu ally tak emu chlonger, b ecau se
theeepromneed ssev eralmillisecond sto programab y teintoits
memory array . D u ringthistime, thePI Ccannotread orwriteto
the
eeprom
.
W ithcontinu ed u se, eepromsev entu ally losetheirab ility tostored ata, so
they arenotsu ited f orapplicationswherethed atachangesconstantly
.
Section- 3 - 1
ExperimentingwiththePicBasicProCompiler
GivingthePICamemory
M ostarerated f orb etween1 millionand 1 0 millionerase/ writes, whichis
O K f ord atathatchangesoccasionally, oreveneveryf ewminu tes
.
It' snotonlyeepromsthatu seaserialinterf ace, otherd eviceswith
synchronou sserialinterf acesinclu d e, A ID , D / A converters, clock s,
and
d isplayinterf acesetc, allof thesed evicesareu sed extensivelyinthis
b ook
. T heref ore, thissectionwillgiveaninsightonhowotherd evices
u singaserialinterf acecommu nicatewiththePIC
. M u ltipled evicescan
connecttoonesetof d atalines, witheachchiphavingitsownChip-
S electline( CS ) orf irmwaread d ress, thisef f ectivelymeansthatif two
d evicesareconnected thenthesecond d evicemayonlyreq u ire one
extrapin .
A f teryou haved ecid ed tou seaserialeeprom, thenextstepistoselect
one of
thethreeserialprotocols . In conventionalassemb ler
programming, the3 - wired eviceswoneasilyb ecau seof thesimplicityof
theirinterf ace . H owever, withthecompiler' s1
2 Cand S hif tcommand s,
interf acingtoanyof thed evicesisgreatlysimplif ied .
T oseehowthed if f erentinterf acescompare, wewilllook ataneeprom
of eachtype
.
T ab le3
. 1 su mmariz esthemaj orf eatu resof eachtypeu sed .
T ab le3
. 1 . Comparisonof S PI, M icrowire, and ICeeproms .
S ection- 3 - 2
Interf aceT ype
M icrowire S PI I` C
D evice
93 C66 2 5LC640 2 4C3 2
M emorycapacity
4K b its 64K b its 3 2 K b its
Nu mb erof Interf acepins
4or3 4or3 2
D atewid th( b its) 8 or1 6 8
8 or1 6
M aximu mclock speed 2 mH z
2 mH z 40 0 k H z
Write( b u sy) time 1 0 ms 5ms 1 0 ms
M axNo . of
b ytesprogrammed Inoneoperation 2 3 2 1 6
Writesb iton( clock state) Risinged ge Risinged ge Lowlevel
Read sb it on( clock state)
Risinged ge F ailinged ge Lowlevel
Chipselectmethod
H ard ware H ard ware S of tware
ExperimentingwiththePicBasicProCompiler
M icrowireinterf aceprincipals
A tmel' s9 3 C6 6 isan8 - pin, 4 K b itserial
eepromwithaM icrowire
interf ace . I thastwod atapins, d atain
( D J ) and d ataou t ( D O ) , aclock
inpu t
( S K ) , and achip- select( CS )
. A d d itionalinpu tsaref ormemory
conf igu ration, ( O RG) ,
whichd etermineswhetherd ataf ormatis8 or1 6 -
b its, and programenab le
( PE) , whichmu stb ehightoprogramthechip
.
T hememory isorganised as2 5 6 word sof 1 6 - b itseachwhentheO RG
pinisattached toV cc, and 5 1 2 word sof 8 - b itseachwhen
O RGis
connected togrou nd
.
A lthou ghitissometimescalled a3 - wireinterf ace, acompleteconnection
actu ally req u iresf ou rsignallines
. H owev er, u seof thePI C' sab ility to
rapid ly switchstatesf rominpu ttoou tpu tmeansthatthe
d atain and d ata
ou t pinsmay b econnected tothesamepinonthePI C
.
T heeepromu nd erstand ssev eninstru ctions, theseare, ERA S E/ W RI T E
ENA BLEand D I S A BLE, W RI T E, REA D , ERA S E, ERA S EA LL
( setsall
b itsto1 ) , and W RI T EA LL
( writesoneb y tev alu etoalllocations)
. Each
instru ctionmu stb eginwithaS tartcond ition, whichoccu rswhenCS and
D I areb othhighontheclock srisinged ge
. D I isb rou ghthighnatu rally
whenaninstru ctioniswritten, b ecau seallof theinstru ctionsb eginwith
one
. T hePI Cmu stb ringCS lowaf tereachinstru ction,
exceptf ora
seq u entialread
. W henCS isb rou ghthigh, theeepromisplaced into
stand b y , ignoringallinstru ctionsu ntilitd etectsanewstartcond ition
.
T owritetotheeeprom, thePI Cmu stf irstsend an
ERA S E/ W RI T E
ENA BLEinstru ctiontoD I , f ollowed b y aW RI T Einstru ction, thewriteb its
arewrittenontheclock sf allinged ge, and theeepromlatcheseachb iton
thenextrisinged ge
. A f tersend ingthef inald atab itinaprogramming
seq u ence, thePI Cmu stb ringCS lowb ef orethenextrisinged geof the
clock ( S K )
. T hiscau sestheeepromtob eginitsinternalprogramming
cy cle
. T heprogrammingisself - timed whichmeansthatitreq u ires
no
clock cy cles
. I f CS retu rnshigh
b ef oretheprogrammingcy cleis
complete, D O willind icateRead y / Bu sy statu s
. CS mu stthen
golow
againtocompletethewriteoperation .
T hePI Cneed stosend theErase/ W riteEnab leinstru ctionj u stonceper
programmingsession
. T hed ev iceremainswrite- enab led u ntilitreceiv es
anErase/ W riteD isab leinstru ctionorpowerisremov ed
.
Giv ingthePI Camemory
S ection- 3 - 3
ExperimentingwiththePicBasicProCompiler
GivingthePICamemory
T oread f romtheeeprom, thePICsend saR EA D instru ctionto
D I,
f ollowed b ythead d resstoread
. W hentheeepromreceivesthef inal
ad d ressb it, itwritesa
d u mmyz ero toD O , thenwritesthereq u ested d ata
ontheclock srisinged ges .
If CS remainshighaf teraread operation, ad d itionalclock transitionswill
cau sethechiptocontinu etoou tpu td ataatseq u entialad d resses
. If CS
goeslow, thenextread operationmu stb eginwiththeread instru ction
and anad d ress
.
S PIInterf aceprincipals
S PIisverysimilartoM icrowire, althou ghpolaritiesand
otherd etails
vary
. A swithM icrowire, S PIeepromswriteb itsontheclock ' s
rising
ed ge, however, u nlik eM icrowire, theylatchinpu tb itsonthef allinged ge
.
T hepolarityof CS ( activelow)
isalsooppositef romtheM icrowire
convention
M icrochip' s2 5 L C6 4 0 isa6 4 K b iteepromwithanS PIinterf ace, organised
as8 1 9 2 word sx8 - b its
. Inad d itiontothef ou rinterf acelines, thechiphas
twootherinpu ts . W P
( writeprotect) ,
whichmu stb ehightoprogramthe
d evice
. M oreover, f orinterf aceswithmu ltipleslaves,
theH O L D inpu t
enab lesthePICtopau seinthemid d leof atransf erinord erto
d o
somethingmoreu rgentontheS PIb u s
. T heeepromignoresallactivity
ontheS PIb u su ntilH O L D retu rnshigh, thenb othd evicescarry
on
wheretheylef tof f
.
T heeepromu nd erstand ssixinstru ctions, theseare, S ET A N D R ES ET
T H EW R IT EEN A BL EL A T CH , R EA D A N D W R IT ET O T H ES T A T US
R EGIS T ER , and R EA D A N D W R IT ET O T H EM EM O R YA R R A Y
. T he
eepromhasseverallevelsof writeprotection, which
mayb eu sed to
virtu allygu aranteethattherewillb enou nintentionalwritestothed evice
.
If W Pislow, nochangestothed ataareallowed
. If W Pishigh, twonon-
volatileb itsinthechip' s
statu sregister canb lock writestoall, ora
portionof thed evice
. F inally, if W Pishigh, b ef oreyou canwritetothe
statu sregisterortheportionof memoryenab led inthestatu sregister,
theeeprommu streceiveaS etW riteEnab leL atchinstru ction
.
S ection- 3 - 4
ExperimentingwiththePicBasicProCompiler
GivingthePICamemory
T owritetotheeeprom, thePICsend saS ET W R IT EEN A BL EL A T CH
instru ctiontoS I, f ollowed b yaW R IT Einstru ction, thenthehighb yteand
lowb yteof thead d ressaresent, thenthed atatowrite
. T hePICmay
send u ptof ou rd atab ytesf orseq u entialad d ressesinoneoperation
.
A f terclock ingthef inald atab itwithS CK low, CS mu stgohightob egin
programmingtheb yteintotheeeprom .
W hiletheeepromisprogrammingthed ata, thePICcanread the
eeprom' sstatu sregister
. W henb it- 0 of thestatu sregisteris0 , the
eepromhasf inished programming, and thenextwriteoperationmay
b egin
. T hechipiswrite- protected af tereachprogrammingoperation
;
theref ore, eachwritemu stb eginwithaS ET W R IT EEN A BL EL A T CH
instru ction
.
T oread theeeprom, thePICsend saR EA D instru ctionf ollowed b ythe
highb yteand thelowb yteof thead d ress
. T heeepromrespond swiththe
d atab itsinseq u enceonS O
. A swithM icrowire, ad d itionalclock swill
cau setheeepromtosend ad d itionald atab ytesinseq u ence
.
S ection- 3 - 5
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
SC L
e a
SDA
( s t a r t c o n di t i o n ) . ( u p

h
i
o i
~ r m e

a
SDA
( s t o p c o n di t i o n )
Fi g u r e 3 . 1 . 1 2 C START/STOP c o n di t i o n s
.
s e t u p
t i m e
Se c t i o n - 3 - 6
Gi vi n g t h e P IC a m e m o r y
1 2 C In t e r fa c e p r i n c i p a l s
1 2 C i s a s yn c h r o n o u s s e r i a l bu s , de ve l o p e d by P h i l i p s t o a l l o w
c o m m u n i c a t i o n be t w e e n di ffe r e n t p e r i p h e r a l s . Ma n y de vi c e s s u c h a s
e e p r o m s , ADC s , LC D dr i ve r s , DAC s , e t c s u p p o r t t h e 1 2 C bu s p r o t o c o l .
Th e s e de vi c e s c o m m u n i c a t e t h r o u g h a 2 - w i r e bu s , w i t h da t a t r a n s fe r
r a t e s o f 1 0 0 K bi t /s , 4 0 0 K bi t /s , a n d e ve n 1 Mbi t /s . Th e n u m be r o f de vi c e s
o n t h e bu s i s l i m i t e d by t h e m a x i m u m bu s c a p a c i t a n c e o f 4 0 0 p F .
Mo s t de vi c e s a r e u s e d a s s l a ve s w h i l e m i c r o c o n t r o l l e r s a r e t yp i c a l l y
m a s t e r s . 1 2 C a l s o s u p p o r t s m u l t i - m a s t e r i n g , w h i c h m e a n s m o r e t h a n o n e
de vi c e i s a l l o w e d t o c o n t r o l t h e bu s . 1 2 C h a s c o l l i s i o n de t e c t i o n a n d
a r bi t r a t i o n t o m a i n t a i n da t a i n t e g r i t y . Th e t w o l i n e s u s e d fo r 1 2 C
i n t e r fa c i n g a r e , Se r i a l Da t a Addr e s s Li n e
( SDA) a n d Se r i a l C l o c k Li n e
( SC L) . B o t h o f t h e s e a r e bi - di r e c t i o n a l .
1 2 C : P r o t o c o l
1 2 C i s a m u l t i - m a s t e r /s l a ve p r o t o c o l . Al l de vi c e s c o n n e c t e d t o t h e bu s
m u s t h a ve a n o p e n - c o l l e c t o r o r o p e n - dr a i n o u t p u t . A t r a n s a c t i o n be g i n s
w h e n t h e bu s i s fr e e ( i . e . bo t h SC L a n d SDA a r e h i g h ), a m a s t e r m a y
i n i t i a t e a t r a n s fe r by g e n e r a t i n g a START c o n di t i o n . Th e n t h e m a s t e r
s e n ds a n a ddr e s s byt e t h a t c o n t a i n s t h e s l a ve a ddr e s s a n d t r a n s fe r
di r e c t i o n . Th e a ddr e s s e d s l a ve de vi c e m u s t t h e n a c kn o w l e dg e t h e
m a s t e r . If t h e t r a n s fe r di r e c t i o n i s fr o m m a s t e r t o s l a ve , t h e m a s t e r
be c o m e s t h e t r a n s m i t t e r a n d w r i t e s t o t h e bu s . Wh i l e t h e s l a ve be c o m e s
t h e r e c e i ve r a n d r e a ds t h e da t a a n d a c kn o w l e dg e s t h e t r a n s m i t t e r , a n d
vi c e - ve r s a . Wh e n t h e t r a n s fe r i s c o m p l e t e , t h e m a s t e r s e n ds a STOP
c o n di t i o n a n d t h e bu s be c o m e s fr e e . In bo t h t r a n s fe r di r e c t i o n s ; t h e
m a s t e r g e n e r a t e s t h e c l o c k SC L a n d t h e START/STOP c o n di t i o n s .
i - s a -
ExperimentingwiththePicBasicProCompiler
GivingthePICamemory
T heS T A R T cond itionisgenerated b yahightolowtransitionontheS D A
lined u ringtheH ighperiod of theS CL line, asshowninf igu re3 . 1 .
A stopcond itionisgenerated b yalowtohightransitionontheS D A line
d u ringtheH ighperiod of theS CL line, alsoshowninf igu re3 . 1
.
T henu mb erof b ytestransf erred perS T A R T / S T O Pf rameisu nrestricted .
D atab ytesmu stb e8 - b itslongwiththemostsignif icantb it
( M S B) f irst .
Eachvalid d atab itsenttotheS D A linemu stremainhighf or' 1 ' orlowf or
' 0 ' d u ringthehighperiod of theS CL , otherwiseanytransitionintheS D A
linewhileS CL ishighwillb eread asaS T A R T / S T O Pcond ition
. T hu s,
transitionscanonlyb emad ed u ringthelowperiod of S CL
. A n
ack nowled geb itmu stf olloweachb yte . A f terthelastb itof theb yte is
sent, anA CK clock ( ack nowled gementclock ) isgenerated b ythemaster
( 9t` ' clock ) . A nA CK
( ack nowled geb it, low) mu stb esentb ythereceiver
and remainlowd u ringthehighperiod of theA CK clock .
If theslave ( receiver)
d oesn' tretu rnanA CK ( e. g. anerror, orisu nab leto
receivethed ata) ,
thentheslaved evicemu stleavetheS D A linehigh
( N A CK ) . T hemasterwillab ortthetransf erb ygenerating aS T O P
cond ition . If theslaved oesretu rnanA CK , b u tsometimelateritisu nab le
toreceiveanymored ata . T hentheslavemu stgenerateaN A CK ( not
ack nowled ge, high) onthef irstb ytetof ollow
. T heslavewillthenneed to
k eeptheS D A linehighf orthemastertogenerateastopcond ition . If the
receiveristhemasterand thetransf erisend ing . T henthemasterneed s
tosend aN A CK af terthelastb yteissent
. T heslave ( nowatransmitter)
mu streleasetheS D A linetohigh, thisallowthemastertogeneratea
S T A R T / S T O Pcond ition .
A ttheb eginningof eachtransf er, themastergeneratestheS T A R T
cond itionthensend saslavead d ress . T hestand ard slavead d ressis7 -
b its ( sometimes1 0 - b its) f ollowed b yad irectionorR / W b it ( 8 "t b it) as
showninf igu res4
. 2 and 4
. 3
. W henthed irectionb itisaW R IT E ( z ero) ,
thead d ressed slaved eviceb ecomesthe
receiverand themaster
b ecomesthetransmitter . W henthed irectionb itisaR EA D ( one) , the
ad d ressed slaved eviceb ecomes thetransmitterand themaster
b ecomesthereceiver
.
S ection- 3 - 7
ExperimentingwiththePicBasicProCompiler
GivingthePICamemory
1 2 CS erialeeprominterf aceprincipals
M icrochip' s2 4 C3 2 isa3 2 K b itserialeepromu singan1
2 Cinterf ace, the
memoryorganisationis4 0 9 6 word sx8 - b its, or2 0 4 8 word sx1 6 - b its
.
T heslavead d ressassigned tothisd eviceb ythe
manu f actu reris
1 0 1 O X X X , whereX = D on' tCare
. T heeepromsu pportsseveraltransf er
mod essu chas, BYT EWRIT E, PAGEWRIT E, CURRENT AD D RES S
READ , RAND O M READ , and S EQUENT IALREAD .
T operf ormaByteWrite, themastergeneratesaS T ART cond itionand
send stheslavead d resswiththed irectionb itsettoWRIT E
(zero) asin
f igu re3 . 2
. Whentheslaved evicematchesthead d ress, itsend sanACK
tothemasterd u ringtheninthclock cycle
. T henextb ytesenttothe
eepromwillb etheword ad d ressthatmovesitsinternalad d resspointer
.
T henthed atasentb ythemasterwillb ewrittentothememorylocation
pointed tob ythisad d ress
. F inally, themaster
generatesaS T O P
cond ition, whichwillsignaltheeepromtoinitiatetheinternalwritecycle .
Atthistimetheeepromwillnotgenerateanyack nowled gesignalsu ntil
thetransactioniscomplete
.
APageWriteissimilartoaByteWrite, exceptthemastermaytransmit
u ptoeightb ytesb ef oregeneratingastopcond ition
. Eachb ytesentto
thed evicewillincrementthe ad d resspointerf orthe
nextb yte
transaction
. T heeepromstoresthed atainaneight- b yteb u f f er, whichis
thenwrittentomemoryaf terthed evicehasreceived astopcond ition
f romthemaster, asinf igu re3 . 2 .
ControlByte
Word Ad d ress
I
A
S ection- 3 - 8
ByteWrite
_- i, oiByte
I I 0 I 1 I 0 I X I X

W d Ad d ress(n] IA` D ate(n) I A(D ata
(n+1 1
A I
D t(n+7
A I P i
F igu re3 . 2
. Writetransf ers .
Page Write
S
:S T ART cond ition P:S T O Pcond ition El
F romM astertoS lave
W :Writeb it(low) R :Read b it(high)
A :ACK b it N :NACK b it
F romS lavetoM aster
ExperimentingwiththePicBasicProCompiler
0 ®
©UUBU
Data
an
C d
W ordA ddressI n) I A A I Data [ n]
I ° N ;
P- J
RandomRead
~ bo0~

Data I n] J , A jData [ n+ 1 ] lk r Data [ n+ 2 ] A I
arls
SequentialRead
S :STA RTconditionP :STO Pcondition
W :W ritebit( low)

R:Readbit( high)
A :A CK bit

N :N A CK bit

D
F romSlav etoM aster
Data( n ]
Do
F romM astertoSlav e
F igureP . 3 . Readtransf ers .
Section- 3 - 9
Giv ingthePI Camemory
Readoperationsareinitiatedthesamewayasawriteoperationexcept
thedirectionbitissettoREA D( one) . Theeepromk eepstheaddress
pointerf romthelastbyteaccessedincrementedbyone . I naCurrent
A ddressReadtransaction, theeepromack nowledgesthemasteraf ter
receiv ingtheslav eaddressandtransmitsthedatabytepointedbyits
internaladdresspointer, seef igure3 . 3 . Thepointerisincrementedby
onef orthenexttransaction
. SequentialReadsbehav ethesamewayas
aCurrentA ddressReadtransactionexceptdataiscontinually
transmittedbytheslav edev iceuntilthemastergeneratesaSTO P
conditionseef igure3 . 3 .
F orRandomRead, themastergeneratesaSTA RTconditionthensends
theslav eaddresswiththedirectionbitsettoW RI TE ( zero) . Thenthe
nextbytesentisthewordaddresstobeaccessed . Thisoperationwill
changetheeeprom'sinternaladdresspointer
. Thenwithoutgeneratinga
STO Pcondition, aCurrentA ddressReadorSequentialReadtransaction
willf ollow .
N oticethattheCurrentA ddressReadandSequentialReadtransaction
generateanotherSTA RTcondition, asshowninf igure3 . 3 .
ExperimentingwiththePicBasicProCompiler
Programs - 24C32 .BAS,24X_TST .BASand24XXX .INC
Interf acingtothe24C321 2Ceeprom
Nowthatwek nowtheprincipalsb ehindserialeeprominterf acing, we
candev elopapairof su b rou tinesthatwillau tomatereadingandwriting
tothem
.TheM icrochip24C32isan1 2Cdev icethatcanstore40 9 6 b y tes
of data .F igu re3 .4showstheeepromsconnectionstothePIC
.
+5Volts
SSA
SCL
VCc
24C32
WP
AO
A1
A2
i
v
F igu re3 .4 .24C32eepromconnections .
Writingtotheeeprom
Thesu b rou tine EWRITE
isu sedf orthispu rpose . Itexpectstwo
v ariab lestob epre-loadedb ef oreitsu se
.Thef irstistheaddresswithin
theeepromwherethedataistob estored,thisisheldinthe
1 6 -b it
v ariab le ADDR,
thesecondisthedatatowritetotheeeprom,thisisheld
inthev ariab le EBYTEOUT .
Withinthe
EWRITE su b rou tine,theI2CWRITEcommandsendsthree
lotsof datatothe1 2Cb u s ; f irstly the
slav e addressissent, ( thismu st
alway sstartwith% 1 0 1 0 ,whichistheserialeepromdev iceidentif ier)
. If
thereismorethanoneeepromonthe1
2 Cb u sthenthenextthreeb itswill
ref lectthepatternontheA2,Al andAOpins
.H owev er,f orthis
demonstrationweareonly u singonedev ice,theref ore,they arecleared .
Sotheslav eaddressis% 1 0 1 0 0 0 0 0 ,theI2CWRITEcommandwill
au tomatically settheread/ writeb it
.Thenextlotof datasentisthe1 6 -b it
memory address,andf inally theBYTEorWORDsiz edv alu eto
b e
placedattheaddresslocationissent
.Adelay of 1 0 msisreq u iredaf ter
thewriteisperf ormed
; thisallowstheeepromtimetoallocatethedata
intoitsmemory array
: -
EWrite:
1 2CWRITESDA,SCL,% 1 0 1 0 0 0 0 0 ,Addr,[ E_By teOu t] ' Writetheb y te
Pau se1 0

' Delay 1 0 msaf tereachwrite
Retu rn
Interf acingtothe24C32eeprom
Section-3-
1 0
ExperimentingwiththePicBasicProCompiler
I nterf acingtothe2 4 C3 2 eeprom
I f thev ariab leE _BYTEOUT
isd eclared asaBYTE, then8 - b itswillb e
written
. I f thev ariab leisd eclared asaW OR D then1 6 - b itswillb ewritten
R ead ingf romtheeeprom
Thesu b rou tine ER EAD
isu sed f orthispu rpose
. I tread s8 or1 6 - b its
f romtheeeprom
. Bef orethesu b rou tineiscalled , thead d ressof interest
mu stb eload ed intothev ariab le AD D R .
Uponretu rningf romthe
su b rou tine, thed ataf romthespecif ied ad d resslocationisheld
inthe
v ariab le EBYTEI N .
The ER EAD
su b rou tineu sestheI 2 CR EAD command
. Theslav e
ad d ress
( asinEW R I TE)
and the1 6 - b itmemory ad d ressaresent . Then
thed ataisread intotheassigned v ariab le
. I tsu seis : -
AD D R = 1 0 2 4
` Pointtolocation1 0 2 4 withintheeeprom
G osu b Eread
' R ead thed ataf romthespecif ied location
Thev ariab leE_BYTEI Nnowhold stheb y teof d ata
ER ead :
1 2 CR EAD S D A, S CL , % 1 0 1 0 0 0 0 0 , Ad d r, j EBy telnJ
' R ead theb y te
R etu rn
Unf ortu nately , thecompiler' s1 2 CR EAD and I 2 CW R I TEcommand s
d o
notu setheack nowled geretu rnsf romtheb u s
. Theref ore, thismethod
cannotb eu sed tov erif y whetherasu ccessf u lwritehasb eenperf ormed
.
Oneway togetrou nd this, istoread thed atab ack f romthesame
ad d ressthatithasj u stb eenwrittento, and comparetheresu lt
.
F orexample .
W rite :
AD D R = 1 0 2 4

` Pointtolocation1 0 2 4 withintheeeprom
EBYTEOUT= 1 2 8

' Placethev alu e1 2 8 inthead d ress
G osu b EW rite

`
W ritetheb y tetothespecif ied ad d ress
G osu b Eread

` R ead thed ataf romthesamead d ress
I f EBy tein< > EBy teou tthengotoW R I TE

` Comparethem
Thiscomparesthev ariab le
E_BYTEI N
withthev ariab le EBYTEOUT,
and if they arenotthesamethentheW R I TEprocessiscarried
ou t
again
. Thiswillslowd ownthewritingprocessslightly , b u tasu ccessf u l
writeisgu aranteed
. Unlesstheeepromhascometotheend of itslif e
.
S ection- 3 -
1 1
ExperimentingwiththePicBasicProCompiler
I nterf acingtothe2 4 C3 2 eeprom
T heinclu d ef ile 2 4 XXX . I N C, containsthetwosu b rou tines,
EREAD, and
EWRI T E .
T hisshou ld b eload ed neartheb eginningof themainprogram,
j u staf terd eclaringtheS CL and S DApinassignments
: -
S CL
VAR PortB . 0 ` AssignPortB . 0 toS CL
S DAVAR PortB . 1
` AssignPortB. 1 toS DA
I nclu d e" 2 4 XXX. I N C"

` L oad theread / writesu b rou tines
T hev ariab le, ADDR isalread y pre- d eclared withintheinclu d ef ile
. T he
v ariab les
E_BYT EI N and EBYT EOUT need tob ed eclared withinthe
mainprogram
. Depend ingonhowthesev ariab lesared eclared d ictates
if an8 or1 6 - b itread / writeisperf ormed . F orexample .
Declaring EBYT EI N
asaWORDty pewillenab le1 6 - b itread s, and
d eclaringitasaBYT Ety pewillenab le8 - b itread s
. T hesameappliesf or
EBYT EOUT .
T hisispossib led u etothe1 2 Ccommand ' sab ility to au tomatically
d etectingif av ariab leisab y teoraword , thu stransf erring8 or1 6 - b its
.
N OT E . T hesu b rou tinesmay b eu sed f orthe2 4 C1 6 , 2 4 C3 2 , 2 4 C6 4 , and
2 4 C6 5 eeproms . T hey may work onother2 4 xxxserieseeproms,
b u t
hav enotb eentested .
S ection- 3 - 1 2
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o
Co m p i le r
P r o g r a m s -SSP _24XX
.B AS, SSP _TST, a n d SSP 24XX.INC
In t e r f a c i n g t o t h e 24C3 2 e e p r o m , u s i n g t h e M SSP m o du le
Th e n e w m i d-r a n g e P ICs , 1 6 F 8 7 2, 8 7 3 , 8 7 4, 8 7 6 , a n d 8 7 7 a ll h a v e a
m a s t e r s y n c h r o n o u s s e r i a l p o r t m o du le
(M SSP ), w h i c h m a y be
c o n f i g u r e d a s a n SP I m a s t e r / s la v e o r 1 2
C m a s t e r / s la v e . W e a r e i n t e n di n g
t o r e a d a n d w r i t e t o a 24C3 2 e e p r o m , t h e r e f o r e , w e w i ll di s c u s s h o w t o
c o n f i g u r e , a n d u s e t h e M SSP a s a n 1
2 C m a s t e r de v i c e . Th e r e a r e s e v e r a l
r e g i s t e r s a n d bi t s t h a t n e e d t o be m a n i p u la t e d f o r m a s t e r m o de t o be
c o n f i g u r e d
. W e w i ll lo o k a t e a c h r e g i s t e r i n t u r n
.
F i r s t ly , t h e SD A (P ORTC.4) a n d SCL
(P ORTC.3 ) p i n s n e e d t o be m a de
i n p u t s .
Th e CK E bi t (SSP STAT
.6 ) n e e ds t o be c le a r e d
. Th i s w i ll c o n f i g u r e t h e
M SSP m o du le t o c o m p ly w i t h n o r m a l 1
2 C s p e c i f i c a t i o n s .
Th e SM P bi t (SSP STAT
.7 ) n e e ds t o be s e t
. Th i s di s a ble s t h e s le w r a t e
c o n t r o l,
(w h i c h i s n o t n e e de d f o r a 1 0 0 k H z bu s s p e e d) .
Th e f i r s t f o u r bi t s o f SSP CON a r e g i v e n t h e v a lu e s o f % 1 0 0 0
.
Th i s
c o n f i g u r e s t h e M SSP a s a n 1
2 C m a s t e r .
Th e ba u d r a t e g e n e r a t o r r e g i s t e r
(SSP AD D ) i s n e x t lo a de d w i t h t h e bu s
s p e e d r e q u i r e d . Th e f o r m u la f o r t h i s i s : -
SSP AD D v a lu e = (OSC / (B U S SP E E D * 4)) -1
In t h i s e x p e r i m e n t , w e a r e g o i n g t o u s e a bu s s p e e d o f 1 0 0 k H z , a n d a n
o s c i lla t o r o f 20 m H z
. Th e r e f o r e , t h e v a lu e p la c e d i n SP P AD D i s 49
. Th i s
i s a u t o m a t i c a lly c a lc u la t e d f o r u s i n t h e p r o g r a m s
.
La s t ly , t h e M SSP m o du le h a s t o be e n a ble d
. Th i s i s a c c o m p li s h e d by
s e t t i n g t h e SSP E N bi t (SSP CON
.5 ) .
No w t h a t w e h a v e t h e M SSP c o n f i g u r e d, t h e n e x t t h i n g t o do i s w r i t e a
p a i r o f s u br o u t i n e s t h a t m a n i p u la t e t h e 1 2
C bu s f o r r e a di n g a n d w r i t i n g t o
t h e e e p r o m .
Se c t i o n -3 - 1 3
ExperimentingwiththePicBasicProCompiler
I nterf acingtothe2 4 C3 2 eepromu singtheM S S Pmod u le
A ty picalseq u encef orW R I T I N G toaserialeepromis : -
S end S T A R T : T hestartcond itionenab leb it, S EN (S S PCON 2 . 0 ) mu st
b eset . A f terthestartcommand hasb eensent, theS EN
b itwillb e
cleared . I f ab u scollisionoccu rred , theinterru ptf lagBCL I F (PI R 2 . 3 ) will
b eset .
S end slav ead d ress : T heslav ead d ressisload ed intotheS S PBU F
registerwiththeR / W b it(D O) cleared . T hecod emu stcheck theR W f lag
(S S PS T A T . 2 toseewhetherthePI Chasf inished transmittingits8 - b its .
U poncompletingthetransmission, theb u f f erf u llf lag, BF (S S PS T A T . O)
willb ecleared
. T heeepromnowack nowled gestheb y te, and this is
placed intheack nowled gestatu sf lagA CK S T A T (S S PCON 2 . 6 ) . I f an
ack nowled gewasreceiv ed , thisf lagwillb ecleared , if notthenthef lag
willb eset .
S end highb y te(M S B) of memory ad d ress : T hesameseq u enceas
ab ov e, b u tthehighb y teof thememory ad d ressissentinstead of the
slav ead d ress .
S end lowb y te(L S B) of memory ad d ress : T hesameseq u enceas
send slav ead d ress, b u tthelowb y teof thememory ad d ress issent
instead of theslav ead d ress
.
S end theb y tetoplaceintotheeeprom : T hesameseq u enceassend
slav ead d ress, b u twiththeb y tetoplaceintotheeepromsentinstead of
theslav ead d ress .
S end S T OP : T hestopseq u enceenab leb it, PEN (S S PCON 2 . 2 ) mu stb e
set
. A f terthestopcommand hasb eensent, thePEN b itwillb ecleared ,
and theinterru ptf lag, S S PI F (PI R 1 . 3 ) is set .
S ection- 3 - 1 4
ExperimentingwiththePicBasicProCompiler
I nterf acingtothe2 4 C3 2 eepromu singtheM S S Pmod u le
A ty picalseq u encef orR EA D I N G f romaserialeepromis : -
S end
S TA R T: Thestartcond itionenab leb it, S EN (S S PCON 2 . 0 ) mu st
b eset . A f terthestartcommand hasb eensent, theS EN b itwillb e
cleared . I f ab u scollisionoccu rred , theinterru ptf lagBCL I F
(PI R 2 . 3 ) will
b eset .
S end slav ead d ressf orwrite : Theslav ead d ressisload ed intothe
S S PBU F registerwiththeR / W b it (D O) cleared . Thecod emu stcheck
theR W f lag (S S PS TA T. 2 ) toseewhetherthePI Chasf inished
transmittingits8 - b its . U poncompletingthetransmission, theb u f f erf u ll
f lag, BF (S S PS TA T. O) willb ecleared . Theeepromnowack nowled ges
theb y te, and thisisplaced intheack nowled gestatu sf lagA CK S TA T
(S S PCON 2 . 6 ) . I f anack nowled gewasreceiv ed , thisf lagwillb ecleared ,
if notthenthef lagwillb eset .
S end highb y te(M S B) of memory ad d ress : Thesameseq u enceas
ab ov e, b u tthehighb y teof thememory ad d ressissentinstead of the
slav ead d ress .
S end lowb y te (L S B) of memory ad d ress : Thesameseq u enceas
send slav ead d ressf orwrite, b u tthelowb y teof thememory ad d ressis
sentinstead of theslav ead d ress .
S end R ES TA R T: Therepeated startcond ition enab leb it, R S EN
(S S PCON 2 . 1 ) mu stb eset . A f tertherestartcond ition hasb een
transmitted , theR S EN b itiscleared , and theS S PI F f lagisset .
S end slav ead d ressf orread : Theslav ead d ressisload ed intothe
S S PBU F registerwiththeR / W b it (D O) set . A nd thesameseq u enceof
ev entsoccu rasf orthef irstslav ead d resstransmission
.
S end EN A BL ER ECEI VE : Thereceiv eenab leb it, R CEN (S S PCON 2 . 3
mu stb eset . Thishastheef f ectof mak ingtheslav e (eeprom) a
temporary master . A f terreceiv ingthe8 - b itsf romtheeeprom, theR CEN
b itiscleared and theb u f f er- f u llf lag (BF ) isset . Thecontentsof theb u f f er
(S S PBU F ) isthenread , thisau tomatically clearstheb u f f er- f u llf lag(BF ) .
S ection- 3 - 1 5
ExperimentingwiththePicBasicProCompiler
I nterf acingtothe2 4 C3 2 eeprom
SendNACK: T heslav e (eeprom)is
stillatemporary master, theref ore,
tonotif y ittob easlav eagainitmu stb esentaNACK
(notacknowledge)
command (thisreleasestheSD Aline) . F irstly , theacknowledgedatab it,
ACKD T
(SSPCON2 . 5 ) andtheacknowledgeseq u enceenab le b it,
ACKEN (SSPCON2 . 4 )
mu stb eset . T heACKENb itisau tomatically
clearedwhentheNACKcommandisov er .
SendST OP :
F inally , thestopseq u enceenab leb it, PEN (SSPCON2 . 2 )
mu stb eset
. Af terthestopcommandhasb eensent, thePENb itwillb e
cleared, andtheinterru ptf lag, SSPI F
(PI R 1
. 3 )
isset .
T heprogram
SSP2 4 XX . BAS, readsandwritestoa2 4 C3 2 eeprom .
T hef irstelev enb y tesof theeepromarewrittento, andthenreadb ack,
thisisdisplay edonaserialL CD connectedtoPortA . O
. T heprogram
b reaksu ptheab ov eprocedu resintoasetof su b rou tines, send_ start,
sendstop, sendnacketc, andthenu sestwomainsu b rou tines f or
writingandreadingtoandf romtheeeprom
.
T hewritingsu b rou tine, EWR I T E, expectstwov ariab lestob epre- loaded
b ef oreitiscalled
. T hev ariab le AD D R , holdsthememory addresswithin
theeeprom, and EBYT EOUT ,
holdtheb y tetoplaceintotheeeprom .
T hereadingsu b rou tine, ER EAD , mu sthav ethe AD D R v ariab leloaded
b ef oreitiscalled
. Uponretu rning, theb y tereadf romtheeepromisheld
inthev ariab le EBYT EI N .
Onethingthaty ou mu sthav enoticed (I knowI did)is thatf orahardware
solu tiontheresu reisalotof codeneeded
. T ominimiz e thecode
ov erhead, assemb lersu b rou tinesmu stb eu sed
. T hatisthepu rposeof
theinclu def ile
SSP2 4 XX . I NC, thishasexactly thesamelay ou tasthe
BASI Cprogram, exceptitisalotsmaller
. T hetwosu b rou tines, ER EAD
and EWR I T E areagainu sed, withoneexception
. T heslav eaddress
mu stb epre- loadedb ef orethesu b rou tinesarecalled, thisisheldinthe
v ariab le,
SL AVEAD D R . Asthe1 2 Cb u scansu pportu ptoeightserial
eeproms, thev alu eplacedwithinthisv ariab lemay b eb etween 0 . . 7 .
T heM SSPmodu leisau tomatically conf igu redwhentheinclu def ile
is
loaded, alsothev ariab les, AD D R , EBYT EI N, E_ BYT EOUT , and
SL AVEAD D R arepre- declared .
Section- 3 - 1 6
ExperimentingwiththePicBasicProCompiler
I nterf acingtothe2 4 C3 2 eeprom
UsingthepseudocommandsEREADandEWRI TE
Analternativ emethodf orreadingandwritingtotheeepromistheuseof
twonew
pseudocommands . Thesearealsonamed EREAD and
EWRI TE,
andareready f orusewhentheincludef ile SSP2 4 XX . I N C is
loaded . Theirsy ntaxanduseareexplainedb elow .
Theeepromwritingcommandiscalled
EWRI TE, itssy ntaxis : -
EWRI TEslav eaddress,memory address,b y tewrittentotheeeprom
Theslav eaddressmustb eaconstantb etween0 - 7 . Thememory
addressmay b eany WO RDv ariab le
. Theb y tewrittenmay b eany BY TE
v ariab le . I tsuseis : -
Address VarWO RD

SY STEM ' Eeprommemory address
By te- SentVarBY TE

SY STEM
` By teplacedintoeeprom
Address= 1 0 0 0

` Pointtoaddress1 0 0 0
By te_ Sent= 1 2 8

` Write1 2 8 intotheeeprom
@ EWRI TE0 ,Address,By teSent` Writetheb y te
Theeepromreadingcommandiscalled EREAD, itssy ntaxis : -
EREADslav eaddress,memory address,b y tereadf romtheeeprom
Theslav eaddressmustb eaconstantb etween0 - 7 . Thememory
addressmay b eany WO RDv ariab le
. Theb y tereadmay b eany BY TE
v ariab le . I tsuseis : -
Address VarWO RD

SY STEM ' Eeprommemory address
By te_ RecVarBY TE

SY STEM ` By tereadf romeeprom
Address= 1 0 0 0

` Pointtoaddress1 0 0 0
Cam? EREAD0 ,Address,By te_ rec
` Readtheb y te
` Thev ariab leBy teRecnowholdsthev aluereadf romtheeeprom
The@sy mb olmustalway sprecedethepseudocommand,asitis
essentially anassemb lermacro .
Section- 3 - 1 7
ExperimentingwiththePicBasicProCompiler
Programs - 93C66
.BAS
I nterf acingtothe93C66M icrowireeeprom
R ead ingand writingtotheAtmel93C66eepromisslightly moreinv olv ed
thanits1 2 Ccou nterpart, b ecau seitu sesinstru ctionsinthef ormof
op-
cod es
toinf ormtheeepromastowhatf u nctionitshou ld perf orm
.Also,
theexactamou ntof b itsperinstru ctionmu stb esent, otherwisethe
eepromwillignoretheinstru ctionand retu rntostand b y
.
Ab rief d escriptionof thesev eninstru ctionsisshownintab le3
.2 .
T ab le3 .2
.I nstru ctionsetf or93C66 : O R G = 0 ( x8 organiz ation)
.
T heprogram 93C66.BAS,
writesthestringof characters" H EL L O
W O R L D " tothef irstelev enlocationswithintheeeprom, thenread sthem
b ack and d isplay sthemonaserialL CD connected toPortA .0 .F igu re3 .5
showstheeeprom' sconnectionstothePI C
.
F ou rsu b rou tinesareu sed withinthemainprogram, theseare
: -
EW R I T EEN,
enab lestheeepromf orwritingb y shif tingou ttheop- cod e
% 1 0 0 1 1 , f ollowed b y sev en d u mmy b its .No
v ariab lesneed b eset .
EW R I T ED S,
d isab lestheeepromf orwritingb y shif tingou ttheop- cod e
% 1 0 0 0 0 , f ollowed b y sev en d u mmy b its .No
v ariab lesneed b eset .
EW R I T E,
b ringstheCSlinehigh ( enab lingtheeeprom) ,
thenwritesa
b y tetotheeepromb y f irstshif tingou ttheop- cod e% 1 0 1 0 , f ollowed b y
thememory ad d ress, held inthev ariab le
AD D R , thentheb y tetosend to
theeepromisshif ted ou t, whichisheld inthev ariab le
EBYT EO UT . T he
CSlineisthenpu lled b ack low
( d isab lingtheeeprom) , and ad elay of
1 0 msisexecu ted , thisallowstheb y tewrittentotheeeprom
tob e
allocated withinitsmemory array : -
Section- 3- 1 8
I nstru ction Start- b it O pcod e Ad d ress D ataI n
D ataO u t R eqClk cy cles
R EAD
1 1 0 A8 - AO - - - D 7- D O
2 0
EW EN 1
0 0 1 1 XXXXXXX - - - H igh - Z
1 2
ER ASE
1 1 1 A8 - AO - - - ( R D Y/BSY)
1 2
ER AL 1 0 0
1 oxxxxxxX - - - ( R D Y/BSY) 1 2
W R I T E 1 0 1
A8 - AO D 7- D O ( R D Y/BSY) 2 0
W R AL 1 0 0
O 1 XXXXXXX D 7- D O ( R D Y/BSY) 2 0
EW D S 1 0 0
O O XXXXXXX - - - H iqh - Z 1 2
ExperimentingwiththePicBasicProCompiler
F igu re3 . 5 . 9 3 C6 6 eepromconnections .
R 1 allowsthed ata- inand thed ata- ou tlinestosharethesamePI Cpin .
R 2 isprecau tionary only , itensu resthatwhenthecircu itisf irstpowered
u pthechipisd isab led . T hismay b eomitted if req u ired .
S ection- 3 - 1 9
I nterf acingtothe9 3 C6 6 eeprom
Ewrite :
H ighCS

' Enab letheeeprom
S end WR I T Ecommand , ADDR ES S and DAT A
S hif tou tDI , S K , M S BF I R S T , [ EWR I 4 , Ad d r, EBy teou t]
LowCS

' Disab letheeeprom
Pau se1 0

' Allowtheeepromtoallocatetheb y te
R etu rn
ER EAD, b ringstheCS linehigh ( enab lingtheeeprom) , thenread sa
b y tef romtheeepromb y f irstshif tingou ttheop- cod e% 1 1 0 0 , f ollowed b y
thememory ad d ress, held inthev ariab leADDR , itthenshif tsintheb y te
f romtheeepromtothev ariab le E_BYT EI N . T heCS lineisthenpu lled
b ack low ( d isab lingtheeeprom) : -
Eread :
H ighCS

' Enab letheeeprom
' S end R EADcommand and ADDR ES S
S hif tou tDl, S K , M S BF I R S T , [ ER DI 4 , Ad d r]
' R ead thed ataintoE_BYT EI N
S hif tinDO , S K , M S BPO S T , ( EBy teinJ
LowCS

' Disab letheeeprom
R etu rn
Caremu stb etak enwhenchoosingaM icrowired ev ice . F orexample,
M icrochiphastwov ersionsof the9 3 C6 6 , onehasthed enomination' A'
af terthename, theotherhasa ' B' . T heAty peispermanently conf igu red
as5 1 2 word sx8 - b its, whiletheBty peisconf igu red as2 5 6 word sx1 6 -
b its . I nb othty pes, theO R G pinisnotimplemented . T hesameapplies
f ortheir9 3 LC6 6 v ersions .

+ 5 v
T oR BO
T oR B1
T oR B2
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
P r o g r a m s - 25LC 640.8AS
In t e r fa c i n g t o t h e 25LC 640 SP I e e p r o m
Mi c r o c h i p 's 25LC 640 i s a 64Kbi t s e r i a l e e p r o m , w h i c h i s o r g a n i s e d a s
8192 w o r ds x 8-bi t s , a n d us e s a n SP I i n t e r fa c e . Re a di n g a n d w r i t i n g t o
t h e 25LC 640 h a s s i m i l a r i t i e s t o Mi c r o w i r e i n t e r fa c i n g , a l t h o ug h i t i s
s o m e w h a t e a s i e r t o i m p l e m e n t
(t h i s c o ul d be o n e p o s s i bl e r e a s o n w h y
t h e Mi c r o w i r e i n t e r fa c e i s be c o m i n g un p o p ul a r w i t h de s i g n e r s ) . SP I
e e p r o m s a r e c e r t a i n l y e a s i e r t o i m p l e m e n t w i t h l o w l e ve l p r o g r a m m i n g
(a s s e m bl e r ), t h a n t h e i r 1 2C c o un t e r p a r t s .
SP I e e p r o m s s t i l l us e i n s t r uc t i o n s t o p e r fo r m s p e c i fi c fun c t i o n s (r e a d,
w r i t e e t c ), h o w e ve r , i t i s n o t a s s t r i n g e n t w i t h i t s p r o t o c o l a s Mi c r o w i r e
. A
br i e f de s c r i p t i o n o f t h e s i x i n s t r uc t i o n s i s s h o w n i n t a bl e 3
.3 .
Ta bl e 3
.3 . In s t r uc t i o n s e t fo r 25LC 640 .
Th e p r o g r a m 25LC 640.B AS w r i t e s t h e s t r i n g , "HE LLO WORLD" t o t h e
fi r s t 11 l o c a t i o n s w i t h i n t h e e e p r o m
. Th e n r e a ds t h e m ba c k a n d di s p l a ys
t h e c h a r a c t e r s o n a s e r i a l LC D c o n n e c t e d t o P o r t A .O . F i g ur e 3 .6 s h o w s
t h e e e p r o m 's c o n n e c t i o n s t o t h e P IC .
Th e p r o g r a m i s ba s e d a r o un d t w o s ubr o ut i n e s ,
E RE AD, a n d E WRITE ,
t h e s e p e r fo r m t h e r e a di n g a n d w r i t i n g t o t h e e e p r o m .
Th e s ubr o ut i n e E WRITE , e n a bl e s t h e e e p r o m by p ul l i n g t h e C S l i n e l o w ,
t h e n s h i ft s o ut t h e WRITE E NAB LE o p -c o de
(6) . Th e C S l i n e i s t h e n
br o ug h t h i g h t o l a t c h t h e i n s t r uc t i o n i n t o t h e e e p r o m , a n d i m m e di a t e l y
p ul l e d l o w a g a i n . Th e WRITE o p -c o de (2) i s t h e n s h i ft e d o ut , a l o n g w i t h
t h e h i g h byt e a n d l o w byt e o f t h e a ddr e s s va r i a bl e , ADDR .
Th e byt e t o be
p l a c e d i n t o t h e e e p r o m i s t h e n s e n t , t h i s i s h e l d i n t h e va r i a bl e
E B YTE OUT . Th e C S p i n i s r e t ur n e d t o i t s h i g h p o s i t i o n (di s a bl i n g t h e
e e p r o m ), a n d a de l a y o f 5m s i s e x e c ut e d, a l l o w i n g t h e byt e t o be w r i t t e n
t o t h e e e p r o m s m e m o r y a r r a y .
Se c t i o n -3 - 2 0
In s t r uc t i o n Op -c o de In s t r uc t i o n De s c r i p t i o n
RE AD 0000 0011
Re a d m e m o r y fr o m m e m o r y a r r a y, be g i n n i n g a t s e l e c t e d a ddr e s s
WRITE 0000 0010
Wr i t e da t a t o m e m o r y a r r a y, be g i n n i n g a t s e l e c t e d a ddr e s s
WRE N
0000 0110 Se t t h e w r i t e e n a bl e l a t c h (e n a bl e w r i t e o p e r a t i o n s )
W RDI
0000 0100 Re s e t t h e w r i t e e n a bl e l a t c h (di s a bl e w r i t e o p e r a t i o n s )
RDSR 0000 0101 Re a d t h e St a t us r e g i s t e r
WRSR 0000 0001 Wr i t e t o t h e St a t us r e g i s t e r
ExperimentingwiththePicBasicProCompiler
To RBO
ToRBI
ToRB2
VSS
R2
10K
R1
1K
0V
F igu re3 . 6 . 25 L C6 4 0eepromconnections .
Resistor, R1allowsthed ata- inand thed ata- ou tlinestosharethesame
PICpin . Resistor, R2isprecau tionary only , itensu resthatwhen the
circu itisf irstpowered u pthechipisd isab led . Thismay b eomitted if
req u ired .
A siscommonpracticenow, aninclu d ef ilehasb eenad d ed toallowthe
read ingand writingof SPIeeproms . Thisiscalled 25 XXXX. IN C, and
containsthetwosu b rou tines, EREA D, and EWRITE . Thisshou ld b e
load ed neartheb eginningof themainprogram, j u staf terd eclaringthe
CS, SCK, and SIpinassignments : -
CS
Var PortB . 0
' A ssigntheCSlinetoPortB . 0
SCK VarPortB . 1 ' A ssigntheSCKlinetoPortB . 1
SI

VarPortB . 2 ' A ssigntheSIlinetoPortB . 2
Inclu d e" 25 XXXX
. IN C"

' L oad intheeepromsu b rou tines
TheSOlineisau tomatically assigned tothesamepinastheSIline, and
thev ariab les, A DDR, E_BYTEIN , and E_BYTEOUT
arealread y pre-
d eclared withintheinclu d ef ile .
N OTE . OtherSPIeepromsinthesamed ev icef amily asthe25 L C6 4 0,
su chasthe25 L C04 0orthe25 L C08 0, may alsob eu sed withthese
su b rou tines
.
Interf acingtothe25 L C6 4 0eeprom
Thesu b rou tine, EREA D, b ringstheCSlinelow, enab lingtheeeprom,
and shif tsou ttheREA Dop- cod e ( 3 ) .
Thehighb y teand lowb y teof the
. ad d ressv ariab le, A DDRarethensent, and theb y tef romtheeeproms
memory array isshif ted intothev ariab le E_BYTEIN . Theeepromisthen
d isab led b y retu rningtheCSlinetoitshighstate
.
Section- 3 - 2 1
ExperimentingwiththePicBasicProCompiler
Section-4
Experimenting
with
AnaloguetoDigital
Converters
InterfacingwiththeMAX186ADC
.
Usinga3-wireinterfacewiththeMAX186 .
UsinganexternalreferencevoltagefortheMAX186
.
Quantasizingtheresult .
UsingtheMAX187ADC
.
InterfacingtotheMAX127ADC .
UsingtheonboardADC
.
AchievinggreateraccuracythroughSLEEP .
UsingtheADCINcommand
.
Analternativequantasizingformula .
Ironingoutnoisyresults
.
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o
C o m p i l e r
P r o g r a m - MAX1861.B AS
In t e r fa c i n g w i t h t h e MAX186 AID C o n ve r t e r
Mo s t r e a l w o r l d a p p l i c a t i o n s w o r k w i t h a n a l o g ue l e ve l s
: - t e m p e r a t ur e ,
l i g h t , e t c
. Th i s a n a l o g ue da t a n e e ds t o be c h a n g e d i n t o a fo r m a t t h a t a
P IC c a n un de r s t a n d, a n d us e
. Th i s i s n o r m a l l y a c h i e ve d w i t h a n
An a l o g ue t o Di g i t a l C o n ve r t e r (ADC ).
So m e o f t h e P IC s e r i e s o f
m i c r o c o n t r o l l e r s h a ve bui l t i n AID C o n ve r t e r s , but a r e l i m i t e d t o 8-bi t o r
10 -bi t r e s o l ut i o n , i n m o s t c a s e s t h i s i s e n o ug h , but fo r a p p l i c a t i o n s t h a t
r e qui r e a h i g h e r r e s o l ut i o n , a n e x t e r n a l AID C o n ve r t e r i s n e c e s s a r y
.
Th e MAX186 i s a n e i g h t c h a n n e l , 12-bi t , s uc c e s s i ve a p p r o x i m a t i o n AID
C o n ve r t e r , ut i l i z i n g a 3 , 4 o r 5 -w i r e i n t e r fa c e
(c l o c k, c s , da t a o ut , da t a i n
a n d o p t i o n a l s t r o be ) .
It m a y be c o n fi g ur e d t o us e i t s o w n i n t e r n a l
r e fe r e n c e vo l t a g e o r a n e x t e r n a l s o ur c e , a n d i s c a p a bl e o f p e r fo r m i n g a
c o n ve r s i o n i n 6 - 10 us .
F i g ur e 4
.1 s h o w s a de m o n s t r a t i o n a l c i r c ui t t o i n t e r fa c e w i t h t h e MAX186 .
B e fo r e a s a m p l e c a n be r e a d fr o m t h e MAX186 a c o n t r o l -byt e h a s t o be
s e n t , t h i s c o n t r o l -byt e ,
(w h i c h i s t h e p ur p o s e o f p i n DIN ), i n fo r m s t h e c h i p
a s 't o w h i c h i n p ut t o s a m p l e fr o m , a s w e l l a s w h a t fo r m o f s a m p l i n g t o
t a ke
(bi p o l a r o r un i -p o l a r ) e t c . Th e r e i s n o t e n o ug h r o o m t o g o t h r o ug h a l l
t h e fe a t ur e s o f t h e MAX186, Th e da t a s h e e t fo r t h e MAX186 m a y be
fo un d o n t h e a c c o m p a n yi n g C DROM
. Ho w e ve r , t a bl e 4 .1, s h o w s a
s um m a r y o f e a c h bi t w i t h i n t h e c o n t r o l -byt e .
Ta bl e 4 .1 . MAX186 c o n t r o l byt e
.
Se c t i o n -4 - 1
B i t N a m e
De s c r i p t i o n
7 (MSB )
START Th i s m us t a l w a ys be o n e , de fi n e s t h e be g i n n i n g o f t h e c o n t r o l
byt e
6
5
4
SE L2
SE L1
SE LO
Th e s e t h r e e bi t s s e l e c t w h i c h o f t h e e i g h t c h a n n e l s a r e us e d fo r t h e
c o n ve r s i o n
3
UN I/B IP
1= un i p o l a r , O= bi p o l a r . Se l e c t s un i p o l a r o r bi p o l a r c o n ve r s i o n
m o de .
In un i p o l a r m o de , a n i n p ut s i g n a l fr o m OV t o V RE F c a n be c o n ve r t e d
.
In bi p o l a r m o de , t h e s i g n a l c a n r a n g e fr o m -V RE F /2 t o +V RE F /2
.
2
SGUDIF
1= s i n g l e e n de d, O= di ffe r e n t i a l . Se l e c t s s i n g l e -e n de d o r di ffe r e n t i a l
c o n ve r s i o n s .
In s i n g l e -e n de d m o de , i n p ut s i g n a l vo l t a g e s a r e r e fe r r e d t o AGN D .
In di ffe r e n t i a l m o de , t h e vo l t a g e di ffe r e n c e be t w e e n t w o c h a n n e l s i s
m e a s ur e d .
1,0 (LSB ) P D1
P DO
Se l e c t s c l o c k a n d p o w e r -do w n m o de s
.
P D1 P DO Mo de
0

0

F ul l p o w e r -do w n
0

1

F a s t p o w e r -do w n
1

0

In t e r n a l c l o c k m o de
1

1

E x t e r n a l c l o c k m o de
ExperimentingwiththePicBasicProCompiler
T ab le4 . 2
. M A X 1 8 6 channelselectb its .
T heM A X 1 8 6 hasaninternalref erenceof 4 . 0 9 6 V , whichmeansthata
v oltageof u pto4
. 0 9 5 V onany of theinpu tchannelswillresu ltinthe
samev alu eb eingsentserially tothePI C
. T heprogram M A X 1 8 6 I . BA S
d emonstratesthis
. T hepotentiometerV R 1 , actsasav ariab lepotential
d iv id erconnected tochannel0 of theM A X 1 8 6 , thu sv ary ingthev oltage
applied totheinpu t, f rom0 to5 V
. T hisv oltageisd isplay ed onaserial
L CD setu pf orI nv erted 9 6 0 0 b au d , and connected toPortA
. 0 .
T hecod ef orread ingtheM A X 1 8 6 isinthesu b rou tine M A X 1 8 6 I N , b u t
b ef orethissu b rou tineiscalled , thechannelof interestisload ed intothe
v ariab le M A X CH.
T hesu b rou tineu sestheL O O K U Pcommand , which
hold sall8 comb inationsof the3 - b itchannelad d resses
( asintab le3 ) .
T hecontrol- b y te v ariab le CN T R L is pre- load ed withthev alu e
% 1 0 0 0 1 1 1 0 , ( start, u nipolar, single- end ed and internalclock ) , and the3 -
b itad d ressnowheld in M A X - CH
isO R ed withit, thissu perimposesthe
channelb itsintothecontrol- b y te .
L ook u pM axCh, j 0 , 6 4 , 1 6 , 8 0 , 3 2 , 9 6 , 4 8 , 1 1 2 1 , M axCh
Cntr1 = % 1 0 0 0 1 1 1 0 / M axCh

' " O R " intheChannelb its
T heM A X 1 8 6 isthenactiv ated b y pu llingtheCSpinlow, and thecontrol-
b y teisshif ted ou t
. I mmed iately af terthis, the1 2 - b itv oltageconv ersion is
shif ted in, and theM A X 1 8 6 isd e- activ ated b y b ringingtheCSpinhigh
.
T hev ariab le M A X _V A L
nowhold sthe1 2 - b itv oltageread ing ( 0 - 4 0 9 5 ) .
Section- 4 - 2
I nterf acingtotheM A X 1 8 6 N D Conv erter
I nthisseriesof experiments, wewillb eu singsingle- end ed u nipolar
inpu ts ( 0 toV ret) and aninternalclock
. T heref ore, theonly partof the
control- b y tethatneed stob echanged arethechannelselection
b its
( SEL
0 - 2 ) , T heseb itsareshownb elowintab le4 . 2 .
SEL 2 SEL 1 SEL O Channel
0 0 0 CHO
1 0 0 CH1
0 0 1 CH2
1 0
1 CH3
0
1 0 CH4
1 1 0
CH5
0 1 1 CH6
1 1 1 CH7
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
Ih
n
e
2
> Y
O
~ I I- I~ Im l ~ l m l
S e c t i o n - 4 - 3
In t e r f a c i n g t o t h e M A X 1 8 6 A ID C o n v e r t e r
Lo w C S

`A c t i v a t e t h e M A X 1 8 6
S h i f t o u t D i n , S c l k , M s b f i r s t , [ C n t r l l 8 ]

`S h i f t o u t t h e C o n t r o l b y t e
S h i f t i n D o u t , S c l k , M s b p o s t , [ M a x Va l l 1 2] `S h i f t i n 1 2 b i t s
Hi g h C S

`D e a c t i v a t e t h e M A X 1 8 6
Th e S S TRB p i n m a y b e u s e d t o m a k e s u r e t h a t t h e M A X 1 8 6 h a s f i n i s h e d
a c o n v e r s i o n b e f o r e t h e 1 2- b i t v a l u e i s s h i f t e d i n
. Th i s p i n g o e s h i g h
w h e n a c o n v e r s i o n i s c o m p l e t e , h o w e v e r t h e P IC i s f a s t e n o u g h i n m o s t
c a s e s t o j u s t i g n o r e t h i s p i n : -
Wh i l e S S TRB =O: We n d

`Wa i t f o r e n d o f c o n v e r s i o n
Fi g u r e 4 .1
. M A X 1 8 6 de m o n s t r a t i o n .
n
o
m
7
R
9
N
V o
.-
U
o
1 II- -
1 IF-
N
n
¢e
e ,
V
$
a v
~ , ~ a ~ ~

0 C D
U.
m
t 0
0
O

a.
-
z> a >
¢¢¢¢¢¢¢¢ ¢ ¢¢
N!
0 .
f f °
F
y J
m o m '
f ~ O
G
a t
0 !
o M I- J - , Wi
Im
a
o °
0

>N~
o w
¢ m
$ >
o
O N 0 7 • f u 7 < O f ~

^
52222=22

Z
1 1 0 0 0 0 0 0 0 0
ExperimentingwiththePicBasicProCompiler
Program - 3_WIRE .BAS

Interf acingtotheM AX 1 8 6 A/ D Conv erter
U singathreewireinterf acewiththeM AX 1 8 6
U singa5 -wireinterf acetod emonstratetheu seof theM AX 1 8 6 is
acceptab le, b u tinnormalu seweonly req u ire3wires .T hisispossib le
d u etothePIC' sab ility tochangeitspinstatef rominpu ttoou tpu talmost
instantaneou sly , whichmeansweareab letoconnecttheM AX 1 8 6 ' sD IN
and D O U T pinstogether, R1 isinplacetolimitthecu rrentf lowb etween
thePICI/ O pinand theM AX 1 8 6 ' sd ataou tpu t, incaseaprogramming
errorcau sesab u sconf lict, thishappenswhenb othpinsareinou tpu t
mod eand inoppositestates( 1 v s0 )
.Withou tR1 , largecu rrentswou ld
f lowb etweenthepins, possib ly cau singd amagetooneif notb othof the
d ev ices .Wealread y k nowthattheSST RBpinmay b eomitted
.T his
leav esj u st3pinsu sed b y thePIC, and asmallchangeof cod e .
T heprogram 3-WIRE .BAS
showshowthe3-wireinterf aceisu sed , and
f igu re4 .2 showsthenewlay ou tf ortheM AX 1 8 6 .
M AX 1 8 6
VREF
AGN D VSSD GN D
1 3
i
7 4 C5

C6
4 .7 u f
T
T 0 .0 1 u t
Figu re4 .2 .M AX 1 8 6 3-wireinterf ace .
Section-4 -4
Regu lated 5 v olts
VR1
1 0 k linear
2 0
IC1
CHO VD D
a
CH1

SHD N
-
R2
i
CH2
1 3
s
CH3

SST RB 1 5
ik
s
CH4

D O U T 1 7
CH5

D IN 1 B
O RB2
B
CH6

IN
1 9O T o RB1
CH7

SCLK
0 RBO
1 2
REFAD J
ExperimentingwiththePleBasicProCompiler
Program -
MAX186E .BAS
U singanexternalV R EF f ortheMAX186
Asmentioned earlier, b ecau seof itsinternalv oltageref erencethe
MAX186giv esaf u ll-scaleread ingof 4
.0 9 5 V .H owev er, any v oltage
ab ov ethisisnotconv erted
.I f thef u ll-scaleread ingneed stob elesseror
greaterthanthisv oltage, anexternalv oltageref erenceisreq u ired
.T his
cantak ethef ormof asimplepotentiometer, actingasav ariab le
potentiald iv id er, connected totheV ref pin
( cru d e, b u tef f ectiv e) , asin
f igu re4
.3 .O rtheV ref pincanb econnected toV d d ,
( whereV d d is
regu lated 5 V j ,
asinf igu re4 .4 .
Connect
toV d d
R EF AD J
V R 2
V R EF

4 7k
D GND

linear
V R EF
AGND V SS D GND
R egu lated 5 v olts
s

C5
0 .0 1 of
F igu re4 .4
.MAX186externalV ref connections .
Section-4 -5
I nterf acingtotheMAX186AI D Conv erter
F igu re4 .3 V ariab leV ref
.
V R 1
10 k linear
20
I c1
CH O
V D D
10 2
3
CH 1
SH D N
a
CH 2
15
R 2
1k
5
s
i
CH 3
CH 4
CH 5
SST R B
D O U T
D I N
15
O R B2
1
8 o T o R B1
8
CH 6
CH 7 SCLK
lam`
1
9 O
R BO
R EF AD J
12
MAX186
11
ExperimentingwiththePicBasicProCompiler
I nterf acingtotheM A X I 8 6 A I D Conv erter
Quantasizingtheresult
WhentheVref pinisconnectedtoVdd,thef ull-scalereadingof 4095
nowrepresents5V,sotheoutputf romtheA I D ,nolongerrepresentsthe
inputi . e
. 2000isnolonger2 . OOV . Thisisbecauseouranalogueinput
containsanalmostinf initenumberof possiblev aluesbetween0to5V
.
Howev er,theresolutionof theM A X 18 6 is12-bits
(4096 ), whichf orces
theA I D touseeachof itspossiblecombinationstorepresentasegment
of theanalogueinput.
Forexample,if wewereconv ertinga0to5Vanalogueinputusinga4-bit
A I D . The4-bitbinarynumberwouldrepresentarangeof 0-15
. D iv iding
the5Vanaloguerangeinto15equalsegmentswouldresultin
approximately
. 33Vpersegment . Thesesegmentsarecalled
quanta
lev els. To calculatethe quantalev el
f ortheM A X 18 6 weneedtodiv ide
theVref v oltage (+5Vinthiscase)
withtheresolutionused,whichis
4096 : -
quantalev el=VREF/A /D resolution
Theref ore: -
quantalev el=5/
4096
Thisgiv esusa
quanta lev el of
. 0012207V,howev er,becausethe
compileronlyworkswithrealnumbers
(integers), thisistoosmallav alue
f orittohandle,theref ore,wewillroundituptoamoremanageablev alue
of 123,onehasbeenaddedtothef inalquantalev eltotakeintoaccount
thatthecompilertruncates
(roundsdown) anyresultof adiv ision
. We
nowhav eour quantalev el
. To calculatetheactualv oltageontheinputof
theA I D weuse: -
A ctualv oltage=Resultof conv ersion*quantalev el
Letssupposeaconv ersionhastakenplaceandtheresultreturnedis
238 2,ourcalculationwillnowbe: -
A ctualv oltage=238 2*123
Thiswouldgiv earesultof 29298 6 ,butthisv alueistoolargef orthe
compilertohandle,soonepartof thecalculationneedstobereduced
.
S ection-4-6
ExperimentingwiththePicBasicProCompiler
S ection- 4 - 7
I nterf acingtotheM A X 1 8 6 A I D Conv erter
T oacheiv emoreaccu rateresu ltsitwou ld b eb ettertored u cethelarger
of thetwonu mb ers
. T heref oreou rcalcu lationnowlook slik ethis : -
A ctu alv oltage= ( 2 3 8 2 / 1 0 ) * 1 2 3
T heactu alv oltageisnow2 9 2 9 8
. 6 , b u tb ecau sethecompilerhand les
arithmeticwithintegerv alu esonly and alsotru ncates, theactu al
resu lt
placed inthev ariab le
M A X _VA L is2 9 2 7 4 .
T hev alu e2 9 2 7 4 isanicerealnu mb ertowork withinsid ethecod eitself ,
b u tf ord isplay pu rposesitismoremeaningf u ltov iewitas2
. 9 2 7 4 Volts .
T heref orewemu stsplitof f thenu mb erstotherightof thed ecimalpoint,
lu ck ily , b u tnotsu rprisingly thecompilerhasacommand tocalcu latethe
integerremaind erof ad iv ision
. T heoperatorf ord iv ision is/ and the
operatorf orcalcu latingtheremaind eris/ / .
F orexample : -
T heintegercalcu lation, VO LT S = 2 9 2 7 4 / 1 0 0 0 0
wou ld resu ltinVO LT S
hold ingthev alu e2 .
A nd theintegercalcu lation,
M I LLI VO LT S = 2 9 2 7 4 / / 1 0 0 0 0 wou ld resu lt
in M I LLI VO LT S
hold ingtheremaind erof thecalcu lation, whichis9 2 7 4 .
I nthed emonstrationprogramstheactu alcod elook slik ethis
: -
VO LT S = M A X VA L/ 1 0 0 0 0
M I LLI VO LT S = M A X VA L/ / 1 0 0 0 0
S onowwehav etwonewv ariab les, VO LT S and M I LLI VO LT S and we
cand isplay themwithad ecimalpointplaced in- b etween
: -
D EBUGd eciVO LT S ,
" . " , d ec4 M I LLI VO LT S , " Volts"
W hichwilld isplay ontheLCD
2 . 9 2 7 4 Volts
T heprogram, M A X 1 8 6 E . BA S d emonstratesthesecalcu lations
.
T hisf ormu laisnotonly u sef u lf ortheM A X 1 8 6 d emonstration, itwork s
f orallA / D Conv erters, whether8 , 1 0 , 1 2 or1 6 - b it
.
------------ ---- --- -- ---- -- -------
------- ~ ~

----· ----- ---- --- ~ --· -----
· -- ~ -- --- ------ · ------ -· --- ~
- ---· --- --- ---· - · · -
----- --- - -----· ---- -· · --- --· --- · -- --- --------
· --· ------
· ------ -- --· -------- --· ---
· -- ---· --· · - ----- -· -· - ---- ---- -- ---
---- -· · -- -- -- --- -- ------- · · -- -- --· · - -------· ---- ---· -----
----- -· --· -
--· -- ---· ------- --- · -- ---- -- -------- · --· --· · --· --
----- --- -------- · --· --· --------
--- ---- --- --- -· -- · -
--------· -- · · · -- · -- --- -------- --· ------ --· --· - · ---------· --
· -- -- -------- --· ---
· -- ---- --· · · ------- -- ---
- --- -- -- --- ---· --· - ------- -- --- ~

·
·
------

· ·




~


·
·
--· --
~ -------- · --·
------
·
· ·




~


·
·
--· -- ~ -------- · --·

------
~ · ----------- --- ---· -- -- -- · -- ---- · -· ---

- · --- --- -------- · --· -----· --- --- · ------ ---- --- · - ---· -- --
·
------ ~
-- · ---· -- -- -------- · --· --------· --
· ---- ---- --· - - ----- -· -- · · · -- -----

· -- -· · --· ----
~ -
---· --- --- ~ · · · ------- ---
---- --- ---- ---· -- -- --- · -- ---· -- -- ---- · - ----· ---- --- ----
· ·
------- --- ---- ----
- --- -------- · --· ------- --- ---- · ---· --
· -- ----· ------ --- · · ---- --· --- --- · --· -- -- --- ~ --· -
· --
· ------ ---· --- -- ---· ---· -- --- · ----· --
~ ·
--

· ---· --- --- ~
--· --- -· - --· -· ---- --- · -- · --· - -- · ---
---

· -----· --- --- ~
- - · -- -- · -- · ----

· ------· - --- ---· --
ExperimentingwiththePicBasicProCompiler
Program - MAXI27_5.BAS
Interf acingwiththeMAX1 27AID Conv erter
T heMAX1 27isalsoaneightchannel, 1 2-b itAID Conv erter, b u tu sesa
2-wire1 2Cinterf ace
(SCL, SD A) . W hatmak esthisAID Conv erterd if f erent
isitsab ility toconv ertav oltagegreaterthanitssu pply line, withou tthe
u seof anexternalV ref
.T hisisd u etothef actthattheinternalV ref is
sof twarecontrolled .Bit-3 of thecontrolb y te (RNG) conf igu restheV ref to
5V or1 0 V f u ll-scale .
Bef oreaconv ersioncanb eread f romtheMAX1 27acontrol-b y tehasto
b esent, thisinf ormsthechipastowhichinpu ttosamplef rometc .T ab le
4
.3 showsasu mmary of theb itswithinthecontrolb y te, and their
pu rpose .
T ab le4 .3
.MAX1 27controlb y te .
Inthisexperiment, wewillb eu singtheu nipolarinpu ts (0 toV reO, and
the5V f u ll-scaleconv ersion, theref ore, theonly partof thecontrol-b y te
thatneed stob echanged arethechannelselectionb its
(SEL 0 -2),
T heseb itsareshownb elowintab le4 .4 .
T ab le4 .4
.MAX1 27channelselectb its .
Section-4 -9
SEL2 SEL1 SELO Channel
0
0 0 CHO
0 0 1 CH1
0 1 0
CH2
0
1 1 CH3
1 0 0
CH4
1 0 1 CH5
1 1 0 CH6
1 1 1 CH7
Bit Name
D escription
7(MSB) ST ART T hismu stalway sb eone, d ef inestheb eginningof thecontrolb y te
.
6
5
4
SEI2
SEL1
SELO
T hesethreeb itsselectwhichof theeightchannelsareu sed f orthe
conv ersion
.
3 RNG
Selectsthef u ll-scaleinpu tv oltage .0 = (0 -5v ), 1 = (0 -1 0 v )
2 BIP Selectsu nipolarorb ipolarconv ersion
.0 = u nipolar, 1 = b ipolar
1 , 0 (LSB)
PD 1
PD O
Selectspower-d ownmod es .
PD 1 PD OMod e
0

X

Normaloperation
1

0

Stand b y power-d ownmod e
1

1

Fu llpower-d ownmod e
ExperimentingwiththePIcBasicProCompiler
M A X 1 2 7 f iv eV oltf u ll- scaleread ing
F igu re4 . 7 showsthecircu itf ortheM A X 1 2 7 , u singthe5 V internal
ref erence
. T hepotentiometerV R 1 , actsasav ariab lepotential d iv id er
connected tochannel0 of theM A X 1 2 7 , thu sv ary ingthev oltageapplied
totheinpu t, f rom0 to5 V
. S CL and S D A connecttoR BO and R B1 of the
PIC, asintheM A X 1 8 6 d emonstration . R 1 isapu llu presistorreq u ired b y
the1
2
Cb u sprotocol
.
R egu lated 5 v olts
F igu re4
. 7 . M A X 1 2 7 5 V oltref erence .
T heprogram M A X 1 2 7 S . BA S , d emonstratestheu seof theab ov e
circu it
. T heinpu tchannelof interestisload ed intothev ariab le M A X - CH
and thesu b rou tine M A X 1 2 7 _IN iscalled
. T hissu b rou tineshif tsthe
channelb itsintotheircorrectplacewithinthecontrolb y te, and setsb it- 7 ,
whichmu stb ea1
( seetab les5 . 3 & 5 . 4 ) .
T heslav ead d ressof thed ev iceisthensent, tomak esu rethatweare
talk ingtothecorrectd ev iceonthe1 2 Cb u s, and thenthecontrolb y teis
sent
. T hesameslav ead d ressissent, b ef orethe1 2 - b itresu lt of the
conv ersionisread in . T he1 2 CR EA D command read sinaf u ll
1 6 - b it
word , sotheresu lthastob eshif ted 4 placestotherighttocorrectthis .
T heq u antalev elcalcu lationisthencarried ou t, and theresu ltisplaced
in M A X V A L .
S ection- 4 - 1 0
Interf acingtotheM A X 1 2 7 A ID Conv erter
ExperimentingwiththePicBasicProCompiler
Program - MAX127_9.BAS
MAX127tenV oltf u ll-scaleread ing
Asmentioned atthestartof thisexperiment, theMAX127iscapab leof
conv ertingav oltagethatisgreaterthanitspowersu pply , u pto10 V in
f act
.T hisisachiev ed b y settingb it-3 of thecontrolb y teto1, f igu re4 .8
showsad emonstrationcircu itf orthis
.
F igu re4 .8 .MAX12710 V oltref erence .
T heprogram MAX1279 .BAS,
d emonstratestheu seof theab ov e
circu it .T heprogramisb asically thesameas MAX1275.BAS,
except
thatwithinthesu b rou tine
MAX127I N , b it-3 of thecontrolb y teissetto
1
.And b ecau seweareconv ertingav oltageof u pto10 V , theq u anta
lev elisalsochanged f rom123 to24 5
(10 1//4 0 96) .
I nterf acingtotheMAX127AI D Conv erter
Section-4 -11
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
P r o g r a m - 1OB ITADC .B AS
Us i n g t h e o n -bo a r d An a l o g ue t o Di g i t a l C o n ve r t e r
Th e ADC IN c o m m a n d t a ke s a l o t o f t h e w o r k a w a y fr o m a c c e s s i n g t h e
o n -bo a r d An a l o g ue t o Di g i t a l C o n ve r t e r , h o w e ve r t o m a ke e ffi c i e n t us e o f
t h i s c o m m a n d t h e p r i n c i p a l s be h i n d us i n g t h e ADC n e e d t o be
un de r s t o o d . We s h a l l t a ke a l o o k a t t h e p r o c e dur e fo r r e a di n g a n
a n a l o g ue vo l t a g e , t h e o l d fa s h i o n e d w a y. Th e n w e s h a l l l o o k a t t h e
ADC IN c o m m a n d i t s e l f .
Th e P IC s w e s h a l l be us i n g a r e t h e n e w 16F87X r a n g e , t h e s e h a ve a n
o n -bo a r d 10-bi t s uc c e s s i ve a p p r o x i m a t i o n ADC , w h i c h us e s a ba n k o f
i n t e r n a l c a p a c i t o r s t h a t be c o m e c h a r g e d by t h e vo l t a g e be i n g s a m p l e d .
Th e 28 p i n de vi c e s h a ve fi ve c h a n n e l s o f ADC , w h i l e t h e 40 p i n de vi c e s
h a ve e i g h t c h a n n e l s .
Th e P IC p o w e r s up w i t h a l l t h e ADC p i n s c o n fi g ur e d a s a n a l o g ue i n p ut s .
Th i s m a y be a c c e p t a bl e i f a l l t h e c h a n n e l s a r e be i n g us e d fo r a n a l o g ue
p ur p o s e s . Ho w e ve r , i f o n l y a fe w o f t h e m a r e fo r a n a l o g ue a n d t h e r e s t
a r e t o be us e d a s di g i t a l l i n e s t h e n t h e fi r s t 4-bi t s ( P C FG ) o f t h e
ADC ON1 r e g i s t e r n e e d t o be m a n i p ul a t e d
. Th e r e s e e m s t o be n o p a t t e r n
i n vo l ve d w i t h t h e s e bi t s , t h e r e fo r e t a bl e 4
.6 m us t be us e d t o de t e r m i n e
w h i c h bi t s t o s e t o r c l e a r e d fo r a s p e c i fi c i n p ut c o n fi g ur a t i o n .
A = An a l o g ue i n p ut

D = Di g i t a l i n p ut
Ta bl e 4.6
. P C FG O t o P C FG 3 c o n fi g ur a t i o n .
Th e p o r t p i n s t h a t a r e de s i r e d a s a n a l o g ue i n p ut s m us t a l s o h a ve t h e i r
TR IS va l ue s e t a s i n p ut ( 1) .
Se c t i o n -4- 12
P C FG AN7 AN6 AN5 AN4 AN3 AN2 AN1 ANO VR E F+
VR E F-
0000 A A A A A A A A AVdd AVs s
0001 A
A A A Vr e f+ A A A AN3 AVs s
0010
D D D A A A A A AVdd AVs s
0011 D D D A
Vr e f+ A A A AN3 AVs s
0100 D D
D D A D A A AVdd AVs s
0101 D D D D Vr e f+ D A
A AN3 AVs s
011X D D D D D
D D D --- ---
1000 A A A A Vr e f+ Vr e f- A A AN3
AN2
1001 D D A A A A A
A AVdd AVs s
1010
D D A A Vr e f+ A A A AN3 AVs s
1011 D D A A Vr e f+ Vr e f- A A AN3 AN2
1100 D
D D A Vr e f+ Vr e f- A A AN3 AN2
1101 D
D D D Vr e f+ Vr e f- A A AN3 AN2
1110 0 D D D D
D D A AVdd AVs s
1111 D D D
D Vr e f+ Vr e f- D A AN3 AN2
Experimenting w ith th e PicBasic
ProCompiler
Usingth eon-boardADC
T h ech annelof interestisch osenby bits3 to5 of th e
ADCONOregister
(CHS2 : CHSO) .
T able4 . 7 sh ow sth eirarrangementf oraspecif ic
ch annel .
T able4 . 7
. Ch annelselectionbits .
T h e1 0 -bitresu ltish eldinth eregisters ADRESH and ADRESL
. Bit
ADFM (ADCON1 . 7 ),
dictatesw h eth erth eresu ltsw illbelef tj u stif ied
(ADRESHh oldinglsb) orrigh tj u stif ied
(ADRESLh oldingI sb) . Setting
ADFMw ill
enablerigh tj u stif ication (normal),
w h ileclearing ADFMw ill
enablelef tj u stif ication
.
T h e ADC'sclock
sou rcemu stnow bech osen, th isisselectedby bits6
and7 of th e
ADCONO register (ADS1 : ADSO) .
T h ef ou rch oicesare
sh ow nbelow intable4 . 8 .
Bits7 . . 6
0 0
0 1
1 0
1 1
Clockty peselected
2/Fosc
8 /Fosc
3 2/Fosc
FRC(I nternalRCoscillator)
T able4
. 8 . Clockselectionbits .
T h e ADC's conv ersiontimeperbitisdef inedasT
AD . For correct
operation, th e ADC
req u iresaminimu mT AD of 1 . 6 u s . W h ich meansw e
mu stbev ery caref u lw h ench oosingth e
clock sou rce, aw rongly
conf igu red clockw ill resu ltinredu ced ADC
resolu tionornon-atall .
T ocalcu lateth eT AD
f oraspecif icoscillatorw ecanu seth ef ollow ing
f ormu la : -
T AD = x/Fosc
W h erex= 2, 8 , or3 2, andFoscisinmHz
Section-4 -1 3
Bits5
. . 3
Ch annelselected Pinname
0 0 0 Ch annel0
RAO/ANO
0 0 1 Ch annelt
RA1 /AN1
0 1 0
Ch annelt RA2/AN2
0 1 1 Ch annel3
RA3 /AN3
1 0 0 Ch annel4
RA5 /AN4
1 0 1 Ch annel5
REO/AN5 (only on4 0 pin)
1 1 0 Ch annel6
RE1 /AN6 (only on4 0 pin)
1 1 1 Ch annel7 RE2/AN7 (only on4 0 pin)
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
U s i n g t h e o n - b o a r d A D C
Fo r e x a m p l e , us i n g a 20m Hz c r ys t a l , w e c a n c h o o s e w h i c h c l o c k s o ur c e
i s s ui t a b l e b y c h a n g i n g t h e v a l ue o f x un t i l t h e r e s ul t i s 1 . 6 us o r o v e r : -
TA D = 3 2 / 20 = = 1 . 6 us
TA D = 81 20 = = 0. 4us
W e c a n s e e f r o m t h e r e s ul t s t h a t a c l o c k s o ur c e o f 8/ Fo s c w i l l b e t o o f a s t
f o r t h e A D C t o f ul l y m a ke a c o n v e r s i o n
. Ho w e v e r , a c l o c k s o ur c e o f
3 2/ Fo s c i s p e r f e c t .
W h e n FRC i s s e l e c t e d a s t h e c l o c k s o ur c e , t h e TA D t i m e i s a p p r o x i m a t e l y
2 - 6 us .
Th e A D C m o d ul e i s n o w r e a d y t o b e e n a b l e d , t h i s i s d o n e b y s e t t i n g t h e
A D ON b i t (A D C ONO. 0)
To a l l o w t h e i n t e r n a l s a m p l e a n d h o l d c a p a c i t o r s t i m e t o c h a r g e , w e m us t
w a i t a s p e c i f i c t i m e b e f o r e a c t ua l l y m a ki n g a c o n v e r s i o n
. Th i s t i m e p e r i o d
d e p e n d s o n t h e i m p e d a n c e o f t h e s o ur c e b e i n g s a m p l e d , a s w e l l a s t h e
t e m p e r a t ur e o f t h e P IC i t s e l f , h o w e v e r , a d e l a y o f b e t w e e n 2 t o 20us w i l l
s uf f i c e i n m o s t c a s e s .
W e a r e n o w r e a d y t o t a ke a s a m p l e , t h i s i s a c c o m p l i s h e d b y s e t t i n g t h e
GO- D ONE b i t
(A D C ONO. 2)
Th e c o n v e r s i o n m us t b e g i v e n t i m e t o c o m p l e t e , t h i s m a y t a ke t h e f o r m
o f a d e l a y a f t e r t h e GO_ D ONE b i t i s s e t , o r t h e GO_ D ONE b i t m a y b e
p o l l e d t o s e e i f i t i s c l e a r
. Th e l a t t e r i s t h e b e s t a n d m o s t a c c ur a t e
m e t h o d a s t h e GO D ONE b i t i s c l e a r e d b y h a r d w a r e a f t e r c o m p l e t i o n o f
a c o n v e r s i o n .
To r e d uc e c ur r e n t c o n s um p t i o n , w e c a n n o w d i s a b l e t h e A D C b y c l e a r i n g
t h e A D ON b i t (A D C ONO. 0) Th e 1 0- b i t a n a l o g ue t o d i g i t a l c o n v e r s i o n
r e s ul t i s n o w h e l d i n t h e r e g i s t e r s , A D RE SH a n d A D RE SL.
P r o g r a m 1 0B ITA D C . B A S, i l l us t r a t e s t h e us e o f t h e a b o v e t e c h n i que
.
A n d f i g ur e 4 . 9 s h o w s t h e c i r c ui t l a yo ut f o r a P IC 1 6 F876 . A s t h e
p o t e n t i o m e t e r (VR1 ) i s t ur n e d t o w a r d s t h e + 5 V o r OV l i n e t h e r e s ul t w i l l
i n c r e a s e o r d e c r e a s e
. Th i s w i l l b e d i s p l a ye d o n a s e r i a l LC D , c o n f i g ur e d
f o r N96 00 b a ud , c o n n e c t e d t o P o r t C . 7 .
Se c t i o n - 4- 1 4
ExperimentingwiththePicBasicProCompiler
Program- ADC_SLP .BAS

U singtheon-b oard ADC
Achiev inggreateraccu racy throu ghSLEEP
Accord ingtothePI Cd atasheets, amoreaccu ratesampleisob tained
whenthePI Cisplaced insleepmod eb ecau setheswitching noise
cau sed b y thePI C' sinternalregistersisminimiz ed .PlacingthePI Cinto
lowpowermod eisd iscu ssed withmored etailinsection-1 0 , and thishas
many similarities .
T hreenewcontrolb itsareu sed f orwak ingthePI CwhentheADChas
tak enasample .T heseare : -
PEI E (I NT CON.6 ) . Peripheralinterru ptsareenab led whenset, su chas
theADC, M SSPetc
.W hencleared theinterru ptsared isab led .
ADI E (PI E1 .6 ) . W henset, theADCinterru ptisenab led , and d isab led
whencleared .
ADI F (PI R1 .6 ) . T hisf laggetssetwhenanADCinterru pthasoccu rred , in
otherword swhentheADChasf inished tak ingasample .T hisf lag is
mainly of u sewhenaninterru pthand lerisimplemented .
F igu re4 .9 and program ADC_SLP .BAS d emonstratetheSLEEP
process .T hef irstthingthecod ed oesisd isab leglob al interru ptsb y
clearingtheG I Eb itof I NT CON (I NT CON.7 ) .
W henthePI Cisplaced intolow-powermod ethe externalcry stal
oscillatorishalted ; theref ore, thecod eattachestheADCclock sou rceto
theinternalRCoscillatorb y settingb its6 and 7 of theADCONOregister
(ADS1
: ADSO) . Peripheralinterru ptsarethenenab led b y settingthe
PET Eb it .T hentheADI Eb itissetwhichenab lestheADCtoactu ally
wak ethePI C .
W hentheRCclock sou rceisselected f ortheADC, thePI Cwaitsone
instru ctioncy cleaf tertheG ODONEb it (ADCONO.2 isset .T hisallows
theSLEEPinstru ctiontob eexecu ted b ef oreasampleisstarted .
T heSLEEPinstru ctionthenplacesthePI Cintolowpowermod eu ntilthe
ADChasf inished asample, thisisthend isplay ed ontheserialLCDand
thewholeprocessisrepeated .
Section-4 -1 5
ExperimentingwiththePicBasicProCompiler
Program - ADCIN
.BAS
UsingtheADCINcommand
Nowwthatwehav eab etter
insightintotheon-b oard ADC,wecanusethe
ADCINcommandwith moreconf idenceand ef f iciency .
T herearethreedef ines
usedb y theADCINcommand, theseare : -
Def ine ADC_BIT S
Def ine ADCCLOCK
Def ineADCSAMPLEUS
T hef irstdef ine ( ADCBIT S) ,is usedtoinf ormthecompilerastowhat
resolutionthe
on-b oardADCis .SomePIC'shav e an8 -b itADC,while
thenewerty pes hav ea10-b it
ADC,or12-b its f orthePIC16C77X
dev ices .
T hesecond def ine ( ADC_CLOCK) , selectsthe ADC'sclock source
( 2/Fosc,8 /Fosc,32/Fosc,or
FRC) . T hiswasdiscussed earlier.
T hethirddef ine ( ADCSAMPLEUS) , inf ormsthecompiler howlongto
wait
( inmicroseconds)toallowtheinternal sampleandhold capacitors
tochargeb ef oreasampleistaken
.T hisisthedelay af terthe ADONb it
isset,b utb ef orethe GO-DONEb itisset .
Bef oretheADCIN commandmay b eused, thepinof interest mustb e
conf iguredasaninput,b y settingitsT RISv aluetoone .
T henthef ourinputconf igurationb its ( PCFG) of ADCON1mustb eset or
cleared ( seetab le
4
.6) .
T hiswillconf iguretheappropriatepins todigital
oranalogue .
T hej ustif ication
b it( ADFM) of ADCON1 mustalsob esetorcleared . In
normaloperation,
theADFMisset,which enab lesrightj ustif ication .
Finally ,theADCINcommanditself is used,thiswill
makeaconv ersion
f romthechosenchannel andplacetheresult intothev ariab le assigned .
T heADCIN command
usesapolling techniq ue todetermine if a
conv ersionhasb een completed,theref ore,nodelay isreq uired
af terits
use .
Section-4-16
Usingtheon-b oardADC
Program ADCIN.BAS,
illustrateshowtousetheADCINcommand
.T he
mainp artof thep rogramisshownb elow
: -
PCFGO=O
PCFG1=1
PCFG2=1
PCFG3=1
ADFM=1
Exp erimentingwiththePicBasicProComp iler
Usingtheon- b oardADC
Ink
ADCIN0,AD_Result

' Placetheconv ersionof channel- 0
' intoADRESULT
Deb ugl,Line1,#AD_Result,"
"' Disp lay theresult
Pause200

'
Asmalldelay
GotoInf

' Doitf orev er
T hecircuitinf igure4
.9 isalsousedf orthedemonstration .
C
T o17
Serial
s
LCD
L'
13
n
0
RC7
VDD
RC6
RC5
RC4
RC3

MCLR
RC2
RC1
RCO
' Conf iguref orANOasanalogueinp ut
` Rightj ustif iedresultinADRESLand
' ADRESH
Section- 4 - 17
Regulated5Volts
23
RB7

a
C1
v

-

10uf
2 RB5

C2
23
RB4

O.tuf
2'
RB3

20mHz
23
RB2

Cry stal
22
RB1

0SC1 3
21
RBO
PIC16FO7 6
- ' RA5
- ° RA4
e
RA3

OSC2
C4

C3
VR1

RA1

15p f 15p f _
look *RAO
VSSVSS
linear
19
31 ° 1
Figure4 .9
.On- b oard10- b itADC .
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
P r o g r a m - Q_ADC IN.B AS
An a l t e r n a t i ve qua n t a s i zi n g fo r m ul a
In t h e p r e vi o us de m o n s t r a t i o n s , w e h a ve o n l y l o o ke d a t t h e r a w da t a
p r e s e n t e d fr o m t h e ADC , w h e r e a c o n ve r s i o n o f 5V w i l l p r o duc e a r e s ul t
o f 1023
. Qua n t a s i zi n g t h e r e s ul t w a s di s c us s e d e a r l i e r i n t h i s s e c t i o n
( un de r , i n t e r fa c i n g t h e M AX 18 6 )
. Ho w e ve r , a di ffe r e n t t e c h n i que w i l l be
di s c us s e d fo r t h e qua n t a s i zi n g o f t h e 10- bi t ADC
. Th i s m a ke s us e o f t h e
' * / ' o p e r a t o r , w h i c h r e t ur n s t h e m i ddl e 16 - bi t s o f a 32- bi t m ul t i p l i c a t i o n
.
Th i s w i l l a l l o w t h e c o m p i l e r ' s i n t e g e r m a t h s t o m ul t i p l y a fr a c t i o n a l
c o n s t a n t .
An y qua n t a s i ze d r e s ul t de p e n ds o n t h e a c c ur a c y o f t h e qua n t a l e ve l ,
w h i c h i n t h i s c a s e i s ,
( 5/ 1024) . Th i s g i ve s t h e r e s ul t , .0048 8 28 125,
c l e a r l y t h i s i s t o o s m a l l fo r t h e c o m p i l e r ' s i n t e g e r m a t h s t o us e , t h e r e fo r e ,
w e w i l l m o ve t h e de c i m a l p o i n t r i g h t a fe w t i m e s , t h i s w i l l l e a ve us w i t h a
qua n t a l e ve l o f 4 .8 8 28 125 . To m a ke t h e qua n t a l e ve l a r e a l n um be r w e
m ul t i p l y i t by 256 : -
4.8 8 28 125 * 256 = 1250
We n o w h a ve a n i c e r e a l n um be r fo r o ur qua n t a l e ve l . Th e fo r m ul a fo r
c a l c ul a t i n g t h e a c t ua l vo l t a g e i s : -
Ac t ua l vo l t a g e = R e s ul t o f c o n ve r s i o n * / qua n t a l e ve l
Fo r e x a m p l e , s up p o s e w e h a ve t a ke n a s a m p l e fr o m t h e ADC a n d i t h a s
r e t ur n e d t h e r e s ul t o f 512, t h e c a l c ul a t i o n n o w l o o ks l i ke t h i s : -
Ac t ua l vo l t a g e = 512 * / 1250
Th i s w i l l p r o duc e a r e s ul t o f 2500, o r 2 .5V . To a c h i e ve a s l i g h t l y m o r e
a c c ur a t e r e s ul t , t h e r e s ul t o f t h e c o n ve r s i o n n e e ds t o be i n c r e a s e d by
m ul t i p l yi n g i t by 10 : -
Ac t ua l vo l t a g e = ( 512* 10) * / 1250
Wh i c h w i l l p r o duc e a r e s ul t o f 25000, a g a i n 2
.5 Vo l t s .
P r o g r a m
Q ADC IN .B AS, i l l us t r a t e s t h e a bo ve m e t h o d, us i n g t h e c i r c ui t
i n fi g ur e 4.9.
U s i n g t h e o n - bo a r d ADC
Se c t i o n - 4- 18
ExperimentingwiththePicBasicProCompiler
Program
- SAMPLING.BAS
Ironingou tnoisy resu lts
Sometimesaccu racy isof apremiu m, theref ore, certainprecau tions
hav etob etak enwhenu singAID Conv erters, especially if they are1 0 -b it
ormorety pes .Any inaccu racy willmanif estitself asnoise, thisiswhen
theLSBof theread ingchangescontinu ou sly f romonev alu etoanother .
T heintegermathu sed b y thecompilerironsou tmostof the noise,
howev er, if y ou areu singtherawd atapresented b y theAD Ctheny ou
mu stf irstf ind ou twherethenoiseiscomingf rom .
Amaj orcau seof noiseisinad eq u ated ecou plingof thepowersu pply .
T hismay b eallev iated b y theu seof capacitorsprolif ically placed arou nd
thecircu it, and located asphy sically closetotheAD Caspossib le .
If theinpu ttotheAD Cisnotarapid ly mov ingsignalthenacapacitor
shou ld b eplaced f romitsinpu ttogrou nd , thev alu ed epend s onthe
f req u ency of thesignalb eingsampled , theref ore, atrialand errormethod
shou ld b ead opted ( af ewthou sand pF isnormally su f f icient) .
Also, whend esigningthePCBorstripb oard f orf inalconstru ction, alarge
grou nd planeshou ld b eemploy ed .
Alway sensu rethatthesu pply lineiswell regu lated and thatif an
externalref erencev oltageisu sed itisprecise .W henprototy pingy ou r
circu itonab read b oard , noisewillb emoreapparent, theref ore, if the
d ecou plingand regu lationof thepowersu pply work wellonthismed iu m
itwillb eminimiz ed inthef inalprod u ct .
Anothermethod f orred u cingthenoiseisasof twareone .Sev eral
samplesaretak enf romtheAD C, thenav eraged ou t .F orinstance, if we
weretak ingsamplesf romtheb u iltin1 0 -b itAD C, whichhasarangeof
0 . .1 0 2 3 , wewou ld sampletheAD C1 0 times, ad d themtogetherand
placetheminaW O R D v ariab le, thiswillgiv eu samaximu mv alu eof
1 0 2 3 0 , whichiswellwithinthe1 6 -b itcapab ilitiesof thecompiler
.
W henallthesampleshav eb eenaq u ired , thev ariab lecan thenb e
d iv id ed b y thenu mb erof samplestak en, whichis1 0 inou rcase .T his
willgiv eu stheav eragev alu ethatwassampled
.T hismethod
isnot
1 0 0 % accu rate, howev er, theresu ltsob tained aread eq u atef or most
practicalpu rposes .T heprogram SAMPLING.BAS, d emonstratesthe
u sef u lnessof thismethod .
Ironingou tnoisy resu lts
Section-4 -1 9
ExperimentingwiththePicBasicProCompiler
Section-5
Experimenting
with
DigitaltoAnalogue
Converters
UsingthePWMcommandasaD/AConverter .
ControllingthehardwarePWMmodules .
BuildinganR-2RD-AConverter .
InterfacingtotheMAX5352D/AConverter
.
InterfacingtotheAD8402digitalpotentiometer .
ExperimentingwiththePicBasicProCompiler
Program - 8BIT_PWM .BAS
Asy ou wou ld expect, aD igitaltoAnalogu econv erter
istheexact
oppositeof anAnalogu etoD igitalconv erter
.Ittak esab inary v alu eand
conv ertsittoav oltage
.Therearesev eralway stoachiev ethis,
pu lse
wid thmod u lationisthesimplestmethod , aresistorlad d erisaslightly
moreref ined way , and aseparateICisthemostaccu ratety pe
.Inthis
sectionwewillexploreallthreemethod s, inclu d ingthePWMmod u les,
incorporated inthenew1 6 F 87 X rangeof microcontrollers .
U singthePWMcommand asaD igitaltoAnalogu eConv erter
Becau sePu lsewid thmod u lationisrelativ ely easy toimplementwiththe
compiler, it' sof tenov erlook ed asav iab le8-b it
d igitaltoanalogu e
conv erter, y ettheresu ltsachiev ed aresu rprisingly accu rate
.
Pu lse-wid thmod u lation
(PWM) allowsad igitald ev icetogeneratean
analogv oltage
.Theid eais, thatif y ou mak eapin' sou tpu thigh,
the
v oltageonthatpinwillb e5 V
.O u tpu tlowwillb eO V . H owev erif y ou
switchthepinrapid ly b etweenhighand lowsothatitwashighf orhalf
thetimeand lowf orhalf thetime, theav eragev oltageov ertimewou ld
b ehalf way b etweenO V and 5 V (2
.5 V olts) . Theratioof highstolowsin
PWMiscalled thed u ty cy cle
.Thed u ty cy clecontrolsthe analogu e
v oltage, thehigherthed u ty cy clethehigherthev oltage
.SincethePWM
command u sesab y te
(8-b its) tocontrolthed u ty cy cle, wecanresolv e
thev oltaged owntoav alu e, d ef ined b y thef u nction
: -
R angeof O u tpu t/ R angeof inpu t
Whereou tpu tisthe0 . .5 V swing, and inpu tisthe8-b it
(0 -2 5 5 ) v alu eof
d u ty , so
5 V / 2 5 6 = .0 1 9 5
whichmeans, f oreach1 -b itchangeinthed u ty ,
theou tpu tv oltagewillchangeb y
.0 1 9 5 V , thisiscalled theq u antalev el
.
Theref ore, b ased onagiv eninpu twecancalcu latetheou tpu tv oltage
withthef ollowingf ormu la : -
V ou t= d u ty * q u antalev el
F orexample, ad u ty of 1 5 0 wou ld resu ltinanou tpu tv oltageof 2
.9 2 5 V
V ou t= 1 5 0 * .0 1 9 5 ,
V ou tnoweq u als 2 .9 2 5
Section-5 -1
ExperimentingwiththePicBasicProCompiler
U singthePW M command asad igitaltoanalogconv erter
T hisisimportanttok nowb u tnotterrib ly u sef u lwithinou rcod e, weneed
tok nowthev alu etoplaceintod u ty thatrepresentsthev oltagereq u ired
ontheou tpu t . T hef ormu lawewillu seis
: -
d u ty = V ou t/ q u antalev el
O u rq u antalev elwork ed ou tas
. 0 1 9 5 , howev erthisnu mb eristoosmall
f orthecompiler' sintegercalcu lationstohand le, theref orewewillscaleit
u ptoamoremanageab le1 9 5 . W ewillalsoscaleu pV ou tf oramore
accu rateresu lt . S oou rf ormu lanowlook slik ethis ; -
d u ty = ( V ou t* 1 0 0 ) / 1 9 5
I nord ertoconv ertthechopped PW M intoasmoothanalogv oltagewe
need tof ilterou tthepu lsesand storetheav eragev oltage . R 2 and C3 in
f igu re5
. 1 f ormanR / Cnetwork . T hecapacitorhold sthev oltagesetb y
PW M ev enaf tertheinstru ctionhasf inished . T helengthof timeitwill
hold thev oltaged epend sonhowmu chcu rrentisd rawnb y any external
circu itry connected toit
. I nord ertohold thev oltagereasonab ly stead y ,
wemu stperiod ically repeatthePW M command togiv ethecapacitora
re- charge . J u stasittak estimetod ischargethecapacitor, italsotak es
timetochargeitinthef irstplace
. T hePW M command letsy ou specif y
thechargingtimeintermsof cy cles
. T od eterminehowlongtocharge
thecapacitor, u sethisf ormu la : -
Chargetime= 4 * R ( in
k Q ) * C ( inu F ) .
F orinstance, f igu re5
. 1 u sesa1 0 k O resistorand a1 pF capacitor : -
Chargetime= 4 * 1 0 * 1 = 4 0 ,
whichis4 0 ms .
W hichmeansitwilltak e4 0 cy clestochargethecapacitor,
howev er,
sincethecompiler' sPW M command cy cletimeisd epend anton
the
cry stalf req u ency ,
( a4 mH z cry stalwillgiv easinglecy cletimeof 5 ms, a
2 0 mH z cry stalwillgiv easinglecy cletimeof imsetc) . T o
giv eacy cle
timeof 4 0 msu singa4 mH z cry stalweu sethisf ormu la
: - .
Cy cle= chargetime/ ( 2 0 / O S C)
T hiswillgiv eu sacy cletimeof 8 toplacewithinthePW M command
.
S ection- 5 - 2
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o
C o m p i l e r
Us i n g t h e P WM c o m m a n d a s a di g i t a l t o a n a l o g c o n ve r t e r
If w e w a n t e d t o p r o duc e a vo l t a g e o n P o r t B
.O o f 2 .5V w i t h a 4m Hz
c r y s t a l , us i n g a 1 Ok Q r e s i s t o r a n d a 1 uF c a p a c i t o r , w e w o ul d us e
: -
Aft e r o ut p ut t i n g t h e P WM p ul s e s , t h e c o m p i l e r l e a ve s t h e p i n a s a n i n p ut
.
Wh i c h m e a n s t h e p i n 's o ut p ut dr i ve r i s e ffe c t i ve l y di s c o n n e c t e d
. If i t w e r e
n o t , t h e s t e a dy o ut p ut o f t h e p i n w o ul d di s c h a r g e t h e vo l t a g e o n t h e
c a p a c i t o r a n d un do t h e vo l t a g e s e t t i n g e s t a bl i s h e d by P WM
. Th e P WM
c h a r g e s t h e c a p a c i t o r , a n d t h e l o a d c o n n e c t e d t o y o ur c i r c ui t di s c h a r g e s
i t . Ho w l o n g t h e c h a r g e l a s t s
(a n d t h e r e fo r e h o w o ft e n y o ur c o de s h o ul d
r e p e a t t h e P WM c o m m a n d t o r e fr e s h t h e c h a r g e ) de p e n ds o n h o w m uc h
c ur r e n t t h e t a r g e t c i r c ui t dr a w s , a n d h o w s t a bl e t h e vo l t a g e m us t be
. If
y o ur l o a d o r s t a bi l i t y r e qui r e m e n t s a r e m o r e t h a n t h e p a s s i ve c i r c ui t o f
fi g ur e 5
.1 c a n h a n dl e , a n Op - a m p fo l l o w e r m a y be a dde d t o t h e o ut p ut o f
t h e R/C n e t w o r k
. Th i s i s i l l us t r a t e d i n fi g ur e 5 .2 .
Th e o p - a m p c h o s e n m us t h a ve r a i l - t o - r a i l c h a r a c t e r i s t i c s s uc h a s t h e
Na t i o n a l Se m i c o n duc t o r LMC 662 o r t h e An a l o g ue De vi c e s OP 296
;
o t h e r w i s e t h e m a x i m um vo l t a g e s w i n g i s a p p r o x 1 V t o 3 .9V
. Th e us e o f
9V fo r t h e o p - a m p 's s up p l y a l l o w s t h e m a x i m um o ut p ut o f 5V t o be
a c h i e ve d
; i f t h e o p - a m p 's s up p l y w a s 5V, t h e m a x i m um o ut p ut w o ul d be
a p p r o x 4 .8V .
Th e p r o g r a m 8B IT_P WM .B AS,
s i m p l y o ut p ut s a vo l t a g e o f 3 .5V, a n d
t h e n p a us e s fo r 1 00m s , w i t h o ut t h e o p - a m p c o n n e c t e d t h e LE D fl a s h e s ,
a s t h e P WM c o m m a n d i s n o t be i n g c a l l e d i n t i m e t o s t o p t h e c a p a c i t o r
fr o m di s c h a r g i n g due t o t h e l o a d t a k e n by t h e l e d
. Wi t h t h e o p - a m p
fo l l o w e r t h e LE D r e m a i n s s t a bl e , a s t h e o p - a m p n o w c a r r i e s t h e l o a d
.
Se c t i o n - 5 - 3
Vo ut
Va r Wo r d
`Out p ut vo l t a g e r e qui r e d
Dut y
Va r B y t e `Dut y va r i a bl e fo r P WM c o m m a n d
Q ua n t a C o n 1 95
`Our qua n t a l e ve l ba s e d o n 5V
Vo ut = 250 'We r e qui r e 2
.5V
Dut y = (Vo ut * 1 00) /qua n t a
`C a l c ul a t e t h e dut y
P w m P o r t B .0, Dut y , 8
`Out p ut t h e vo l t a g e fo r 40m s
9 V o l t s
I n
Experimen t in g wit h t he PicBas ic Pro Co mpil er
M C6
MAW
7 8 1 .05
I N OUT
GN D
I C1
F igu re 5 .1 . Un bu ffered R/C n et wo rk .
Regu l at ed 5 V o l t a
R1
4.7 k
C1

C2
1 Ou 1

0 .l u t
0
C3

C4
6pf

56pt
T
Us in g t he PWM co mman d as a digit al t o an al o g co n vert er
PWM demo n s t rat io n circu it s
a
4mHz
Crys t al
I C2
OSC2
V SS
1 4
PI C1 6F 8 4
5
Sect io n - 5 - 4
V DD RB7
MCLR RB6
RB5
RB4
RB3
OSCI RB2
RBI
R8 0
RA4
RA3
RA2
RA1
RAO
F igu re 5 .2 . Bu ffered o u t pu t .
Regu l at ed 5 V o l t s
R1
4 .7 k
1 4
V DD
3
RB7
MCLR

RB6
2
1 1
RB5
1 0
4mHz R8 4
Crys t al
1 6
RB3
OSCI

RB2
RB1
9
e R2

V o l t age
- 1 0k

Ou t
I
C1 C2
RBO
6
1 001 - 1 :1 o f
PI C1 6F 8 4
RA4
LED
a
OSC2

RA3
z
C5
W MI SS
C3 C4
RA2 1 o f
6pf 56pt
RA1
V SS RAO
8
R3
47 0
Ov .
Ti
I S
ExperimentingwiththePicBasicProCompiler
Programs-1OBITPWM
.BAS,HPWMTST.BASandHPWM .INC
Controllingthe10 -b itHardwarePWM
Althou ghhardwarePWMisn' tu ncommonon somePICs,thenew
PIC16 F 8 7 X rangehav emadethisf eatu rev iab letoexperiment with
b ecau seof theirf lasheepromcapab ilities .Inthisexperiment,wewillb e
u singthePIC16 F 8 7 6 ,b u tany of the8 7 X rangemay b esu b stitu ted .The
16 F 8 7 6 hastwohardwarePWMmodu les ; thesearelocatedonpins12
and13 andarenamedCCP1& CCP2 .U singthesePWMmodu lesisn' t
aseasy toimplementasthecompiler' sPWMcommand,sev eral
hardwareregistersneedtob emanipu lated,andareasonab leamou ntof
mathsisreq u iredtorealiz ethef inalPWMperiodanddu ty cy cle .Wewill
f ocu sonj u stoneof thetwoPWMmodu les,namely CCP1 .
InordertogenerateaPWMsignalf romCCP1acertainseq u enceof
registershastob esetorcleared,theref orewewilllook atthisseq u ence
asaprocessof stepstocarry ou t .
Step1 .
TheCCP1pinalsoaliasesasPortC .2 ,theref orethef irstthingwehav e
todoisconf igu reitasanou tpu t, ( TR / SC .2 = 0 ) .
Step2 .
BothPWMmodu lesareattachedtoTMR 2 ,whichmeansthat b oth
modu leswillsharethesamef req u ency .SoTMR 2 hastob einitialised
F irstly TMR 2 ' s prescalerratio hastob eestab lished . This is
accomplishedb y settingorclearingb its-0 & 1of theT2 CONregister :
-
0 -0 willsettheprescalerratioto1
: 1( TMR 2 willtick onev ery instru ction
cy cle) .
0 -1 willsettheprescalerratioto1 : 4 ( TMR 2 willtick onev ery f ou rth
instru ctioncy cle) .
1-X willsettheprescalerratioto1
: 16 ( TMR 2 willtick onev ery sixteenth
instru ctioncy cle) .
TMR 2 nowhastob etu rnedon ; thisisdoneb y settingb it-3 of T2 CON,
clearingthisb itwilltu rnTMR 2 of f .
Section-5 -5
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
S e c t i o n - 5 - 6
C o n t r o l l i n g t h e 1 0 - b i t h a r d w a r e P W M
S t e p 3
Th e p e r i o d (o r
fr e que n c y) o f TM R2 n o w h a s t o b e e s t a b l i s h e d . Th i s i s
p l a c e d i n t h e P R2 r e g i s t e r
. Th e fo r m ul a t o a c c o m p l i s h t h i s i s : -
P e r i o d = (P R2+1 )* 4 *(1 /Fo s c ) * (TM R2 p r e s c a l e r va l ue )
Th e ' (1 /Fo s c )'
p a r t o f t h e fo r m ul a w i l l a l w a ys yi e l d a fr a c t i o n a l r e s ul t i . e .
(0 . 25 ) .
Th e r e fo r e , i n r e a l i t y w e a r e d i vi d i n g e a c h t i m e w e m ul t i p l y b y t h a t
n um b e r i
. e . (1 0 0 * 0 . 25 = 25 ), w h i c h i s t h e s a m e a s 1 0 0 / 4 . Th i s m e a n s
t h a t , ' * (1 /Fo s c )'
m a y b e r e p l a c e d w i t h ' /Fo s c ' . Our fo r m ul a n o w l o o ks
l i ke t h i s : -
P e r i o d = ((P R2+ 1 ) * 4 / Fo s c ) * (TM R2 p r e s c a l e r va l ue )
S o , fo r a 4m H z o s c i l l a t o r , p r e s c a l e r s e t t o 1
: 1 , a n d P R2 = 25 5
((25 6 * 4)
1 4) *
1 = 25 6
Th e p e r i o d o f t h e P W M w i l l b e 25 6 us
. I n r e a l i t y t h i s i s o n l y a s a c c ur a t e
a s t h e c r ys t a l o r r e s o n a t o r us e d .
To c a l c ul a t e t h e fr e que n c y t h a t t h i s r e p r e s e n t s w e us e t h e fo r m ul a , (1 0 0 0
/ P e r i o d ) . Th i s m e a n s o ur fr e que n c y
(i n kH z ) w i l l b e (1 0 0 0 / 25 6 ) w h i c h
e qua l s 3
. 90 6 25 kH z .
I t w o ul d b e b e n e fi c i a l t o i n c r e a s e t h e fr e que n c y t o a s h i g h a s i t w o ul d g o ,
h o w e ve r , a s t h e fr e que n c y i n c r e a s e s s o t h e r e s o l ut i o n d e c r e a s e s
. To
c a l c ul a t e t h e r e s o l ut i o n o f a g i ve n fr e que n c y w e us e t h e fo r m ul a : -
(l o g (
Fo s c /Fp w m )) / l o g (2)
W h e r e Fo s c i s t h e c r ys t a l fr e que n c y a n d Fp w m i s t h e fr e que n c y o f t h e
P W M s i g n a l , a s c a l c ul a t e d a b o ve
. Th i s fo r m ul a c a n b e b r o ke n d o w n
fur t h e r b y t h e fa c t t h a t t h e l o g o f 2 i s a c o n s t a n t va l ue o f
. 30 1 , t h e r e fo r e
o ur fo r m ul a n o w l o o ks l i ke
: -
(l o g ( Fo s c /Fp w m )) / . 30 1
ExperimentingwiththePicBasicProCompiler
Controllingthe1 0 - b ithard warePW M
S o, f oraf req u ency of 3
. 9 k H z , u singa4 mH z cry stal, ou rf ormu laisnow .
(log(4 0 0 0 0 0 0 /3 9 0 0 ))1 . 3 0 1 =1 0 . 0 0 3
W hichmeansthatwehav earesolu tionof 1 0 - b its
. T hatwasn' ttoob ad ,
wasit?
S tep4
T he1 0 - b itd u ty cy clev alu ehastob eload ed intotwoseparateregisters
inaratherpecu liarway . T hemostsignif icant8 - b itsof thed u ty hav eto
b eplaced intheCCPR L 1 register, and thef irsttwob itsof thed u ty hav e
tob eplaced inb its- 4 & 5 of theCCP1 CO N register . T heref ore, wehav e
toplaceb it- 0 of the1 0 - b itd u ty intotheCCP1 CO N registerb it- 4 and
placeb it- 1 of the1 0 - b itd u ty intotheCCP1 CO N registerb it- 5
. T his
sou nd smored if f icu ltthanitactu ally is, asisd emonstrated inthe
program
1 0 BIT PW M . BA S . W enowneed tocalcu latethev alu etoplace
intothed u ty registerstoprod u ceareq u ired PW M v oltage . F irstly , we
need tocalcu lateou rq u antalev elf ora1 0 - b itresolu tion(0 -
1 0 2 3 ) . T his
ismoref u lly explained intheA ID section . H owev er, thecalcu lation is
(5 /1 0 2 4 )whicheq u als
. 0 0 4 8 8 , wewillmov ethed ecimalpointrightaf ew
timesand rou nd u ptocompensatef orthecompiler' stru ncation
of a
d iv ision, whichmak esou rq u antalev el4 9 . T hef ormu laf orcalcu lating
thed u ty cy clef oragiv env oltageis : -
d u ty =V ou t/q u antalev el
W hereV ou tisanu mb erf rom1 to5 0 0 , wemu stincreasethev alu eof
V ou t, soastoincreasetheaccu racy of ou rresu lt, thiswillb ed oneb y
mu ltiply ingitb y 1 0 0 . S oou rcalcu lationwithintheprogramnowlook slik e
this : -
d u ty =(V ou t* 1 0 0 )/q u anta
S teps
A llthatneed stob ed onenowistotu rnthePW M on, thisisachiev ed b y
settingb its- 2 & 3 of theCCP1 CO N register
. Clearingtheseb itswilltu rn
of f theCCP1 PW M mod u le .
S ection- 5 - 7
ExperimentingwiththePicBasicProCompiler
F igu re5 . 3
. H ard warePW M circu it .
S ection- 5 - 8
Controllingthe10- bithard warePW M
I f CCP2 mod u leisbeingu sed thenregisterCCP2 CO N shou ld be
exchanged f orCCP1CO N
. And CCPRL1shou ld bechanged to
CCPRL2 .
By placingd if f erentd u ty cy clev alu esintothetwo10- bitCCPregisters, a
d if f erentv oltagewillbeprod u ced f romeachCCPmod u le
. H owev er, they
willbothsharethesamef req u ency asthey arebothattached toT M R2
.
T hereisanI nclu d ef ile
H PW M . I N C onthed isk thatsimplif iestheu seof
thePW M mod u les
. T heinclu d ef ilehastobeplaced atthebeginningof
y ou rprogram . T henpriortocallingthe H PW M
su brou tine, twov ariables
hav etobeload ed . T hev ariableV O U T hold sthev oltageou tpu treq u ired ,
and thev ariableCCPhold sthePW M mod u leof interest : -
CCP= 0willtu rnO F F bothPW M mod u les .
CCP= 1willou tpu tthev oltageheld inV O U T toPW M mod u le1
.
CCP= 2 willtu rnPW M mod u le1
O F F
CCP= 3 willou tpu tthev oltageheld inV O U T toPW M mod u le2
.
CCP= 4 willtu rnPW M mod u le2 O F F
CCP= 5 willou tpu tthev oltageheld inV O U T tobothPW M mod u les
T heprogram H PW M T S T . BAS ,
d emonstratestheu seof theinclu d ef ile .
Regu lated 5 V olts
12 0
R1
V DDRC7
17
RC6
e
4 . 7k
RC5
RC4
Ls-

R2
M CLR

RC3 ' - °

10k
RC2 /CCP1
RC1 12 V oltage
RCO ' - ' O u t
RB7
xe
2 7
RB6
ss o
C5
RB5
RB4
2 a

M M M
2 4
lu f
4 mH z
RB3
C2 C7

Cry stal RB2
!-I
I O u f . I ° S lu f
0S C1

RB1
2 2
RBO
s,
°
i PI C16F B76
° RA5
C3
RA4
O S C2

RA3
e
s
C4
se
, 5 6p1 5 6pf
RAI
V S S V S S RAO
2 - oT o
S erialLCD
. T T
1
.

ExperimentingwiththePicBasicProCompiler
Program
- R2R.BAS
Bu ild inganR-2RD igitaltoAnalogu eConv erter
T heR-2RD igitaltoAnalogconv erterissu rprisingly simpletoimplement,
withonly 1 6 externalresistorsconnected inthelad d erf ormation, an
extremely f astand reasonab ly accu rate8 -b itD / Aconv ertercanb e
realiz ed .
T heR-2Rarrangementof resistorswork sb y d iv id ingeachv oltage
presentatitsinpu tsb y increasingpowersof two, and presentsthetotal
of allthesed iv id ed v oltagesatitsou tpu t
.SincethePI Ciscapab leof
d riv ingitsou tpu tsf rom0 to5 V , theR-2Rlad d erconv ertstheb inary
nu mb eronPortBintoaproportionalv oltagef rom0 to5 V instepsof
approximately 20 mV
.
Agreatmany commercialD igitaltoAnalogconv erterswork onthissame
principle, b u talsohav einternalv oltageregu latorsand latches .O u r
d emonstrationd oesn' treq u ireany of thosethings ; theref orewecanu se
theresistorarray alone .F igu re5
.4 showsthecircu itf ortheR-2RD igital
toAnalogconv erter .
Section-5 -9
F igu re5 .4 .R-2RD / Aconv erter .
R2

V oltage
2k

O u t
AN
R3

R1 0
2k

1 k
R4

R1 1
2k

1 k
Regu lated 5 V olts
R5

R1 2
2k

1 k
R1
4 .7 k
V D D RB7
1 7
R6

R1 3
1 2
2k

1 k
MCLR

RB6
1 1 a C5
4 mHz
RB5
RB4
1 0
R7

R1 4 M0
.1 u f
9
Cry stal RB3
2k

1 k
0 SC1

RB2
RB1
e
R8

R1 5
C1 C2
RBO
6
2k

1 k
1 0 u f 1 u f
PI Ci6 F 8 4
E-] : -
RA4
z
R9

R1 6
MEN MEN
C3 C4 a
O SC2

RA3
RA2
i
2k

1 k
6 pf 5 6 pf
RA1
1 e
R1 7
T T 1 1
s

1 k
ExperimentingwiththePicBasicProCompiler
BuildinganR-2Rdigitaltoanalogconverter
T heR-2RdesignhastheadvantageoverPW M inthat, asPW M isa
trainof pulsesthatreq uiref ilteringtheR-2Rdoesnot
. A lso, withthe
sof twareimplementationof PW M , thishastob eref reshedperiodically
;
theR-2Rdesignwillholdtheoutputvoltageuntilthevalueplaced on
PortBischanged
. T heaccuracy of thisdesignreliesonthetoleranceof
theresistorsused, b utevenwithstandard1 0 % resistorstheresultsare
acceptab le
. I f dif f iculty inob taining2k saresistorsisencountered, they
may b esub stitutedf or2 . 2k Q ty pes, withavery marginaldecreasein
accuracy .
T hesof twaretocontroltheR-2RD / A converterisextremely easy to
write, thef ormulatoconverttheb inary representation presentedon
PortBintoavoltageisb asically thesameasf orthePW M command
: -
Bval= V out/ q uantalevel
W hereBvalisthe8 -b itb inary numb erthatisplacedontoPortB,
we
already k nowtheq uantalevelf or5 V and8 -b it (1 95 ) .
W ewillagainscale
upV outf oramoreaccurateresult
. S othecalculationnowlook slik ethis :
Bval= (V out* 1 0 0 )/ 1 95
Program R-2R. BA S ,
demonstratestheuseof theR-2Rdigitaltoanalog
converter . T heoutputvoltagereq uiredisloadedintothevariab leV O U T ,
andthenacallismadetothesub routine R2R . T hiswillcalculatethe
valueof BV A L , asintheab ovecalculation, andoutputitsresulttoPortB .
S ection- 5 -1 0
E x p e r i m e n t i n g w i t h t h e Pi cBasi c Pr o Com p i le r
Pr og r am - MAX5352R
.BAS
I n t e r f aci n g t o t h e MAX5352 D / A Con v e r t e r
T h e MAX5352 i s a 1 2-b i t d i g i t al t o an alog con v e r t e r , w h i ch u se s a 3-w i r e
se r i al i n t e r f ace ( SCL K , D I N, CS) .
I t h as a b u i lt i n op -am p f ollow e r t h at w i ll
allow a f u ll-scale ou t p u t of 0 t o 5V
. H ow e v e r i t d oe s n ot h av e an i n t e r n al
V r e f , t h e r e f or e an e x t e r n al sou r ce h as t o b e ap p li e d . Also, t h e e x t e r n al
V r e f m u st b e 1 .4 V b e low t h e V d d r ai l
. W h i ch m e an s t h e m ax i m u m ou t p u t
v olt ag e , u si n g t h i s t e ch n i q u e , i s 3 .6 V , f i g u r e 5
.5, sh ow s t h e ci r cu i t f or
t h i s
. Bu t all i s n ot lost b e cau se , b y ad d i n g t w o r e si st or s, an d m ak i n g t h e
V r e f 2
.5V , w e can ob t ai n t h e f u ll-scale ou t p u t of 0 t o 5V , f i g u r e 5
.6 sh ow s
t h e r e le v an t ci r cu i t .
Alt h ou g h t h e MAX5352 on ly u se s 1 2-b i t s t o ou t p u t a v olt ag e , i t r e q u i r e s
all 1 6 -b i t s t o b e se n t , t h i s i s b e cau se , w i t h i n t h e 1 6 -b i t s, t h e t h r e e m ost
si g n i f i can t b i t s, an d t h e le ast si g n i f i can t b i t ar e con t r ol f lag s
. T ab le 5.1
sh ow s t h e com m an d b i t s w i t h i n t h i s w or d .
( X = d on ' t car e )

T ab le 5.1
. Bi t s w i t h i n t h e com m an d b y t e .
T h e f i r st d e m on st r at i on u se s an e x t e r n al V r e f of 3.6 V , t h i s
i s
accom p li sh e d , as sh ow n i n f i g u r e 5
.5, b y u si n g a t r i m p ot p ot e n t i om e t e r t o
act as a v ar i ab le v olt ag e d i v i d e r , w h i ch e n ab le s t h e V r e f t o
b e an y
v olt ag e b e t w e e n 0 an d 5V . F or t h i s d e m on st r at i on , ad j u st t h e t r i m m e r
u n t i l 3
.6 V i s ob t ai n e d on p i n 6 of t h e MAX5352 .
Pr og r am MAX5352R.BAS,
i s f or u se w i t h t h i s ci r cu i t . T h e m ai n p r og r am
r e v olv e s ar ou n d t h e su b r ou t i n e MAX-OUT , b u t b e f or e t h i s su b r ou t i n e i s
calle d , t h e v ar i ab le V OUT h as t o b e load e d w i t h t h e r e q u i r e d ou t p u t
v olt ag e , t h i s can b e an y v alu e b e t w e e n 0 an d 36 0 , w h e r e 36 0 i s e q u al t o
3.6 V
. T h e su b r ou t i n e , m u lt i p li e s V OUT b y 1 0 , w h i ch w i ll g i v e u s ou r 1 2-
b i t v alu e
. I t t h e n sh i f t s V OUT , on e p lace t o t h e le f t ; t h i s m ov e s t h e 1 2-b i t s
of v olt ag e d at a i n t o t h e i r cor r e ct p lace w i t h i n t h e 1 6 -b i t w or d an d e n su r e s
b i t -0 i s cle ar . I t t h e n cle ar s b i t s-1 3 . .1 5
( se e t ab le 5. 1 ), b e f or e sh i f t i n g ou t
t h e 1 6 -b i t s .
Se ct i on -5 1 1
1 6 -BI T SE RI AL I NPUT
F UNCT I ON

I
C2
C1 CO D 1 1 D O SO
X 0 0
1 2 b i t s of d at a 0
L oad i n p u t r e g i st e r ; T h e D AC r e g i st e r i s i m m e d i at e ly
u p d at e d ( also e x i t sh u t d ow n )
X 0 1 1 2 b i t s of d at a 0
L oad i n p u t r e g i st e r ; T h e D AC r e g i st e r i s u n ch an g e d
X 1 0 XXXXXXXXXX X
Up d at e t h e D AC r e g i st e r f r om t h e i n p u t r e g i st e r
( also e x i t sh u t d ow n ; r e call p r e v i ou s st at e )
1 1 1 XXXXXXXXXX X Sh u t d ow n
0
1 1 XXXXXXXXXX X No Op e r at i on ( NOP)
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
P r o g r a m - MAX5352.B AS

I n t e r f a c i n g w i t h t h e MAX5352 D / A c o n v e r t e r
Th e s e c o n d de m o n s t r a t i o n o f t h e MAX5352 us e s a 2
.5V V r e f , b ut t h i s
t i m e i t i s g e n e r a t e d b y a Te x a s i n s t r um e n t s TLE 2425, p r e c i s i o n v i r t ua l
g r o un d I C
. Th i s I C , o ut p ut s a r e g ul a t e d 2
.5V f r o m a 5V i n p ut
. Th e r e f o r e ,
w e a r e g ua r a n t e e d a s t e a dy V r e f , w h i c h w i l l g i v e us g r e a t e r o v e r a l l
a c c ur a c y
. I n o r de r f o r t h e MAX5352 t o p r o duc e a m a x i m um v o l t a g e
s w i n g o f 0 t o 5V , t h e i n t e r n a l o p -a m p i s c o n f i g ur e d w i t h a c l o s e d l o o p
g a i n o f t w o
; t h i s i s a c c o m p l i s h e d b y R2 a n d R3
. F i g ur e 5 .6 s h o w s t h e
c i r c ui t f o r t h i s t e c h n i q ue
.
No w t h a t w e a r e o ut p ut t i n g a v o l t a g e g r e a t e r t h a n 3
.6V , w e n e e d t o us e
t h e f o r m ul a s f o r q ua n t a s i z i n g t h e r e s ul t
. F i r s t l y, w e n e e d t o c a l c ul a t e t h e
q ua n t a l e v e l
(s e e p r e v i o us e x p e r i m e n t s ), w h i c h i s
(5/ 4096), t h i s w i l l g i v e
us a q ua n t a l e v e l o f 122
. We n o w n e e d t o c a l c ul a t e t h e v a l ue t o s e n d t o
t h e MAX5352 w h i c h w i l l r e p r e s e n t t h e o ut p ut v o l t a g e r e q ui r e d, jus t t o
r e m i n d yo u, t h e f o r m ul a f o r t h i s i s : -
B v a l = V o ut / q ua n t a l e v e l
Wh e r e , B v a l i s
t h e 12-b i t b i n a r y w o r d t h a t w i l l b e s e n t t o t h e D / A, a n d
V o ut i s t h e r e q ui r e d o ut p ut v o l t a g e
. I n o r de r t o o b t a i n a m o r e a c c ur a t e
o ut p ut v o l t a g e , w e s h a l l b e us i n g a s l i g h t l y di f f e r e n t a p p r o a c h t o t h e
c a l c ul a t i o n s us e d w i t h i n t h e c o m p i l e r c o de
. We w i l l b e us i n g t h e
di v i s i o n a l r e m a i n de r o p e r a t o r , w h i c h i s (/ / ) .
Our f o r m ul a f r o m a b o v e c a n
b e b r o k e n do w n i n t o t h r e e p a r t s , t h e f i r s t w i l l c a l c ul a t e t h e m a i n b o dy o f
t h e r e s ul t , t h e s e c o n d p a r t w i l l c a l c ul a t e t h e r e m a i n de r , a n d t h e t h i r d p a r t ,
a dds t h e s e v a r i a b l e s t o g e t h e r , w h i c h w i l l g i v e us t h e f i n a l r e s ul t
.
F o r e x a m p l e , Le t 's s a y t h a t w e w i s h t o p r o duc e a n o ut p ut v o l t a g e o f 3
.8V
,
t h e c a l c ul a t i o n s w i t h i n t h e c o m p i l e r c o de w i l l l o o k l i k e t h i s
: -
V o ut = 380
Re s ul t = ((V o ut * 100) l q ua n t a l e v e l ) * 10
Re m a i n de r = ((V o ut * 100) / / q ua n t a l e v e l ) / 10
V o ut = Re s ul t + Re m a i n de r
Yo u w i l l n o t i c e t h a t t h e v a l ue s h a v e b e e n s c a l e d up b y a f a c t o r o f 10 o r
100
; t h i s e n s ur e s t h a t w e w i l l a c h i e v e a m o r e a c c ur a t e r e s ul t f r o m t h e
di v i s i o n s
. Th i s t e c h n i q ue c a n b e us e d f o r 8, 10, o r 12-b i t D i g i t a l t o
An a l o g ue c o n v e r t e r s , i f a c c ur a c y i s o f a p r e m i um
.
Se c t i o n -5-
1 2
ExperimentingwiththePicBasicPro Compiler
InterfacingwiththeMAX5352D/Aconverter
MAX535212-bitD/Aconvertercircuits
AdjustableVref
Regulated5Volts
RB2
To RBI
RBO
IC1
TLE2425
TLE2425
( Bottomview)
GND
D
OUT
D IN
Figure5 . 5 .
2
. 5VoltVrefwithop-ampgainofx2
Regulated5Volts
Figure5 . 6 .
IC2
VDD
SCLK
~~DIN
IN1

MAX5352
OUT

REF

FB
GND
GND
OUT
Section-5- 1 3
Voltage
Out
R1
10k
R2
10k
ExperimentingwiththePicBasicProCompiler
Program - AD840X .BAS
I nterf acingtotheAD8402 d igitalpotentiometer
T hed igitalpotentiometer (DP)
allowsmany of theapplicationsof
mechanicaltrimmingpotentiometerstob e
replaced b y asolid -state
d ev ice
.T hed igitalpotentiometer hassev eral
b enef itsov er its
mechanicalcou nterpart, inclu d ingcompactsiz e, f reed omf romshock or
v ib ration, and theab ility towithstand oil, d u st, temperatu reextremes,
and moistu re
.T heserialinterf aceof aDPallowsittob eelectronically
controlled b y amicrocontrollersothattheu sercanad j u stsy stem
parametersq u ick ly and precisely
.SomeDPapplicationsinclu d e
: -
Powersu pply ad j u stment
Au tomaticgaincontrol
V olu mecontroland panning
L CDcontrastcontrol
Programmab lef ilters, d elay s, and timeconstants
T hetwomaj orconf igu rationsof theDPinclu d ethe
RHEOST AT (2 -
terminalconf igu ration) and thePOT ENT I AL
DI V I DER (3 -terminal
conf igu ration) .
And althou ghthed igitalpotentiometerisnotspecif ically
d esigned f oru seasaD/ Aconv erter, itisj u stoneof sev eralj ob sthat
theseremark ab led ev icesarecapab leof achiev ing
.
T heAnalogDev ice' sAD8402 isamemb erof
aseriesof d igital
potentiometers
.T hisf amily consistsof one, two, orf ou rpotentiometer
d ev ices
.T hesearetheAD8400, AD8402 , and AD8403
.Eachof these
d ev icescomeinarangeof resistancev alu es, 1 k Q , 1 0k Q , 5 0k Q ,
and
1 00k Q
.W ewilllook atonly oneof thesed ev ices, namely theAD8402
witha1 Ok Q f ixed resistanceperpotentiometer
.
T heAD840Xseriesprov id es2 5 6 -positiond igitally controlled
v ariab le
resistors (RDAC) .
T heRDACisd esigned withaf ixed resistorv alu ethat
hasawipercontactthattapstheresistoratapointthatisd etermined b y
an8-b itd igitalcod e
.T heresistanceb etween
thewiperand either
end pointof thef ixed resistorv arieslinearly withrespecttothe
d igital
cod elatched intotheRDAC
.Each
RDACof f ersaprogrammab le
resistanceb etweentheAterminaland thewiper(W )and theBterminal
and thewiper (W )
.Au niq u eswitchingcircu itminimiz estheinherent
glitchf ou nd intrad itionalswitched resistord esigns
b y av oid ingany
mak e-b ef ore-b reak orb reak -b ef ore-mak eoperation .
Section-5
1 4
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C
o m p i l e r
I n t e r f a c i n g t o t h e A D 8 4 O X d i g i t a l p o t e n t i o m e t e r s
E a c h R D A C h a s i t s o w n l a t c h t o h o l d t h e 8 - b i t d i g i t a l v a l u e d e f i n i n g t h e
w i p e r p o s i t i o n
. T h e s e l a t c h e s a r e u p d a t e d f r o m a 3 - w i r e S P I ( s e r i a l
p e r i p h e r a l i n t e r f a c e ) . T e n b i t s m a k e u p t h e d a t a w o r d n e e d e d f o r t h e
s e r i a l i n p u t r e g i s t e r . T h e f i r s t t w o a d d r e s s b i t s s e l e c t a n R D A C t o m o d i f y
a n d a r e t h e n f o l l o w e d b y e i g h t d a t a b i t s f o r t h e R D A C l a t c h . T h e b i t s a r e
c l o c k e d o n t h e r i s i n g e d g e o f t h e s e r i a l c l o c k M S B
( m o s t s i g n i f i c a n t b i t )
f i r s t . T h e C S p i n s t a r t s a s e r i a l t r a n s a c t i o n b y g o i n g l o w a n d t h e n l a t c h e s
t h e 1 0 - b i t s o f d a t a c l o c k e d b y g o i n g b a c k h i g h .
T h e A D 8 4 0 2 p r o v i d e s e n h a n c e m e n t s o v e r t h e A D 8 4 0 0 , s u c h a s r e s e t
a n d s h u t d o w n . W h e n t h e R S p i n i s p u l l e d l o w , t h e v a l u e s o f t h e R D A C
l a t c h e s r e s e t t o a m i d s c a l e v a l u e o f $ 8 0 ( 1 2 8 ) . W h e n t h e S H D N p i n i s
p u l l e d l o w , t h e p a r t f o r c e s t h e r e s i s t o r t o a n e n d - t o - e n d o p e n c i r c u i t o n
t h e A t e r m i n a l a n d s h o r t s t h e B t e r m i n a l t o t h e w i p e r
( W ) . W h i l e i n
s h u t d o w n m o d e , t h e R D A C l a t c h e s c a n b e u p d a t e d t o n e w v a l u e s
.
T h e s e c h a n g e s w i l l b e a c t i v e w h e n t h e S H D N p i n i s b r o u g h t b a c k h i g h
.
F i g u r e 5
. 6 s h o w s t h e i n t e r n a l s o f t h e A D 8 4 0 2 .
VD D - - o p .
D GN D - 1 1 o -
A GN D - 1 0 -
A D D R E S S
D E C O D E R
1 S B I T
S E R I A L
LA T C H
- t • VR 1
0 - 8 I T LA T C H
VR 2
8 - B I T
LA T C H
S e c t i o n - 5 - 1 5
VR 1
R 2
A l
W 1
B 1
A 2
W 2
B 2
F i g u r e 5 . 6 . B l o c k d i a g r a m o f t h e A D 8 4 0 2 d i g i t a l p o t e n t i o m e t e r .
ExperimentingwiththePicBasicProCompiler
T ab le5
. 2 . D ataf ormat
T ab le5 . 3 . A d d ressb itf ormat .
ProgrammingtheV ariab leR esistor
T henominalresistance, R
A B , b etweenterminalsA and Bof theA D 8 4 0 2
u sed inthisd iscu ssionis1 O k u . T heR
A B of theR D A Chas2 5 6 resistiv e
contactpointsthatcanb eaccessed b y thewiperterminalplu sthe
B
terminalcontact .
F oran8 - b itv alu eof $ 0 0 , thewiperstartsattheBterminal
. T heB
terminalhasaninherentresistanceof 5 0 Q
. T henextresistiv econnection
hasad igitalv alu eof $ 0 1
. I thasav alu eeq u altothe
Bterminal
resistanceplu sanL S Bresistorv alu e
. F orthe1 0 k Q partu sed , thisL S B
amou ntiseq u alto1 O k oJ 2 5 6 or3 9
. 0 6 2 5 Q
. T heref ore, theresistiv ev alu e
at$ 0 1 is8 9 . 0 6 2 5 Q
(5 0 sa+3 9 . 0 6 2 5 Q ) .
EachL S Bincreasemov esthe
wiperu ptheresistorlad d eru ntilthelasttappointishit
.
S ection- 5 . 1 6
I nterf acingtotheA D 8 4 O X d igitalpotentiometers
T heserialinterf acereq u iresd atatob einthef ormatshownintab le5
. 2 .
F irst, thead d ressb itsof A 1 and A O mu stb esent, tab le5
. 3 showsthe
f ormatf orthetwoad d ressb its
. T henexteightb itsarethed atav alu eto
b elatched intotheselected R D A C
.
F igu re5 . 7
. V ariab leresistoror(R H EO S T A T )conf igu ration .
A D D R
D A T A
B9 B8
B7 B6 B5 B4 B3 B2 B1 BO
A l A O D 7
D 6 D 5 D 4 D 3 D 2 D 1 D O
MS B L S B MS B
L S B
2 " 2 " 2 '
2 °
A l A O R O A Cd ecod ed
0 0
R D A C#1
0
1 R D A C#2
1 0
R D A C#3 A D 8 4 0 3 only
1 1
R D A C#4 A D 8 4 0 3 only
ExperimentingwiththePicBasicProCompiler
I nterf acingtotheA D 8 4 0 X d igitalpotentiometers
TheresistancebetweenterminalBand thewiperWcanbed escribed
usingthef ormula : -
RWB=D *(R
ae /256)+RB
where
R WB
=theresistancebetweenthewiperW,and terminalB
D =d igitalv alueof theRD A Clatch
R A B
=thenominalresistancebetweenterminalA and
B (10 ks4
R B
=theresistanceof terminal
B (50 x4
Table5 . 4 . R WB
Resistancev alueswithR
A B =1
Q
Notethatthezero- scalev alueprod ucesaresistanceof 50 Q
. Care
should alsobetakentolimitthecurrentf lowbetweenthewiperand
terminalBtoamaximumof 5mA
.
TheRD A Cisf ullysymmetrical
. TheresistancebetweenthewiperWand
terminalA alsoprod ucesaresistancev alueof R
WA . Whensettingthe
resistancef orR WA ,
thed igitalv alueof $ 0 0 startstheresistancesettingat
itsmaximumv alue
. A sthed igitalv alueisincreased ,theR WA
resistance
d ecreases
. Thiscanbed escribed usingthef ormula
: -
R WA =(256- D )*(R
A B /256)+RB
where
RWA
=theresistancebetweenthewiperW,and terminalA
D =d igitalv alueof theRD A Clatch
R A B
=thenominalresistancebetweenterminalA and
B(10 ks4
R B
=theresistanceof terminal
B (50 Q)
Table5 . 5 . RWA Resistancev alueswithR
A B =10 Q
Section- 5- 17
D RWB (9)
OutputState
255 10 0 10 . 9375
F ullscale
128 50 50
Mid scale
1 8 9. 0 625
1 8 1 L eastsignif icantbit(L SB)
1 50
Zero- Scale
D
RWB(Q)
OutputState
255
8 9. 0 625
F ullscale
128
50 50
Mid scale
1
10 0 10 . 9375 1 8 1
L eastsignif icantbit(L SB)
1 50
. 0 50 Zero- Scale
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
In t e r fa c i n g t o t h e AD840X di g i t a l p o t e n t i o m e t e r s
In t h i s e x p e r i m e n t , w e a r e o n l y i n t e r e s t e d i n us i n g t h e AD8402 a s a
di g i t a l t o a n a l o g ue c o n ve r t e r t h e r e fo r e
; w e s h a l l l o o k a t a m e a n s o f
p r o vi di n g a va r i a bl e vo l t a g e o ut p ut
. Th i s i s a c c o m p l i s h e d by t h e us e o f
t h e p o t e n t i a l di vi de r c o n fi g ur a t i o n , i l l us t r a t e d i n fi g ur e 5
.8 . Th e DP c a n
e a s i l y be us e d t o g e n e r a t e a n o ut p ut vo l t a g e p r o p o r t i o n a l t o t h e vo l t a g e
a p p l i e d be t w e e n t e r m i n a l s A'a n d B
. If t e r m i n a l A i s c o n n e c t e d t o t h e + 5 V
s up p l y, a n d t e r m i n a l B i s c o n n e c t e d t o g r o un d, t h e w i p e r vo l t a g e h a s a
r a n g e o f OV up t o 1 LSB l e s s t h a n + 5 V
. E a c h LSB i s e qua l t o t h e vo l t a g e
a c r o s s t e r m i n a l s A a n d B di vi de d by 25 6
. Th e w i p e r 's o ut p ut vo l t a g e c a n
t h e r e fo r e be c a l c ul a t e d by us i n g t h e fo l l o w i n g fo r m ul a : -
V w = ( D/25 6) * V
AB
+ V B
w h e r e
V w
= vo l t a g e o n w i p e r
D = di g i t a l va l ue o f t h e R DAC l a t c h
V AB
= vo l t a g e a c r o s s t e r m i n a l A a n d B
V B = vo l t a g e a t t e r m i n a l B
Fi g ur e 5 .8
. P o t e n t i a l di vi de r c o n fi g ur a t i o n ,
Fo r e x a m p l e i f w e a r e us i n g t h e 10ko p a r t w i t h + 5 V c o n n e c t e d t o
t e r m i n a l A, a n d a m i ds c a l e va l ue o f $80 ( 128)
i s p l a c e d i n t o t h e R DAC 's
l a t c h
. Th e vo l t a g e o n t h e w i p e r t e r m i n a l ( V w ) w o ul d be : -
V w = ( 128 / 25 6) * 5 = 2
.5 V o l t s
In t h e a bo ve e x a m p l e , t h e
( VAB + V B ) p a r t o f t h e c a l c ul a t i o n m a y be
r e p l a c e d w i t h 5 a n d 0 r e s p e c t i ve l y, a s t h e s up p l y vo l t a g e
( VAB )
w i l l
i n va r i a bl y a l w a ys be + 5 V a n d t h e vo l t a g e o n t h e B t e r m i n a l
( VB ) w i l l
us ua l l y be OV
.
Ho w e ve r , w e n e e d t o kn o w w h a t va l ue t o p l a c e i n t o
D ( R DAC l a t c h ) t o
o ut p ut a s p e c i fi c vo l t a g e
.
Se c t i o n - 5 - 1 8
ExperimentingwiththePicBasicProCompiler
Section-5- 1 9
InterfacingtotheAD840Xdigitalpotentiometers
T hecalcu lationweshallu seisb asically thesamefortheprev iou sDAC
experiments . W ecalcu latetheq u antalev elfor8-b itsofdatau singa5V
su pply (5/256) .
T hisgiv esu sou ru su alq u antalev elof . 01953 , rou nding
thisu pandmov ingthedecimalpointtotherightafewtimes, giv esu s
ou rfinalq u antalev elof196
. T herefore, thecalcu lationplacedinthe
programwilllook lik ethis : -
D=(V w *100) /q u antalev el
T hev alu eofV W
hasb eenincreasedb y afactorof100, toenab leamore
accu rateresu lt .
Program AD840X
. BAS andthecircu itinfigu re5. 9demonstratetheu se
ofanAD8402toou tpu tav oltagefrom0to4
. 99V . Itiscentredarou nd
thesu b rou tine RDACOUT , thissu b rou tineou tpu tsthe10-b itwordtoan
AD8400, AD8402, orAD8403 digitalpot . T heinternalRDACofchoice
(1 .
. 4)isloadedintothev ariab le RDAC, andthev oltagetoou tpu tis
placedintoV OUT . T hesu b rou tinecalcu latesthev alu ewhichisto
b e
placedintothespecificRDAClatchandcheck sthev ariab le RDAC .
A
seriesof if-thens
determinetheaddressb itstosetorclear . T henthechip
isenab ledb y pu llingtheCSpinlowandthe10-b itsofdataareshifted
ou t
. T hechipisdisab ledb y b ringingtheCSpinb ack high, andthe
su b rou tineisexited .
T hemainb ody oftheprogramlook sattheswitchesconnected to
PortB . 3 (SW 1)andPortB . 4(SW 2)
. Dependingonwhich oftheseis
pressedtheprogramwillincreaseordecreasetheou tpu tv oltage .
Regu lated5V olts
F igu re5. 9. D/Aconv erteru singadigitalpotentiometer .
R1
T o
SerialLCD
4. 7 k
V DDRB7
MCLR

RB6
L- V DD
V oltage
RB5 SHDN

Al
4mHz RB4
iB
R-9

W 1
, 2

Ou t
Cry stal
RB3
B
81
14
0SC1

RB2
RB1
e
B
SDI
A08402/10
RBO
PIC16F 84
8
CLK
A2
swi
L=
C1
RA4
OSC2

RA3 O
W 2
B2
T
lou t
C3 C4
RA2
AGNDDGND
C2 W M
22pf 22pf
RAI
a
Ov
v ss RAO
r
1sw2
e0 °
.
1 afI T T
ExperimentingwiththePicBasicProCompiler
Section-6
Experimenting
with
RemoteControl
SonyinfraredremotecontrolReceiver .
Assemblercoded,SonyremotecontrolReceiver .
SonyinfraredremotecontrolTransmitter
.
Assemblercoded,SonyremotecontrolTransmitter .
InfraredTransmitter .
InfraredReceiver .
TransmittingandReceivingserialinfrared .
418mHz,A . M. radioTransmitter .
418mHz,A. M
. radioReceiver .
ExperimentingwiththePicBasicProCompiler
Programs- SONY_REC . BA S & SONY_RX. I NC
Sony , inf rared remotecontrolReceiv er
T herearethreemainprotocolsu sed f orthetransmissionand reception
of inf rared signals . RC5 , whichisu sed b y Philips, Rec- 8 0 , whichisu sed
b y Panasonic, and theSony f ormat ( SI RCS) , whichwillb ed escrib ed
here . Eachf ormof inf rared signallinghasonethingincommon, thatis
theu seof mod u lated inf rared light . M od u lation isu sed toenab lea
certainamou ntof immu nity f romamb ient lightsou rces, especially
f lu orescentlighting . T hef req u ency of mod u lationv ariesf rom3 6 k H z to
4 0 k H z , d epend ingonthemanu f actu rer . A ninf rared d etectorisreq u ired
toconv ertthismod u lated lightintoad igitalsignal
. T heseare read ily
av ailab leinj u stab ou tev ery T V , V CR, and satellitereceiv ermad ewithin
thepast2 0 y ears . T hety peu sed f ortheseseriesof experimentsisthe
SiemensSF H 5 0 6 - 3 8 , ( u nf ortu nately it' snowou tof prod u ction, b u tthe
alternativ esaretheSF H 5 1 1 0 ortheL T - 1 0 5 9 ) . T hesearesmallthree
terminald ev icesthathav eacentref req u ency of arou nd 3 8 k H z .
H owev er, j u stab ou tany ty pemay b esu b stitu ted , theonly d if f erencethat
willb eapparentwillb easlightlack of range .
F ortheSony protocol, theremotesend sastartb it, sometimescalled an
A G Cpu lse, thatis2 . 4 msin length . T hisallowsthereceiv erto
sy nchroniz e, and ad j u stitsau tomaticgaincontrol, thisoccu rsinsid ethe
inf rared d etectormod u le . A f terthestartb it, theremotesend saseriesof
pu lses . A 6 0 0 u spu lserepresentsaz ero, and a1 2 0 0 u spu lserepresents
aone, thereisa6 0 0 u sgapb etweeneachpu lse . Notallmanu f actu rers
stick stringently tothesetimings, sowewill
consid erthemas
approximates . A llof thesepu lsesb u ild u pa1 2 - b itserialsignalcalled a
pack et . T hiscomprisesof a7 - b itb u ttonv alu e ( theremoteb u tton
pressed ) , and a5 - b itd ev icev alu e ( T V , V CR, etc)
.
T heserialsignalis
transmitted withtheleastsignif icantb itsentf irst .
F igu re6 . 1 , showsthereceiv ercircu it . PortA . 0 isanou tpu ttoaserial
L CD mod u le, setf orinv erted 9 6 0 0 b au d
. T hegreenL ED f lasheswhena
v alid 1 2 - b itpack etisreceiv ed .
T heprogram SONY_REC . BA S, u sesaninclu d ef ile, SONY_RX. I NC to
load inthereceiv ersu b rou tine . W henthesu b rou tine SONYI N iscalled ,
itretu rnsthreev alu es . T heb u ttonpressed ontheremote isheld in
I RD A T A , thed ev icecod eisheld in I RD EV , and theb itf lag, I RV A L I D
issetif av alid signalwasd etected , and clearif not
.
Section-
6 - 1
ExperimentingwiththePicBasicProCompiler
T heref ore, ou rcod ewilllook lik ethis : -
A gain :
G osu b S ony _ I n

` R eceiv ethe1 2 - b itpack et
I f I R V alid = 0 thengotoA gain

` T estif av alid pack etreceiv ed
T hethreev ariab les, I R _ D A T A , I R _ D EV , and I R V A L I D arealread y pre-
d eclared withintheinclu d ef ile . H owev er, thePortand pinonwhichthe
inf rared d etectorisconnected mu stb echanged withintheI nclu d ef ile, if
PortA . 4 isnotu sed .
T hecod ewithinthesu b rou tine S ONY- I N work slik ethis, F irst, ittests
theinpu tonwhichtheinf rared d etectorisconnected , thiswillb elowif
wearealread y inthemid d leof apack et, ( note : thed etectorpu llsits
ou tpu tlowwhenasignalisd etected ) . I f wearenotalread y inthemid d le
of ad atapack et, thehead erpu lseislook ed f oru singthePU L S I N
command ; theresu ltisplaced inthev ariab le, S T . Notallremotessend
anexact2 . 4 mshead erpu lse ; theref orewetestf orapu lsewithinthe
limitsof 2 msto2 . 7 ms . T hePU L S I Ncommand , u sed witha4 mH z cry stal
hasaresolu tionof 1 0 u s; theref oreapu lseof 2 . 4 ms ( 2 4 0 0 u s) willb e
retu rned as2 4 0 . I f ahead erisnotd etected thef lag I R V A L I D iscleared ,
and thesu b rou tineisexited . H owev er, if av alid head erisd etected , a
loopof 1 2 issetu p, withinthislooptheind iv id u ald atab itsareinpu tted ,
againu singthePU L S I Ncommand . W ek nowthata1 b ithasapu lse
d u rationof 1 2 0 0 u s, and thata0 b ithasad u rationof 6 0 0 u s, theref orewe
cansplitthed if f erenceand say thatapu lsed u rationof ov er9 0 0 u smu st
b ea1 b it, and any v alu eu nd erthis, mu stb ea0 b it . T heloopcou nter
d oesthis1 2 timestob u ild u pthe1 2 - b itpack et . Eachtimeapu lseof
ov er9 0 isreceiv ed theappropriateb itof thev ariab le
I R W OR D isset,
elseitiscleared .
A f terthe1 2 - b itshav eb eenreceiv ed , the7 - b itb u ttoncod eand the5 - b it
d ev icecod emu stb eseparated intotheirappropriate v ariab les . T o
separatetheb u ttoncod e, thev ariab le I R W OR D isA ND ed with
% 0 1 1 1 1 1 1 1 , thishastheef f ectof mask ingallb u tthef irst7 - b its, the
resu ltisthenplaced intothev ariab le I R D A T A . T oseparatethed ev ice
cod e, thev ariab le I R _ W OR D isshif ted rightsev entimes, the5 - b itcod e
nowstartsatb it- 0 of I R W OR D , againitisA ND ed , thistimewith
% 0 0 0 1 1 1 1 1 , theresu ltisthenplaced intothev ariab le I R _ D EV . T hef lag,
I R _ V A L I D isset, whichind icatesav alid pack ethasb eenreceiv ed , then
thesu b rou tineisexited .
S ection- 6 -
2
S ony , inf rared remotecontrolR eceiv er
ExperimentingwiththePicBasicProCompiler
Programs - SONY_ASM.BAS & ASM_RX.INC
Section-6
- 3
Sony , inf rared remotecontrolReceiv er
Assemb lercod ed , Sony remotecontrolReceiv er
.
T heinclu d ef ile
ASM_RX.INC, achiev esthesameresu ltsasthe
prev iou s, BASICcod ed v ersion, exceptthatit-isalotsmaller, only 7 7
b y tes, and isalsoonly execu tab leu singa4 mH z cry stal
.Exactly the
samev ariab lesare retu rned ,
namely , IRD AT A, IRD EV , and
IRV AL ID .
Inad d ition, twonewd ef ineshav eb eenad d ed , toinf ormthe
su b rou tineastowhichpintheinf rared d etectoristob eplaced , these
are : -
D ef ine IRINPORT

Port

` Port f orthe IRd etector
D ef ine IRIN_BIT

Bit

` Bit f orthe IRd etector
If theseareomitted f romtheprogram, thed ef au ltisPortA .4 .Asalway s,
theinclu d ef ilemu stb eplaced attheb eginningof y ou rprogramtoav oid
any pageb ou nd ary conf licts .
+ 5 V olts
SFH 5 06
H
211
1
.V OU l
2.V cc
3 .Gnd
v cc -
v ou l
Gnd
T o
SerialL CD
N9600b au d
Figu re6
.1 .Sony , Inf rared remotecontrolReceiv er .
Rl
4 .7 k
V D D
RB7
L 3_
MCL R

RB6
2
4 mH z
RB5
0
Cry stal
RB4
RB3
s
SFH 5 06-38
1s
0SC1

RB2
e
IRd etector
7
RB1
6
C1 C2
RBO
lOu f
PIC16F8 4
3
~O.iu f
0 la
RA4
C3 C4
OSC2

RA3
t
"'
2pf 22pf
RA2
RA1
e
R2
4 7 0
RAO
T
S
V ~5
s

Green
ti
L ED
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o
C o m p i l e r
P r o g r a m s - B AS_TX.B AS, SNY_SE ND & SONY TRUNC
So n y, i n fr a r e d r e m o t e c o n t r o l Tr a n s m i t t e r
Th e t r a n s m i t t e r de s c r i be d h e r e c o m p l e m e n t s t h e p r e vi o us r e c e i ve r
e x p e r i m e n t s
. Th e t r a n s m i t t e r s e n ds o ut a 2 .4m s h e a de r p ul s e , t h e n a 12 -
bi t w o r d c o n s i s t i n g o f a 7 -bi t but t o n c o de , a n d a 5 -bi t de vi c e c o de . Un l i k e
o t h e r p r o g r a m s t h a t r e qui r e a g a t e d o s c i l l a t o r t o g e n e r a t e t h e 38k Hz
m o dul a t i o n , t h i s i s a c h i e ve d w i t h i n t h e c o de i t s e l f . 38k Hz h a s a t i m e
dur a t i o n o f 2 6us
; t h e r e fo r e , by t ur n i n g t h e i n fr a r e d LE D o n fo r 13us a n d
o ff fo r 13us , a p ul s e o f 38k Hz i s t r a n s m i t t e d, (Ti m e (i n us ) = 1000 /
Fr e que n c y (i n k Hz)) .
Th i s i s a c c o m p l i s h e d by t h e s ubr o ut i n e , IR_MOD,
t h i s t ur n s t h e i n fr a r e d LE D o n , w a i t s 8us , t h e n t ur n s t h e i n fr a r e d LE D o ff,
a n d w a i t s a fur t h e r 7 us . As s um i n g a 4m Hz c r ys t a l , t h e c o m m a n ds LOW
a n d HIGH e a c h t a k e 4us t o c o m p l e t e , t h e NOP 's t a k e 1 us e a c h , a n d t h e
GOSUB a n d RE TURN c o m m a n ds t a k e a fur t h e r 3us
. So a l t o g e t h e r , w e
h a ve a m o dul a t i o n t i m e o f (2 +4+8+4+7 +1=2 6us ) .
If a P IC i s us e d w i t h
m o r e t h a n 2 k o f ROM, t h e n t h e c o m p i l e r w i l l p l a c e e x t r a c o de t o
m a n i p ul a t e t h e P C LATH r e g i s t e r fo r t h e GOSUB c o m m a n d . Th i s w i l l
n e e d t o be c o m p e n s a t e d fo r , by r e duc i n g t h e a m o un t o f NOP 's . Th e
P AUSE US c o m m a n d c o ul d n o t be us e d, a s i t s m i n i m um de l a y i s 2 4us
w i t h a 4m Hz c r ys t a l , h e n c e t h e us e o f t h e NOP 's . Th e IR_MOD
Se c t i o n -6 -
4
s ubr o ut i n e i s s h o w n be l o w : -
IR_Mo d.-
Hi g h IR LE D 'Tur n o n t h e IR LE D . (4 c yc l e s )
• No p
Q No p
'E a c h NOP t a k e s 1 i n s t r uc t i o n c yc l e
@ No p ' a s s um i n g a 4m Hz c r ys t a l
No p
No p
'Re m o ve fo r P IC s w i t h m o r e t h a n 2 K ROM
No p
No p
No p
'Do n o t h i n g fo r 8 c yc l e s
Lo w IR LE D
'Tur n o ff t h e IR LE D. (4 c yc l e s )
No p
Q No p
Q No p
Q No p 'Re m o ve fo r P IC s w i t h m o r e t h a n 2 K ROM
C '
No p
Q No p
C ! No p
'Do n o t h i n g fo r a fur t h e r 7 c yc l e s
Re t ur n 'Re t ur n fr o m t h e s ubr o ut i n e , (1 c yc l e )
ExperimentingwiththePicBasicProCompiler
Sony,infraredremotecontrolledTransmitter
Transmittingthepu lsedu rations,6 0 0 u s,1 2 0 0 u s,and2 4 0 0 u s,is
performedb ythesu b rou tine,
BURST, thiscreatesaloopofdifferent
lengthsforeachdu ration
. Thetimingsofthisloopwereaccomplishedb y
trialanderror,asit' snotaseasytocou nt
. thecyclesu sedinthis
su b rou tineasitwasin
IRMOD . W ithintheloop,theinfraredL ED
modu lationsu b rou tineiscalled,thu stransmittingamodu latedsignalfor
agiv entime
. Thev ariou spu lsedu rationsareplacedinthev ariab le,
B- TIME .
Bu rst :
F orB_ L oop= itoBTime
' L oopforthepu lsedu rationreq u ired
G osu b IRMod

' Modu latetheIRL ED,( 2 cycles)
N extB- L oop

' Closethepu lsedu rationloop
Pau seu s6 0 0

' Pau sefor6 0 0 u safterev erypu lse
Retu rn

' Exitthesu b rou tine
N owthatwehav ethemeanstosendtheinfraredsignal,weneedto
b u ildu pthe1 2 - b itword
( k nownasapack et) , whichcontainstheb u tton
anddev icecodes
. F irstly,weneedtoplacethetwocodesintheircorrect
positionswithinthepack et,
( theb u ttoncodeinthefirst7 - b itsandthe
dev icecodeinthenext5 - b its)
. Thev ariab le IR_ W ORD
holdsthepack et
thatwillb esent . Thedev icecode,heldin
IRCMD isfirstplacedintothe
highb yteof IR_ W ORD,
thenshiftedrightoneb it
. Thiswillplaceit
startingattheb it- 8
. Bit- 7 oftheb u ttoncode, IRBYTE
isclearedasa
precau tionagainstav alu egreaterthan1 2 7 b eingentered
. Thenitis
ORedinto
IRW ORD,
thishastheeffectofsu perimposingonev alu einto
another
. W enowhav eou rtwocodesintheircorrectpositionswithin
IR_ W ORD readytosend
. A for- nextloopissetu ptoexaminethefirst1 2 -
b itsof
IRW ORD, iftheb itisa1 then
B- TIME isloadedwiththev alu e
forapu lselengthof1 2 0 0 u s,elseitmu stb eaz ero,andapu lselengthof
6 0 0 u sisplacedinto B- TIME .
A fterall1 2 - b itshav eb eensent,adelayof3 5 msisimplemented
; thiswill
b ringthetotaldelaytimeofthepack etsent,toapprox4 5 ms
.
Tou setheinfraredtransmitter,placetheb u ttonv alu ewithinthev ariab le
IRBYTE, andthedev icecodewithinthev ariab le
IRCMD .
IR
_
CMD= 1

' Setdev icecodeto1 ( telev ision)
IR_ BYTE= 1 8

' Sendv olu meu pcommand
G osu b Sony_ Ou t

' Sendthe1 2 - b itpack et
Section- 6 - 5
ExperimentingwiththePicBasicProCompiler
Sony,infraredremotecontrolledTransmitter
Program, BASTX.BAS,
demonstrates the u se ofthe infrared
transmitter,witha1 2 - b u ttonk eypad,asinfigu re6 .2 .Thek eypadisu sed
tosendthechannelb u ttonsandv olu meu panddown, " * " isu sedfor
v olu medown,and" # " isu sedforv olu meu p .Thelook u ptab leconv erts
thev alu esretu rnedfromthe INKEYS
su b rou tine,intothev alu eexpected
b ytheSonydev iceyou wishtocontrol,atelev isioninthisinstance .
Program, SNYSEND.BAS,
doesexactlythesameastheab ov e
program,b u tu singtheinclu defile
SONYTRX.INC
.
F igu re6 .2 ,showstheconnectionstothepic .Transistor,Q 1 amplifiesthe
ou tpu toftheinfraredL ED,you willhav enoticedthatthereisnoseries
resistorwiththeinfraredL ED,thisisb ecau setheL EDisnev erfu llyon,it
isalwaysmodu latedwitha3 8 k H z signal .ThisactsasaformofPW M
.If
Q 1 isomitted,theinfraredL EDmayb econnecteddirectlytothepinof
thePIC,howev er,thiswillresu ltinadrasticlack ofrange .Thegreen
L EDisillu minatedwhenev erasignalistransmitted
.
+5Volts
C1
°
T
1 Ou t

C3

C4
C2 -
_2 2 pf2 2 pf
I O.1 u l
TTT
O
v
9
4mH z
Crystal
VDDRB7
M CL R RB6
RB5
RB4
RB3
0SC1 RB2
RB1
RBO
PIC1 6 F 8 4
RA4
0SC2 RA3
RA2
RA1
VSSRAO
F igu re6 .2 .Sony,infraredremotecontrolTransmitter .
Section- 6 - 6
ExperimentingwiththePicBasicProCompiler
Programs-SONY_TX
.BAS & SONYTX.INC
Sony , IR remotecontrolled Transmitter
Assemb lercod ed , Sony Inf rared remotecontrolTransmitter
Thisassemb lercod ed transmitteru sesthesameprincipalsasd escrib ed
f ortheBASICcod ed v ersion, b u tu sesalotlessmemory withinthePIC
.
Theassemb lersu b rou tineistransparenttoy ou rBASICprogramasitis
inthef ormof aninclu d ef ile, SONYTX
.INC, and acalltoasu b rou tine,
SONYOUT.
Aswiththereceiv ersu b rou tineitisonly compatib lewitha
4 mH z cry stal
.Theassemb y cod ewillnotb eexplained , howev eritisf u lly
commented if y ou wishtoexamineitmoreclosely
.
Tou setheinf rared transmitter, placetheb u ttonv alu ewithinthev ariab le
1 R _BYTE,
and thed ev icecod ewithinthev ariab le IR _CMD
. Thev ariab le
nameshav eb eenchanged f romthereceiv errou tine
toav oid any
d u plicatev ariab leerrorsoccu rringif b othareu sed
withinthesame
program
.Again, therearetwonewd ef inesad d ed , these
inf ormthe
su b rou tinewhichportand b ittoplacetheinf rared L ED
.Theseare : -
Def ine
IR OUTPOR T Port

` Portf ortheIR L ED
Def ine IR OUT_BIT

Bit

` Bitf ortheIR L ED
If thesed ef inesareomitted f romy ou rprogramthed ef au ltsare, PortA
.O .
Program SONYTX
.BAS, d emonstrates
theu seof the inf rared
transmitter, witha1 2 -b u ttonk ey pad , asinf igu re6
.2
.Thek ey pad isu sed
tosend thechannelb u ttonsand v olu meu pand d own,
" * "
isu sed f or
v olu med own, and " # " isu sed f orv olu meu p
.TheL OOKUPcommand
conv ertsthev alu esretu rned f romthe
INKEYS
su b rou tine, intothev alu e
expected b y theSony d ev icey ou wishtocontrol, atelev ision
inthis
instance .
Section-6 -
7
ExperimentingwiththePicBasicProCompiler
Programs -IRTRANS.BAS &
IR_RX_TX .INC
Inf rared Transmitter
Theprev iou stwoproj ectsareid ealif aremotecontrolhand setisallthat
isb eingimplemented .H owev er, if af u ll8 -b itb y te
istob esentor
receiv ed thentheproj ectpresented herecanb eu sed .W ithintheInclu d e
f ile, IR_RX_TX .INC,
therearetwosu b rou tines, IRO U T, whichwill
transmitan8 -b itb y te, alongwithau niq u etransmitternu mb er, and
IRIN,
whichwillreceiv etheIRsignalf romitscomplementary transmitter
. Both
su b rou tinesarewritteninassemb ler, and aref oru se witha4 mH z
cry stal
.H owev er, thisistransparenttoy ou rBASICprogram, and allthat
isreq u ired arethataf ewv ariab lesb eload ed , and acallmad etothe
relev antsu b rou tine .Thead d ed ad v antageisthat, b oththe
IRO U T and
IRIN
su b rou tinescomb ined , only u se1 1 2 b y tesof RO M .The
transmissionand receptionmethod u sed , isb ased ontheSony protocol,
howev er, instead of send ing1 2 -b its, 1 6 -b itsaresent
.Thismeansthata
f u ll8 -b itscanb esentf orthed atab y te, and another8 -b itscansignif y a
u niq u enu mb erf oreachtransmitteru sed .F ou rnewd ef ineshav eb een
ad d ed , toinf ormthesu b rou tinesof theportand pintoconnect the
inf rared d etectorand theinf rared L ED
.Twoof thesed ef inesaref orthe
transmittersu b rou tine, IRO U T and theseare : -
D ef ine IRO U T_ PO RT Port

` Port f ortheIRL ED
D ef ine IRO U TBIT

Bit

` Bitf ortheIRL ED
If thed ef inesarenotu sed iny ou rprogramthed ef au ltisPortA .O
Tou sethetransmittersu b rou tine, load theb y tetosend intothev ariab le
IRBY TE, and thetransmitterid into IRID , thenmak eacallto
IRO U T .
F orexample : -
IR_
ID = 2

` Thisistransmitter2
IR_ BY TE= 2 5 4

` L et' ssend thev alu e2 5 4
Gosu b IRO U T

` Transmitthetwob y tes
Thetwov ariab les, IRBY TE and IRID arepre-d eclared withinthe
inclu d ef ile,
IRRXTX .INC, theref ore, they d onotneed tob ed eclared
withiny ou rprogram .
Thecircu itf orthe IRO U T
su b rou tineisthesameastheSony remote
controltransmitter, f igu re6 .2
.Bu tthek ey pad may b ed iscard ed .
Section- 6 -8
E x p e r i m e n t i n g w i t h t h e P i c B a s i c
P r o Com p i le r
P r og r a m s -IR_RE C .B AS & IR_RX_TX .IN C
In f r a r e d Re c e i v e r
Th e r e c e i v e r d e f i n e s , a g a i n i n f or m t h e IRIN
s u b r ou t i n e a s t o w h i c h p or t
a n d p i n t o p la c e t h e IR d e t e c t or , t h e s e a r e : -
D e f i n e
IRIN P ORT

P or t

` P or t f or t h e IR d e t e c t or
D e f i n e IR/ N B IT

B i t

` B i t f or t h e IR d e t e c t or
If t h e MIN d e f i n e s a r e n ot u s e d , t h e d e f a u lt i s P or t A
.4 .
To u s e t h e r e c e i v e r s u b r ou t i n e , m a k e a c a ll t o
IRIN , a n d t h e r e a r e t h r e e
v a r i a b le s r e t u r n e d , t h e s e a r e
IR_B Y TE , IR ID , a n d IR_V AL ID
. As y ou
w i ll h a v e g u e s s e d , IR_B Y TE
c on t a i n s t h e b y t e t r a n s m i t t e d , a n d IR ID
c on t a i n s t h e t r a n s m i t t e r i d v a lu e . I R
V AL ID i s a b i t v a r i a b le , w h i c h
r e t u r n s t h e v a lu e s 1 or 0
. If a v a li d 1 6 -b i t p a c k e t h a s b e e n r e c e i v e d
c or r e c t ly , t h e n t h i s f la g i s s e t , h ow e v e r i f a v a li d p a c k e t w a s r e c e i v e d
i n c or r e c t ly i t i s c le a r . F or e x a m p le
: -
Ag a i n :
G os u b IR/ N
` Re c e i v e a 1 6 -b i t p a c k e t
If IR V AL ID = 0 t h e n g ot o Ag a i n
` Ch e c k i f p a c k e t i s v a li d
If IR_
ID = 2 t h e n
` Ch e c k t h e TX ID c od e
D o t h e c od e w i t h i n t h e IF s t a t e m e n t ' D o t h i s c od e i f c or r e c t
E n d i f
Th e c i r c u i t f or t h e IRIN
s u b r ou t i n e i s t h e s a m e a s t h e Son y r e m ot e
c on t r ol r e c e i v e r , f i g u r e 6
.1 .
Se c t i on - 6 - 9
ExperimentingwiththePicBasicProCompiler
Programs -
IRSEROUT.INC,IRSERIN .BAS &SER_IR
.BAS
TransmittingSerialinf rared
Thef inalmethod weshalllook atf ortransmittingand receiv inginf rared
signals,isthatof normalRS2 3 2 serialprotocol
( i .e . inv erted 2 4 0 0 b au d
etc) .
Thiswillallowu stosend morethanoneb y teatatime
.H owev er,
wecannotsimply connectaninf rared L ED tothePICand inv ok ethe
SEROUTcommand ,theL ED mu stb emod u lated at3 8 k H z
.Theref ore,
thetransmittersu b rou tinehashad tob ewritteninassemb ler,
b u tis
compatib lewith4 ,8 ,1 0 ,and 1 2 mH z cry stals
.Asalway s,theinclu d ef ile,
IRSEROUT
.INC mu stb eplaced attheb eginningof y ou rprogram
.In
ad d ition,F IV Enewd ef ines
hav eb eenad d ed ,whichenab lethe
IRSEROUT su b rou tinetob ecu stomiz ed
.Thef irsttwod ef ines,conf igu re
theportand pinonwhichtoconnecttheIRd etector,theseare
: -
D ef ine IRSEROUT_PORT
Port

` Portf ortheIRL ED
D ef ine IRSEROUTBIT

Bit

` Bitf ortheIRL ED
If thesed ef inesarenotu sed iny ou rprogram,thed ef au ltsarePortA
.0
Thethird d ef ine,conf igu resthed esired transmissionb au d rate
.
There
aref ou rb au d ratestochoosef rom,namely ,3 0 0 ,6 0 0 ,1 2 0 0 ,and 2 4 0 0
.
D ef ine IRSEROUTBAUD
Bau d

` D esired b au d rate
If thisd ef ineisomitted f romy ou rprogramthed ef au ltis1 2 0 0 b au d
Themaximu mb au d rateachiev ab lewithany accu racy is2 4 0 0
; thisis
b ecau sethecomponentswithintheinf rared d etectormod u lecau se
a
f inited elay b etweenreceiv ingtheinf rared signaland ou tpu ttingthelogic
lev el
.Theb au d mod eis,inv erted ,1 start-b it,8 d ata-b its,and 1 stop-b it
.
Thef ou rthd ef ine,setsthed elay b etweenb y testransmitted
.Sometimes
thetransmissionratesof
IRSEROUT
may presentcharacterstooq u ick ly
tothereceiv er
.Theref ore,ad elay of 1 to2 5 5 millisecond s( ms) ,
may b e
implemented .
D ef ine IRSEROUTPACING
1 . . . 2 5 5
` d elay b etweenchrs( ms)
If thisd ef ineisnotu sed ,thed ef au ltis1 ms
Section-6 - 1 0
E x p e r i m e n t i n g w i t h t h e P I c B a s i c P r o
Co m p i le r
T r a n s m i t t i n g S e r i a l i n f r a r e d
T h e f i f t h d e f i n e , s w i t c h e s o n o r o f f a 3 - b y t e h e a d e r t h a t p r e c e d e s e v e r y
d a t a b y t e t r a n s m i t t e d
.
D e f i n e
I RS E ROUT HE AD E R 1 o r 0

T u r n o n / o f f h e a d e r
T h e 3 - b y t e h e a d e r , c o n s i s t i n g o f " # 0 K " , a llo w s t h e r e c e i v e r t o a d j u s t i t s
i n t e r n a l AG C, a n d s y n c h r o n i z e w i t h t h e s t a r t o f a t r a n s m i s s i o n
. Un li k e
a s y n c c o m m u n i c a t i o n s o v e r w i r e s , t h e r e a r e p le n t y o f 3 8 k Hz m o d u la t e d
s i g n a ls a r o u n d , n a m e ly t h e T V r e m o t e
. T h e s e c a n b e p i c k e d u p b y o u r
r e c e i v e r a n d i n t e r p r e t e d a s v a li d s i g n a ls , w i t h d i s a s t r o u s r e s u lt s
. T h u s ,
w e p la c e a u n i q u e s e q u e n c e o f c h a r a c t e r s t h a t s i g n i f y t h a t a s i g n a l f r o m
o u r t r a n s m i t t e r h a s b e e n s e n t
. T h e li k e li h o o d o f t h e s a m e t h r e e
c h a r a c t e r s b e i n g r a n d o m ly p r o d u c e d i s v i r t u a lly n o n - e x i s t e n t
. T h e
i n t e r n a lly p r o d u c e d h e a d e r i s u s e f u l i f o n ly o n e b y t e o f d a t a i s b e i n g
t r a n s m i t t e d , o t h e r w i s e , e v e r y b y t e s e n t w i ll h a v e a 3 - b y t e h e a d e r
p r e c e d i n g i t
. T o i llu s t r a t e t h e u s e o f t h e h e a d e r c h a r a c t e r s , a n d t o s h o w
h o w e a s y i t i s t o t r a n s m i t s e v e r a l b y t e s , y o u r c o d e c o u ld lo o k s o m e t h i n g
li k e t h i s : -
I R B y t e = ' W " : G o s u b I RS e r o u t
` S e n d a t h r e e b y t e h e a d e r
I R_ B y t e =
" 0" : G o s u b I RS e r o u t
' t o s y n c h r o n i s e t h e r e c e i v e r
I R B y t e = W "
: G o s u b I RS e r o u t ` w i t h t h e a c t u a l b y t e s s e n t
I R_ B y t e = 12 7
: G o s u b I RS e r o u t ` S e n d a b y t e w i t h v a lu e 12 7
I R B y t e = 2 5 4
: G o s u b I RS e r o u t ` S e n d a b y t e w i t h v a lu e 2 5 4
I R B y t e = 2
: G o s u b I RS e r o u t

` S e n d a b y t e w i t h v a lu e 2
T h e v a r i a b le , I R_ B YT E
h a s t o b e p r e - lo a d e d w i t h t h e b y t e t o b e
t r a n s m i t t e d , a n d t h e n a c a ll i s m a d e t o
I RS E ROUT . I f t h e h e a d e r d e f i n e
i s n o t u s e d , t h e d e f a u lt i s N O h e a d e r
. T h e r e i s n o n e e d t o d e c la r e t h e
v a r i a b le , I R_ B YT E
i n y o u r p r o g r a m , a s i t i s a lr e a d y p r e - d e c la r e d w i t h i n
t h e i n c lu d e f i le . T h e p r o g r a m
S E R_ I R. B AS , i llu s t r a t e s t h e u s e o f t h e
I RS E ROUT s u b r o u t i n e .
S e c t i o n - 6 - 1 1
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
R e c e i v i n g s e r i a l i n f r a r e d
R e c e i v i n g Se r i a l In f r a r e d
To r e c e i v e t h e s e r i a l i n f r a r e d s i gn a l , w e s i m p l y us e t h e c o m p i l e r 's
SE R IN2 o r DE B UGIN c o m m a n d s
. Th e s e a r e m o r e d e s i r a bl e t h a n t h e
n o r m a l SE R IN c o m m a n d , s i n c e t h e y c a n a ut o m a t i c a l l y w a i t un t i l t h e 3-
byt e h e a d e r i s f o un d , us i n g t h e WAIT o p e r a n d : -
Se r i n 2 P o r t A .4, B AUD, [ w a i t ( " #OK" ) , IR _R c v
]
Th i s w i l l w a i t f o r t h e c h a r a c t e r s , " #OK" t o be r e c e i v e d be f o r e i t r e c e i v e s
t h e a c t ua l byt e , w h i c h i t p l a c e s i n t o t h e v a r i a bl e IR R C V. Th i s h e l p s t o
s yn c h r o n i ze t h e s t a r t o f t h e a c t ua l t r a n s m i s s i o n , a n d a l s o p r e v e n t s f a l s e
c h a r a c t e r s be i n g i n t e r p r e t e d a s v a l i d d a t a .
To c a l c ul a t e t h e ba ud r a t e us e d i n t h e SE R IN2 c o m m a n d , t h e f o r m ul a i s
( 1000000 /ba ud ) - 20, a l s o t h e ba ud m o d e m us t a l w a ys be s e t t o Tr ue ,
t h i s i s t h e o p p o s i t e o f t h e t r a n s m i t t e r 's m o d e , be c a us e t h e i n f r a r e d
d e t e c t o r p ul l s i t s o ut p ut l o w w h e n i t r e c e i v e s a s i gn a l , t h e r e f o r e , i t i n v e r t s
t h e i n c o m i n g s i gn a l
. Th e t a bl e be l o w s h o w s t h e v a l ue t o p l a c e i n t o t h e
C o n s t a n t B AUD, f o r t h e d e s i r e d ba ud r a t e .
T2400 ba ud 396
T1200 ba ud 813
T600 ba ud 1646
T300 ba ud
3313
Th e p r o gr a m , IR SE R IN.B AS
i l l us t r a t e s o n e t e c h n i que f o r r e c e i v i n g
s e v e r a l byt e s . Th e c i r c ui t f o r t h e r e c e i v e r i s t h e s a m e a s t h a t f o r t h e So n y
r e m o t e c o n t r o l r e c e i v e r , f i gur e 6.1 .
Se c t i o n -6- 1 2
ExperimentingwiththePicBasicProCompiler
Program - AMTX.BAS
Markdenotes
positiv eside
I I
AM-TX1 -4 1 8 Pinou ts .
Signalin
F igu re6 .3
.Basiccircu itarrangement .
4 1 8 mH z , AMR adioTransmitter
R emotecontrolsy stemsareb ecomingincreasingly popu lar, andthe
introdu ctionof pre-tu nedradiomodu lesandtheirev erdecreasingprices
hasmaderadioapracticalalternativ etoinf rared
.Theadv antageof radio
istheab ility of thesignaltopassthrou ghob j ectsandwalls .I tsrangeis
alsoimpressiv e, 1 0 0 metresormore
( in f reespace) b eingnormal .N o
licenceisreq u iredintheU K , prov idingtheradiomodu lesoperateonthe
4 1 8 mH z or4 3 3 mH z wav eb ands .Theradiomodu lesmay b eu sedina
similarway tothoseintheinf raredremotecontrolsections .
Althou ghthemodu lesdescrib edinthissectionarethea
.m .ty pe, thef .m .
ty pesmay b edirectly su b stitu ted .
I nordertocarry inf ormationthereq u iredsignalmu stb esu perimposed
ontheradiowav e ( knownasthecarrierwav e)
. W ithAmplitu de
Modu lationtransmissions, itisthe amplitu de of thecarrierwav ethatis
madetochangeinaccordancewith
thereq u iredsignal .Thisis
reasonab ly easy togenerate, b u tcansu f f erf romexternalinterf erence
.
AM-TX1 -4 1 8 Transmitter
TheR F Solu tionsa .m
.transmittermodu le, ty peAM-TX1 -4 1 8 , isa2 -pin
dev icethatissimilarinappearancetoacapacitor .I t' sincredib ly simple
tou se, thestandardcircu itarrangementisshowninf igu re6
.3 .
AM-7X1 .4 1 8
Section-6 - 1 3
· · · · · · · · · · · · · · · · · · · · · · · ·
· · · · · · · · · · · ·
· · - · · · · · · · · · · · · · · · · · --· · · · · · · · · ·
· - · · - · · · · · · - · ·
· · - · · · · · · ·
· · · · · · · · -· · · - ·· · · -· · · · · · -· ·
· · · ·
· · · · · · · · · · · · · · - · · · ·· · · -· · · · · · · · ·
· · · · · · · · ·
-· · · - ·· · ·

· · · · · · · · -


-


-


-
· ·





-· · · · · · · -· · · · · ·
· · -· · ·
· · · · · · · · · -·
· · · · - · -·
- - · ·
· · -· · --· · · · · · · · · · · · · · · · - · · · · · -· ·
· · · · -· · · ·
· · · · · · · · · · · · · -· · · · · ·
· -·
· · · · · · · · · · · · · ·· · · · · · · · · · · · ·
· --· · · · -· · --· · · · · · · ·· · · · · · · · · · · · ·
· ·
· · · · · · · · · · · · -· · -· · · · · · · · · · · · · · · ·

· · -·

·
· · · · · · · ·
· -·
· · · · · · · · · · · · · · · · · · · · ·

· · · ·
ExperimentingwiththePicBasicProCompiler
1
AM-RT4-41 8
,
TopView
4
2

3
1 . . . VCC
2
. . . GND
3DATAIN
4AERIAL
F igu re6 . 5
. AM-RT4-41 8Pinou tsand Basiccircu itarrangement .
Theoperatingv oltagef orthismod u lemay b eany thingb etween
2to
1 4V,itd raws4mAwhenasignalisb eingtransmitted , and hasa
maximu md atarateof 4k H z
( 480 0 b au d max) . Theaerialf oru sewiththis
mod u lemay b eawhipty peorahelicalcoil
. Thehelicalcoilconsistsof
34tu rnsof 0 . 5 mmenamelled copperwire,closewou nd ona2
. 5 mm
d iameterf ormer
. Thisu sesalotlessspace thanthewhipaerial,
howev er,itsperf ormanceisalittleinf erior,and smallad j u stmentstoits
lengthmay b ereq u ired . Awhipaerialisthesimplestty pe
f orthis
transmitter . Itcanb eassimpleasapieceof wire ( or pcb track ) 1 7cm
long
. Thewireshou ld b easstraightaspossib le . Thereisnoneed f ora
v ariab lecapacitorwiththistransmittermod u le
. Again,if anaerial isnot
u sed ,theu sef u lrangeisred u ced toapprox1 0 metres .
Interf acingatransmittermod u letothePICisaseasy asattaching its
inpu ttooneof thePIC' sou tpu ts
. Thereisnoneed tomod u latethesignal
with38k H z ,theref oreany of theS ERIALcommand smay b eu sed ,orthe
PU LS O U Tcommand ,and withthead d ed lu xu ry of any d esired oscillator
f req u ency
. Theu seof asy nchronisinghead erisalway srecommend ed
whensend ingseriald ata,thiscanb eassimpleasthe3-b y tehead er
u sed
intheserial inf rared transmitterexperiment . W ithou tthe
sy nchronisinghead er,rand ominpu tscou ld b einterpreted asv alid d ata .
O therthanthat,thesemod u lesmay b etreated asif awireinterf acewas
b eingu sed .
S ignalin
S ection-6 - 1 5
41 8mH z ,AMRad ioTransmitter
AM-RT4-41 8Transmitter
Analternativ e41 8mH z a . m . transmittermod u leistheRF S olu tions,AM-
RT4-41 8. Thisishou sed inaD
. I . L. pack ageand itsb asiccircu it
arrangementand pinou tsareshowninf igu re6
. 5
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
P r o g r a m - AM_RX.B AS
AM- RX-
T T T

T
1 2 3

7
1 .. .RFVC C
2.. .RFGND
i 3.. . AE RI AL
7 . . .RF GND
10 . .AF VC C
1 . . AF GND
12 . .AF VC C
13 . .T E ST P O I NT
14 . .O UT P UT
15 . .AF VC C
T T T T T T
10

1s
4 18m Hz, AM Ra di o Re c e i ve r
T h e r e a r e t h r e e t yp e s o f a .m
. r e c e i ve r a va i l a b l e . T h e y a l l h a ve t h e s a m e
p i n l a yo ut s a n d a r e i n t e r c h a n g e a b l e w i t h e a c h o t h e r
. T h e t h r e e ve r s i o n s
a r e : -
AM-HRR1- 4 18: T h i s i s t h e l e a s t e x p e n s i ve , a n d a l t h o ug h i t w a s
s up e r s e de d b y t h e HRR3 t yp e , i t s p e r fo r m a n c e i s s ur p r i s i n g l y g o o d
.
AM-HRR3- 4 18:
As a b o ve , b ut i s l a s e r t r i m m e d fo r g r e a t e r a c c ur a c y a n d
l e s s fr e q ue n c y dr i ft .
AM-HRR5 - 4 18:
T h e s a m e l a s e r t r i m m e d de s i g n a s a b o ve , b ut w i t h a
l o w e r c ur r e n t c o n s um p t i o n (0
.5 m A) .
T h e t h r e e r e c e i ve r s h a ve t h e fo l l o w i n g s p e c i fi c a t i o n s : -
Sup p l y vo l t a g e : 4 .5 V t o 5 .5 V
Sup p l y c ur r e n t : 2 .5 m A (HRR5 ve r s i o n : 0
.5 m A)
C MO S/T T L c o m p a t i b l e o ut p ut
Ma x i m um da t a r a t e 2kHz
(i n p r a c t i c e 4 80 0 b a ud h a s b e e n a c h i e ve d)
T h e p i n l a yo ut a n d b a s i c c i r c ui t a r r a n g e m e n t fo r a l l t h r e e r e c e i ve r s i s
s h o w n i n fi g ur e 6 .6
. T h e a e r i a l fo r t h e s e r e c e i ve r s i s t h e s a m e a s fo r t h e
AM-RT 4 - 4 18 t r a n s m i t t e r
.
+5 Vo l t s
RF VC C
RF GND
AE RI AL
AM
HRRX- 4 18
RF GND
AF VC C
AF GND
AF VC C
T E ST P T
O UT P UT
AF VC C
E q
i
M

V
Ae r i a l
0 .1 o f
Da t a
O ut
Fi g ur e 6 .6 . AM-HRRX-4 18 p i n o ut s a n d b a s i c c i r c ui t a r r a n g e m e n t
.
Se c t i o n -6- 1 6
ExperimentingwiththePicBasicProCompiler
418mHz,AMRadioReceiver
Aswiththetransmittermodu les,interf acingthereceivertothePI Cisa
simplecaseof connectingitsdataou tpintooneof thePI C' spins
. T hen
b y u singoneof thecompiler' smany serial- incommands
(DEBUGI N,
SERI N etc) ,
thedataf romthetransmitterisreceived .
T hereceiversdiscu ssedmay receivedatau ptoalimitof 480 0 b au d
;
however,therearereceiversavailab lethatarecapab leof receivingdata
many timesf asterthanthis,alongwiththeircorrespondingtransmitter .
Bu t,asthetransmissionrategoesu psodoestheprice
. W ithgoodaerial
design,thesimpleandinexpensive418mHzmodu lesarecapab leof
remark ab ledistanceswithahighdegreeof accu racy .
T heaccompany ingCDRO Mhasacomprehensivesetof datasheetsand
applicationnotesf ormostof themorecommontransmitter/ receiver
modu lesavailab le .
Section- 6 - 1 7
ExperimentingwiththePicBasicProCompiler
Section-7
Temperature
Measurement
Experiments
InterfacingwiththeDS1820.
Dallas1-wireinterfaceprincipals .
InterfacingwiththeLM35temperaturesensor
.
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
P r o g r a m - DS1820 .B AS
In t e r fa c i n g w i t h t h e DS1 820, 1-w i r e t e m p e r a t ur e s e n s o r
Th e Da l l a s DS1820 i s a c o m p l e t e di g i t a l t h e r m o m e t e r o n a c h i p . It c a n
m e a s ur e t e m p e r a t ur e s fr o m -55°C t o +125°C i n 0
.5°C i n c r e m e n t s .
Th e DS1820 c o m m un i c a t e s w i t h t h e P IC t h r o ug h a 1-w i r e c o n n e c t i o n .
Th i s h a s a m a s t e r , w h i c h i s t h e P IC , a n d o n e o r m o r e s l a ve s
. Th e
DS1820 a c t s a s a s l a ve , r e c e i vi n g c o m m a n ds t h e n t r a n s m i t t i n g i t s da t a
ba c k t o t h e m a s t e r . Th e 1-w i r e s ys t e m r e qui r e s s t r i c t p r o t o t o c o l s fo r
t r a n s m i s s i o n a n d r e c e p t i o n o f da t a , t h e s e a r e c a l l e d t i m e -s l o t s .
1-w i r e i n t e r fa c e p r i n c i p a l s .
Al l t r a n s a c t i o n s o n t h e 1-w i r e bus m us t be g i n w i t h t h e m a s t e r s e n di n g a n
i n i t i a l i s a t i o n s e que n c e .
Th e INITIALIZATION SE QUE NC E c o n s i s t s o f t h e m a s t e r p ul l i n g t h e DO
l i n e l o w fo r a m i n i m um o f 480us . Th e m a s t e r t h e n r e l e a s e s t h e DO l i n e
(w h i c h i s h e l d h i g h vi a a p ul l up r e s i s t o r )
a n d g o e s i n t o r e c e i ve m o de .
Aft e r de t e c t i n g t h e r i s i n g e dg e o n t h e DO l i n e , t h e DS1820 w a i t s
15 .
.60us , t h e n t r a n s m i t s i t s p r e s e n c e p ul s e . Th i s i s a l o w s i g n a l , w h i c h
l a s t s fo r 60 . .120us
. If fo r a n y r e a s o n t h e DS1820 di d n o t , o r i s n o t
c a p a bl e o f s e n di n g a p r e s e n c e p ul s e t h e DO l i n e w i l l r e m a i n h i g h a n d a n
e r r o r fl a g m a y be s e t : -
DS-
In i t .-Lo w DQ
P a us e us 500
DQ DIR=1
P a us e us 100
If DQ=1 t h e n
DS_
Va l i d=0
Re t ur n
E n di f
P a us e us 400
DS Va l i d=1
Re t ur n
'Se t t h e da t a
p i n l o w t o i n i t i a l i z e
'Wa i t fo r m o r e t h a n 480us
'Re l e a s e t h e da t a p i n (s e t t o i n p ut fo r h i g h )
'Wa i t fo r m o r e
t h a n 60us
'Is t h e r e a DS1820 de t e c t e d?
'If n o t , t h e n c l e a r DS_ VALID fl a g
'Re t ur n w i t h DS VALID h o l di n g 0 (e r r o r )
'E l s e
' Wa i t fo r e n d o f p r e s e n c e p ul s e
'Se t DS_ VALID fl a g
'Re t ur n w i t h DS_ VALID h o l di n g 1(n o e r r o r )
Se c t i o n - 7 -1
ExperimentingwiththePicBasicProCompiler
I nterf acingtotheD S 1 8 2 0 , 1 - wiretemperatu resensor
T heD S 1 8 2 0 aswithallthe1 - wired ev icesoperatewith instru ctions,
thesearetransmitted b y themasterimmed iately af tertheb u sis
initialised
. T heD S 1 8 2 0 u nd erstand selev eninstru ctions (op- cod es), the
mostcommonof theseareexplained b elow .
S K I PR O M [ CCh]
T hiscommand allowsthemastertoaccessthememory f u nctions
withou tprov id ingthe6 4 - b itromcod e . I f morethanoneslav eispresent
and aread command issentaf tertheS k ipromcommand , d atacollision
willoccu rontheb u sasmu ltipleslav estransmitsimu ltaneou sly
.
R EA D R O M [ 3 3 h]
T hiscommand allowsthemastertoread theD S 1 8 2 0 ' s8 - b itf amily cod e,
(au niq u e4 8 - b itserialnu mb er),
and 8 - b itCR C . T hiscommand canonly
b eu sed if thereisasingleD S 1 8 2 0 ontheb u s . I f morethanoneslav eis
present, ad atacollisionwilloccu rwhenallslav estry totransmitatthe
sametime .
R EA D S CR A T CHPA D [ BEh]
T hiscommand read sthecontentsof thescratchpad . R ead ingwillb egin
atb y te0 , and willcontinu ethrou ghthescratchpad u ntiltheninth
(b y te- 8 ,
CR C)
b y teisread
. I f notalllocationsaretob eread , themastermay
issu earesettoterminatetheread ingatany time .
CO PYS CR A T CHPA D [ 4 8 h]
T hiscommand copiesthescratchpad intotheeepromof theD S 1 8 2 0 ,
storingthetemperatu retriggerb y tesin non- v olatilememory . I f the
masterissu esread timeslotsf ollowingthiscommand , theD S 1 8 2 0 will
ou tpu taz eroontheb u saslongasitisb u sy copy ingthescratchpad to
eeprom, itwillretu rnaonewhenthecopy processiscomplete . I f the
D S 1 8 2 0 isparasitepowered , themasterhastoenab leastrongpu llu p
f oratleast1 O ms immed iately af tersend ingthiscommand
.
CO NVER T [ 4 4 h]
T hiscommand b eginsatemperatu reconv ersion . Nof u rtherd atais
req u ired . T hetemperatu reconv ersion willb eperf ormed , thenthe
D S 1 8 2 0 willremainid le
. I f themasterissu esread timeslotsf ollowing
thiscommand , theD S 1 8 2 0 willou tpu taz eroontheb u saslongasitis
b u sy mak ingatemperatu reconv ersion, itwillretu rnaonewhen
the
temperatu reconv ersioniscomplete .
S ection- 7 - 2
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
DS_ Wr i t e :
Fo r B i t_ C n t =1 t o 8
If C m d.0=0 t h e n
Lo w DQ
P a us e us 60
DQ_DIR=1
E l s e
Lo w DQ
No p
DQ DIR=1
P a us e us 60
E n di f
C m d=C m d >> 1
Ne x t
Re t ur n
In t e r fa c i n g t o t h e DS182 0, 1- w i r e t e m p e r a t ur e s e n s o r
WRITE SC RATC HP AD [4E h ]
Th i s c o m m a n d w r i t e s t o t h e s c r a t c h p a d o f t h e DS182 0, s t a r t i n g a t
a ddr e s s 2
. Th e n e x t t w o byt e s w r i t t e n w i l l be s a ve d i n s c r a t c h p a d
m e m o r y, a t a ddr e s s l o c a t i o n s 2 a n d 3
. Wr i t i n g m a y be t e r m i n a t e d a t a n y
p o i n t by i s s ui n g a r e s e t .
To r e a d a va l ue fr o m t h e 1- w i r e s l a ve , o r t o t r a n s m i t a n i n s t r uc t i o n , t h e
m a s t e r /s l a ve m a n i p ul a t e s t h e DQ l i n e fo r s p e c i fi c l e n g t h s o f t i m e , w h i c h
w i l l t r a n s m i t /r e c e i ve a o n e o r a z e r o
.
Al l o f t h e i n s t r uc t i o n s a r e m a de up o f 8- bi t s
. To Tr a n s m i t a n i n s t r uc t i o n
a c r o s s t h e 1- w i r e bus , t h e m a s t e r m us t s c a n t h e 8- bi t s
( l e a s t s i g n i fi c a n t
bi t fi r s t )
t h a t m a ke up t h e i n s t r uc t i o n t h e n s e n d e i t h e r a o n e o r a z e r o
a c c o r di n g l y .
A ONE i s t r a n s m i t t e d by p ul l i n g t h e DQ l i n e l o w fo r l e s s t h a n 15us , t h e n
r e l e a s e d ( s e t t o i n p ut )
. As t h e w r i t e t i m e - s l o t m us t be a m i n i m um o f 60us
i n l e n g t h , t h e r e s t o f t h e t i m e - s l o t i s p a dde d o ut w i t h a 60us de l a y
.
A ZE RO i s t r a n s m i t t e d by p ul l i n g t h e DQ l i n e l o w fo r 60us , t h e n r e l e a s e d
by c o n fi g ur i n g t h e p i n a s a n i n p ut .
Al l w r i t e t i m e - s l o t s m us t h a ve a t l e a s t 1 us be t w e e n bi t t r a n s m i s s i o n s
.
Th e s ubr o ut i n e be l o w , w r i t e s a n i n s t r uc t i o n a c r o s s t h e 1- w i r e i n t e r fa c e
: -
'C r e a t e a l o o p o f 8- bi t s ( B YTE )
'C h e c k bi t - 0 o f C MD
'Wr i t e a 0- bi t
'Se n d a l o w fo r m o r e t h a n 60us
fo r a 0- bi t
'Re l e a s e da t a p i n ( s e t t o i n p ut fo r h i g h )
'E l s e
'Se n d a l o w
fo r l e s s t h a n 15us fo r a 1- bi t
'De l a y l us a t 4m Hz
'Re l e a s e t h e da t a p i n ( s e t t o i n p ut fo r h i g h )
'Us e up t h e r e m a i n i n g t i m e s l o t
'Sh i ft t o t h e n e x t bi t
'C l o s e t h e l o o p
Se c t i o n - 7 - 3
ExperimentingwiththePicBasicProCompiler
I nterf acingtotheD S 1 8 2 0 , 1 - wiretemperatu resensor
A lthou ghthed ataf romtheD S 1 8 2 0 isinthef ormof a9 - b itword , the
actu ald atalengthsentis1 6 - b its . T heref ore, themastermu stread 1 6 -
b itsf romtheslav e
( mostsignif icantb itf irst) and constru cttheword
accord ingtowhetheraoneoraz erowasreceiv ed .
T oR eceiv eab itf romtheslav e, themastermu stpu lltheD Q linelowf or
aminimu mof 1 u s, thenreleasetheD Q line, whichenab les
receiv e
mod e . T heD S 1 8 2 0 ( whichisnowthetransmitter) pu lls
theD Q linelow
f orZ ER O , orhighf orO N Ewithinatime- slotof 1 5 u s
. A stheread time-
slotmu stb eaminimu mof 6 0 u sinlength, therestof thetime- slotis
pad d ed ou twitha6 0 u sd elay .
A llread time- slotsmu sthav eatleast1 u sb etweenb itreceptions .
D S _ R ead :
F orBitCnt= 1 to1 6 ' Createaloopof 1 6 - b its( W O R D )
T emp= T emp > > 1

' S hif td ownb its
T emp . 1 5 = 1

' Presetread b itto1
LowD O

' S tartthetimeslot
@
nop

' D elay 1 u sat4 mH z
D Q D I R = 1

' R eleased atapin( settoinpu tf orhigh)
I f D Q = O T hen

' Else
T emp . 1 5 = 0

' S ettheb itto0
End if
Pau seu s6 0

' U seu ptheremainingtimeslot
N ext

' Closetheloop
R etu rn
T heab ov eexplanationand cod eisb y nomeansonly f ortheD S 1 8 2 0
d ev ice
. A ll1 - wired ev icesoperateon asimilarprotocol . O nly the
instru ctionsf orthespecif icd ev iceu sed willb ed if f erent
.
S ection- 7 - 4
ExperimentingwiththePicBasicProCo
mp il er
MSB
0
=+24.5°C
2"compl imentformatfornegativetemperatures
MSB
1
=-24 .5°C
Figure7 .1 .9 -b itd ataformat .
InterfacingtotheDS1 820,1 -wiretemperaturesensor
Measuringthetemperature .
Toread thetemperaturefromasingl eDS1 820connected totheb uswe
cand ispensewiththe64-b itromcod e
.
Firstl y ,the1 -wireb usisinitial ised ,thenask iprominstruction
(CCh) is
transmitted ,fol l owed b y aconvertinstruction(44h) .
TheDS1 820isagaininitial ised and anothersk iprominstructionissent,
fol l owed b y aread scratchpad instruction
(BEh) . The1 6-b itsofd atamay
thenb ereceived fromtheDS1 820
.
Weareonl y concerned withthefirst9 -b itsofthe1 6-b itsreceived from
theDS1 820,therefore,thel ast7-b itsmay b ed isregard ed
.
TheDS1 820hasaresol utionof0
.5°C ;thisisrepresented b y theLSB
(b it-0)
ofthe9 -b its .A 1 signifiesa0
.5°increment,whil ea0signifiesan
integerval ue .
Bits1 to7arethetemperatureread ing,b it-1 canb enowthoughtofas
theLSBofthetemperatureval ue .
Bit-8isthesignb it,whenthisis1 theresul tisanegativetemperature
and thefirst8-b itsaretwo' scompl iment
(1 b ecomesa0and vice-versa)
.
Figure7
.1 ,il l ustratestherel ationshipofthe9 -b itsofd ataforb otha
positiveand negativetemperature
.
Normal formatforpositivetemperatures
0 0 1 1 0 0 0
LSB
1
1
1 1 1
Section-7 - 5
LSB
1
0
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
I n t e r f a c i n g t o t h e D S 1 8 2 0 , 1 - w i r e t e m p e r a t u r e s e n s o r
P r o g r a m D S 1 8 2 0 .B AS , di s p l a ys , t h e t e m p e r a t u r e o f a s i n g l e D S 1 8 2 0
c o n n e c t e d t o P o r t B .O . F i g u r e 7 .2 s h o w t h e c o n n e c t i o n s t o t h e P I C .
Th e p r o g r a m i s c e n t r e d a r o u n d t h r e e s u br o u t i n e s
; t h e s e a r e D S _I NI T,
D S - RE AD , a n d D S - WRI TE .
Th e f i r s t t o be c a l l e d i s D S _I NI T, t h i s s u br o u t i n e i n i t i a l i s e s t h e 1 - w i r e bu s
a n d c h e c ks f o r a p r e s e n c e f r o m t h e D S 1 8 2 0
. I f n o de vi c e w a s de t e c t e d
t h e n t h e f l a g , D S _VALI D w i l l r e t u r n h o l di n g 0 , e l s e i t w i l l r e t u r n h o l di n g 1
i f a l l i s w e l l .
F o u r i n s t r u c t i o n s a r e t h e n t r a n s m i t t e d by u s i n g t h e D S - WRI TE
s u br o u t i n e . Th e i n s t r u c t i o n t o s e n d i s f i r s t l o a de d i n t o t h e va r i a bl e
C MD .
Th e s u br o u t i n e s c a n s t h e C MD va r i a bl e by e x a m i n i n g bi t - 0 , i f i t i s c l e a r
t h e n a 0 i s t r a n s m i t t e d o n t h e bu s , a n d i f i t i s s e t t h e n a 1 i s t r a n s m i t t e d .
C MD i s t h e n s h i f t e d r i g h t o n e p l a c e , a n d t h e s a m e p r o c e s s i s c a r r i e d o u t
e i g h t t i m e s t o t r a n s m i t t h e 8 - bi t byt e ( l e a s t s i g n i f i c a n t bi t f i r s t )
. Af t e r t h e
f o u r i n s t r u c t i o n s h a ve be e n t r a n s m i t t e d, t h e s u br o u t i n e D S - RE AD i s
c a l l e d
. Th i s r e a ds t h e i n c o m i n g bi t s t r e a m ( m o s t s i g n i f i c a n t bi t f i r s t ) a n d
p l a c e s t h e m i n t o t h e 1 6- bi t va r i a bl e
TE MP . Th i s i s a c c o m p l i s h e d by
r e a di n g a bi t f r o m t h e D S 1 8 2 0 a n d p l a c i n g i t i n t o bi t - 1 5 o f
TE MP , t h e
va r i a bl e TE MP i s t h e n s h i f t e d r i g h t 1 p l a c e . I f t h e bi t r e a d i s a 0 t h e n bi t -
1 5 w i l l be c l e a r e d, a n d i f t h e bi t r e a d i s a 1 t h e n bi t - 1 5 w i l l be s e t . Th i s i s
c a r r i e d o u t 1 6 t i m e s t o bu i l d u p t h e 1 6- bi t r e s u l t
.
We n o w h a ve o u r 1 6- bi t r e s u l t f r o m t h e D S 1 8 2 0 , h o w e ve r , w e a r e o n l y
i n t e r e s t e d i n t h e f i r s t 9 - bi t s
. F i r s t l y, bi t - 8 i s e x a m i n e d, i f i t i s s e t ( 1 ) t h e n a
n e g a t i ve t e m p e r a t u r e h a s be e n m e a s u r e d a n d t h e f l a g
NE GATI VE i s s e t
t o i n di c a t e t h i s f a c t . Th i s a l s o i n di c a t e s t h a t t h e f i r s t 8 - bi t s a r e t w o 's
c o m p l i m e n t . Th e r e f o r e , t h e l o w byt e o f t h e va r i a bl e TE MP m u s t be
XO Re d w i t h 2 55, t o c o n ve r t i t ba c k t o n o r m a l f o r m a t ( x o r i n g w i t h a 1 h a s
t h e e f f e c t o f r e ve r s i n g t h e bi t , 1 be c o m e s 0 a n d vi c e - ve r s a ) .
Re g a r dl e s s w h e t h e r a p o s i t i ve o r n e g a t i ve r e s u l t w a s r e c e i ve d, t h e
va r i a bl e TE MP n o w h o l ds t h e 7 - bi t s o f t e m p e r a t u r e a n d t h e 0
.5°C
i n c r e m e n t ( bi t - 0 ) . To c o n ve r t t h i s i n t o a f o r m a t w e c a n u s e , t h e l o w byt e o f
TE MP i s s h i f t e d r i g h t 1 p l a c e a n d t h e r e s u l t i s p l a c e d i n t o t h e va r i a bl e
D E G,
t h i s n o w h o l ds t h e c o r r e c t 7 - bi t t e m p e r a t u r e r e a di n g ( 0 - 1 2 7 ) . I n
o r de r t o p l a c e t h e 0 .5 i n c r e m e n t , t h e r e s u l t h e l d i n
D E G h a s t o be s c a l e d
u p by a f a c t o r o f 1 0 . Th i s w i l l n o w g i ve u s a t e m p e r a t u r e va l u e o f
be t w e e n 0 a n d 1 2 7 0 .
S e c t i o n - 7 - 6
ExperimentingwiththePicBasicProCompiler
InterfacingtotheDS1820,1-wiretemperaturesensor
T oinclud ethe0
. 5 incrementv alueinourfinalresult,weexamineb it-0of
T EMP
( theoriginalv aluewasnotaltered b y shiftingitright)
. A nd multiply
itsresultb y 5 ,ifb it-0wasclearthentheprod uctwillb e0( 0* 5 ) ,howev er,
iftheb itwassetthentheprod uctwillb e5
( 1' ' 5 ) . T hisprod uctisthen
ad d ed tothev alueheld inDEG
.
U ponthesub routinesreturn,onev ariab leand aflaghav eb eenload ed ,
DEG , and
NEG A T IVE .
T hiswillallowustod isplay aminussignifthe
temperatureisnegativ e,aswellasinformtheprogramastotheactual
temperature .
T od isplay theminussign,theflag,
NEG A T IVE isexamined ,and
d epend ingonitsv alue,thev ariab le NEG _POS
isload ed withthe
character, ` -` orspace .
T hefinald isplay issplitintofourpartswithinthesamed eb ugcommand
.
F irstly ,thev ariab leNEG POSisd isplay ed ,thishold aminussignora
space,d epend ingonthev alueof
NEG A T IVE . T henthev alueleftofthe
d ecimalpointisd isplay ed ,b y d iv id ingthev ariab le
DEG b y 10 . T hev alue
totherightofthed ecimalpointisd isplay ed b y calculatingtheremaind er
of DEG d iv id ed b y 10
( / / ) . A nd finally thed egreessignisd isplay ed ,this
wassetupattheb eginningoftheprogram
.
T oRBO
+5 Volts
VDD
DDDS1820
G ND
0
DS1820
iii
123
1 . . G ND
2 . . VQ
3. . VDD
F igure7
. 2 . DS1820configuration .
Section- 7 -7
ExperimentingwiththePicBasicProCompiler
Program - LM35.BAS
I nterf acingwiththeLM35temperatu resensor
I nterf acingtotheN ationalSemicond u ctorsLM35istotally d if f erentf rom
theD S1 8 2 0 , and issimplertou seinmany respects
.T heLM35was
d esigned withanalogu einterf acinginmind , theref oreitou tpu tsav oltage
thatisproportionaltothetemperatu re ( in° C) in 1 0 mV steps
.F or
example, if theLM35' sou tpu tv oltageis0
.2 2 V , thenthetemperatu reis
2 2 ° C .T hemaximu mtemperatu rethattheLM35willmeasu resaf ely is
1 2 5° Cwhichwillprod u ceav oltageof 1 .2 5V .
Program LM358 7X .BAS
u sesa1 6 F 8 76 ( orany otherpicwithanon-
b oard AD C)
tod isplay atemperatu reb etween0 and 1 2 5° Cand its
correspond ingv oltage, onaserialLCD connected toPortC
.6 .
T heAD CI N command issetu p
( asd escrib ed intheanalogu etod igital
section) toconv ertav oltagepresented toitsAN O inpu t
( PortA.0 ) . T he
temperatu reisthend isplay ed b y mov ingthed ecimalpointoneplaceto
theright .
AD CO N 1 = % 1 0 0 0 1 1 1 0

' Conf igu ref orAN O asanalogu e
' inpu twithrightj u stif ied resu lt
Again :
AD CI N 0 , AD Res

' D otheAD Cconv ersion
D eb u gl, Line1 , # ( AD _ Res/ 1 0 0 ) , " . , # ( A5Resu lt/ / 1 0 0 ) , 4 , " C"
' D isplay thetemperatu re
D eb u gl, Line2 , # ( AD _ Res/ 1 0 0 0 ) ,
" ." , # ( AD Resu lt/ / lO O O ) , " V olts "
' D isplay thev oltage
Pau se2 0 0

' Asmalld elay
G otoAgain

' D oitf orev er
F igu re7 .3showstheconnectionstothePI C .
F igu re7 .3.LM35conf igu ration .
Section-7- 8
LM35
n
+5V olts
1 3
2
T oRAO
V D D
V ou t
LM35
I I I
32 1
( AN O )
O v
G N D
1 . .G N D
2 . .V O UT
3. .+V S
I
ExperimentingwiththePicBasicProCompiler
Program - MAX_TEMP .BAS
I f aPI Cisu sed thatd oesnothav eanon-b oard AD C, su chasthe
PI C1 6 F 8 4 , thenanexternald ev icemu stb eemploy ed
.Thisisaperf ect
applicationf ortheextrasimpleMAX1 8 7 1 2 -b itAD C
.F igu re7 .4 shows
thecircu itf orsu chahook -u p
.
LM35
RB2
To RB1
RBO
VD D
D OUT
C$

SHD N
SCLK
MAX1 8 7
VREF

AI N
VSS
VD D
VolLM35
GND
F igu re7 .4
.LM35connectionstotheMAX1 8 7 AD C .
Theprogram MAX
_TEMP .BAS isu sed f orthisd emonstration
.The
programsimply callsthe
MAXI Nsu b rou tinetoacq u ireav oltagesample
f romtheMAX1 8 7 .
MAXI n:
Max_VaI = O
LowCs

' Activ atetheMAX1 8 7
Shif tinD ou t, Sclk , Msb post, [ MaxVall1 2 ] ' Clock in1 2 -b its
HighCs

' D eactiv atetheMAX1 8 7
Retu rn

' Exitthesu b rou tine
Theresu ltheld inthev ariab le MAX_VAL
isd iv id ed b y 1 0 toprod u cethe
d egreesand theremaind erisalsod iv id ed b y 1 0 toprod u cethe
d ecigrees .
D eb u gl, Line2 , d ec2 ( MaxVal/ i0 ) , " ." , d ec1 ( Ma) Val/ / 1 0 ) , 4 , " C"
Thereisnoneed toq u antasiz etheresu ltf romtheMAX1 8 7 , asthe
v oltagef romtheLM35willnotexceed 1 .2 5V
.W hichistheeq u iv alentto
1 2 5° C .
Section-
7
-9
I nterf acingwiththeLM35Temperatu resensor
ExperimentingwiththePicBasicProCompiler
Section-8
Experimenting
with
Robotics
Proximitydetectionprincipals .
Singledirectioninfraredproximitydetector
.
Infraredproximitydetectorwithdistancegauge
.
Directionalinfraredproximitydetector .
Ultrasonicproximitydetector
.
DrivingaDCmotorusinganH-Bridge .
DrivingaDCmotorusingtheL293D
.
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
P r o x i m i t y de t e c t i o n p r i n c i p a l s
De t e c t i n g a c o l l i s i o n o n a r o bo t i s n o r m a l l y a c c o m p l i s h e d by s e n s i n g
w h e n a s w i t c h h a s be e n t r i g g e r e d by bum p i n g i n t o s o m e t h i n g , h o w e ve r ,
a vo i di n g t h e c o l l i s i o n a l t o g e t h e r i s a m uc h m o r e de s i r a bl e g o a l . Th e r e
a r e t w o m a i n w a ys o f p r o vi di n g p r o x i m i t y de t e c t i o n fo r t h e p ur p o s e o f
a vo i di n g c o l l i s i o n s , t h e s e a r e l i g h t a n d s o un d
. In fr a r e d l i g h t a n d
ul t r a s o n i c s o un d t o be e x a c t
.
Fi r s t , w e s h a l l l o o k a t t w o p o s s i bl e w a ys o f us i n g i n fr a r e d l i g h t a s a
p r o x i m i t y de t e c t o r
. Th e fi r s t i s a s i n g l e di r e c t i o n de vi c e , w h i l e t h e s e c o n d
i s a di r e c t i o n a l de vi c e ( l e ft , r i g h t , a n d c e n t r e ) .
P r o x i m i t y de t e c t i o n us i n g i n fr a r e d l i g h t i s p o s s i bl e due t o t h e fa c t t h a t
l i g h t a l w a ys t r a ve l s i n a s t r a i g h t l i n e , a n d bo un c e s o f jus t a bo ut
e ve r yt h i n g
( t o a g r e a t e r o r l e s s e r e x t e n t ) . We c a n us e t h i s fa c t t o o ur
a dva n t a g e by t r a n s m i t t i n g a p ul s e o f l i g h t t h e n l o o ki n g fo r i t s r e fl e c t i o n
. If
t h e r e i s n o r e fl e c t i o n t h e n n o t h i n g m us t be i n fr o n t o f t h e de t e c t o r .
We s h a l l be us i n g t h e s a m e i n fr a r e d de t e c t o r t h a t w a s us e d i n t h e
r e m o t e c o n t r o l s e c t i o n , n a m e l y a n SFH506-38 . Th i s i s s e n s i t i ve t o
i n fr a r e d l i g h t m o dul a t e d a t 38kHz
. As w i t h t h e i n fr a r e d r e m o t e c o n t r o l
e x p e r i m e n t s , m o dul a t e d l i g h t i s us e d t o e l i m i n a t e un w a n t e d a m bi e n t
l i g h t , c a us e d by t h e s un , o r m a n m a de s o ur c e s s uc h a s fl uo r e s c e n t
l i g h t i n g
. Th e i n fr a r e d s o ur c e fo r t h e s e e x p e r i m e n t s i s a 5m m i n fr a r e d
LE D, a g a i n t h e s a m e t yp e us e d i n t h e i n fr a r e d r e m o t e c o n t r o l
e x p e r i m e n t s .
We s h a l l a l s o l o o k a t de t e c t i o n us i n g ul t r a s o n i c s o un d . As w i t h i n fr a r e d
l i g h t , ul t r a s o un d i s a l s o m o dul a t e d but t h i s t i m e a t 4 0kHz i n a n a t t e m p t t o
e l i m i n a t e ba c kg r o un d n o i s e s . B ut un l i ke l i g h t , s o un d t r a ve l s m uc h
s l o w e r , t h e r e fo r e , w e a r e a l s o a bl e t o s e n s e t h e di s t a n c e t o t h e o bje c t
t h a t h a s be e n de t e c t e d .
Se c t i o n -8 -1
ExperimentingwiththePicBasicProCompiler
Program -IR_PROX .BAS
Singled irectioninf rared proximity d etector .
F igu re8 .1 showsthecircu itf ortheinf rared proximity d etector (IRPD) .
Althou ghthePICiscapab leof sou rcingcu rrentsof u pto2 0 mA, asingle
transistorb u f f erwillincreasetherangeof theIRPDtwo-f old .
Section-8 - 2
Green
LED
F igu re8 .1 .Inf rared proximity d etector .
Areq u irementinthef inalprod u ctisthattheLEDmu stnotleak any light
f romitssid es, whichwou ld triggerthed etectorconstantly .T ohelp
allev iatethis, heatshrink sleev ingisplaced ov ertheLEDwithonly the
lensatthef rontlef tclear, showninf igu re8
.2
.
F igu re8 .2 .H eatshrink sleev ingov ertheinf rared LED .
Anotherconsid erationwhenb u ild ingthef inalproj ectisthepositioningof
thed etectorand LED .T hey shou ld ob v iou sly b epointinginthesame
d irection, howev er, theLEDmu stb eslightly f orward of thed etectoror
thelightwillpenetratethrou ghtheb ack of it .Intheprototy pe, theIR
d etectorwaspainted b lack onallsid es, leav ing only thef rontlens
exposed .F igu re8 .3 showsthearrangementu sed .
+5Volts
R1
Inf ra-red
4 .7k
1 4
LED
VDDRB7
Is
0 1
MCLR

RB6
RB5
BC549
4mH z RB4
Lo.
Inf ra-red
Cry stal1 e RB3
0 SC1

RB2
_L
e
Vcc
sensor
I
0
RB1
RBO
Vou t
e
Gnd
id 1
PIC1 6F 8 4
SF H 50 6
RA4
z
1 0 u f C3 C4
OSC2

RA3
i
C2
_
2 2 pf 2 2 pf
RA2
RAI
I,
VSSRAO
L7_
R2
1 3 1 2
1
1
Ov
1 0° .1 af T T T
1 1
470
1 VOlt
2 .0 0 0
3 .Gnd
ExperimentingwiththePicBasicProCompiler
S ingled irectioninf rared proximity d etector
F igu re8 . 3 . A rrangementof d etectorand L ED
.
Program IR_PROX . BA S
u sesthecircu itinf igu re8 . 1 tod etectanob j ect
u pto2 4 inchesinf ront
. Ittransmitsapu lseof mod u lated lightf or4 0 0 u s
thenwaitsf oraref lection . Inord ertoeliminatef alse ref lectionsthe
processiscarried ou ttentimesand only whenten ref lectionsare
receiv ed isthegreenL ED lit, whichind icatesthatanob j ecthasb een
positiv ely d etected . T heprogramisb ased arou nd the
PING su b rou tine,
thissend sou tthe3 8 k H z mod u lated inf rared
light . T hemethod f or
mod u latingtheL ED isexplained intheremotecontrolsection .
A f or- nextloopof 1 0 issetu pand the PING su b rou tineiscalled
. PortB . 1
isthenexamined ( lR d etectoO, if it' slowthenaref lectionhasb een
d etected and thev ariab leH IT S isincremented . If PortB . 1 ishighthen
therehasb eennoref lectionand H IT S islef talone .
A f terthetentransmissionshav ef inished , thev alu eof H IT S isexamined
.
If tenref lectionswered etected thev ariab leH IT S willhold thev alu e1 0 ,
and thegreenL ED isillu minated tosignif y apositiv econtactinf ront .
If y ou f ind thattheIRPD isov ersensitiv eand isd etectingd istantob j ects
ortheL ED isconstantly illu minated , thef req u ency of themod u lationmay
b eincreased ord ecreased
. T hisisaccomplished b y increasingor
d ecreasingthenu mb erof NOP' sinthe PING su b rou tine . Remov ing
NOP' swillincreasethef req u ency of themod u lation, and ad d ingNOP' s
willd ecreasethef req u ency . T hiswillhav etheef f ectof loweringthe
sensitiv ity of thed etector .
A lternativ ely , theinf rared L ED may b eattached d irectly tothePIC, and
Q 1 may b ed iscard ed .
S ection- 8 - 3
ExperimentingwiththePlcBasicProCompiler
Program -DIS_PROX .BAS
Inf rared proximity d etectorwithd istancegau ge
.
If y ou b u iltthesingled irectionIRPDy ou willhav enoticed thatatthe
periphery of itsd etectionrangetheL EDf lashes
.T hisisb ecau se the
f u rtheraway theob j ectisf romtheIRd etectorthelesslik ely that
1 0
ref lectionswillb ecou nted .W ecanpu tthisob serv ationtogood u se .
By cou ntinghowmany ref lectionshav eb eenreceiv ed wecangetan
approximationof d istance
.F orexample, if all1 0 ref lectionswere
receiv ed thentheob j ectmu stb eclosetothed etector, howev er, if only 5
ref lectionsof thepossib le1 0 wered etected , theob j ectmu stb ealittle
f u rtheraway
.F orpracticalu se1 0 samplesisnotenou gh, theref ore, the
program DIS_PROX .BAS tak es3 0 samplesand incrementsthev ariab le
H IT Swhenaref lectionisd etected .
If H IT Shasthev alu eof 1 0 , thenonly 1 0 ref lectionswered etected f rom
3 0 samplestak en, whichisj u stontheperiphery of theIRPD' slimit
.T he
greenL EDisillu minated toind icatead istantob j ectwasd etected .
If H IT S hasthev alu eof 2 0 f romapossib le3 0 samplestak en, thenthe
ob j ectmu stb ealittlecloserand they ellowL EDisillu minated .
If H IT Shasthev alu eof 3 0 f romapossib le3 0 samples, thentheob j ect
mu stb eclosetothed etector, and thered L EDisillu minated
.
F igu re8 .4 showsthecircu itlay ou tf orthismethod .
+5 Volts
Ov
R1
4 .7k
a
VDDRB7
MCL RRB6
RB5
RB4
RB3
OSC1 RB2
1 1 1 3 1
RBO
n
1 3
i
9
e
e
Section-8 - 4
Inf ra-red
L ED
G1
BC5 4 9
PIC1 6F 8 4
RA4 -
OSC2

RA3
-
C3

C4
2 2 pf 2 2 pf

RA1 ' -°
VSS
RAO
' -'
R2

R3

R4
i 6
4 70

4 70

4 70
1
GreenYellow Red
L ED

L ED

L ED
F igu re8 .4 .IRPDwithd istancegau ge .
Inf re-red
sensor
v cc
Vou t
2 nd '
SF H 5 O6
1 3 1
2
h
x .2 66
3 .2 nd
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
P r o g r a m - LR_P ROX.B AS
Di r e c t i o n a l i n fr a r e d p r o x i m i t y de t e c t o r
.
Th e Di r e c t i o n a l IRP D us e s t h e s a m e m e t h o d a s t h e p r e vi o us
e x p e r i m e n t s , t r a n s m i t t i n g a p ul s e o f l i g h t a n d de t e c t i n g a r e fl e c t i o n
.
Ho w e ve r , i t i s c a p a bl e o f de t e r m i n i n g w h e t h e r a n o bje c t i s t o t h e l e ft ,
r i g h t , o r c e n t r e .
Tw o i n fr a r e d LE Ds a r e p l a c e d e i t h e r s i de o f t h e i n fr a r e d de t e c t o r ,
p o i n t i n g a w a y fr o m i t a t a n a n g l e o f a p p r o x 30 t o 45 de g r e e s
. F i g ur e 8 .4
s h o w s t h e a r r a n g e m e n t
.
R1
4.7k
e
4m Hz
C r ys t a l
IB
C 7
u
10u1 C 3 C 4
C 2
.

2p f 22p f
0.1ufT T T
OV
F i g ur e 8 .4 . LE D a n d de t e c t o r a r r a n g e m e n t fo r di r e c t i o n a l IRP D
.
E a c h i n fr a r e d LE D i s p ul s e d i n t ur n a n d a r e fl e c t i o n i s de t e c t e d
. If a
r e fl e c t i o n i s de t e c t e d w h e n t h e l e ft LE D w a s p ul s e d t h e n a n o bje c t i s t o
t h e l e ft
. If a r e fl e c t i o n i s de t e c t e d w h e n t h e r i g h t LE D w a s p ul s e d t h e n a n
o bje c t i s t o t h e r i g h t
. Ho w e ve r , i f a r e fl e c t i o n w a s de t e c t e d fo r bo t h l e ft
a n d r i g h t t h e n t h e o bje c t m us t be i n fr o n t
. F i g ur e 8
.5 s h o w s t h e c i r c ui t fo r
t h e di r e c t i o n a l IRP D .
r 5 V o l t s
I
B C 540
,o B C 549
9
B
t
8
R3
470
Il l
Z
/ 44
R4
470
2
i
3
Le ft C e n t r e Ri g h t
F i g ur e 8 .5
. Di r e c t i o n a l IRP D .
LE D LE D LE D
Se c t i o n - 8 - 5
Ri g h t
i n fr a r e d
LE D
In fr a - r e d
a e n s o r
SF H506
n
1 .V n u1
2 .V C c
3 .On d
ExperimentingwiththePicBasicProCompiler
D irectionalinf rared proximity d etector
T heprogram LR_PROX. BA S
u sesthesamemethod asthelasttwo
experiments
. H owev er, therearenowtwoinf rared pu lsingsu b rou tines,
onef orthelef tLED ,
PINGLEFT , and onef ortheright, PING-RIGH T
.
EachLED ispu lsed tentimesb y callingeach
PING su b rou tineintu rn
and theamou ntof ref lectionsf romeachareplaced inthev ariab les
H IT S LEFT , and H IT S -RIGH T
.
T hetwov ariab lesarethenexamined , if they areb othgreaterthansev en
thenb othLED sprod u ced aref lection7 ormoretimes, which
means
theremu stb eanob j ectinf rontof b othof them
. S othelef tand right
LED sareextingu ished and thecentreled isillu minated .
Next, thev ariab le H IT S LEFT isexamined
; if thishold sav alu eof ten
thenthelef tLED prod u ced aref lection1 0 timesou tof 1 0
. W hichmeans
theremu stb eanob j ecttothelef tof thed etector
. S otherightand centre
LED sareextingu ished and thelef tled isillu minated
.
Finally thev ariab le H IT S -RIGH T
isexamined , if thishold sav alu eof ten
thentherightLED prod u ced aref lection1 0 timesou tof
1 0 . W hich
meanstheremu stb eanob j ecttotherightof thed etector
. S othelef t
and centreLED sareextingu ished and therightled isillu minated
.
T heplacementof theLED sisev enmorecriticalinthisapplication
as
therearenowtwoinf rared lightsou rces
. Caremu stb etak entoensu re
thatnolightleak sf romeitherLED , allthelightshou ld
b ed irected
f orward s .
If y ou f ind theLED isconstantly illu minated ,
thef req u ency of the
mod u lationmay b eincreased ord ecreased
. T hisisaccomplished b y
increasingord ecreasingthenu mb erof NOP' sinthePINGsu b rou tines
.
Remov ingNOP' swillincreasethef req u ency of themod u lation,
and
ad d ingNOP' swilld ecreasethef req u ency
. T hiswillhav etheef f ect of
loweringthesensitiv ity of thed etector .
A lternativ ely , theinf rared LED may b eattached d irectly tothePIC,
and
Q 1 and Q 2 may b ed iscard ed .
S ection-8 - 6
ExperimentingwiththePIcBasicProCompiler
Program
- SONPROX.BAS
U ltrasonicproximity d etector .
U singu ltrasonicsou nd instead of inf rared lightf orproximity d etection
is
thesameinmany respects
.H owev er, assou nd trav elsmu ch slower
thanlight ( approximately 0 .3 m/ msor1 f t/ ms, and 0
.3 m/ nsorif t/ ns
respectiv ely ) ,
wecanu samethod called timeof
f light( T OF ) toj u d gethe
d istanceof anob j ectaswellasd etectitspresence .T imeof f lightisthe
timetak enf romthetransmittersend ingitspingtothereceiv erd etecting
theecho .
T osend and receiv etheu ltrasonicsignalsweu setwotransd u cers, the
transmittransd u cer ( T X)
isaf ormof speak erwhoseresonantf req u ency
is4 0 k H z .T hereceiv ingtransd u cer ( RX)
isaf ormof microphonewiththe
sameresonantf req u ency .M od u latingthef req u ency of the
sou nd at
4 0 k H z hasthesameef f ectasmod u latingtheinf rared signals,
thatof
amb ientnoiseelimination ( almost)
.
F igu re8 .7 showsthecircu itf ortheu ltrasonicproximity d etector
. U nlik e
theinf rared experiments, thereisnoread y -mad ed etectorf orsou nd that
willconv ertitssignalintoaT T L v oltage
.T hishastob eaccomplished b y
anamplif ier, anop-ampinthiscase .
T heT Xtransd u cerisconnected toPortA .0 and PortA .1 of thePIC, T his
actsasaf ormof pu sh-pu lld riv e, onepinalternatesf romhightolow,
whiletheotherpinalternatesf romlowtohigh
.T hismethod achiev es
greaterd riv etothetransd u cer .Any ob j ectinthepathof thesignalwill
cau searef lection
.T heref lected signalisatasignif icantly lower
amplitu d ecompared totheoriginaltransmitted signal, theref oreweneed
toamplif y itb y approximately 1 0 0 times, thisissetb y R4 and R5 of the
op-ampIC1
.CapacitorR7 f eed satransistor ( Q 1 ) , whosepu rposeisto
prov id eT T L lev elpu lsestothePIC
.V R1 and R6 ad j u sttheb iasonthe
b aseof Q 1 , whichd eterminestheov erallsensitiv ity of thecircu it
. T he
transistor' snormalstateishigh( 5 V ) b u tispu lled lowwhenasu itab ly
strongechohasb eend etected
.
Initially , theb iaslev elontheb aseof Q 1 shou ld b ead j u sted to0
.4 V .T his
willgiv eu sasensitiv ity of approximately twof eet .Any moresensitiv e
and wewillincreasethechanceof d etectingstray ref lections .
Red u cingtheb iaslev elwilld ecreasethesensitiv ity of thecircu it
.
Section-8 - 7
ExperimentingwiththePicBasicProCompiler
U ltrasonicproximity d etector
T heprogram SON_PROX
.BAS transmitsapu lseof 4 0 k H z mod u lated
sou nd f orad u rationof 6 0 0 u su singthePI NG
su b rou tine .Asthe
transd u cerhastob eswitched f romhightolowextremely rapid ly f orthe
pu sh- pu llef f ecttowork , assemb ly cod ehas
had tob eu sed .T he
principalsof thissu b rou tinearev ery similartotheinf rared remotecontrol
experiments .
Af terthe
PI NG su b rou tinehassentou titspu lse, wemu stlook f oran
echoonPortA .2
.I f weweretoexaminePortA .2 and continu ewiththe
cod e, wewou ld missthesignal, asitwou ld n' thav ereached thereceiv er
y et
.Rememb er, sou nd trav elsalotslowerthanlight
.W emu sttheref ore
giv ethereceiv ertimetod etecttheecho .
T hisisaccomplished b y creatingaloopcou ntingu pto2 5 5
; withinthis
loopwecontinu ally examinePortA
.2 f oralow, whichwillsignif y thatan
echohasb eenheard .I f anechohasb eenheard theloopisexited , and
thev alu eof theloopv ariab le
(E T I ME) nowcontainsanu mb er
representingad istance, thef u rtheraway theob j ect, thecloseritwillb e
to2 5 5
.I f anechowasnotheard thentheloopexitsnormally and the
E- T I ME
v ariab leiscleared .
T hishasgiv enu sameansof d etectingand gau gingthed istanceof an
ob j ect, howev er, totry and eliminatef alseref lectionsweu sethesame
principalthatwasu sed intheinf rared proximity d etectors .
W esample
theincomingechotentimesand eachtimeanechoisheard thev ariab le
H I T Sisincremented
.I f , attheend of tensamplesH I T Scontainsthe
v alu e1 0 , therehasb eenapositiv econtactwithanob j ect, and thegreen
L ED isillu minated
.AserialL CD connected toPortB .0 d isplay sthe
v ariab leE T I ME,
whichisarepresentationof thed istance .
Eachtimethetransmittersend sou taping,
thereceiv erphy sically
v ib rates (rings)
insy mpathy .T hisringingcancau sethe receiv ing
sof twaretoseeaf alseref lectionimmed iately af tertheping
.I nord erto
comb atthisprob lemthereceiv ertransd u cermu stb epad d ed
.T hiswas
accomplished intheprototy peb y placingastripof f eltarou nd theb od y
of thetransd u cer, and alsoontheb ottomwheretheconnectingwires
protru d e .F igu re8 .6 illu stratesthis .
Section- 8 - 8
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
3
7
Q
Fi g ur e 8 . 6 . P o s i t i o n i n g a n d c us h i o n i n g o f t h e t r a n s duc e r s .
U
o
V
o ~,
i n
J I M
7 V a
I ~

N ~ y
C J

0

b 0
O

V

y
s >

a

>
¢¢¢¢¢¢¢¢ ¢¢¢¢¢
Se c t i o n -8 - 9
101
x Y
Fi g ur e 8 . 7 . Ul t r a s o n i c p r o x i m i t y de t e c t o r .
M ! g
Ul t r a s o n i c p r o x i m i t y de t e c t o r
ExperimentingwiththePicBasicProCompile r
Program -
H_BRIDGE .BAS
Driv ingaDCmotoru singanH-Brid ge .
F orthisexperiment, themotoru sed wastheDCty pesu pplied withthe
LEGOROBOTICSSYSTEM.Theseare9 V ty pes, whichd rawaf ew
hu nd red milliAmps .Howev er, any ty peof motormay b eu sed aslongas
thev oltageand cu rrenthand linglimitsof thecircu itsormotorsarenot
exceed ed .
Tocontrolthed irectionof amotorwithlogiclev elspresented f romthe
PIC, weu seanH-b rid gecircu it
.F igu re8 .8 showsaty picallay ou t . It' s
called anH-b rid ge, b ecau seitresemb lestheletterHinitsconf igu ration
.
+ 9 v olts
03

04
R3TIP32

TIP32
R4
27

27
R1

R2
1K

1K
D1
..41N 4001
F igu re8 .8
.DiscreteH-Brid ge .
Thecircu itisconf igu red insu chaway thatonly twotransistors
are
cond u ctingatany onetime
.W hentransistorsQ 3and Q 6 areon, the
motorspinsinoned irection
.W hentransistorsQ 4and Q 5 areon, the
motorspinsintheopposited irection
.W henallthetransistorsareof f then
themotorremainsmotionless .TransistorsQ 1and Q 2actasb u f f ersto
thePIC, theref oreallowingasmallcu rrenttocontrolf ou rlargercu rrent
transistors .D1toD4are
f ly b ack su ppression d iod esand areinplaceto
protectthetransistorsf romany highv oltagespik es
created b y the
motor' swind ings .
Q 3toQ 6 shou ld b echosentosu itthemotoru sed , inthiscaseTIPSare
morethanad eq u ate .If alargermotorisu sed thentransistorswith
a
largercu rrentcapab ility mu stb eu sed .
Tocontrolthed irectionof themotortwopinsarereq u ired f romthePIC
.
TheseconnecttoAand Bof theH-b rid ge .W heneitheroneof these
linesisb rou ghthighwhiletheotherispu lled lowthenad if f erent
d irectionischosen
.If b otharepu lled lowthenthemotorremainsstill .
Section-8 -10
ExperimentingwiththePicBasicProCompiler
DrivingaDCmotorusinganH-Bridge
T hedirectionof themotordependsonwhichway itisinsertedintothe
circuit . ConnectingitspositiveterminaltoQ 4 andQ 6 willhaveadif f erent
directionthanconnectingittoQ 3 andQ 5 .
N ote
. L inesA andBshouldneverb eb othb roughthighf orany lengthof
time, asthiswillturnonallf ourtransistors, resultinginanearshort
circuit . However, wecanusethistoouradvantage, whena
motor' s
terminalsareshortedtogetherthemotor' sshaf tishardtoturnb y hand .
U singthisprincipalwecansetL inesA andBof theH-b ridgehighf ora
f ewmilliseconds( ms) toactlik eab rak eandstopthemotorinitstrack s,
insteadof j ustslowingtoastop .
Program
H_BRIDGE . BA S demonstratesthesimplicity of controllingthe
H-b ridgecircuitof f igure8 . 8
. L ine-A of theH-b ridge isconnectedto
PortB . O of thePIC, andL ine-BisconnectedtoPortB
. 1 . T heprogram
cy clesthrough, turningthemotorf irstoneway andstoppingthenturning
itintheoppositedirection . T hedirectionitshouldb eturningisdisplay ed
onaserialL CDconnectedtoPortA . O .
T odemonstratetheb rak ingmethod, sub routine BRA KE iscalledj ust
b ef oreastop
. T hisb ringsb othL ineA andBhighf or1 0 0 ms, j ustenough
timef ortheb rak ingef f ecttowork b utnotenoughtimef orany damageto
b ecausedtothetransistors .
W hencontrollingmotors, orindeedany heavy load . A largecapacitor
shouldb eplacedacrossthePIC' ssupply lines
. A 3 3 0 0 uF isnormally
suf f icient . T hishelpsmoothoutany spik escausedb y themotorb eing
initially activated .
S ection- 8 -1 1
ExperimentingwiththePicBasicProCompiler
Program -
L293D.BAS
Driv ingaDCmotoru singtheL293D
.
T heSG S-T hompsonL293Distherob otenthu siastsf av ou ritemotor
d riv er
.T hed ev icecontainsf ou rpu shpu lld riv ersaswellastheirf ly b ack
protectiond iod es
.Eachd riv eriscapab leof prod u cing6 0 0 mAcontinu ou s
ou tpu tcu rrent
.
F igu re8
.9showstheinternalconf igu rationof oneof thesed ev ices .
I N I
EN 1
VS
OUT , OUT S
h Mh,
W

1-1 9
I N 2
iG u

I N 4
OUT !
F igu re8
.9 .L293Dinternals .
T hemostcommonconf igu rationf ortheL293DisastwoseparateH -
b rid ges
.T hisallowsthed ev icetosu pply u ptolAmptothemotor
.I f
su chhighcu rrentsareb eingimplemented aheatsink mu stb eu sed
.
F igu re8 .10 showsanL293Db eingu sed intheH -b rid geconf igu ration
.
T heI N 1and I N 2pinsactlik etheAand Blinesof thed iscreteH -b rid ge
.
T heEN 1pinisanenab leline,whenthisispu lled lowtheou tpu tv oltage
tothemotorisd isengaged .
T oallowthed ev icetob econtrolled b y lowv oltage
( T T L) lev els,a
separatelogicv oltagemay b eapplied totheVSSpin
.Whilethemotor' s
su pply v oltage,whichisu su ally alothigher,isconnected totheVSpin
.
Section-8 - 1 2
I N S
EN 2
I N 1
I N 2 MotorDirection
1
0 F orward
0
1 R ev erse
0 0
Stopped ( Brak eapplied tomotor)
1 1
Stopped ( shou ld b eav oid ed )
ExperimentingwiththePicBasicProCompiler
DrivingaDCmotorusingtheL293D
F igure8 . 1 0 . L293DH - Brid gemotorcontrol
.
Program L293D
. BA S d emonstratescontrolof theL293D . T heprogram
cy clesthrough, turningthemotorf irstoneway and stoppingthenturning
itintheopposited irection . T hed irectionitshould b eturningisd isplay ed
onaserialLCDconnected toPortA
. 0 .
T hed atasheetsf orallthepartsused inthissectioncanb ef ound onthe
accompany ingCDROM .
S ection- 8 - 1 3
+5Volts
R1
4. 7 k
1
VDD
RB7
1 3 +9Volts
MCLR

RB6
L2.
RB5
I
1 0
4mH z
Cry stal
1 6
RB4
RB3
B
e
z
vS

VS S
0 S C1

RB2
I N I

OUT 1
1
RB1
H BO
B

i
EN 1
I N 2

OUT 2
Motor
C1 0
PI C1 6F 8 4
220 0 uf
RA 4 L293D
0
OS C2

RA 3
z
OUT 3
C3 C4
RA 2
0
I N 3
C2 -
22pf 22pf
RA 1
v ss RA O
1 3
n 1 s
EN 2
I N 4 OUT 4
1 4
Ov
.
uT
j0 1 t
T T
GN D
i4 . 8 . 1 2 . 1
ExperimentingwiththePicBasicProCompiler
S ection- 9
Experimenting
with
AudioControl
Devices
AddingavoicetothePICwiththeIS D1416 .
Recordingandplayingbackmultiplemessages .
AllowingthePICtoaudiblycount .
DigitalvolumecontrolusingtheAD840X .
Controllingthegainofanop- amp .
Digitalactivebassandtreblecontrols .
ExperimentingwiththePicBasicProCompiler
A d d ingav oicetothePI CwiththeI S D 1 4 1 6
.
I maginehav ingy ou rlatestd igitalthermometertelly ou thetemperatu re,
ortherob oty ou hav ej u stb u iltactu ally telly ou thatitneed sitsb attery
recharged . A nd what' smore, itcantelltoy ou iny ou rownv oice!
T hisisallpossib lethank stoanewseriesof d ev icesf romI S D ; called
Chipcord ers . A
rangeof d ev icesareav ailab lethatallowmorethan2 0
second sof speechtob erecord ed ontothechip, and play ed b ack
complete, orsev eralsmallermessagesmay b erecord ed and selectiv ely
play ed b ack
. T hed ev iceweshallb eu singistheI S D 1 4 1 6 , whichwill
allowacompletemessageof 1 6 second sorsev eralsmallermessages .
T heI S D 1 4 1 6 may alsob eu sed asastand - aloneproj ectf oru seasa
memopad
. F igu re9 . 1 showsthecircu itf orj u stthisty peof operation .
. 5 v olts
II
f
A OVCCD VCCA RECA NA A NA
A1
A 2
A 3
A 4
A 5
A 6
. . ®
A 7
I S D 1 4 1 6
LED I NOUT
S PKR+
S PKR-
MI CREF
PLA YL
PLA YE
RECA GC
VS S D VS S A XCLK
MI C

S 2
z o
A 5
4 . 7k
n
S PKR
1 6 ohmS
e
C4
. 1 of
R6
1 0 k
0 v
Play b ack Play b ack Record
part

lu ll
messagemessage
F igu re9 . 1 . I S D 1 4 1 6 memopad .
I nthecircu itab ov e, arecord ingismad eb y pressingS 3
. T heLED will
illu minatetoind icaterecord mod eisoperational . W henthemessageis
complete
; releasingS 1 willd isengagerecord mod e . T olistentothe
messageS 1 orS 2 may b eu sed
. S 1 willplay themessageaslongasit
remainspressed . S 3willplay themessagetoitscompletionwitha
momentary pressand pu lsetheLED whenitisf inished .
S ection- 9 - 1
ExperimentingwiththePicBasicProCompiler
/
∎∎∎∎∎/I©
s
e
B
1 0
i
A d d ingav oicetothePICwiththeIS D 1 4 1 6
O ncethemessageisrecod ed ontothechipitwillremain, ev enwhenthe
powerisremov ed
. A ccord ingtothed atasheetitwillstay record ed f or
1 0 0 y ears . ( howd othey k now? ) .
W ecanu seasinglemessageasan
au d ioind icatororwarningb y apply ingapu lsetothePL A Y Epininstead
of u singapu shswitch
. T hepu lsemu sthav eahightolowtransitionf or
theIS D tod etectit
. T hisiseasily accomplished b y thelinesof cod e
b elow : -
PL A Y S PIN

V arPortA . 0
HighPL A Y S PIN

` S etthelineinitially tohigh
Nop

` A 1 u sd elay
L owPL A Y E_PIN

` Bringthelinelow
R ecord ingand play ingb ack mu ltiplemessages
.
T orecord and play b ack mu ltiplemessages
; thead d resslinesof theIS D
mu stb eu sed
( A 0 . . A 7 ) . F igu re9
. 2 showstheconnectionof aD IL switch,
whichwillallowd if f erentportionsof theIS D ' snon- v olatileR A M tob e
accessed
. T herestof thecircu itisid enticaltof igu re9 . 1 .
+5v olts
A ll
4 7 k
A O
A 1
A 2
A 3
A 4
A 5
A 6
A 7
S ection- 9 -
2
IS D 1 4 1 6
O v
6 way
D IL
switch
F igu re9 . 2
. D IL switchconnection .
T heR A M withintheIS D d ev icemay b ethou ghtof asapieceof
au d iotape, changingthev alu eapplied tothead d resslinesA 0 .
. A 7 is
lik ened toplacingtheau d iohead any whereonthetape
. Placingthe
b inary v alu e% 0 onthead d resslinesmay b ethou ghtof asplacingthe
au d iohead attheb eginningof thetape
. T he1 6 second sof record ing
timemay b esplitinto1 6 0 segments
; eachsegmentis1 0 0 msinlength
.
T hisislik emov ingthehead ev ery f ewinchesalongthetape
. T his
meansthatthev alu eplaced onthead d resslineshasarangeof 0 to
1 6 0 .
ExperimentingwiththePicBasicProCompiler
Address
00000000
Address
00101000
O n O f t
I ER Z - 3
CzM
Elm
Address
01010000
Section- 9 - 3
Addingav oicetothePI CwiththeI SD 14 16
AddresslinesA6 andA7 hav eadu alpu rpose . W henthey are
b oth
b rou ghthighthenasy stemnamed
operationalmode isenab led, which
allowsloopingof themessageaswellassev eral
otherf u nctions .
O perationalmodehasnorelev ancetoou rdesign, theref ore, wewillnot
discu ssit
. I f y ou wishtof indmoreab ou toperationalmode, thereare
v ery comprehensiv edatasheetsontheaccompany ingCD R O Mf ormost
of theI SD rangeof dev ices .
Aslongasanaddressab ov e16 0isnotchosen, operationalmodewill
notb eenab led .
W ewillnowlook atamethodof recordingandplay ing
b ack f ou r
separatemessages
. Eachmessagewillhav eamaximu mlengthof f ou r
seconds
. T hisdoesn' tseemalot, b u ty ou willb esu rprisedathowmu ch
canb esaidinsu chasmallamou ntof time
.
T orecordthef irstmessage, av alu eof 0mu stb eplacedontheaddress
lines . T heD I L switchshou ldb esetu pasinf igu re9
. 3 a . N owpressthe
recordb u tton( S1) u ntilthemessageisspok en
. Pressingtheplay b u tton
willplay b ack thef reshly recitedmessage
. Eachconsecu tiv emessage
mu sthav etheD I L switchpositionedaccordingtotheremainingthree
settingsof f igu re9 . 3
. T oplay b ack eachmessagethesamev alu emu st
b eplacedontheaddresslines .
Address
01111000
AO
A7
Message1

Message2

Message3

Message4
a

b

c

d
F igu re9 . 3
. D I L switchconf igu rationf ormessages .
N owthatwehav eou rf ou rdistinctmessagesrecorded
ataddresss
%00000000 ( 0) , %00101000
( 4 0) , %01010000( 8 0) , and%01111000
( 120) theI SD chipmay b ehook edu ptothePI C
. T hisisasimplerlay ou t
thantherecordingv ersionasthemicrophonesectionisnotreq u ired
.
F igu re9
. 4 showsthecircu itf orthis .
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o Co m p i le r
P r o g r a m - 4_ME SGE .B AS
+ 5 V o lt s
R2 &3
47 k
u
V DD
RB 7 e
4 MCLR

RB 6
2
RB 5
Hz

R84
t a i

RB 3
OSCI

RB 2
RB I
RB o
P IC16F84
RA4
C4
OSC2

RA3
RA2
22p 1

RA1
v s s RAO
e
z
IB
n
2
2
2e
li e
a 16 o h m
V CCA

V CCD

Sp e a k e r
R1
4.7 k
4m
Cr y s
C2 -
° ' T T T
T o
p

Se r i a l LCD
Se c t i o n -9 - 4
Addi n g a vo i c e t o t h e P IC w i t h t h e ISD1416
26
SP KR+
A7
AS

SP KR-
A5
A4
A3

ANA IN
A2
A1

ANA OUT

ISD1416
RZ.~

MIC RE F
P LAYL
P LAYS

MIC
RE C LE D
XCLK

AGC
V SSD

V SSA
p I
a
e
9
Fi g ur e 9.4. ISD1416 c o n n e c t i o n s t o t h e P IC
.
P r o g r a m 4_ME SGE .B AS
de m o n s t r a t e s p la y i n g b a c k t h e f o ur m e s s a g e s
t h a t h a ve b e e n r e c o r de d
. T h e P LAYS li n e i s n o t us e d i n t h i s
de m o n s t r a t i o n t h e r e f o r e
; i t i s di s c o n n e c t e d b y m a k i n g P o r t A
.2 a n i n p ut
a llo w i n g R3 t o k e e p s i t p ulle d h i g h
. T h e p r o g r a m i t s e lf i s ve r y p r i m i t i ve ,
a ll i t do e s i s lo a d t h e c o r r e s p o n di n g m e s s a g e a ddr e s s ' s o n t o P o r t B a n d
c a ll t h e SAYIT s ub r o ut i n e .
T h e SAYIT s ub r o ut i n e w a i t s 5 0 m s b e f o r e e n a b li n g t h e ISD c h i p
. T h i s
g i ve s i t t i m e t o p r o c e s s t h e c o n t e n t s o n t h e a ddr e s s li n e
. T h e P LAYL li n e
i s t h e n h e ld h i g h a n d a de la y o f 1 us i s i m p le m e n t e d b e f o r e t h e li n e i s
p ulle d lo w
. T h i s w i ll t r i g g e r t h e ISD i n t o p la y i n g
t h e c o r r e s p o n di n g
m e s s a g e
. T o e s t a b li s h w h e n t h e m e s s a g e h a s f i n i s h e d, t h e RE C_LE D
li n e i s p o lle d
. T h i s p uls e s lo w w h e n t h e m e s s a g e h a s e n de d .
T h e de la y s w e r e f o un d n e c e s s a r y i n o r de r f o r t h e ISD c h i p t o p la y t h e
p r o p e r m e s s a g e a n d w e r e f o un d b y t r i a l a n d e r r o r , s m a lle r de la y s m a y
w o r k j us t a s w e ll .
If m o r e m e s s a g e s a r e r e q ui r e d t h e n t h e s a m e m e t h o d a p p li e s
. Ho w e ve r ,
t h e m e s s a g e le n g t h s w i ll n e e d t o b e s m a lle r .
ExperimentingwiththePicBasicProCompiler
Program -
SAYCOUNT.BAS

Ad d ingav oicetothePI CwiththeI SD 1 4 1 6
AllowingthePI Ctoau d ib ly cou nt
W ecangoonef u rtherand mak etheI SD chipspeak nu mb ersorev en
cou nt .F irst, wemu strecord mu ltipleseparatemessages
.Thesewillb e
thed igits0 to9 and alsotheword ' point'
.I f thisprogramistob eu sed f or
ad igitalthermometerthentheword ` d egrees' mu stb ealsob erecord ed
.
Asanexamplewewillassu meatalk ingd igitalthermometeris
b eing
implemented .Theref ore, 1 2 messagesneed tob erecord ed
.F irstwe
mu stcalcu latethelengthof eachmessage
.Thisisaccomplished b y
d iv id ingthemaximu mlength ( insecond s)
thatthechipwillallow ( 1 6 in
ou rcase) b y thenu mb erof messagesreq u ired : -
1 6 1 1 2 = 1 .3
Thisgiv esu salengthof 1 .3 second spermessage
.Toconf igu rethisas
anad d resstopresenttotheI SD chip, simply mu ltiply thelengthof the
messageb y ten, whichwillgiv eu s1 3
.Theneachmessage' sad d ressis
amu ltipleof thisnu mb erplu s1 . i .e .
M essageonead d ress= 0
M essagetwoad d ress=
1 4 ( whichiseq u alto( 0 + 1 3 ) + 1
M essagethreead d ress= 2 8
( whichiseq u alto ( 1 4 + 1 3 ) + 1
M essagef ou rad d ress= 4 2

( whichiseq u alto
( 2 8+ 1 3 ) + 1
Thev alu eoneneed stob ead d ed tothemessagead d resstoav oid the
end of messagemark erthattheI SD chipplaces
( notsu rprisingly ) atthe
end of eachmessage
.W hentheend of messagemark erisreached , the
R ECL ED lineispu lsed low
.W ithou tthispu lse, thePI Cwillk eepon
pollingf oritand b ecomestu ck inanend lessloop
.
Tab le9
.1 showsthev alu estoplaceonthead d resslinesf oreachof the
twelv emessagesreq u ired f orad igitalthermometerexample . Orany
programthatreq u ires1 2 messagestob espok en
.
Section-9 - 5
ExperimentingwiththePicBasicProCompiler
A d d ingav oicetothePI CwiththeI S D 1 4 1 6
T ab le9 . 1
. A d d ressv alu esf orthed emonstrationprogram
.
U singthe1 2 messagesthathav eb eenprev iou sly record ed
; theI S D chip
isnowab letospeak any d igitf rom0 to9
. W iththeab ility tospeak the
d igits
; thenextstepwastob u ild u pthed igitsintoacou ntingprogram
.
Program I S D
CNT . BA S d oesj u stthat .
I tiscentred arou nd the
su b rou tineS A Y I T , whichtak esthe1 6 - b itv alu eheld in
S _NU M and
speak stheind iv id u ald igitsof thatv alu e
.
T he S A Y I T
su b rou tinework slik ethis
. A loopiscreated toextractthe
ind iv id u ald igitsf romthe1 6 - b itv alu e
; u singtheD I G operand . T he
v ariab le
S N nowhold stheind iv id u ald igit
. W ed onotwishtohearthe
lead ingz eroesof eachnu mb erb eingspok en, theref orelead ing
z ero
su ppressionisaccomplished b y agrou pof
if - then . A look u ptab leis
thenu sed toextractthead d ressf orthespecif icnu mb ertob espok en
.
A nd thisv alu eisplaced ontoPortB
. T he PLA Y su b rou tineisthancalled
whichtriggerstheI S D 1 4 1 6
.
A sad emonstrationof thecapab ilitiesof thisprogramtheword s' PO I NT '
and ` D EG R EES ' arealsospok en
. T heword ' PO I NT ' isspok enb y
placingthead d ressf orthe1 1
th
messageontoPortBand callingthe
PLA Y su b rou tine
. T heword ` D EG R EES ' isspok eninasimilarmanner,
exceptthead d ressf orthe1 2 th
messageisplaced ontoPortBb ef orethe
PLA Y
su b rou tineiscalled .
S ection- 9 - 6
MessageNo#
MessageS pok en
A d d ressof message D ec
1
ZER O 0 0 0 0 0 0 0 0
0
2
O NE 0 0 0 0 1 1 1 0
1 4
3
T W O 0 0 0 1 1 1 0 0
2 8
4 T HR EE
0 0 1 0 1 0 1 0
4 2
5 FO U R
0 0 1 1 1 0 0 0 56
6 FI VE
0 1 0 0 0 1 1 0 70
7 S I X
0 1 0 1 0 1 0 0 84
8 S EVEN
0 1 1 0 0 0 1 0 9 8
9
EI G HT
0 1 1 1 0 0 0 0 1 1 2
1 0
NI NE
0 1 1 1 1 1 1 0 1 2 6
1 1
PO I NT
1 0 0 0 1 1 0 0 1 4 0
1 2
D EG R EES 1 0 0 1 1 0 1 0
1 54
ExperimentingwiththePicBasicProCompiler
Program - DIGVOL.BAS
0
C1
lou t
DigitalVolu mecontrolu singtheAD8 4 0X
Digitalv ariab leresistorswerecov ered ind etailinthed igitaltoanalogu e
section .H owev er, they aresov ersatileand capab leof extremely low
noiseoperationthatitwasinev itab lethatthey wou ld b eu sed inau d io
eq u ipment.F igu re9 .5 showsoneof theob v iou sapplicationsf orad igital
resistor, thatof av olu mecontrol .
R egu lated 5 Volts
R 1
4 .7 k
0
0
C3

C4
C2
.
_5 6pf 5 6pf
Va"u "TTT
VDD
R B7
MCLR R B6
R B5
R 8 4
R B3
OSC1 R B2
R B1
R BO
PIC1 6F 8 4
R A4
OSC2 R A3
R A2
R AI
VSS
R AO
To
SerialLCD o
z
i
9
e
6
0
z
2-
Section-9 - 7
Digitalv olu mecontrolu singtheAD8 4 OX
R 2
1 0k
s
5
C9
SDI
CLK
F igu re9 .5 .Digitalv olu mecontrol .
Program AD8 4 00 .BAS u sesthecircu itinf igu re9 .5 .TheAlpinof an
AD8 4 00may b econnected totheinpu tof anamplif ierand theW 1
pin
may b econnected d irectly toamicrophoneortheou tpu tf romapre-amp .
SW 1 controlsVolu meu p, and SW 2controlsVolu med own, SW 3stores
thecu rrentv olu melev elinthePIC' sinternaleeprom .The programs
mainsu b rou tinecalled
POTOUT, controlstheAD8 4 00v iaits3-wire
interf ace .Instead of selecting aspecif icresistancetoou tpu t, the
su b rou tinecalcu latesthepercentageof theresistance .Thisisnecessary
b ecau seof thed if f erentresistancety pesav ailab le ( i .e . J k a2,
1 0k 2,
5 0k sa
and 1 00k b . Thereisnorealneed tok nowthespecif icresistance, aswe
k nowthat% 9 0of a5 0k Q resistanceis4 5 k Q , and % 9 0of a 1 Ok s2
resistanceis9 k Q .
VDD
W 1
1 31
AD8 4 00
GND
z
i At 8 0Ou tpu t
-0 Inpu t
ExperimentingwiththePicBasicProCompiler
Program - AD8400 .8AS

U singtheAD8400d igitalpotentiometer
W ek nowthatthed igitalpotshav earesolu tionof 2 5 6
( 0-2 5 5 ) .
Soto
calcu latethepercentagewej u std iv id eb y 1 00
.H owev erwiththe
limitationsof themathrou tinesinthecompiler, thev alu eshad tob e
scaled u pand thend ownagain
.L ik ethis : -
PO u tpu t= ( Percent* 2 5 5 ) / 1 00
T hev ariab le PERCENT
hold sav alu e ( notsu rprisingly )
b etween0to
1 00
.T hev ariab le PO U T PU T
hold sthed atab y tetob esenttotheDCP
.
W henu singtheAD8400, thead d ressb its
( b it-8and b it-9 )
mu stb oth
containz eroes
.T hisisachiev ed b y simply clearingb othb its
: -
P_ O u tpu t .8= 0
PO u tpu t .9 = 0
T heAD8400isenab led b y b ringingtheCSlinelowand the1 0-b itword
isshif ted ou t, withtheM ostSignif icantBitsentf irst
: -
SH I F T O U T SDI , CL K , M sb f irst, [ PO u tpu t1 1 0]
T heCSlineisb rou ghthightod isab lethechip, and thesu b rou tineis
exited .
Section-9 -
8
ExperimentingwiththePicBasicProCompiler
Program - AD8400 .BAS
T hesecond d emonstrationu singtheAD8400, showninf igu re9 .6 .U ses
thetwoterminalorR EO ST AT mod e, thegainof aninv erted op-amp
amplif ieriscontrolled b y theDCP .T hed igitalpotisconnected b etween
theinv ertinginpu tand theou tpu tof theop-amp
.A1 O k Q partwasu sed in
thisd emonstrationb u thighergainscou ld b eachiev ed b y u singa1 00k Q
part .W hentheDCPisat% 0( 5 0Q ) thereislessthanu nity gain, when
theDCPisat% 1 0( 1 k Q ) thereisu nity gainand whentheDCPisat
% 1 00( 1 0k 5 2 ) thereisagainof 1 0 .T he3 -wireinterf aceconnectstothe
PI Casinf igu re9 .5 .SwitchesSW 1 and SW 2 controlthegain, SW 3
storesthecu rrentgainlev elinthePI C' sinternaleeprom .
T heProgramf orthisd emonstrationis AD8400.BAS .
+ 5 V olts
F igu re9 .6
.O p-ampgaincontrol .
T hev ersatility of thesed ev icesisnev erend ing, v irtu ally any thing that
u sesamechanicalpotentiometercanb econtrolled withoneof these
remark ab leI C' s .
Section- 9 -9
U singtheAD8400d igitalpotentiometer
Controllingthegainof anop-amp
ExperimentingwiththePicBasicProCompiler
Program - AD8402 .BAS U singtheAD8402d u ald igitalpotentiometer
Digitalactiv eb assand treb lecontrols
F igu re9 .8illu stratestheu seof thed u ald igitalpot( AD8402) asamono
b assand treb lecontroller
.T hecircu itlook smorecomplicated thanit
actu ally is, f igu re9 .7 showsasimplif ied lay ou tof thesamecircu it
.
T reb le
F igu re9 .7 .Simplif ied b assand treb le .
I t' saconv entionaltonecontrolf ou nd inmostau d ioamplif iers, only one
channelisshown .I f stereooperationisreq u ired anAD8403 willhav eto
b eu sed asitcontainsf ou rR DAC' s .
T heb assisad j u sted b y R DAC1 ( Al, Bi, and W1 ) , whilethetreb leis
ad j u sted b y R DAC2( A2, 82, and W2) . T hef ou rswitches ( SW1 . .4)
attached tothelower4-b itsof PortBcontrolb assu pord own, and treb le
u pord own, and ared isplay ed onaserialL CDattached toPortB .7 .
Switches1 and 2controlBasswhileSwitches3 and 4controlT reb le
.
Program AD8402
.BAS isf oru sewithf igu re9 .8 .I tiscentred arou nd the
su b rou tine POT OU T , thissu b rou tineou tpu tsthe1 0-b itword toan
AD8400, AD8402, orAD8403 d igitalpot .T heinternalR DACof choice
( i . .4) isload ed intothev ariab leR DAC, and thepercentage of the
resistanceisload ed intothev ariab le
PER CENT . F orexample, if the
b ass, whichiscontrolled b y R DAC1 istob eincreased to% 9 0, v ariab le
R DACisload ed with1 , and PER CENT isload ed with9 0thenthe
POT OU T
su b rou tineiscalled : -
R DAC= 1

` PointtoR DAC1
PER CENT = 9 0

' % 9 0of theR DAC' sresistance
Gosu b POT OU T

' Shif tou tPER CENT toR DAC1
Section-9 - 1 0
ExperimentingwiththePicBasicProCompiler
0
• 3
0

0I

0
- n

<
D
S~D7J31~~7J]7~7]D m D
oAw
D
w
D
a O- Nw~CD~~
I z iI « I - I NI -
- - 00
T
y
^ ^ O
f T
T
w^ ^ O
aT'°m I 'oL

r- oNI
AM
Z

x-

o

>Z
w
f
f
w
v
win
∎-
an
Section- 9 - 11
U singtheADS4 02 d u ald igitalpotentiometer
Therestof theprogramisessentiallyaseriesof
if - then's
thatscanthe
lower4 - b itsof PortBtoseewhichswitchhasb eenpressed
. And thenact
u ponwhicheverswitchisoperated .
g
1D~
O2 en
11
11
A
s
N

9

( )
NN
O
A

TC)
0

C
F igu re9 . 8
. Actived igitalBassand Treb lecontrol
.
ExperimentingwiththePicBasicProCompiler
Section-10
Programming
Techniques
IntegratingAssemblylanguageintoyourprograms
.
DeclaringVariablesforusewithAssembler .
PassingparametersusingtheDEFINEstatement
.
UsingINCLUDEfilestotidyupyourcode .
WakingthePICfromSLEEP .
AbriefintroductiontoHardwareINTERRUPTS
.
UsingtheONINTERRUPTcommand .
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
In t e g r a t i n g As s e m bl y l a n g ua g e i n t o yo ur p r o g r a m s
Th i s m a y c o m e a s a bl o w t o a n y di e -h a r d B ASIC p r o g r a m m e r s o ut t h e r e ,
but a s s e m bl y l a n g ua g e s ubr o ut i n e s a r e o c c a s i o n a l l y un a vo i da bl e .
E s p e c i a l l y w h e n t i m e -c r i t i c a l o r ul t r a e ffi c i e n t c o de i s r e qui r e d . No t
e ve r yo n e a g r e e s o n t h i s , a n d I w o ul d be m o r e t h a n h a p p y t o be p r o ve d
w r o n g . Ho w e ve r , un t i l s uc h t i m e , I fe e l dut y bo un d t o t r y a n d s h o w yo u
h o w t o i n c o r p o r a t e a s s e m bl e r r o ut i n e s s e a m l e s s l y a n d p a i n l e s s l y i n t o
yo ur B ASIC c o de .
If yo u do n o t us e a s s e m bl y l a n g ua g e a t a l l , t h e n yo u m a y w i s h t o s ki p t h e
n e x t fe w p a g e s . Ho w e ve r , I ur g e yo u t o g a i n e ve n a r udi m e n t a r y
un de r s t a n di n g o f a s s e m bl e r . Yo u w i l l a c h i e ve a g r e a t e r i n s i g h t i n t o h o w
t h e P IC fun c t i o n s a t i t s ba s e l e ve l , a n d i t w i l l a l s o a l l o w i n fo r m a t i o n t o be
g l e a n e d fr o m Mi c r o c h i p 's m a n y da t a s h e e t s a n d a p p -n o t e s (s o m e t i m e s !) .
Th i s w i l l ul t i m a t e l y l e a d t o be t t e r c o m p i l e r p r o g r a m s be i n g w r i t t e n .
Th e a bi l i t y t o p l a c e i n -l i n e a s s e m bl e r i n t o yo ur c o de c a n be a p o w e r ful
t o o l i f us e d a p p r o p r i a t e l y, h o w e ve r , i t c a n a l s o be yo ur w o r s t n i g h t m a r e i f
a bug o r g l i t c h s h o ul d a r i s e . Th e r e fo r e , i t i s a l w a ys a dvi s a bl e t o s e e k a
s t a n da r d B ASIC a p p r o a c h t o s o l vi n g a p a r t i c ul a r c o di n g p r o bl e m , i f a t a l l
p o s s i bl e . So m e o f t h e e x p e r i m e n t s i n t h i s bo o k us e a s s e m bl e r
s ubr o ut i n e s o ut o f n e c e s s i t y t o a c h i e ve a c e r t a i n g o a l . P r i m e e x a m p l e s o f
t h i s a r e t h e MSSP , e e p r o m s ubr o ut i n e s , E RE AD a n d E WRITE ,
di s c us s e d i n s e c t i o n -3 . Th e B ASIC c o de d ve r s i o n i s 204 B yt e s i n l e n g t h ,
w h i l e t h e a s s e m bl e r c o de d ve r s i o n , w h i c h h a s e x a c t l y t h e s a m e fun c t i o n
a n d
i s t r a n s p a r e n t t o t h e p r o g r a m m e r , i s o n l y 116 B yt e s . Sur e l y, t h e
s a vi n g o f 88 B yt e s o f p r e c i o us ROM i s w o r t h t h e us e o f a s s e m bl e r .
A m a jo r c o n s i de r a t i o n w h e n us i n g a s s e m bl e r s ubr o ut i n e s a r e ba n k
bo un da r y c o n fl i c t s . Al l t h e 14-bi t c o r e de vi c e s us e ROM bo un da r i e s o f 2k
(0-2048) . Th e p r o bl e m w i t h c r o s s i n g t h e s e bo un da r i e s i s t h a t , t h e
a s s e m bl e r 's GOTO a n d C ALL i n s t r uc t i o n s o n l y s up p l y 11-bi t s o f t h e 13-
bi t s r e qui r e d by t h e p r o g r a m
c o un t e r
t o a c c e s s ROM p a s t 2k . Th e
r e m a i n i n g 2-bi t s a r e s up p l i e d by bi t s -3 :4 o f t h e P C LATH r e g i s t e r . Th e s e
m us t be s e t o r c l e a r e d be fo r e a n a s s e m bl e r GOTO o r C ALL i n s t r uc t i o n i s
i m p l e m e n t e d w i t h i n yo ur c o de . Fo r e x a m p l e , i f a p o r t i o n o f yo ur
a s s e m bl e r c o de c r o s s e s a bo un da r y, t h e n a c a l l o r jum p t o a r o ut i n e
w i t h i n t h a t ba n k w i l l n o t a c t ua l l y g e t t h e r e . If, h o w e ve r , t h e e ve r p o p ul a r
16F84 i s us e d, t h e n t h e s e i s s ue s do n o t a r i s e . Ho w e ve r , i f t h e m i d-r a n g e
P IC 's a r e us e d, t h e n ALL a s s e m bl e r s ubr o ut i n e s s h o ul d be p l a c e d a t t h e
s t a r t o f yo ur p r o g r a m , t h us e n s ur i n g t h e y w i l l be l o c a t e d w i t h i n ba n k-0.
Se c t i o n - 1 0 - 1
ExperimentingwiththePicBasicProCompiler
IntegratingAssemblylanguageintoyourprograms
Inord ertoaccessyourassemblersubroutinef romBAS IC, thecompiler' s
CAL L command should alwaysbeused
. T hismanipulatesthePCL AT H
registertoconstructthef ull1 3 - bitsreq uired toaccessR O M anywherein
thePIC . T heCAL L command d if f ersf romtheG O S U Bcommand inthat
anund erscoremustpreced ethesubroutine' snamewhen itisf irst
d eclared : -
Call M y_ S ub

` CallthesubroutineM y_ S ub
Asm
_ M y_ S ub

; N otetheund erscore, _ M y_ S ub
{ Y oursubroutinegoeshere )
R eturn

; Exitthesubroutine
End asm
T heR ET U R N instructiond oesnotreq uirethatthe PCL AT H is
manipulated , asithasaccesstothef ull1 3 - bitad d ress, which itpulls
f romthe stack.
N ote
: whenassemblermod ehasbeenentered , thecommentsymbol
mustchangetoasemicolon (
; ) instead of aq uote( ' ) . If thisis
f orgotten, thenascreenf ullof extremely
conf usingerrorswillbe
d isplayed .
D eclaringV ariablesf orusewithAssembler
Anotherimportantissuewhend esigningassemblerroutinesistheuseof
v ariables
. AL L v ariablesshould bed eclared inBAS IC, asthecompiler
willnotrecogniz eassemblerd eclared types .
Inf act, d eclaringany
v ariableinassemblerwillwreakhav ocwithyourprogram, theassembler
d oesnotrecogniz ecompilerv ariables, and thecompilerd oesnot
recogniz eassemblerv ariables
. S oimaginewhatwould happenif ( when! )
theywerebothassigned tothesameR AM location?
Inmostcases, whenusingPICswithmorethan2 kof R O M , ( and a
selectf ewwithless) ,
userR AM issplitintosev eralbanks . T heref ore, all
v ariablesused inanyassemblerroutineshould beassigned tobank- 0
.
EachR AM bankis1 2 8 bytesapart, thesealsoincorporatethe PIC' s
hard wareregisters . Bits- 5
: 6 of theS T AT U S registercontrolwhich bank
thePICispointingto .
S ection- 1 0 - 2
ExperimentingwiththePicBasicProCompiler
I ntegratingA ssemb ly langu ageintoy ou rprograms
I f thecompilerassignsav ariab lethatweareu singf oranassemb ler
rou tinetoab ank otherthanb ank - 0 , thesu b rou tinehas
noway of
k nowingthis, theref ore, any ref erencestothisv ariab lewou ld b epointing
toanentirely d if f erentlocation .
W henwritingpu rely inBA S I C, thecompilertak escareof thisissu ef or
u s, whichmeansthatitd oesn' tcarewhatb ank itassignsaparticu lar
v ariab leto
. I nmostcases, if asmallprogramisb eingd ev eloped ona
largerPI C, thecompilerwillassignthef irstlotof v ariab lesto
b ank - 0
au tomatically , u ntilitmu stmov etoanotherb ank
. H owev er, wecannot
b e1 0 0 % certainthatthev ariab lesu sed inou rassemb lersu b rou tinewill
alway sb elocated inb ank - 0
. S owemu stf orcethecompilertoassigna
particu larv ariab leintob ank - 0 , thisisaccomplished b y u singtheBA N K
operand af terd eclaringthev ariab le
: -
M y _ V ar
V ar By te BA N K O

` A ssignM y _ V artoBank - 0
I f f orany reason, y ou wishthev ariab letob elocated intoanotherb ank
thenBA N K 1 , BA N K 2 , orBA N K S willd oj u stthat
.
U singtheD EF I N Ecommand topassparameters
.
A v ery u sef u lway of passingparameterstoanassemb lersu b rou tineis
withtheD EF I N Estatement
. T heu seof D EF I N Eisrestricted tov alu es
thatwillremainconstantthrou ghou ttheprogram( i . e .
theport and b it
whereaninf rared sensorisattached ) ,
asthesamed ef inemay only b e
u sed oncewithinthecod e
. T hisisu su ally placed attheb eginningof the
program
. A sanexample, let' ssu pposewehav ewrittenasu b rou tineto
ou tpu taninf rared signaltoanL ED connected toPortA
. 1 .
D ef ine I R_
POR T POR T A ` Port onwhichtoattachI R L ED
D ef ine I R BI T

1

` BitonwhichtoattachI R L ED
A sm
# D ef ine lR _ L ED
lR POR T , I R BI T
End asm
S ection- 1 0 - 3
ExperimentingwiththePIcBasicProCompiler
Asm
If nd ef IR PO R T
IR PO R T = PortA
End if
If nd ef IR BIT
IR BIT =0
End if
End asm
UsingtheDEFINEcommand topassparameters
T he# DEFINEisanassemb lerd irectiv e, itsu seisthesameasitsBAS IC
cou nterpart, asintheexampleab ov e, ev ery timethenameIR _ L EDis
encou ntered itwillb ereplaced b y thestringIR _ PO R T , IR _ BIT ,
and as
IR PO R T hasb eengiv enthev alu eof PO R T A
( 5 ) , and IR BIT hasb een
giv enthev alu e1 , thenameIR L ED
isnoweq u altothestring PO R T A, 1 .
T hisisu sed asaninterf aceb etweenBAS ICand assemb ler
. And canb e
placed intheassemb lerrou tinelik ethis : -

Bcf IR _ L ED
; CleartheappropriatePortand Bit

Bsf S T AT US , 5 ;
PointtoBANK 1 registers

Bcf IR L ED ; M ak ethe
Port and BitanO UT PUT

Bcf S T AT US , 5
; Back toBANK O registers

Bsf IR L ED
; T u rnontheIR L ED
Def au ltv alu escanalsob ecreated incasetheDEFINEisnotu sed ornot
req u ired
. Inthecaseof ou rexample, letssu pposethatthed ef inesare
notu sed , thed ef au ltswillb ePO R T Aand BIT - 0
. Forthisweu sethe
assemb ler' sIFDEF, IFNDEF, and ENDIFstatements
. IFDEF, asits
nameimplies, willretu rntru eif the# DEFINEhasb eend eclared , IFNDEF
willretu rntru eif the# DEFINEhasnotb eend eclared
. W ecanu sethis
( cond itionalassemb ly )
tosettheportand b itd ef initionstotheird ef au lt
v alu esif thed ef inehasnotb eeninclu d ed intheprogramlik ethis : -
W earenowinassemb lermod e
Check if lR _ PO R T hasb eend eclared
If not, thenlR PO R T =PO R T A
E

nd of IFstatement

Check if IR BIT hasb eend eclared

If not, thenlR _ BIT =0
E

nd of IFstatement
B

ack toBAS ICmod e
T hisisav ery u sef u land ef f icientway of passingparameters,
asthe
compileritself prov eswiththeL CD, DEBUG , S ER IN2 etc, d ef ines
. And
isu sed inmany of theprogramsthrou ghou tthisb ook .
S ection- 1 0 - 4
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o
Co m p i le r
U s i n g I N CL U D E f i le s t o t i d y u p y o u r c o d e
U s i n g I N CL U D E f i le s t o t i d y u p y o u r c o d e
I n c lu d e f i le s a r e a ls o u s e d e x t e n s i v e ly t h r o u g h o u t t h i s b o o k
. I t a i d s i n t h e
r e a d a b i li t y o f t h e c o d e a n d i s a n e a s y w a y t o i n c o r p o r a t e c o m m o n ly u s e d
s u b r o u t i n e s
. I n c lu d e f i le s a r e b y n o m e a n s a n e w i d e a ; t h e y h a v e b e e n
u s e d s i n c e t h e f i r s t a s s e m b le r s w e r e d e v e lo p e d , a n d a r e u s e d a lo t i n
la n g u a g e s s u c h a s C a n d P A S CA L
. H o w e v e r , m o s t p e o p le c o n s i d e r t h e
P B P t o b e j u s t a n o t h e r v e r s i o n o f t h e B A S I C S t a m p a n d w r i t e c o d e i n i t s
s t y le . T h i s c o u ld n o t b e f u r t h e r f r o m t h e t r u t h
; i t i s t r u e t h a t m o s t B A S I C
S t a m p a n d B A S I C S t a m p I I p r o g r a m s m a y b e d i r e c t ly c o m p i le d
. B u t i f
y o u a r e w r i t i n g p u r e ly w i t h t h e P B P t h e n S t a m p c o d e c a n b e a w k w a r d
a n d c lu m s y .
I f t h e i n c lu d e f i le c o n t a i n s a s s e m b le r s u b r o u t i n e s t h e n i t m u s t a lw a y s b e
p la c e d a t t h e b e g i n n i n g o f t h e p r o g r a m , j u s t a f t e r t h e
MOD E D E FS . B A S
f i le
. T h i s a llo w s t h e s u b r o u t i n e / s t o b e p la c e d w i t h i n t h e f i r s t b a n k o f
m e m o r y (0. . 2048),
t h u s a v o i d i n g a n y b a n k b o u n d a r y e r r o r s . P la c i n g t h e
i n c lu d e f i le a t t h e b e g i n n i n g o f t h e p r o g r a m a ls o
a llo w s a ll o f t h e
v a r i a b le s u s e d b y t h e r o u t i n e s h e ld w i t h i n i t t o b e p r e - d e c la r e d
. T h i s
a g a i n m a k e s f o r a t i d i e r p r o g r a m , a s a lo n g li s t o f v a r i a b le s i s n o t p r e s e n t
i n t h e m a i n p r o g r a m .
T h e r e a r e s o m e c o n s i d e r a t i o n s t h a t m u s t b e t a k e n i n t o a c c o u n t w h e n
w r i t i n g c o d e f o r a n i n c lu d e f i le , t h e s e a r e
: -
1 )
. A lw a y s j u m p o v e r t h e s u b r o u t i n e s .
W h e n t h e i n c lu d e f i le i s p la c e d a t t h e t o p o f t h e p r o g r a m t h i s i s t h e f i r s t
p la c e t h a t t h e c o m p i le r s t a r t s , t h e r e f o r e , i t w i ll r u n t h e s u b r o u t i n e / s f i r s t
a n d t h e R E T U R N c o m m a n d w i ll b e p o i n t i n g t o a r a n d o m p la c e w i t h i n t h e
c o d e
. T o o v e r c o m e t h i s , p la c e a G OT O s t a t e m e n t j u s t b e f o r e t h e
s u b r o u t i n e s t a r t s . Fo r e x a m p le : -
G o t o OVE R T H I S S U B R OU T I N E

` J u m p o v e r t h e s u b r o u t i n e
` T h e s u b r o u t i n e i s p la c e d h e r e
OVE R - T H I S _S U B R OU T I N E :

` J u m p t o h e r e f i r s t
S e c t i o n - 1 0 - 5
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
U s i n g I N C L U D E f i l e s t o t i d y u p y o u r c o d e
2 ) . Va r i a bl e a n d L a be l n a m e s s h o u l d be a s m e a n i n g f u l a s p o s s i bl e
.
Fo r e x a m p l e
. I n s t e a d o f n a m i n g a va r i a bl e L OOP , c h a n g e i t t o
I SU B L OOP .
Th i s w i l l h e l p e l i m i n a t e a n y p o s s i bl e d u p l i c a t i o n e r r o r s ,
c a u s e d by t h e m a i n p r o g r a m t r y i n g t o u s e t h e s a m e va r i a bl e o r l a be l
n a m e . Ho w e ve r , t r y n o t t o m a ke t h e m t o o o bs c u r e a s y o u r c o d e w i l l be
h a r d e r t o r e a d a n d u n d e r s t a n d , i t m i g h t m a ke s e n s e a t t h e t i m e o f
w r i t i n g , bu t c o m e ba c k t o i t a f t e r a f e w w e e ks a n d i t w i l l be m e a n i n g l e s s .
3 )
. C o m m e n t , C o m m e n t , a n d C o m m e n t s o m e m o r e .
Th i s c a n n o t be e m p h a s i ze d e n o u g h . AL WAYS p l a c e a p l e t h o r a o f
r e m a r ks a n d c o m m e n t s
. Th e p u r p o s e o f t h e s u br o u t i n e /s w i t h i n t h e
i n c l u d e f i l e s h o u l d be c l e a r l y e x p l a i n e d a t t h e t o p o f t h e p r o g r a m , a l s o ,
a d d c o m m e n t s a f t e r vi r t u a l l y e ve r y c o m m a n d l i n e , a n d c l e a r l y e x p l a i n t h e
p u r p o s e o f a l l va r i a bl e s a n d c o n s t a n t s u s e d
. Th i s w i l l a l l o w t h e
s u br o u t i n e t o be u s e d m a n y w e e ks o r m o n t h s a f t e r i t s c o n c e p t i o n
. A r u l e
o f t h u m b t h a t I u s e i s t h a t I c a n u n d e r s t a n d w h a t i s g o i n g o n w i t h i n t h e
c o d e by r e a d i n g o n l y t h e c o m m e n t s t o t h e r i g h t o f t h e c o m m a n d l i n e s .
Th e i n c l u d e f i l e u s e d by y o u r p r o g r a m m u s t be i n t h e s a m e d i r e c t o r y a s
t h a t p r o g r a m , o r i n t h e r o o t d i r e c t o r y o f t h e c o m p i l e r ( i .e . P B ASI C ) .
Th e r e a r e s o m e t h i n g s t h a t s h o u l d N OT be d o n e i n s i d e a n i n c l u d e f i l e .
Th e s e a r e : -
D O N OT l o a d i n t h e
MOD E D E FS.B AS i n c l u d e f i l e . Al w a y s p l a c e t h i s i n
t h e m a i n p r o g r a m .
D O N OT u s e t h e OSC d e f i n e , a s t h i s m a y o ve r r i d e t h e OSC s e t t i n g
w i t h i n t h e m a i n p r o g r a m .
Se c t i o n - 1 0 - 6
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o
C o m p i l e r
P r o g r a m - SLE E P
.B AS a n d SLE E P 2 .B AS
Wa ki n g t h e P IC fr o m SLE E P
Al l t h e P l C m i c r o r a n g e h a ve t h e a bi l i t y t o be p l a c e d i n t o a l o w p o w e r
m o de , c o n s um i n g m i c r o Am p s o f c ur r e n t
.
Th e c o m m a n d fo r do i n g t h i s i s SLE E P
. Th e c o m p i l e r 's SLE E P c o m m a n d
o r t h e a s s e m bl e r 's SLE E P i n s t r uc t i o n m a y be us e d
. Th e c o m p i l e r 's
SLE E P c o m m a n d di ffe r s s o m e w h a t t o t h e a s s e m bl e r 's i n t h a t t h e
c o m p i l e r 's ve r s i o n w i l l p l a c e t h e P IC i n t o l o w p o w e r m o de fo r n s e c o n ds
(w h e r e n i s a va l ue fr o m 0 t o 65535) .
Th e a s s e m bl e r 's ve r s i o n s t i l l p l a c e s
t h e P IC i n t o l o w p o w e r m o de , h o w e ve r , i t do e s t h i s fo r e ve r , o r un t i l a n
i n t e r n a l o r e x t e r n a l s o ur c e w a ke s i t
. Th i s s a m e s o ur c e a l s o w a ke s t h e
P IC w h e n us i n g t h e c o m p i l e r 's c o m m a n d .
Ma n y t h i n g s c a n w a ke t h e P IC fr o m i t s s l e e p , t h e WATC HDOG TIME R i s
t h e m a i n c a us e a n d i s w h a t t h e c o m p i l e r 's SLE E P c o m m a n d us e s
.
An o t h e r m e t h o d o f w a ki n g t h e P IC i s a n e x t e r n a l o n e , a c h a n g e o n o n e
o f t h e p o r t p i n s
. We w i l l e x a m i n e m o r e c l o s e l y t h e us e o f a n e x t e r n a l
s o ur c e .
Fo r t h e s e de m o n s t r a t i o n s t h e w a t c h do g t i m e r m us t be di s a bl e d o r i t w i l l
w a ke t h e P IC e ve r y t i m e i t
t i m e s -o ut .
Th i s i s a c c o m p l i s h e d by p l a c i n g t h e
fo l l o w i n g l i n e o f c o de a t t h e be g i n n i n g o f t h e p r o g r a m
: -
De vi c e w dt o ff
No t e
: t h a t t h i s m a y o n l y be us e d w h e n t h e P M a s s e m bl e r i s c h o s e n
.
Al s o , i t i s de vi c e i n de p e n de n t .
Th e r e a r e t w o m a i n w a ys o f w a ki n g t h e P IC us i n g a n e x t e r n a l s o ur c e
.
On e i s a c h a n g e o n bi t s 4
.1 o f P o r t B . An o t h e r i s a c h a n g e o n bi t -0 o f
P o r t B
. We s h a l l fi r s t l o o k a t t h e w a ke up o n c h a n g e o f P o r t B ,bi t s -4
. .7 .
As i t s n a m e s ug g e s t s , a n y c h a n g e o n t h e s e p i n s e i t h e r h i g h t o l o w o r l o w
t o h i g h w i l l w a ke t h e P IC
. Ho w e ve r , t o s e t up t h i s m o de o f o p e r a t i o n
s e ve r a l bi t s w i t h i n r e g i s t e r s INTC ON a n d OP TION_RE G n e e d t o be
m a n i p ul a t e d
. On e o f t h e fi r s t t h i n g s r e q ui r e d i s t o e n a bl e t h e w e a k P o r t B
p ul l up r e s i s t o r s
. Th i s i s a c c o m p l i s h e d by c l e a r i n g t h e RB P U bi t o f
OP TION _RE G
(OP TION RE G
.7 ) . If t h i s w a s n o t do n e , t h e n t h e p i n s
w o ul d be fl o a t i n g a n d r a n do m i n p ut s t a t e s w o ul d o c c ur w a ki n g t h e P IC
up p r e m a t ur e l y .
Se c t i o n -10 - 7
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o
Co m p i le r
W a k i n g t h e P I C f r o m S L E E P
A lt h o u g h t e c h n i c a lly w e a r e e n a b li n g a f o r m o f i n t e r r u p t ,
w e a r e n o t
i n t e r e s t e d
( i n t h i s p r o g r a m )
i n a c t u a lly r u n n i n g a n i n t e r r u p t h a n d le r .
T h e r e f o r e , w e m u s t m a k e s u r e t h a t G L O B A L i n t e r r u p t s a r e d i s a b le d , o r
t h e P I C w i ll j u m p t o a n i n t e r r u p t h a n d le r e v e r y t i m e a c h a n g e o c c u r s o n
P o r t B . T h i s i s d o n e b y c le a r i n g t h e G I E b i t o f I N T CO N
( I N T CO N . 7 ) .
T h e i n t e r r u p t w e a r e c o n c e r n e d w i t h i s t h e R B p o r t c h a n g e t y p e
. T h i s i s
e n a b le d b y s e t t i n g t h e R B I E b i t o f t h e I N T CO N r e g i s t e r
( I N T CO N
. 3 . A ll
t h i s w i ll d o i s s e t a f la g w h e n e v e r a c h a n g e o c c u r s
( a n d o f c o u r s e w a k e
u p t h e P I C) .
T h e f la g i n q u e s t i o n i s R B I F , w h i c h i s b i t - 0 o f t h e I N T CO N
r e g i s t e r
. F o r n o w w e a r e n o t p a r t i c u la r ly i n t e r e s t e d i n t h i s f la g , h o w e v e r ,
i f g lo b a l i n t e r r u p t s w e r e e n a b le d , t h i s f la g c o u ld b e e x a m i n e d t o s e e i f i t
w a s t h e c a u s e o f t h e i n t e r r u p t
. T h e R B I F f la g i s n o t c le a r e d b y h a r d w a r e
s o b e f o r e e n t e r i n g S L E E P i t s h o u ld b e c le a r e d
. I t m u s t a ls o b e c le a r e d
b e f o r e a n i n t e r r u p t h a n d le r i s e x i t e d
.
T h e S L E E P c o m m a n d i t s e lf i s t h e n u s e d
. U p o n a c h a n g e o f P o r t B , b i t s
4 - 7 t h e P I C w i ll w a k e u p a n d p e r f o r m t h e n e x t i n s t r u c t i o n
( o r c o m m a n d )
a f t e r t h e S L E E P c o m m a n d w a s u s e d
.
A s e c o n d e x t e r n a l s o u r c e f o r w a k i n g t h e P I C i s a p u ls e
a p p li e d t o
P o r t B . O
. T h i s i n t e r r u p t i s t r i g g e r e d b y t h e e d g e o f t h e p u ls e , h i g h t o lo w
o r lo w t o h i g h
. T h e I N T E D G b i t o f O P T I O N _ R E G
( O P T I O N R E G . 6 )
d e t e r m i n e s w h a t t y p e o f p u ls e w i ll t r i g g e r t h e i n t e r r u p t
. I f i t i s s e t , t h e n a
lo w t o h i g h p u ls e w i ll t r i g g e r i t , a n d i f i t i s c le a r e d t h e n a h i g h t o lo w p u ls e
w i ll t r i g g e r i t .
T o a llo w t h e P o r t B
. O i n t e r r u p t t o w a k e t h e P I C t h e I N T E b i t m u s t b e s e t ,
t h i s i s b i t - 4 o f t h e I N T CO N r e g i s t e r
. T h i s w i ll a llo w t h e f la g I N T F
( I N T CO N . 1 )
t o b e s e t w h e n a p u ls e w i t h t h e r i g h t e d g e i s s e n s e d
. A s w i t h
t h e p r e v i o u s d i s c u s s i o n , t h i s f la g
i s o n ly o f a n y i m p o r t a n c e w h e n
d e t e r m i n i n g w h a t c a u s e d t h e i n t e r r u p t
. H o w e v e r , i t i s n o t c le a r e d b y
h a r d w a r e a n d s h o u ld b e c le a r e d b e f o r e t h e S L E E P c o m m a n d i s u s e d
( o r
t h e i n t e r r u p t h a n d le r i s e x i t e d )
.
T h e p r o g r a m s S L E E P . B A S , a n d
S L E E P 2. B A S d e m o n s t r a t e b o t h
m e t h o d s d i s c u s s e d
. S L E E P . B A S
w i ll w a k e t h e P I C w h e n a c h a n g e
o c c u r s o n P o r t B , b i t s 4 - 7
. A n d S L E E P 2. B A S
w i ll w a k e t h e P I C w h e n a
p u ls e i s d e t e c t e d o n P o r t B . O .
S e c t i o n - 1 0 - 8
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o
Co m p i le r
P r o g r a m s -TMROCLCK.B AS a n d TMROINT
.B AS
A b r i e f i n t r o du c t i o n t o h a r dw a r e i n t e r r u p t s
Th e r e a r e m a n y w a y s t h a t i n t e r r u p t s m a y b e t r i g g e r e d o n t h e di f f e r e n t
t y p e s o f P IC a v a i la b le
. Th e p r e v i o u s di s c u s s i o n o n SLE E P s h o w e d t w o
p o s s i b le m e t h o ds
. H o w e v e r , w e do n o t h a v e t h e s p a c e t o g o i n t o a ll t h e
v a r i o u s w a y s , a s s o m e o f t h e la r g e r P ICs h a v e m o r e t h a n 3 0 i n di v i du a l
i n t e r r u p t t r i g g e r i n g s o u r c e s . Th e r e f o r e , w e w i ll e x a m i n e h o w t o
e n a b le
i n t e r r u p t s u s i n g t h e m o s t p o p u la r m e t h o d, t h a t o f TIME RO
.
TIME RO, o r TMRO i s a n e i g h t -b i t r e g i s t e r , i n i t s s i m p le s t f o r m
TMRO
i n c r e m e n t s w i t h e v e r y i n s t r u c t i o n c y c le
. W h e n t h e c o u n t r e a c h e s 2 5 5 i t
r o lls o v e r t o 0 a n d k e e p s o n c o u n t i n g
. TMRO a ls o h a s a p r e s c a le r w h i c h
m a y b e a t t a c h e d t o i t
. W h e n t h e p r e s c a le r i s e n a b le d, TMRO i n c r e m e n t s
o n c e e v e r y 2 , 4 , 8 , 1 6 , 3 2 , 6 4 , 1 2 8 , o r 2 5 6 i n s t r u c t i o n c y c le s
. W h e n e v e r
TMRO r o lls o v e r t o 0 a n i n t e r r u p t m a y b e g e n e r a t e d .
Th e c o m p i le r ' s ON INTE RRU P T c o m m a n d i s n o t a n i n t e r r u p t i n t h e t r u e
s e n s e o f t h e w o r d a s i t m u s t f i n i s h t h e B ASIC c o m m a n d i t i s p r o c e s s i n g
b e f o r e t h e i n t e r r u p t h a n dli n g s u b r o u t i n e i s c a lle d . Tr u e i n t e r r u p t s o c c u r
o n a r e g u la r b a s i s , o r a r e t r i g g e r e d b y a n e v e n t , r e g a r dle s s o f w h a t t h e
P IC i s p r o c e s s i n g a t t h e t i m e
. Th e r e f o r e , t h e ON INTE RRU P T c o m m a n d
w i ll n o t b e di s c u s s e d j u s t y e t . In s t e a d w e w i ll e x a m i n e t r u e
h a r dw a r e
i n t e r r u p t s t h a t o c c u r n a t u r a lly w i t h i n t h e P IC . Th e s e , u n f o r t u n a t e ly m u s t
a lw a y s u s e a s s e m b le r w i t h i n t h e i n t e r r u p t h a n dle r
. Th e r e a s o n b e h i n d
t h i s i s t h a t t h e c o m p i le r ' s c o m m a n ds a r e n o t r e -e n t r a n t , w h i c h
m e a n s
o n ly o n e c o m m a n d a t a t i m e m a y b e u s e d
. Th i s s o u n ds li k e s t a t i n g t h e
o b v i o u s , h o w e v e r , i f B ASIC c o m m a n ds w e r e u s e d w i t h i n a h a r dw a r e
i n t e r r u p t , a c o m m a n d i n t h e m a i n b o dy p r o g r a m c o u ld b e i n t e r r u p t e d
m i d-s t r e a m a n d t h e s a m e i n s t r u c t i o n m a y b e e n c o u n t e r e d i n t h e i n t e r r u p t
h a n dle r
. As b o t h c o m m a n ds w o u ld b e u s i n g t h e s a m e SYSTE M
v a r i a b le s , o n e o f t h e c o m m a n ds i s g o i n g t o b e p r e s e n t e d w i t h t h e w r o n g
v a lu e s
. Th i s c o u ld le a d t o m a j o r p r o g r a m c r a s h e s , o r s u b t le b u g s t h a t
w o u ld b e n e x t t o i m p o s s i b le t o t r a c k do w n .
To i n f o r m t h e c o m p i le r w h e r e t o f i n d t h e a s s e m b le r i n t e r r u p t h a n dli n g
s u b r o u t i n e a D e f i n e i s u s e d : -
D e f i n e INTH AND
My _ In t

` P o i n t t o i n t e r r u p t h a n dle r
Th e c o m p i le r w i ll n o w j u m p t o t h e i n t e r r u p t h a n dli n g s u b r o u t i n e MY_ INT
w h e n e v e r a n i n t e r r u p t i s t r i g g e r e d
.
Se c t i o n -1 0 - 9
ExperimentingwiththePicBasicProCompiler
A b rief introd u ctiontohard wareinterru pts
Bef orewecanchangeany b itsthatcorrespond tointerru ptsweneed to
mak esu rethatglob alinterru ptsared isab led
. T hisisd oneb y clearing
theG I Eb itof I N T CO N
(I N T CO N . 7 ) .
S ometimesaninterru ptmay occu r
whiletheG I Eb itisb eingcleared , whichmeansthattheb itisnotactu ally
cleared and glob alinterru ptsarenotd isab led
. T omak esu rethattheG I E
b itisactu ally cleared wemu stpollit
. T hiscanb eaccomplished b y a
simpleloop : -
G I E=O

` D isab leglob alinterru pts
W hile
G I E=1

` M ak esu rethey areof f
G I E=O

` Continu etoclear
G I E
W end

` Exitwhen G I E isclear
T heprescalerattachmenttoT M R O iscontrolled
b y b its0 : 2 of the
O PT I O N _ R EG (PS O , 1, 2 )
. T ab le1
. 1showstheirrelationship tothe
prescaled ratioapplied
. Bu tb ef oretheprescalercanb ecalcu lated we
mu stinf ormthePI Castowhatclock gov ernsT M R O
. T hisisd oneb y
settingorclearingthePS A b itof O PT I O N R EG
(O PT I O N R EG . 3 ) .
I f
PS A iscleared thenT M R O isattached totheexternalcry staloscillator
. I f
itissetthenitisattached tothewatchd ogtimer, whichu sestheinternal
R Coscillator . T hisisimportanttorememb er
; astheprescaleratiod if f ers
accord ingtowhichoscillatoritisattached to .
T ab le1 . 3
. T M R O prescalerratioconf igu rations
.
A scanb eseenf romtheab ov etab le, if wereq u ireT M R O toincrement
onev ery instru ctioncy cle (4/O S G ) wemu stclearPS 2
. . 0 and setPS A ,
whichwou ld attachittothewatchd ogtimer . T hiswillcau seaninterru pt
tooccu rev ery 2 5 6 u s
(assu minga4mH z cry stal) . I f thesamev alu es
wereplaced intoPS 2
. . 0 and PS A wascleared
(attached totheexternal
oscillator)
thenT M R O wou ld incrementonev ery
2 nd
instru ctioncy cleand
cau seaninterru pttooccu rev ery 5 12 u s
.
S ection-
1 0 - 10
PS 2 PS 1 PS O PS A =O (Externalcry stalO S C)
PS A =1 (I nternal W D T O S C)
0
0 0 1 : 2
1 : 1
0 0
1 1 : 4
1 : 2
0
1 0 1 : 8
1 : 4
0 1 1
1 : 16
1
: 8
1 0 0
1 : 3 2 1 : 16
1 0 1
1 : 6 4 1 : 3 2
1 1 0
1 : 12 8 1 : 6 4
1 1 1 1 : 2 5 6
1 : 12 8
ExperimentingwiththePicBasicProCompiler
A b rief introd u ctiontohard wareinterru pts
T hereishowev er, anotherway T M R O may b eincremented
. By setting
theT O CS b itof theO PT I O N R EG (O PT I O N _R EG . 5 )
arisingorf alling
transitiononPortA . O willalsoincrementT M R O . S ettingT O CS willattach
T M R O toPortA
. O and clearingT O CS willattachittotheoscillators . I f
PortA . O ischosenthenanassociated b it, T O S E
(O PT I O N R EG . 4 ) mu st
b esetorcleared . ClearingT O S EwillincrementT M R O withalowtohigh
transition, whilesettingT O S EwillincrementT M R O withahightolow
transition .
T heprescaler' sratioisstillv alid whenPortA
. O ischosenasthesou rce,
sothatev ery n th transitiononPortA . O willincrementT M R O
. W herenis
theprescalerratio .
Bef oretheinterru ptisenab led , T M R O itself shou ld b eassigned av alu e,
asany v ariab leshou ld b ewhenf irststartingaprogram . I nmostcases
clearingT M R O willsu f f ice
. T hisisnecessary b ecau se, whenthePI Cis
f irstpowered u pthev alu eof T M R O cou ld b eany thingf rom0 to2 5 5
W earenowread y toallowT M R O totriggeraninterru pt . T hisis
accomplished b y settingtheT O I Eb itof I N T CO N (I N T CO N . 5 ) . S etting
thisb itwillnotcau seaglob alinterru pttooccu rj u sty et, b u twillinf orm
thePI Cthatwhenglob alinterru ptsareenab led , T M R O willb eone
possib lecau se
. W henT M R O ov erf lows (rollsov erf rom2 5 5 to0 ) the
T G I F (I N T CO N . 2 ) f lagisset
. T hisisnotimportanty etb u twillb ecome
cru cialintheinterru pthand lersu b rou tine
.
T hef inalactistoenab leglob alinterru ptsb y settingtheG I Eb itof the
I N T CO N register (I N T CO N . 7 ) .
T heinterru pthand lersu b rou tinemu stalway sf ollowaf ixed pattern
. F irst,
thecontentsof theW registeralongwithPCL A T H and S T A T U S mu stb e
sav ed , thisistermed contextsav ing .
T heref ore, weneed tosetasid e
sev eralv ariab lesf ortheregisterstob estored into : -
W sav e

VarBy te

S YS T EM `S toragef ortheW register
S sav e

VarBy te

S YS T EM `S toragef ortheS T A T U S reg
Psav e

VarBy te

S YS T EM `S toragef orthe
PCL A T H reg
S ection- l0 -
1 1
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
{ Yo ur i n t e r r up t c o de g o e s h e r e }
S a v i n g o f t h e r e g i s t e r s i s do n e a ut o m a t i c a l l y b y t h e c o m p i l e r i f a P I C w i t h
m o r e t h a n 2k o f ROM i s us e d . Ho w e v e r , w h e n us i n g P I C s w i t h m o r e
t h a n 2K t h i n g s g e t a l i t t l e t r i c ki e r , a s m o r e s t o r a g e s p a c e i s r e qui r e d
a l o n g w i t h t h e i r ADDRE S S a n d B ANK p o s i t i o n s . Th e r e a s o n i n g b e h i n d
t h i s i s t h a t w h e n a n i n t e r r up t o c c ur s , t h e P I C m i g h t b e p r o c e s s i n g
c o m m a n ds i n a b a n k o t h e r t h a n b a n k-0, w h i c h a l s o m e a n s t h a t t h e RAM
a ddr e s s e s h a v e m o v e d t o a n o t h e r b a n k . I f t h e W r e g i s t e r w a s n o w t o b e
s a v e d i n t o t h e v a r i a b l e WS AVE p r i o r t o p r o c e s s i n g t h e i n t e r r up t c o de , i t
w o ul d b e p o i n t i n g t o t h e c o r r e c t l o c a t i o n i n RAM b ut t h e w r o n g b a n k .
Th e da t a m e m o r y (RAM) i s o r g a n i s e d i n b a n ks o f 128 . I n t h e c a s e o f t h e
n e w P I C 16F87X r a n g e t h e f i r s t b a n k o f m e m o r y
(b a n kO)
s t a r t s a t
a ddr e s s $20, t h e s e c o n d a t $A0, t h e t h i r d (i f i t h a s m o r e t h a n 2 b a n ks ) a t
$120, a n d t h e f o ur t h (i f i t h a s m o r e t h a n 3 b a n ks ) a t $1A0 . Th e r e f o r e , i f
t h e i n t e r r up t w a s c a l l e d w h i l e t h e P I C w a s p r o c e s s i n g c o de i n b a n k-1,
t h e n w h a t us e d t o b e RAM a ddr e s s $20 i s n o w a c t ua l l y $A0
. I f a v a r i a b l e
w a s a l r e a dy a s s i g n e d t o $AO i t s c o n t e n t s w o ul d b e o v e r w r i t t e n b y t h e
i n t e r r up t p l a c i n g t h e c o n t e n t s o f W i n t o i t .
To b e e x t r a s a f e , t h e a ddr e s s o f t h e WS AVE v a r i a b l e s a l o n g w i t h t h e i r
b a n k l o c a t i o n s s h o ul d b e us e d . Th e a ddr e s s l o c a t i o n s h o ul d b e t h e s a m e
f o r e a c h b a n k . Fo r e x a m p l e : -
S e c t i o n -l 0- 1 2
A b r i e f i n t r o duc t i o n t o h a r dw a r e i n t e r r up t s
Th e a c t ua l a s s e m b l y c o de p l a c e d a t t h e h e a d o f t h e i n t e r r up t h a n dl e r
t h a t do e s t h e c o n t e x t s a v i n g i s : -
As m
My _l n t
Th e n a m e o f t h e i n t e r r up t
Mo v w f Ws a v e S a v e t h e W r e g i s t e r
S w a p f S TATUS , w
C l r f S TATUS
Mo v w f
S s a v e S a v e t h e S TATUS r e g i s t e r
Mo v f P C LATH, w
Mo v w f P s a v e S a v e t h e P C LATH r e g i s t e r
ExperimentingwiththePicBasicProCompiler
T hiswillallowtheW registertob esav ed atthef irstlocationof R A M in
any b ank regard lessof whichb ank thePI Cwasinwhentheinterru pt
wascalled
. I f itisprocessingb ank - 1 thentheW registerwillb esav ed
intothev ariab le W SA VE1 aswellas W SA VEO.
N ote . T hisonly applieswhenu singinterru pts, asthecompilernormally
tak esthehead acheou tof b ank switching
.
W hentheinterru pthand lerwascalled theG I Eb itwasau tomatically
cleared b y hard ware, d isab lingany moreinterru pts . I f thiswerenotthe
case, anotherinterru ptmightoccu rwhilethe
interru pthand lerwas
processingthef irstone, whichwou ld lead tod isaster .
N owtheT OI F ( T M R Oov erf low) f lagb ecomesimportant . Becau se,
b ef oreexitingtheinterru pthand leritmu stb ecleared tosignalthatwe
hav ef inished withtheinterru ptand areread y f oranotherone . A lsothe
W , PCL A T H and ST A T U Sregistersmu stb eretu rned totheiroriginal
cond itions
. T heassemb lercod ef ord oingthisis : -
{ Y ou rinterru ptcod egoeshere }
T hef inalcommand intheinterru pthand lerretu rnsthePI Cb ack tothe
mainb od y cod ewheretheinterru ptwascalled f rom . R ET FI Emu stb e
u sed asopposed toR ET U R N b ecau se, R ET FI Ealsore- enab lesglob al
interru pts .
Section-
1
0- 1 3
A b rief introd u ctiontohard wareinterru pts
W sav eOVar
$ 2 0 BA N KO SY ST EM ` W storageinb ank - 0
W sav elVar $ A O BA N K1
SY ST EM ' W storageinb ank - 1
W sav e2 Var $ 1 2 0BA N K2 SY ST EM ` W storageinb ank - 2
W sav e3Var$ 1 A OBA N KS SY ST EM ` W storageinb ank - 3
Ssav e Var By teBA N KO SY ST EM ` ST A T U Sstorage
Psav e
VarBy teBA N KO SY ST EM ` PCL A T H storage
M ov f Psav e, w ; R estorePCL A T H register
M ov wf
PCL A T H
Swapf Ssav e, w ; R estoreST A T U Sregister
M ov wf ST A T U S
Swapf
W sav e, f
Swapf W sav e, w ; R estoreW register
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
A b r i e f i n t r o d u c t i o n t o h a r d w a r e i n t e r r u p t s
A s i m p l i s t i c ye t t yp i c a l i n t e r r u p t h a n d l i n g s u b r o u t i n e i s s h o w n b e l o w f o r
u s e o n P IC s w i t h 2k o r l e s s o f ROM : -
A s m
INT
Th e p r o g r a m a b o ve i s t h e c l a s s i c f l a s h i n g l e d p r o g r a m i m p l e m e n t e d t h e
l o n g w a y . E ve r y t i m e t h e i n t e r r u p t i s c a l l e d t h e X o r w f i n s t r u c t i o n w i l l t u r n
t h e l e d o n o r o f f . Th e f l a s h i n g w i l l o n l y b e a p p a r e n t i f t h e p r e s c a l e r r a t i o
i s a s s i g n e d a h i g h va l u e , s u c h a s 1 : 256 .
To m a ke l i f e e a s i e r w h e n u s i n g h a r d w a r e i n t e r r u p t s , t h r e e i n c l u d e f i l e s
h a ve b e e n d e ve l o p e d
. 2K INT. INC , i s f o r u s e w i t h P IC s t h a t h a ve 2k o r
l e s s o f ROM, s u c h a s t h e 1 6F84
. 4K INT. INC , i s f o r u s e w i t h P IC s t h a t
h a ve 4k o f ROM, s u c h a s t h e 1 6F874
. A n d 8K INT. INC , i s f o r u s e w i t h
P IC s t h a t h a ve 8k o f ROM, s u c h a s t h e 1 6F877 .
Th e c h o s e n i n c l u d e f i l e , a s a l w a ys , m u s t b e p l a c e d a t t h e b e g i n n i n g o f
yo u r p r o g r a m . Wi t h i n e a c h i n c l u d e f i l e t h e e x a c t a m o u n t o f va r i a b l e
s p a c e i s a l l o c a t e d f o r c o n t e x t s a vi n g , a l s o t w o m a c r o s a r e d e f i n e d . Th e
r e a s o n b e h i n d d e ve l o p i n g t h r e e i n c l u d e f i l e s i n s t e a d o f a o n e - f o r - a l l
a p p r o a c h i s t h a t i t i s l e s s w a s t e f u l o n p r e c i o u s va r i a b l e s p a c e
.
S e c t i o n - l 0 - 1 4
Mo vw f
S w a p f
C l r f
Mo vw f
Mo vf
Mo vw f
Ws a ve
S TA TUS , w
S TA TUS
S s a ve
P C LA TH, w
P s a ve
; S a ve t h e r e g i s t e r s
;
B e f o r e s t a r t i n g t h e c o d e
; Wi t h i n t h e i n t e r r u p t h a n d l e r
Mo vl w 255
X o r w f P o r t B
; Fl a s h a n LE D e ve r y i n t e r r u p t
B c f INTC ON, TOIF ; C l e a r t h e TMRO o ve r f l o w f l a g
Mo vf P s a ve , w
Mo vw f
P C LA TH
S w a p f S s a ve , w ;
Re s t o r e t h e r e g i s t e r s
Mo vw f S TA TUS ;
B e f o r e e x i t i n g t h e In t e r r u p t
S w a p f Ws a ve , f
S w a p f Ws a ve , w
Re t f i e ;
E x i t t h e i n t e r r u p t s u b r o u t i n e
E n d a s m
ExperimentingwiththePicBasicProCompiler
A b rief introd u ctiontohard wareinterru pts
T hef irstmacro, I N T _ S T A R T ,
sav estheW registeralongwiththe
S T A T US and PCLA T H
. T hismacroisonly req u ired whenu singaPI C
with2 k orlessof R O M , asthecompilerau tomatically sav esthecontext
f orlargerPI Cs
. T ou sethe I N T _ S T A R T
macro, placethef ollowing
templatecod eattheb eginningof y ou rinterru pthand ier : -
A sm
M y _ I nt

; T henameof theinterru pt
I N T S T A R T

; Usethecontextsav ingmacro
{
Y ou rinterru pthand lingcod egoeshere
)
T hesecond macro,
I N T EN D ,
restoresthecontentsof theW register,
S T A T US , and PCLA T H, thenperf ormsaR ET F I Einstru ction . T hismacro
mu stb eu sed regard lessof thePI Csiz e, asthecompilerd oes
not
restorethecontextf orlargerPI Cs
. T ou setheI N T EN D macro,
place
thef ollowingtemplatecod eattheend of y ou rinterru pthand ler
: -
{
Y ou rinterru pthand lingcod egoeshere )
I N T EN D

; Usethecontextrestoremacro
End asm
Eachmacrod ef ined intheseparateinclu d ef ilesu sesexactly the
right
amou ntof instru ctionsaccord ingtothesiz eof thePI Cchosen
. T hu s
red u cingwasted memory
T heprogram
T M R O CLCK. BA S
d emonstratestheu seof aT M R O
interru ptperf ormingthef u nctionsof a
( notv ery accu rate)
clock ,
d isplay ingthetimeonaserialLCD connected toPortA . 0
. T heprescaler
isassigned theratioof 1
: 6 4 , whichmeansthataninterru ptwillb ecalled
ev ery 1 6 . 3 8 4 ms
( 6 4 * 2 5 6 u s) .
A ssu minga4 mHz cry stalisu sed .
Eachtimetheinterru ptiscalled , thev ariab leT I CKS isincremented u ntil
itreaches6 1
. T hiswillgiv eu sanapproximatesecond
( 6 1 * 1 6 3 8 4 =
9 9 9 . 4 2 4 msor
. 9 9 9 4 2 4 of asecond ) .
W henT I CKS reaches6 1 , asecond haspastsothe
S ECO N D S
v ariab le
isincremented and theT I CKS v ariab leiscleared
. W hen S ECO N D S
reaches6 0 , aminu tehas
passed sothe M I N UT ES
v ariab leis
incremented and the S ECO N D S
v ariab leiscleared . W hen M I N UT ES
reaches6 0 , anhou rhaspassed sothe HO UR S
v ariab leisincremented
and the M I N UT ES
v ariab leiscleared .
S ection- 1 0 -
1 5
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
A b r i e f i n t r o d u c t i o n t o h a r d w a r e i n t e r r u p t s
A n d f i n a l l y , w h e n t h e HOURS
va r i a b l e r e a c h e s 23 t h e n a f u l l 24-h o u r d a y
h a s p a s s e d s o
HOURS i s c l e a r e d . If m o r e t h a n a s e c o n d h a s p a s s e d
t h e n t h e f l a g U_FLA G
i s s e t . Th i s w i l l i n f o r m t h e m a i n p r o g r a m l o o p t o
u p d a t e i t s d i s p l a y w i t h t h e c u r r e n t t i m e
.
It m u s t b e n o t e d t h a t TMRO i t s e l f i s e n a b l e d a t p o w e r u p
. Re g a r d l e s s o f
w h e t h e r t h e TOIE b i t i s s e t o r n o t
. Th i s j u s t a t t a c h e s i t t o a n i n t e r r u p t .
Wh i c h m e a n s t h a t t h e TOIF f l a g w i l l a l w a y s b e s e t w h e n a n o ve r f l o w
o c c u r s .
In a d d i t i o n , w h e n t h e p r e s c a l e r i s a t t a c h e d t o t h e w a t c h d o g t i m e r , t h e
c o m p i l e r 's SLE E P a n d NA P c o m m a n d s m a y n o t b e u s e d . A s t h e s e a r e
a l s o a t t a c h e d t o t h e w a t c h d o g , a n d r e l y o n t h e p r e s c a l e r 's r a t i o
.
Th e c o d e w i t h i n t h e i n t e r r u p t h a n d l e r s h o u l d b e qu i c k a n d a s e f f i c i e n t a s
p o s s i b l e b e c a u s e , w h i l e i t 's p r o c e s s i n g t h e c o d e t h e m a i n p r o g r a m i s
h a l t e d .
Wh e n u s i n g a s s e m b l e r i n t e r r u p t s , c a r e s h o u l d b e t a ke n t o e n s u r e t h a t
t h e w a t c h d o g t i m e r d o e s n o t
t i m e -o u t . P l a c i n g a C LRWDT i n s t r u c t i o n a t
r e g u l a r i n t e r va l s w i t h i n t h e c o d e w i l l p r e ve n t t h i s f r o m h a p p e n i n g
. A n
a l t e r n a t i ve a p p r o a c h w o u l d b e t o d i s a b l e t h e w a t c h d o g t i m e r a l t o g e t h e r ,
a s i l l u s t r a t e d i n t h e SLE E P d i s c u s s i o n
.
Se c t i o n -1 0 - 1 6
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o Com p i le r
P r og r a m - INT_CLCK.B AS
Us i n g t h e ON INTE RRUP T c om m a n d
Us i n g t h e ON INTE RRUP T c om m a n d i s s i m i la r t o u s i n g a n a s s e m b le r
i n t e r r u p t
. H ow e v e r , t h e c om p i le r doe s n ot i m m e di a t e ly c a ll t h e i n t e r r u p t
h a n dle r , i n s t e a d i t f la g s i t a n d w a i t s u n t i l t h e c om m a n d b e i n g p r oc e s s e d
i s f i n i s h e d
. As t h e r e m i g h t b e a de la y b e f or e t h e i n t e r r u p t i s c a lle d, t h e
p r e s c a le r ' s r a t i o s h ou ld n ot b e a s s i g n e d t oo low a v a lu e
. F or e x a m p le , i f
t h e p r e s c a le r w a s a s s i g n e d t h e r a t i o 1
: 1 , t h e n a n i n t e r r u p t s h ou ld oc c u r
e v e r y 2 5 6 u s
( a s s u m i n g a 4 m H z os c i lla t or ) . H ow e v e r , i f t h e c om p i le r h a s
t o w a i t u n t i l t h e c u r r e n t c om m a n d i s f i n i s h e d, i t m i g h t n ot h a v e t i m e t o
p r oc e s s t h e i n t e r r u p t a t t h e i n s t a n t TM RO r olle d ov e r
.
Th i n g s b e c om e t r i c k i e r i f a c h a n g e of s t a t e on t h e p or t p i n s i s t r i g g e r i n g
t h e i n t e r r u p t
. B y t h e t i m e t h e i n t e r r u p t h a n dle r h a s b e e n c a lle d, t h e e v e n t
t h a t t r i g g e r e d i t c ou ld h a v e a lr e a dy f i n i s h e d .
H ow e v e r , i t doe s h a v e i t s a dv a n t a g e s , e s p e c i a lly i f a n on t i m e -c r i t i c a l
i n t e r r u p t i s b e i n g i m p le m e n t e d, a s i t w i ll n ot s low dow n t h e P IC w h i le a
s e r i a l or p a u s e c om m a n d i s b e i n g u s e d
. Als o, i t doe s n ot r e q u i r e di f f e r e n t
c ode f or t h e v a r i ou s s i z e s of P IC
. W h i c h m e a n s t h e c ode p r odu c e d
s h ou ld w or k on a n y t y p e .
To u s e t h e ON INTE RRUP T c om m a n d w i t h a TM RO i n t e r r u p t , t h e s a m e
b i t s of INTCON a n d OP TION_RE G m u s t b e s e t or c le a r e d, a s i n t h e
p r e v i ou s di s c u s s i on
. H ow e v e r , i n s t e a d of u s i n g t h e INTH AND de f i n e t o
p oi n t t o t h e i n t e r r u p t h a n dli n g s u b r ou t i n e , t h e ON INTE RRUP T c om m a n d
i s u s e d : -
ON INTE RRUP T G OTO M y _In t
` P oi n t t o t h e i n t e r r u p t h a n dle r
Th e i n t e r r u p t h a n dle r i t s e lf a ls o di f f e r s f r om t h e a s s e m b le r t y p e
. Un li k e
h a r dw a r e i n t e r r u p t s , t h e c om p i le r ' s v e r s i on of a n i n t e r r u p t s i m p ly p la c e s
a c a ll t o t h e i n t e r r u p t h a n dle r b e f or e e a c h c om m a n d i s p r oc e s s e d
. Up on
e n t e r i n g t h e i n t e r r u p t s u b r ou t i n e , t h e s e c a lls m u s t b e di s a b le d
. Th i s i s
t h e j ob of t h e D ISAB LE c om m a n d
. D ISAB LE i s n ' t r e a lly a c om m a n d a t
a ll, i t i s a c t u a lly a di r e c t i v e t h a t i n f or m s t h e c om p i le r t o
di s a b le t h e
i n t e r r u p t f la g g i n g p r oc e s s
. It s e r v e s t h e s a m e p u r p os e a s c le a r i n g t h e
G IE b i t i n h a r dw a r e i n t e r r u p t s
. On t h e s a m e n ot e , t h e G IE b i t i s a c t u a lly
c le a r e d w h e n a c om p i le r i n t e r r u p t i s c a lle d
. Th i s i n t u r n di s a b le s
i n t e r r u p t s oc c u r r i n g w i t h i n i n t e r r u p t s .
Se c t i on -l0 - 1 7
ExperimentingwiththePicBasicProCompiler
UsingtheONINTERRUPTcommand
TheD IS A BL Edirectiv eshou ldb eplacedattheheadof theinterru pt
handlingsu b rou tine
: -
D IS A BL E
M y _ Int
( Interru pthandlerstartshere )
TheW , S TA TUS , andPCL A TH temporary storagev ariab lesdonotneed
tob edeclared, asthecompilerdoesthisf oru s, regardlessof thesiz eof
thePIC .
Thecodedif f ersonexitingtheinterru pthandleraswell
. The RETFIE
instru ctionisnotu sed ; insteaditisreplacedb y theRES UM Ecommand .
Thisdoesasimilarj ob astheassemb ler' sRETFIEinstru ctioninthatit
re- enab lesglob alinterru pts
. TheENA BL Edirectiv emu stb eissu edaf ter
theRES UM Ecommandtoinf ormthecompilertostartf lagging
the
commandsagain : -
( Interru pthandlerendshere)
RES UM E
ENA BL E
TheW , S TA TUS , andPCL A TH v alu esdonotneedtob erestoredas
they didintheassemb lerinterru pt ; thecompileralsodoesthisf oru s
.
Therearecertaingu idelinesthatshou ldb eadoptedwhen u singthe
compiler' sinterru pt, thatdon' tapply toanassemb lerty pe
. Becau sethe
compilermu stf inisheachcommandb ef oreprocessing
aninterru pt,
certaincommandsmu stb ere- arranged . Onesu chcommandisPA US E .
If adelay of 1 secondwerereq u ired, thenormalprocedu rewou ldb e : -
Pau se1 0 0 0
Bu tthiswillcau sethePICtowait1 0 0 0 msb ef oreitcan
processits
interru pthandler .
S ection- 1 0 - 1 8
ExperimentingwiththePicBasicProCompiler
UsingtheONINTERRUPTcommand
A b ettersolu tionwou ldb etob reak u pthedelay intosmalleramou nts : -
ForX=0to10000
Pau seu s100
Next
Thiswillgiv eu sthesame1seconddelay andallowtheinterru pthandler
tob ecalledregu larly . Thesamemethodshou ldb eadoptedwhenu sing
themorecomplexcommands, su chasS EROUT, S ERIN, PUL S INetc,
asalotof thesecommandsdisab leinterru ptswhilethey arework ing .
Inthecaseof S EROUToroneof itsrelativ es, insteadof sendingdataall
inonecommand, splititintosev eralS EROUTcommands . W henu sing
S ERINty pecommands, alway splaceatime- ou tv alu ewithinthem,
shorterthantheinterru pt' sinterv altime . Otherwisenointerru ptwilloccu r
whilethePICiswaitingf ortheserialdatatoarriv e
.
Thedemonstrationprogram INTCL K . BA S hasexactly thesame
f u nction astheassemb ler program, TMROCL K . BA S , inthatit
implementsaclock display ingthetimeonaserialL CD . Inf act, themain
b ody of thecodeisidentical ; only writteninBA S IC . Themaindif f erences
aretheD IS A BL E, ENA BL E, andRES UMEcommandsu sedwithinthe
handler . A ndtheu seof theONINTERRUPTcommandasopposedto
theINTH A ND def ine
.
W hilestu dy ingb oththehardwareandthecompiler' s interru pts, y ou
shou ldseeapatternemergingconcerningtheINTCONregister
. Control
b itsthatendwithan ' E' , su chasTOIE, enab leordisab leaninterru pt .
W hilethosethatendwithan ' F' , su chasTOIF, inf ormthePICasto
whetheranev enthasoccu rredornot . Thisf u ndamentalpattern holds
tru ef orallotherinterru ptregistersaswell .
S ection- l0- 1 9
ExperimentinwittePicBasicProCompiler
S ection- 1 1
PowerinuptePIC
Gettintemostoutofbatteries .
TeperfectPower- up .
ExperimentingwiththePicBasicProCompiler
G ettingthemostou tof b atteries
Battery powerisnecessary whend esigning portab leproj ects, b u t
b atterieshav eatend ency tod ecreaseinv oltageasthey age
. Besid es,
whoev erheard of af iv ev oltb attery ?
PlacingthreeA A or A A A
cellsinserieswillprov id eonly 4 . 5 V (3. 6V f or
nicad s) ,
whichwillcau seprob lemsf ormostPI Cs . A nd u singf ou rcells
willprod u ce1 V toomany , cau singthePI Ctogenerateheat
. W hatis
req u ired isameansof prod u cingthecorrectv oltageataconstantrate
throu ghou ttheb attery ' slif etime
. Entertheswitchmod econv erter .
U ntilrecently switchmod econv erterswerenotf orthef ainthearted
. Bu t
nowav astarray of of f theshelf d ev icesareread ily av ailab le
. Maxim
seemstob ethemostprolif icd esignerof thesed ev ices, withallshapes
and v oltagesav ailab le
.
T hed ev iceweshalllook atf irstisMaxim' sMA X 7 7 7 step- u pconv erter . I t
canprov id eanou tpu tv oltageof 5 V f romaninpu taslowas1
. 5 V , and
ou tpu tcu rrentsinexcessof 2 0 0 mA arepossib le (only witha4
. 5 V inpu t) .
H ighspeed switchingallowstheu seof smallind u ctorsand d ecou pling
capacitors
. I td rawsonly 1 9 0 u A of q u iescentcu rrentwhenoperatingand
anamaz ing2 0 u A whend isab led , which
mak esitid ealf orb attery
operation .
F igu re1 1
. 1 showsaty picalapplicationcircu itf orprov id ing5 V f roma
4 . 5 V sou rce (three
A A orA A A cells) .
I N

LX
I LM
MA X 7 7 7
OU T
SH U N
PG ND A G ND
v
R1
330 k
Section- l 1 - 1
F igu re1 1 . 1
. MA X 7 7 7 , 5 V oltswitchmod econv erter .
W hentheSH DNpinispu lled highthechipisenab led
. R1 ensu resthat
SH DNispu lled lowwhentheon/ of f switchisopen
.
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
Ge t t i n g t h e m o s t o ut o f ba t t e r i e s
Th e n e x t s w i t c h m o de de vi c e w e s h a l l l o o k a t i s Ma x i m 's MAX761
. Th i s
i s c a p a bl e o f p r o duc i n g a va r i a bl e o ut p ut vo l t a g e be t w e e n 5V a n d 16
. 5V
fr o m a n i n p ut vo l t a g e o f 4
. 75V t o 12 V, p r o vi de d t h e i n p ut vo l t a g e i s l e s s
t h a n t h e r e qui r e d o ut p ut vo l t a g e
. Th e MAX761 i s c a p a bl e o f p r o duc i n g
a n o ut p ut c ur r e n t i n e x c e s s o f 150m A
. I f t h a t w a s n 't e n o ug h , t h e de vi c e
a l s o h a s a n o n -bo a r d l o w vo l t a g e de t e c t o r .
Fi g ur e 11
. 2 s h o w s a c i r c ui t t o p r o vi de a 5V o ut p ut us i n g a 4 . 5V i n p ut .
Fi g ur e 11 . 2
. MAX761, 5 Vo l t s w i t c h m o de c o n ve r t e r .
Re s i s t o r s R1 a n d R2 s e t t h e a p p r o p r i a t e o ut p ut vo l t a g e
. Th e r e s i s t o r s
a r e c a l c ul a t e d us i n g t h e fo r m ul a : -
R2 = R1 * ( ( Vo ut / 1 . 5) - 1) )
Th e va l ue o f R2 c a n be a n yw h e r e be t w e e n 1OkQ a n d 2 50kQ, r e m e m be r ,
t h e h i g h e r t h e va l ue o f t h e s e t w o r e s i s t o r s , t h e l o w e r t h e c ur r e n t l o s s
t h r o ug h t h e m .
Th e va l ue o f t h e i n duc t o r ( 0) m us t a l s o be c a l c ul a t e d fo r di ffe r e n t i n p ut
vo l t a g e s
. Th e fo r m ul a fo r t h i s i s : -
L( uH) = 5 "Vi n
Th e di o de D1 m us t be a h i g h s p e e d Sc h o t t ky r e c t i fi e r
. A n o r m a l 1 N4001
w i l l n o t w o r k a s a r e p l a c e m e n t a s i t i s n o t c a p a bl e o f o p e r a t i n g a t t h e
r e qui r e d h i g h fr e que n c i e s
.
B y c h a n g i n g t h e va l ue o f R1, R2 a n d L1, h i g h e r o ut p ut vo l t a g e s c a n be
a c h i e ve d . Fi g ur e 11
. 3 s h o w s c i r c ui t fo r p r o duc i n g 9V fr o m fo ur AAA o r
AA c e l l s ( 6V) .
Se c t i o n -11 - 2
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
Ge t t i n g t h e m o s t o ut o f ba t t e r i e s
Fi g ur e 11 . 3
. MAX761, 9 Vo l t s w i t c h m o de c o n ve r t e r
.
B a t t e r y m o n i t o r i n g i s a c h i e ve d by a ddi n g t w o r e s i s t o r s a n d a n i n di c a t i n g
LE D
. Fi g ur e 11 . 4 s h o w s a c i r c ui t t h a t p r o duc e s 5V fr o m a t h r e e AAA o r
AA c e l l s a n d i l l um i n a t e s t h e LE D w h e n t h e vo l t a g e fr o m t h e s e dr o p s
be l o w 3 V .
Fi g ur e 11 . 4
. 5 Vo l t o ut p ut w i t h ba t t e r y m o n i t o r i n g .
Re s i s t o r s R4 a n d R5 s e t t h e t r i p vo l t a g e
. Th e y a r e c a l c ul a t e d us i n g t h e
fo r m ul a : -
R4 = R5 " ( ( Vt r i p - 1 . 5) / 1 . 5)
R5 m us t h a ve a r e s i s t a n c e be t w e e n 10kQ a n d 500kQ
. Th e LB O p i n c o ul d
a l s o be c o n n e c t e d t o o n e o f t h e P IC 's p i n s , i n di c a t i n g t h a t a p o s s i bl e
s h ut do w n i s i m m i n e n t .
S e c t i o n - 11 - 3
ExperimentingwiththePicBasicProCompiler
S ection- 1 1 - 4
Gettingthemostoutofbatteries
T ouseabattery suchasthePP3 9 V ty petosupply 5 V , aregulatorsuch
asthe7 8 X X seriesarenormally employ ed tored ucethev oltage .
H owev er, thesety pesofregulatorsare asinefficientasthey are
inexpensiv e
. T hev oltageI N / O U T d ifferenceiswasted asheat .
A moreefficientmethod usesswitchmod etechniq uesto red ucethe
v oltage . F igure1 1
. 5 showssuchacircuitforprod ucing5 V from a9 V
battery withcurrentsupto4 5 0 mA av ailable . U singtheM A X I M d ev ice
M A X 7 3 8 A .
F igure1 1
. 5
. S tepd ownswitchmod econv erter .
A sintheprev iousswitchconv erters, therectifierD 1 mustbeaS chottk y
ty pe .
U singtheabov ecircuitswillextractthelastd ropsofenergy from
expensiv ebatteries, withupto9 6 % efficiency .
ExperimentingwiththePicBasicProCompiler
T heperf ectPower- u p .
A lthou ghmostPI Cshav eab u iltinpower- u ptimer
(PWRT ) of 7 2 ms,
whichissu pposed toprev entthemf romnotstartingu pif the
power
su pply tak estolongtostab ilise
. S ometimesitisnotenou ghof ad elay
and thePI Cneed stob emanu ally reset
. T hemid - rangePI Cssu chas
thenew1 6 F 8 7 6 hav ead d itionalb rownou tprotectioncircu its b u iltin
whichhelpov ercometheinad eq u aciesof thePWRT
.
T oensu rethatthePI Calway sstarts, anexternalb rownou td ev ice
is
req u ired
. T hesemonitorthesu pply v oltageu ntilthereq u ired threshold is
reached thenreleasetheM CL Rline .
O nesu chd ev iceistheD allassemicond u ctorsD S 1 8 1 0
. T hisisasimple
and inexpensiv e3 - pind ev icethatlook slik eaT 0 9 2 transistor . T he
M CL Rpinisheld lowu ntilasu pply v oltageof approximately
4 V is
reached
. A twhichtimetheD S 1 8 1 0 d elay sf oraf u rther1 5 0 msb ef ore
b ringingitsRS T pinhighand releasingM CL R
.
F igu re1 1
. 5 illu strateshowextremely simple
thesed ev icesareto
connecttothePI C .
+5 V
D S 1 8 1 0
BottomV iew
1 2 3
PintRS T
PI n2 V CC
Pin3 GND
cc
RS T
GND
GND
F igu re1 1
. 5 . D S 1 8 1 0 Brownou tcircu it .
T heD S 1 8 1 0 alsoresetsthePI Cif thev oltaged ropsb elowapprox4 V ,
thu seliminatingany errorsthatmightoccu rwithinthePI Csmemory d u e
tolowv oltage .
S ection- 1 1 - 5
E x p e r i m e n t i n ƈ w i t ƈ t ƈ e P i c B a s i c P r o C o m p i l e r
Ap p e n di x
ExperimentingwiththePicBasicProCompiler
Componentsuppliers
.
A llthecomponentsused withinthisb ook areav ailab lef romCrownhill
A ssociates
http :llwww . crownhill . co . uk
I ntheunlik ely ev entthatCrownhilld oesnothav etheitem/ sinstock , the
f ollowingsuppliersmay b eab letoassist
:-
FA RNELL.

http :/ / www . f arnell . co m
M A PLI NElectronics . h ttp :/ / www . maplin . co . uk
RSComponents .

http :/ / www . rswww . co m
T hePicBasicProCompilerand it' supgrad esmay alsob epurchased
f romCrownhillA ssociates, picb asicweb site .
h ttp :/ / www . picb asic
. co . uk .
O rd irectly f rommicroEngineering, Lab sI nc .
h ttp :/ / www . melab s
. co m.
T hank salsotoCrownhill, thereisnowaPicBasicemaillist .
T hislistallowsPicBasicand PicBasicProCompilerownerstocompare
notesand shareprogrammingtipswitheachother .
T oad d y ouremailad d resstothelistsend amessageto
:-
majord omo@qunos. net
I nthemessageb od y enter
:-
sub scrib ePI CBA SI C- L
T hiswillthenreply withamessagetov erif y y ouremailad d ressand ask
y outoreply
. O ncethisisd one, messagesmay b esentto :-
picb asic- I @qunos
. net
Devicepinouts .
AO

VCC

CS `JVCC
A1

WP

SO

HOLD
a A2 SCL
WP SCLK
VSS

SDA

V55

51
MAX127
E x per im enting w ith th ePicB a sicPr oCom pil er
23123
1 . . VIN
1 . . RST
2 . . GND2
. . VCC
3. . VOUT

3. . GND
III
123
GNO
2 . . V0
3. . VDD
76L05

DS1810

D31820

LM35

TLE 2425
III
123
1 . . GND
2 . . V0UT
3. . SVS
III
123
1 . . VIN
2 . . GND
3VOUT
TLOB 21
LMC662
ILIM~JN . C .
L293D
IN

SHDN
SAND
OUT
PGND

L%
MAX777
0AGNh y,
81 M
0B 2

A
W
0
A2
02
AGND D1
0AGND4 8302
U04

A3
93
MA4
ID
0W4 AGND I0
0DGD ADD 93
M IDSHUN

IRS 91
6] UI CS

CL
m
01
03SDI

500
02
AD6403
63
0
E NAALE IVSS M
M 0 IN1

14 01
0 OUTI

OUT4
0
0 GD

GND
E U
M 0
GD

GND 10
0
0012

CUTS
M
0
u
VS

E NAB LE 2
NS9]
u
24C32
25LC640
93C66
V"
-SCLK0
La o`''V, OUT'-ADD
CS

GND
DIN

RE F
SCLK

FE
AIN
SHDN
RE F
CS0
ROUT 17
AND 0
LB I

L%
FB

GND
SHDN

RE F
MAX187

MAX5352
MAX761
1S JV
0
B I~J
AI AGNb'B I
CAP,

OSC U

e
GND

WI
B 2

AI
1eDOr
GNO,

L
O

a CS

VDD A2

WI
CAP VOUT 0
SDI

CLIK W2

VOID
ICL7660

AD6400 DGND

RS
C. M

0
SHDN

CLK
CS

SDI
DIN uDOU
0v
AD8402
0V
.

RE F D1

0
DIG-D

SE G-D M
0
DGNDRE FAOJ
0
DIG-4SE GOP D3
CHO

VD .
0
C

N . C .
81

0 AND

B E GE
O
31
0
SCL

N . C
E D

0
DG5 SE AC
0
31
CHI

SCL
0AD

CH7 0

0
DIG

V
0
M
CH2

CS
0
C

N
. C 03

U DIG.

ISE T
0
0
CH

DIN
0
C

SSTRB
0C

CHE
Dl

0
DIG-7

SE G-GM
0
D3
CH

GOUT
M

n GND

B E GB
0
SDA

CH:
A2

CH4
91

ID DIG5

B E GF
0
[a
CHO

DGND
m NC

CH397

M
DIG-1

SE G-A
0
CH7

AGND
91Al

CH2
DI
M
VSS

RE FAD
SHDN

VHE F
M

13LOAD

CL
67SHDN

CH
i
J9

MAX7219
MAX186
10
ANNE

CHO
10
1SD1416
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
De vi c e p i n o ut s (c o n t i n ue d)
0
RA2 RAI
03 B I
C LR/VP P /T1V RB 7IP GD
E U

0
MC LR/VP P /T1 R87/P GD
m
RA3 RAO
87 DI
RAO/ANO RB 6/P GC ASIANS RB 6/P 0C E U
RA4/RTC C OSC IIC LKIN m DI RAVANI RE
M
RA11ANI
RB
6)
0 C LR OSC 2/C LKOU m 0 RA21AN21Vr e f . RB 4 m

0
RA2/AN2IVr e 1- RB 4
0 VSS
VDD
03 DI
RA2IAN31Vr e I, RB 3IP GM
B I
RA2/AN3/Vr e l + RB 3IP GM
8]
RB O/INT RB 7
63 0
RA4/TOC KI AB 2
E ` ]

u
RA41TOC KI RB 2
D1
0
RB 1 RB 8
03 IB
RA5IAN41SS P 81
83

0 P A51AN41SS RE
a
RB 2
DI
RB 3
RE
RB 4
UI
87
u
Vs s
DI OSC VC LKIN
RB O/INT
VOID
a
6T

0
RE OIRD/AN5
RE I/WR/A N6
RB OIINT
ASS
m
P IC 16F84
66 OSC 21C LKOUT ASS M

B 3
AE 2IC S/AN7 ASS
61
ROOIT1060ITl C KI RC 7/RAIDT
83

E U
VOID RD7IP SP m
RC 1IT10S11C C P 2 P C B /TX/C K B 7 ISIS
AS61P SP B
63
-'
C C D
m 0
AO
RC 21C C P I RC S/SOC
ID

67
OSC IIC LKIN RD51P SP 5
m
0 Al RE C
m
RC 319C KISC L RC 41SDI/SDA m
AB C 2IC LKOUT AD41P SP S
m
A2
0
A3
XC LK
RE C LE D 51
P IC 16FO7316 m RC O/TIOSO/TIC KI RC 71RX/DT
63
RC I/TIOSIIC C P 2 RC 6ITX/C
87
DI A
u
A5
P LAYE
P LAVL
91
91
B C 547/9
B I
TIP 31132
AC 2/C C P 1

RC 5/S SS
0 NC NC
63
RC IISC K/S C L

RC IISSIISDA
D)
0 NC ANA OU E U
1 01
E U
RDII P E P S

RD31P SP 3
9 RDIIP SP I

A221P OP 2
u AN
ANA IN
E U
III
P IC 16F874 / 7
DJ A7 AGC M
M
m
C B E
III
B C E
m
C .

MIC RE F
63
ADDS

MIC
VSSA

VC C A
M 3P ,

SP .
m
m
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o
C o m p i l e r
C DROM C o n t e n t s
.
Th e s o ur c e c o de fo r t h e p r o g r a m de m o n s t r a t i o n s us e d i n t h e bo o k m a y
be fo un d i n t h e SOURC E di r e c t o r y
. E a c h s e c t i o n h a s i t s o w n s ub-
di r e c t o r y, a n d e a c h e x p e r i m e n t h a s fur t h e r s ub-di r e c t o r i e s
.
Fo r e x a m p l e . To fi n d t h e MAX
_C NT . B AS p r o g r a m fr o m Se c t i o n -1,
In t e r fa c i n g w i t h t h e MAX7219 . Op e n t h e
SOURC E di r e c t o r y, t h e n t h e
DISP LAYS
di r e c t o r y a n d t h e p r o g r a m w i l l be fo un d i n t h e
MAX7219
di r e c t o r y .
Th e Se m i c o n duc t o r da t a s h e e t s fo r t h e de vi c e s us e d t h r o ug h o ut t h e bo o k
m a y be fo un d i n t h e DATASHE E TS di r e c t o r y
. E a c h t yp e o f de vi c e i s
s e p a r a t e d i n t o t h e i r o w n c a t e g o r y by t h e us e o f s ub-di r e c t o r i e s .
Fur t h e r a p p l i c a t i o n n o t e s fo r va r i o us r e l a t e d de vi c e s m a y be fo un d i n t h e
E XTRAS
di r e c t o r y .
Ag a i n , I t h a n k yo u fo r p ur c h a s i n g t h i s bo o k .
Re m e m be r t o l o o k o ut fo r fur t h e r Sup p l e m e n t s a n d P r o je c t s o n t h e
Ro s e t t a Te c h n o l o g i e s w e b s i t e : -
h t t p : //w w w
. r o s e t t a -t e c h n o l o g i e s . c o . u
k
Al t e r n a t i ve l y, c o n t a c t m e di r e c t l y o n
r
o s e t t a @t e c h n o l o g i e s . fs bus i n e s s . c o . u k
E x p e r i m e n t i n g w i t h t h e P i c B a s i c P r o C o m p i l e r
I n a s s o c i a t i o n w i t h :
Cro w n h i ll As s o c i a t es Lt d
h t t p : //w w w . c ro w n h i l l . c o . u k
h t t p : //w w w . p i c b a s i c . c o
. u k
Th e PI CBASI C Us er Gro u p
h t t p : //w w w . o i c b a s i c . o r
g
Ro s et t a Tec h n o lo g i es