Page 223 - Making PIC Microcontroller Instruments and Controllers
P. 223
22o cREATING accuBATE |xTEFn LS WITH TIMEhS: IHE lf,EtFOllOltES
of l6 aod increase the number oi intenupts received by a faclor of 16 (256116).
This will
be enough to do the job- Increasing thc number of intelTupts by a lactor of 1 6 means we
need !o look at 16 times more counts on the X counter before we loggle the speaker.
The program fora metronome using Tinero and the LAB-Xl is shown in Program
16.3.
,:rPiii0i:iii:ili:3-:l: Working metronome based on Timero and the LAB-X1
CIJEAR ; always sLart with cLear
DEFINE oSC 4 : de6ne oscillaLor speed
DEFIIIE LCD_DREG PORTD , deine LCD condeclions
DEFINE LCD_DBIf 4 :4 biL palh
DEFINE LCD_RSREG PORTE ; SEIECI TEg
DEFINE LD_RSBlt 0 ; selecL bit
DEFINE LCD_EREG PORTE ; enable TegjisteI
DEFIIiIE LCD,EBIT 1 ; enable bit
DEFINE aDC_BITS a ; set nunrber of bits in result
DEFTNE a.Dc_crJocK 3 ; ser clock source (l=rc)
DEFTNE a.Dc sAltPl,Eus 50 i .6r sa pl:ng e us
LOt{ PoRTE.2 r nake Lov fo! wrile only
Se! the port directions. l"ie nust set all of pORTD and all of
PORT'E as ouLputs
ewen though PORTE has onLy I 1ines. The othei 5 lines riLL
be iqnored by lhe systen.
p o o t c c 1 6 a d 6 d p 6 o - D - o t 4 s o ' p - n 2 o - i s o o '
tRISc = %11111011 set PORTC,2 to speaker comectron
ERISD - %00000000 ranes ro ourpuc
ERISE = %00000000 ser aLl PORTE Lines to output
X VAR VIORD set up the variabte
PO:TPOS VAR I{ORD
A2D 1/ VAR BYTE create A2D Value to store resuLt
BDCONl=%00000111 set ihe AnaLo€r !o Digital
control fegister
needed for the 16F8?7A see noLes
PAUSE 500 pause for LCD to start uP
rcDoua gFE. 1
ON INIIERRUPT GOTO IWE ROUTIIiIE Le11s progran where !o go on
INICON.5=1 sets us Lhe inLerrupt enable
INICON.2=0 clears the inteuupt flag so rt
sets Lhe initial value for x
MAIIiI: ihe nain loop of the progran
A.DCIN O, A2D V
0 000011
OPTION-REG=%0 prescalar is 16