Page 121 - Making PIC Microcontroller Instruments and Controllers
P. 121
cout{TERs ttl
il:P,lliiiAfi:gtaal Tlmerl as counter (Timerl counts sisnats from a motor€ncoder)
(Continued)
TlCON.o = 0 ; stop 16-bit Tiner
TDIRI. BytEo = T!'R1L ; read Lor 8 biLs
TURI.EyTEI - TMRUr ; read High 8 bits
$lRl = tDlRl - 11 ; caplrre Correction
IF IMRX = 65525 ![EEN ![OSIG![AL ; see PICBASTC pRO manuat for
LCDOuT SFE, S80, DECs A!tR1, z COIINTS' a.ad-rcr o:splal
PAUSE XO
GO/rO LOOP
NOSIGNAI,3
\No "
rcDour $FE, $80, TGTiIAI ;
GC|IO LOOP
EIiID
PRESCALARS AND POSTSCALARS
Prescalars and postscalars can be confusing for the beginner. Here is a simple
A prescalar is applied to the system clock and affects the timer by slowing down the
system clock as it applies to the timer. Normally, the timer is fed by a fourth of the basic
clock frequency, which is called Fosc/4.In a systcmrunning a 4 MHz clock, rhe timer
sees a clock running at I MHz. If the prescalar is set for I :8, the clock will be slowed
down by another eight times, and the timer will see a clock at t25 KHz. Refer to the
diagram on the bottom half of page 52 (for Timerl) in the darasheet to see how this
applies to Timerl.
Apostscalar is applied after the timer count exceeds its ma\imum value, generating
an overllow condition. The postscalar sening detemines how many over|ows will go
by betbrc an interupt is triggered. If the postscalar is set for 1:16, rhe tjmer will over-
llow 16 times before an jntenupt flag is set. The upper diagram on page 55 (fbr Timer2)
of the datasheet shows this in its diagrammatic form and is worth studying.
All othet things beinq equdl,bothpre and pastscala$ arc usedtohcrease the ti e
bet,reen intertupts.
When starting out,just leave the scalars at l:l values and nothing will be afiected.
We will nol need to use them lbr any ofrhe experiments we will be doing. Once you
get more sophisticated in the use oftimers, you can play with rhe values and leam more
about how to use them. The primaiy use is in creating accurate timing intervals for com-
munications and so on, because no extemal routiDes are necessary when this is done
with scalars. w}len doing it this way, everyrhing becomes internal ro the pIC and is there-
iore not affected by extemal disturbances.
Additional infomation on timer modules is available in he Plcmicro Mi.l-Ra pe
MCU Fanib Re|brence Ma"dl (DS33023).