attached is a working piece of code for the 16F628
Code:
___________________
' Temperature, Relative Humidity and Dewpoint
' Processor : 16F628
' Sensor : SHT1x / SHT7x
' Display : 16x2
'** Pin-Assingments
dta VAR PORTB.1 ' Sensor Data
clk VAR PORTB.0 ' Sensor Clock
'** Constants
cmdtr CON %00000011 ' Command "read temperature"
cmdhr CON %00000101 ' Command "read humitity"
'** Variables
result VAR WORD ' RAW Data from Sensor
chksum VAR BYTE ' Checksum
cmd VAR WORD ' Sensor Command
RHlin VAR WORD ' Rel. Humidity (RH) Linear (% *10)
RHtc VAR WORD ' Rel. Humidity (RH) temp. compensated (% *10)
Temp VAR WORD ' Temperature (°C *100)
DP VAR WORD ' Dewpiont (°C *10)
'** Temp variables used in div. calculations
TempDP VAR WORD
Rchk VAR BYTE
logEW VAR WORD
sign VAR BIT
wy VAR WORD
wz VAR WORD
wj VAR WORD
wx VAR WORD
i VAR BYTE
ix VAR BYTE
bt VAR BIT
Pause 500
LCDOut $FE,1,"** SHT 11 **"
PAUSE 1000
main:
GoSub init
cmd = cmdtr
GoSub readsensor
Temp=result-3995
GoSub init
cmd = cmdhr
GoSub readsensor
RHlin=(26542-(54722**result+result))**result-40 ' RH linear
RHtc=655+(result*5)+(result**15917)
RHtc=(RHtc**(Temp/10+2480))-(RHtc**2730)+RHlin ' RH temp. compensated
GoSub dewpoint
LCDOut $FE,$80," ",$DF,"C"," ",$25,"RH"," DP "
LCDOut $FE,$C0,DEC2 (TEMP/100),".",DEC1 (temp/10)," ",DEC2 RHTC/10,".",DEC1 RHTC," ",DEC2 ABS DP/10,".",DEC1 ABS dp
sleep 10
GoTo main
'** Init Sensor
init:
High dta
Low clk
For i=1 to 10
High clk
Pause 1
Low clk
Pause 1
Next i
Call tstart
Return
'** start transfer
tstart:
High clk
Pause 1
Low dta
Pause 1
Low clk
Pause 1
High clk
Pause 1
High dta
Pause 1
Low clk
Return
'** get Data from Sensor
readsensor:
GoSub tstart
GoSub WaitSensor
ShiftOut dta,clk,1,[cmd\8] ' send command
Input dta ' wait acknowledge
Low clk
While dta=1
Wend
PulsOut clk,10 ' send ack
While dta=0
Wend
While dta=1 ' wait for conversion to complete
Wend
Low clk
ShiftIn dta,clk,0,[result.highbyte\8] ' get first byte
Low dta
PulsOut clk,10 ' send ack
ShiftIn dta,clk,0,[result.lowbyte\8] ' get second byte
Low dta
PulsOut clk,10 ' send ack
ShiftIn dta,clk,0,[chksum\8] ' get third byte (checksum)
High dta
PulsOut clk,10 ' send ack
Input dta ' End of Transmission
Input clk
Return
'** Dewpoint Calculation
' See: SENSIRION Application Note "Dewpoint Calculation"
' logEW = (0.66077+7.5*T/(237.3+T)+(log(RH)-2)
' DP = ((0.66077-logEW)*237.3)/(logEW-8.16077)
DewPoint:
TempDP=Temp/10
logEW=6608
sign=TempDP.bit15
wz=ABS TempDP
wx=(7*wz)+(wz/2)
wy=2373+wz
wj=wx/wy
For ix=15 to 0 step -1
wx=(wx//wy)<<1
wz.bit0(ix)=wx/wy
Next
wx=(-sign^((wj*10000)+(wz**10000)))+sign
logEW=wx+logEW
wx=RHtc
wj=(NCD wx) - 1
wx=wx<<(15-wj)
wz=0
For ix=14 to 0 step -1
wy=wx**wx
wz.bit0(ix)=wy.bit15
bt=~wy.bit15
wx=(wy<<bt)+(bt && wx.bit15)
Next
wx=((wj*4000)**49321)+(wz**6021)
logEW=wx+logEW-30000
sign=logEW.bit15
logEW=(-sign^(((ABS logEW)+2)/4))+sign
wx=1652-logEW
sign=~wx.bit15
wx=ABS wx
wy=20402-logEW
wj=wx/wy
For ix=15 to 0 step -1
wx=(wx//wy)<<1
wz.bit0(ix)=wx/wy
Next
DP=((23730**wz)+5)/10
DP=(-sign^DP)+sign
Return
WaitSensor:
result=4096
Loop:
result=result-1
IF dta && result.bit11 Then Loop
Return
End
___________________
Regards
Ralph
Bookmarks