PROCESSOR 12F509
__config _MCLRE_OFF & _CP_OFF &_WDT_OFF & _LP_OSC
list p=12f509
#include p12f509.inc
;
#define BUZZ GPIO,1 ; piezo buzzer pin
#define LDR GPIO,0 ; LDR/cap pin
org 0
goto start
count equ 08 ; how many highs when cap is discharging
dcount equ 09 ; delay counter
lcount equ 0a ; loop counter to read LDR
scount equ 0b ; loop counter to read LDR
cntmsec equ 0c
cntmsec1 equ 0d
.code
start
top
movlw b'00000000' ; set buzzer and LDR as output
tris GPIO ;
bcf BUZZ ; kill buzzer
bcf LDR ; ground out LDR/cap
movlw d'100' ; for 100 msec
call mdelay
movlw b'00000001' ; flip LDR for input
tris GPIO ;
clrf count ; zero counters, count = number of highs from RC, lcount is passes to check
movlw d'255' ;do 255 checks
movwf lcount
loop
btfsc LDR ; if bit is high
incf count,f ; increment count
decfsz lcount,f ; decrement loop counter
goto loop ; if loop counter <> 0 loop
movlw d'120'
subwf count,f
btfsc count,7 ; test bit 7 if more than 128 highs
goto buzz ; if true go buzz
nap
movlw d'84'
movwf scount
nloop
movlw d'255'
call mdelay
decfsz scount,f
goto nloop
goto top
buzz
movlw h'a0'
movwf lcount
noise
bsf BUZZ ; set piezo high
call msecdelay ; pause
bcf BUZZ ; clr piezo
call msecdelay ; pause
decfsz lcount,f ; if lcount is <> 0
goto noise ; then more noise
goto nap ; else to nap
;
msecdelay movlw h'1' ; allow for 4 microsec overhead..
movwf cntmsec1
micro4 decfsz cntmsec1,1 ; subtract 1 from countmsec
GOTO micro4 ; more loops
retlw 0
mdelay movwf cntmsec ; W to msec count register
clrwdt
msecloop call msecdelay ; 248 * 4 + 2 = 994 here
decfsz cntmsec, f ; decrement count, skip when zero
goto msecloop ; more loops
retlw 0
;
end