Code:
'{$STAMP BS2}
' SHT15.bs2 version C
' (c) 2002, 2004 Tracy Allen, http://www.emesystems.com
' access the Sensirion model sht11 or sht15
' humidity and temperature chip
' temperature to xx.x degrees Celsius, RH to xx.x %
' Repeatedly shows raw data and converted values on debug screen.
' version B, corrects glaring error in humidity computation
' also adjusts timeout to allow use with faster stamps too.
' version C updates the math for temperature compensation of %RH
' and enables display of negative temperatures
' hookup sht11 or sht15 as follows for this program
'
' STAMP SENSIRION
' 220
' p1 ----o-----/\/\---o--pin 3 sck, clock
' |
' ;---/\/\-----' pull-down
' | 4.7k
' Vss--o-o---------------pin 1 common
' |
' === 0.1uf
' |
' Vdd--o-o---------------pin 4 +5 volts
' | 4.7k
' '---/\/\-----; pull-up
' |
' 220 |
' p0 ----------/\/\---o--pin 2 dta, data
'
'
' The following code does not implement the CRC checking
sck PIN 1
dta PIN 0 ' note, 5k-10k pull-up, also 330ohm between the dta on stamp to dta on sht
dtain var in0
shtTR CON 3 ' read temperature
shtRH CON 5 ' read humidity
shtSW CON 6 ' status register write
shtSR CON 7 ' status register read
shtS0 CON 30 ' restore status register defaults (be sure to delay 11 milliseconds)
cmd VAR Byte
result VAR Word ' raw result from sht, also used as counter
r0 VAR result.byte0
r1 VAR result.byte1
degC VAR Word ' degrees Celsius * 100
RH VAR Word ' %RH * 10
RHtc VAR Word ' for temperature compensation of RH
initialize:
outs=0
dirs=%1111111111111101
GOSUB shtrst ' reset communication with sht
DO
getTemperature:
cmd=shtTR ' temperature command to sht
GOSUB shtget16
degC=result+5/10-400 ' from 100ths to 10ths of a degree with rounding
DEBUG tab,REP "-"\degC.bit15,DEC ABS degC/10,".",DEC1 ABS degC
getHumidity:
cmd=shtRH ' humidity command to sht
GOSUB shtget16
RH=(26542-(54722**result+result))**result-40
' temperature compensation follows:
RHtc=655+(result*5)+(result**15917) ' intermediate factor
RHtc=(RHtc**(degC+2480))-(RHtc**2730)+RH ' compensated value
DEBUG tab, DEC result,tab,"%RH=",DEC RH/10,".",DEC1 RH
DEBUG tab,"%RHtc=",DEC RHtc/10,".",DEC1 RHtc,cr
PAUSE 1000
LOOP
' initializes communication with sht
shtRst:
SHIFTOUT dta,sck,lsbfirst,[$ffff\16]
RETURN
' get 16 bits of data, enter with command in "cmd"
shtget16:
gosub shtcmd ' send the command "cmd"
gosub shtwait ' wait for command to finish
shiftin dta,sck,msbpre,[r1] ' msbyte
low dta ' acknowledge
pulsout sck,10
input dta
shiftin dta,sck,msbpre,[r0] ' lsbyte
input dta ' terminate communication
pulsout sck,10
return
' send start sequence and command
shtcmd:
shtStart: ' send the start sequence
' dta: ~~~~~|_____|~~~~~~
' sck: ___|~~~|_|~~~~|____
' while dta is low, clock goes low and then high
input dta ' pullup high
high sck
low dta
low sck
high sck
input dta
low sck
shtcmd1: ' send the command
shiftout dta,sck,msbfirst,[cmd]
input dta ' allow acknowledge
pulsout sck,10
return
shtWait:
' wait for sht to pull data pin low
' or for time out
result=4096
DO
result=result-1
LOOP WHILE dta & result.bit11
RETURN
</pre>
Bookmarks