Page 96 - Making PIC Microcontroller Instruments and Controllers
P. 96
A6 flMENSA D COUNTERS
The use of the PAUSEUS loop in the Prognm 6.1 Fovides a latercy of 100 microsec
jn
onds (worst case) the rcsponse to an jnlemrpt and eliminates most of the effect of
changing the OSC irequency if that should become necessary. It is better than nsing an
empty counter, which would be completely dependent of the frequency of the system
oscillator. (There is an assumption here that the 100 psec latency is completely tolera-
ble to the task at hand, and it is for this program. It may not be for your real world pro
gram though, so itmay need to be adjusted.)
We are tuming one LED OFF and another LED ON to Fovide a more positive fe€d-
back. As long as we are executing the main loop, the LCDS will light alternately and
prcvide a dynamic feedback of the operation of the program in the foreground loop.
we select a relatively fast ON OFF cycle so we will better be able to see minor
delays and glitches that may appear in the operation of the program as we proceed.
Run this Fogram to get familiar with the operation of the two LEDS. Adjusl the
counter (the 300 value) to suityour taste.
Next, we want 1o add the code that will interrupt this progmm periodically ard make
a third LED go ON and then OFF using an approximately one-second cycle. This will
seNe as the inlerrupt'ddven task we are interested in learning how to create. This is the
IMPORTANT task in this pa icular exercise.
Herc is what must be added to the plogmm to get the intenupt-dnven LED operational.
Enable Time0 and its inte[upts with appropriate register/bit settings.
Add the ON INTERRUPT command to tell the prc$am where to go when an inter-
Set up the inlerrupt routine to do what needs to be done.
The inteffupt routirc counts to 6 1 and turns the LED ON if it is OFF and OFF if it
is ON.
Clear the illterrupt flag that was set by Time .
Send the program back to where it was intenupted with the RESUME command.
WHY ARE WE USII{G 6I ?
The prescalar
is set to 64 Oits 0 to 2 are set at 101 h the OPTION REG).
,
The counter interupts every 256 counts
256 x 64 = 16 384
Clock is at4,000000 Hz
Fosc/4 is 1,000,000
1000000 / 16,384 = 61.0532.Its not exactly 61, but it is close enough tbr our pur-
The lines ofcode now look like those in kogram 6.2.