Page 246 - Making PIC Microcontroller Instruments and Controllers
P. 246
244 U DEnSt llDlNG THE COUNTEBS: COUI{T|IG illRBlES
Counting to a Register Using
an Intenupt
Whcn the marbles are comi ng fast and furious, we crnnot stop them one at a time and count
them as we did in the previous program. We need a faster way of doing thejob. Onc way
is to have each marble causc an intcmrpt and then in the intenxpt irutine incremeni the
variable that has the marble counl in il. (Kccping in nind that the largestvddable in PBP
is 16 bits or 65,536, unless we make anangemcnts !o count to a larger number by keep-
ing track ofthe overflows at 65,536.)
The program keeps counting till the ma$]es run oul. Reset takes you back up to the
top. This process willbefast enough for most counting, but there arc instances when it
will nol a topic we will discuss later in this chapter.
The Iront end of Program l7.3 is very simil ar to the top of lhe preceding escapenen!
program. The changes are in how we do the countjng.
lt.?It.ifAi!:!t!,:.9,t: rhis prosram is not executable (Prosram nol for use n real
only)
counling as it is now;Jor discussion and examinalion
The top of lhc program now looks like lbrs:
CLEAR ; always start with cLear variables
DEFINE OSC 4 i alFays deflne osc speed
DEFINE l,cD DREG PoRID ; def,ne LcD conneclions
DEFINE tCD DBII 4 ; and bits
DEFINE I,CD RSREG PORTE
DEFINE ITCD_RSBIT O ;
DEFINE I,CD EREG PORTE
DE!'NE I,CD EBI4 T i
LOW pORlEE.2 ; 1or mean we riLL vrlte onLy
DEFINE LCD_CoMMANDUS 2000 , delay in nicro seconds
DEFITiIE IJCD DATAUS 50 ; delay in nicro seconds
DEFTNE ADc BIES 8 ; set number of biis in resuLt
DEFTNE ADC CLOCK 3 ; set intetnaL clock source (3=rc)
DEFTNE ADc saMPLEus 50 ; set sampling time in us
, \ 6 x w 6 s 6 o l _ , l o p o r o h - r c q L i e d d r . - i o . .
; And set Lheix bits hi€rh and ]ou
TRIsB=%11110000 ; set port directions and values
PORTB=%o0001110
TRIsc-%00000000
PORTC =?00000000
rRIsD=%o0000000
PORTD-%o0000001
' _he rrne