Page 249 - Making PIC Microcontroller Instruments and Controllers
P. 249
COUIITII{G DIRECTLY IIITO AI{ II{TEFNAL COUNTER 247
Every time you press button I on the keypad, the counter in TMRo will increment
by L After it g€ts to 255, the next key press vr'ill show bit 2 on INTCON go to I for
0.25 seconds. Next the counter will reser to 250, and the bit at INTCON.2 will be
"PORTB
cleared.INTCON.O is the change" intefi\tpt flag. lt stays at 1 because we keep
pressing a key that affects PORTB.
The program that reflects that above is shown in Program 17.4.
Demonstration otthe use ofT|MR0 as a Counter
CI,EAa'
DEFINE OSC 4 j osc frequency
DEFINE LCD_DREG PORTD ; define the LCD comections
DEFINE IJCD_DBIT 4 ;
DEFINE LCD.RSREG PORTE ;
DEFINE LCD_RSBIT O ;
DEFINE IJCD_EREG PORTE i
DEFINE LCD_EBIT 1 ;
LOW PORTE.2
ERISA = ?00010000 ; set PORTA
TRISB = %11110000 ; sei PORTB
TRISD = %00000000 ; set PORTD
TRISE = %00000000 ; set PORTE
aDCONI= %00000U1 ; don't forget to set ADCON1
OPTION_REG=%01101000 : sels in - -.pL onqblc Io! 1l'4R0
@
r b+e+ € .e€ f,eleian€ beeaEse biE 3 is +
; bit3=1 sets prescalar to 1:1 or no scalingr
; bit4=0 sets hish to low Lraneition on count
; bit5=1 sets counter node usins A.4 as input
; bit5=0 sets interrupt edqe select
; bitT=o sets pulL ups on PortB
PAUSE 500 , pause ,500 second for LcD !o star! up
t CDOUI SFE. 1 , cfear LCD, 9ro to f,rst line
lMR0-2s0 ; put 250 into countet
[TcoN=ts00100000 : -l-a! -]l oI il.e--rpr -eq'st-'
; except biL 5
BAIN: ; nain loop
I,CDOUT SFE. S80. BlNg MR0, \=T r DEC3 Mlo'z=Mroz ;
rcDouE sFE. sco. atN8 tNTcoN
IF IMECON.2=I EHEN ; II Lh- -nLorrLp- l1a9 is se!
l"cDoul SFE. 580, BtN8 T!1R0, \=2, DEc3 tl[Ro ; show changes to
rcDOm lFE, $C0, BrN8 TNTCON bits" ; lhe two
; registers
rMR0=250 ; rese! coun! in TMRo
PAUSE 750 ; pause so you can see what happens
(ContinueA