Page 111 - Making PIC Microcontroller Instruments and Controllers
P. 111
rlMERl: tHE sEeO{! l!UE8 lql
earlier, so it is set about two times a second. Lct's write a shot Timerl program that is
similal to the original Timero blinl(er program lo sec how the differcnces shape up.
Program 6.5 blinks the LEDS at D0 and Dl ON and OFF aftemately as the foreground
pal1 of the progam. Thc intenupts generated by Timerl are used to blink D3 ON and OFF
at half-second intervals. Sinc€ thc control of D3 is driven by the inlenupt, the timing stays
accurate. Any time used by the intenupt routine is lost by the foregound task and affects
the overall frequency of the D0/D 1 blink rate.lt is important lo understand this loss.
ttPrOltiaii:lqtt UsingTimero (Programs blinks lwo LEDS aternately and blnks a lhird
a half second ON and a hal second OFF)
LED approximalely
CIrE]AR
DEFINE OSC 4
TRISD = %00000000 11nes Lo oucpu!
TRISE - %00000000 l1nes co olrpuE
se! lhe A to D control reqister for
digital porLs D, E
ADcoNl=%00000111 needed, 15F877A because it
has analog properLres
TlcoN = %00000001 turn on Tinero, prescalar = 1
rNacoN = %11000000 . . : . 1 . ' d ]
I VAR WORD counLer wariable
iI VAR I'ORD counter variable
PAVSE 500
set counlers to 0
P I E 1 . 0 = 1 enable TMR1 overflow interrupL
ON IMIERRUPT GOTO INIHANDIJER ;
PORTD=0 ; turn off the entire port
PoRID.3 = 0 i liq' d or b"rqt-ph o eoao . olro.a .nn r
poRTD.2 = 0 , ,rg' - d or b"rqr-ph o -ep6d-. obo\a _4. I
MAINIOOP !
Il'POR!D.I = 0 TIIEN ; routine lights Do and Dl alLernately to
PoRTD.l = 1 ; thar the proqrd is iuminq the min routine
POR!D.0 = 0
EIJSE
PORTD.I = 0
PORTD,o = I
ENDIF
' ' -
i
F O R l = 1 T O 3 0 0 i r l s i r o f o l o n s p a r p 6 . , o n
PAusEus 100 ; so that interrupt is nob conpronised
NEX!! I
GOTO MAITiEOOP , do it all folever
DISAII.E
(Co inu.d)