;********************************************************
;* Capacitance measure with I2C serial interface *
;********************************************************
;* (C) by Jani Lappalainen 2015/10 *
;********************************************************
;* 100kHz is too fast, 32kHz works *
;* MUST delay between reads 0.5s when leads open *
;********************************************************
; 10u
; T---------||---------T
; L-- VSS 1T--+8 VDD --J r1 (optional)
; sda GP5 2| |7 GP0 ----T-----WWW----T
; scl GP4 3| |6 GP1 -||-J cref = cmea
; GP3 4L__J5 GP2 -----|<|--WWW----|
; led r2 gnd
; time constant t=RC=63%, 4t=98%
; for 300pf and 1k time is about 2us
; using 33n on pumppaamo
LIST P=12F509
RADIX dec
#include p12f509.inc
__config ( _IntRC_OSC & _WDT_OFF & _MCLRE_OFF & _CP_OFF )
#define scl 4
#define sda 5
#define deviceAddress (0x31*2)
charge macro
bsf GPIO,0 ; Täällä tarkkana, esim seuraavalla kellolla tuleva bsf GPIO,1
; lukee lathciin gpio:n tilan reaalimuilimasta!!!
movlw b'111010'
TRIS GPIO
nop
nop
nop
movlw b'111011'
TRIS GPIO
endm
flush macro
movlw b'111001'
TRIS GPIO
bcf GPIO,1
nop
nop
nop
movlw b'111011'
TRIS GPIO
endm
empty macro
movlw b'111000'
TRIS GPIO
bcf GPIO,0
bsf GPIO,1
goto $+1
goto $+1
goto $+1
goto $+1
movlw b'111011'
TRIS GPIO
endm
ledBlink macro x
btfss GPIO,x
goto $+3
bcf GPIO,x
goto $+2
bsf GPIO,x
endm
ledOn macro x
bcf GPIO,x
endm
ledOff macro x
bsf GPIO,x
endm
waitSCL0 macro
btfsc GPIO,scl
goto $-1
endm
waitSCL1 macro
btfss GPIO,scl
goto $-1
endm
wait macro x
movlw b'110000'
andwf GPIO,w
xorlw x
btfss STATUS,Z
goto $-4
endm
setBit macro x
rlf x,f
btfss STATUS,C
goto $+3
bsf GPIO,sda
goto $+2
bcf GPIO,sda
endm
getBit macro x
bcf STATUS,C
btfsc GPIO,sda
bsf STATUS,C
rlf x,f
endm
writeMode macro
movlw b'011011'
TRIS GPIO
endm
readMode macro
movlw b'111011'
TRIS GPIO
endm
;----------------------------------------------
; variables
UDATA 0x07
recBuf res 1
sendBuf res 2
counter res 1
lcounter res 1
ucounter res 1
tmp res 1
;--- start point ------------------------------
ORG 0X0000
movlw b'11000000' ; clear option to enable gp2 as out (bit 5)
OPTION
; ports to output (except 3, scl and 2 sda)
movlw b'111000'
TRIS GPIO
goto mainloop
; --- i2c -----------------
sleepa:
#include sleep.inc
readData:
movlw 8
movwf counter
waitBits:
waitSCL0
waitSCL1
getBit recBuf
decfsz counter,f
goto waitBits
retlw 0
writeData:
writeMode
movlw 8
movwf counter
writeDataBits:
waitSCL0
setBit INDF
waitSCL1
decfsz counter,f
goto writeDataBits
waitSCL0
readMode
retlw 0
nack:
waitSCL0
bsf GPIO,sda
writeMode
waitSCL1
waitSCL0
readMode
retlw 0
ack:
waitSCL0
bcf GPIO,sda
writeMode
waitSCL1
waitSCL0
readMode
retlw 0
waitStart:
wait(b'110000')
btfsc GPIO,sda
goto $-1
btfss GPIO,scl
goto waitStart
retlw 0
; --- Main loop -----------------
mainloop:
ledOff(2)
; measure capacitance
empty
empty
empty
empty
empty
clrf sendBuf
clrf sendBuf+1
measure:
charge
flush
movlw b'111001'
TRIS GPIO
btfsc GPIO,0
goto measureDone
movlw b'111011'
TRIS GPIO
incfsz sendBuf,f
goto measure
incfsz sendBuf+1,f
goto measure
measureDone:
ledOn(2)
readMode
serve:
; wait start
call waitStart
; wait address
call readData ; read address
movf recBuf,w
andlw 0xfe
xorlw deviceAddress ; is our address
btfss STATUS,Z
goto serve
call ack
btfsc recBuf,0
goto writing
; monitor stop HERE!!!!!!!!!!!! must
; muuten sekoaa
; wait offset and save pointer to fsr
call readData
;movlw sendBuf
;addwf recBuf,w
;movwf FSR
call ack
goto serve
writing:
movlw sendBuf
movwf FSR
; write 2xdata from INDF
call writeData
waitSCL1
wait b'010000' ; ack
incf FSR,f
call writeData
waitSCL1
wait b'110000' ; nack
goto mainloop
end