Page 220 - Making PIC Microcontroller Instruments and Controllers
P. 220
fli|Eno 2 1 7
ilL!!te!]i!,i!!!iaif Basic interrupt routinetotTime'1. (continuect,
INECON.2=0 clears the interrupt flag so iL
OPTION REG=?000001L1 ; sels the prescalar to 256
x=0 secs the iniLial walue for x
LCDOu*I SFE, Se 0, "Mel!ond6" ; display frrst line
I4AIN:
r,cDouE $FE. sco, DEcs x write x to line 2
IF X>=15 THEN check vaLue of x
rese! bhe walue
TO@IiE PoRTC . 2 toggle the speaker so re ca hear it
TOGGIJE PORTC.l
EIIDTF end of testinq x
GCITO UAIN
DISASI;E reqd instrucrion, Lo the conpiler
IIiI:[-ROlxTINE ! interrup! service routine
Increnent the counter
INTCODI.2=0 Clear the interrupt flag
RESU!dE Go back to where you were
EAIABIJE reqd instruction, to the conpiLer
END aLl programs nust end wiLh End
This program toggles line C.2 each time ttfough its cyc]e and gives us a click about
once a second, or once every 15 internrpts, using a prcscalar of256. This means that the
Llstest interrupts we could get with Timero would come 256 x 15 fastet or at about 3840
intenupts per second if we did not change the clock rate or try to load the timer any other
way. The number 3480 is good to remernber when using Timero because it defnes the
empirical m&dmum interupt rate for us.
=
r 4,000,000/4/256 3,906.25 (the theoretical value for a4 MHz processor)
=
t 3906.251256 15 .26 (used 15 in the preceding program since i t is the closest integer)
| 15+256 = 3480 (as mentioned earliet not an exact value)
The prescalar value we use has an impoftant effect on the length ofthe intenupt
service routine. lf we are using a small value for the prescalar, rhe tine available
between interrupts becomes very small and the monent may come when all the time
available is used up se icing the interrupt routine, leaving no time to do the fore
ground task. We will investigate this by setting the prescalar with the first poten
tiometer on the LAB-X1 board and loading its value into the prescalar in real time
to see what happens. To do this, we need the following code added to the program.
It consists ofreading lhe potentiometer and then placing the rcad value into t]le ibur
prescalar bits.
The code for reading POT I is shown in Program 16.2.