Page 248 - Making PIC Microcontroller Instruments and Controllers
P. 248
2.6 UlrDEnSrAr{D[tG tHE COU TEB$ COUI{lll{O MARBLES
ie!!&{-A!{itli1tr!1 This program is not executable (Prosram nol for use in real
counting as it is now;for discussion
and examination only) lcontinued)
TRIEB =?00001111 we have !o reassigm lhis here
OPTIOAI_REG =%01111111
+TcoN=%10010000
clear hierh byte
we do lhe countingr work here in
Lbis is an enpty Loop for now
GOTO IJOOP
IIIT_ROIIIINE ! show conLents of loop here
rcDour $FE, s80. DEC3 TMRIH.
RESUIIE
TND
This is an acceptable way of counting marbles, but we can do the job even fastet if
we use one of the timers in the counter mode. Wo vrill consider both Timer0 and Timerl
(Remember: Timer2 cannot be used as a counter because it has no input pin.)
Counting Directly into an Internal
Counter
Using 7i e,'0 (8 bit) to leam about counting to counters.
Timero is not suitable for counting the marbles, so we will write a short prcgram to
demonstrate its use as a counter We will set up a sitoation that will let us count how
many times we press butlon I on the LAB-XI keypad, stadIlg with 250 in the counter
Doing this lets us see the counter overllow alter a few buiton presses. We will also mon-
itor the contents of INTCON on the LCD to see what happens as the button is pressed
again and again. You need to master techniques like these to look at what is happening
in the PIC or olher processors your using, whenever necessary.
Set up the counter for Timero as follows:
oPIIoN-nrc=%01101000 , No prescalar and extexnal clocking
; on TocKl (bit 5), risins edge (bit 4)
INTCON=%00100000 ; EnabLe lnlefupts wilh bit5
TOCK I, which is ph 6 of the PIC, will be used as an input for the counter @ORIA 4)
Provide a jumper from D. I to A.4 so the result of the key press, which appears on D I ,
will be rcuted to the counter
The following is what you should see on the LCD: