Page 222 - Making PIC Microcontroller Instruments and Controllers
P. 222
flfiERo 2t0
iiRiffiHi,{l*l* Basic program to see the effecl of the prescalar value on Timero
operation (Cort nued)
{:['CON-5-1 ; sels uP the inierrup! enable bit
IlitTCON. 2 -0 ; ctears lhe interup! flag so it can
; sels lhe iniliat waLue for x
\!4€trononez;
LCDOIXf SFE, S80, display 6rst rine
MAIN: ; the nain loop of the progra
r.Dcr[I 0, a2D_v ; read channel 0 to A2D_value
OPTION_REG= (A2D_v | 32) ; set lhe oplion resisrer 1ow nibble
r,cDour sFE. 9c0, DEC3 A2D_v/32,n ; display value
GOTO MAIN , do i! asain
DISASIJE reqd hslruclion Lo the compiler
IMT_ROUTINE : in!errup! service rouiine
TOGGI,E PORTC.2 logg1e the por!
Increnen! lhe counter
fllllcolr.2=0 cLear the interrupt flag
RSSI'UE go back to where you qere
ENAB'JE reqd InstrucLion to the conpiler
END alL prosrans nust end with End
In Program I 6.2, the LCD shows us the value of X and the bits that have been set in
the OPIION REG register We have had to remove the comparison and resetting of the
X variable because it takes too much time and the toggling has been moved into the inter-
rupt se ic€ routine, so you can hear how often the intenupt is being called. Notice that
as the interupts become more Aequent, the incrementing of the value of X in the main
routine cannot keep up with the speed with which the interrupts are affiving. Even though
a minimal amount of work is being done in the intenxpt seNice routine, it is too much.
We can add to the time taken by the interrupt service routine by adding a PAUSE
in the routine. Play with addiog a PAUSE of between 1 and 25 ltsec in the routine to
see what happens. (Interrupts are missed and the toggling does not respond to the
changes made to the option register bits.)
Now that we have a feel for the problems involved, lefs lvrite the metronome pro
gm.rn. A standard metronome provides between 40 and 208 counts per minute. We are
going to use POT 0 to control the rate so the 256 values that can be read for the POT
must be mapped to the 168 rates (208 40) needed by the metronome, and each rate
must be accurate enough to serve everyday musical needs,
The formula for converting from 0 to 255 to from 40 to 208 is
'N
Ticks=40+[(208 40) POr0]/255
l-ooking back at 3480 as the maximum number of jnterrupts we can handle with ease,
and seeing that we need to genemte 168 different hequencies, we se€ that we can have
about 3480/168 = 20 interrupt counts sepamting each frcquency, so we know that they
can all be differentiated. If we set the option register to %0000001 I, we will get a prcscalar