28X1 SHT11 Temperature/Humidity interface code

matherp

Senior Member
Tidied up code

Code:
#picaxe 28x1
setfreq m8
' humidity translation table
TABLE 0,(0,55,0,83,0,110,0,138,0,165,0,193,0,221,0,249,1,21,1,49)
TABLE 20,(1,77,1,105,1,133,1,161,1,189,1,218,1,246,2,19,2,47,2,76)
TABLE 40,(2,105,2,134,2,163,2,192,2,221,2,250,3,23,3,52,3,82,3,111)
TABLE 60,(3,141,3,170,3,200,3,230,4,4,4,34,4,64,4,94,4,124,4,154)
TABLE 80,(4,185,4,215,4,246,5,21,5,51,5,82,5,113,5,144,5,175,5,207)
TABLE 100,(5,238,6,13,6,45,6,77,6,108,6,140,6,172,6,204,6,236,7,13)
TABLE 120,(7,45,7,77,7,110,7,143,7,176,7,208,7,241,8,19,8,52,8,85)
TABLE 140,(8,119,8,152,8,186,8,220,8,254,9,32,9,67,9,101,9,135,9,170)
TABLE 160,(9,205,9,240,10,19,10,54,10,89,10,125,10,160,10,196,10,232,11,12)
TABLE 180,(11,48,11,85,11,121,11,158,11,195,11,232,12,13,12,50,12,88,12,125)
TABLE 200,(12,163)
symbol SCK = %11111101
symbol SCK_PIN = 1
symbol DAT = %11111011
symbol DAT_PIN = 2
symbol NDAT = %00000100
symbol t_in = 3
symbol h_in = 5
symbol my_trisc = 0x87
symbol relay = 0
symbol halfclock = 10
symbol fullclock = 20
symbol zero=4010
symbol transfer = b0
symbol checkbits = b1
symbol wordlow = b2
symbol wordhigh = b3
symbol wordcalc = w1
symbol humirawlow = b4
symbol humirawhigh = b5
symbol humiraw = w2
symbol temprawlow = b6
symbol temprawhigh = b7
symbol tempraw = w3
symbol itemp=b8
symbol humidity = b9
symbol dloop = b26
symbol scratch = b27


main:
	gosub setup
	gosub connectionreset
	
cycle:
	gosub measuretemp
	gosub measurehumi
	debug
	wait 18
	goto cycle
	end
'
'
'	
setup:
	peek my_trisc ,scratch
	scratch = scratch & SCK ' set the SCK as output
	scratch = scratch | NDAT ' set DAT as input
	trisc=scratch 'clock under my control
	low portc SCK_PIN ' force clock low, data is externally control

return

datout:
	peek my_trisc ,scratch
	scratch = scratch & DAT ' set the DAT as output
	scratch = scratch & SCK ' set the SCK as output
	trisc=scratch ' use the external pullup to set data high, clock under my control

return

datin:
	peek my_trisc ,scratch
	scratch = scratch | NDAT ' set the DAT as input
	scratch = scratch & SCK ' set the SCK as output
	trisc=scratch ' use the external pullup to set data high, clock under my control
return

connectionreset:        ' routine to reset the chip
	for scratch = 1 to 10
		high portc SCK_PIN
		pauseus fullclock
		low portc SCK_PIN
		pauseus fullclock
	next scratch
	gosub transstart
return

transstart: ' routine to start a transaction
	low portc SCK_PIN: high portc DAT_PIN' clock low and data high
	pauseus halfclock
	gosub datout ' set the clock and data as outputs
	high portc SCK_PIN ' clock high and data high
	pauseus halfclock
	low portc DAT_PIN ' clock stays high, data low
	pauseus halfclock
	low portc SCK_PIN' clock goes low, data stays low
	pauseus fullclock
	high portc SCK_PIN ' clock high, data stays low
	pauseus halfclock
	high portc DAT_PIN ' clock stays high data high
	pauseus halfclock
	gosub datin
return

readbit: ' generate a clock pulse to read a bit into b1
	pauseus halfclock
	high portc SCK_PIN  ' set clock high, data stays high
	pauseus halfclock
	checkbits=pins & NDAT	
	pauseus halfclock
	low portc SCK_PIN' set clock low, data stays high
	pauseus halfclock
return	

writebyte: 'outputs the byte in b0
	low portc SCK_PIN: high portc DAT_PIN ' data high and clock low
	gosub datout ' set the clock and data as .outputs
	if bit7=0 then: gosub clocklow :else gosub clockhigh: endif
	if bit6=0 then: gosub clocklow :else gosub clockhigh: endif
	if bit5=0 then: gosub clocklow :else gosub clockhigh: endif
	if bit4=0 then: gosub clocklow :else gosub clockhigh: endif
	if bit3=0 then: gosub clocklow :else gosub clockhigh: endif
	if bit2=0 then: gosub clocklow :else gosub clockhigh: endif
	if bit1=0 then: gosub clocklow :else gosub clockhigh: endif
	if bit0=0 then: gosub clocklow :else gosub clockhigh: endif
	gosub datin ' sets data back to an input
	gosub readbit
return



readbyteack: 'inputs a byte to b0, send ack
	gosub datin
	transfer=0
	gosub readbit:if checkbits!=0 then:  bit7 = 1:endif
	gosub readbit:if checkbits!=0 then:  bit6 = 1:endif
	gosub readbit:if checkbits!=0 then:  bit5 = 1:endif
	gosub readbit:if checkbits!=0 then:  bit4 = 1:endif
	gosub readbit:if checkbits!=0 then:  bit3 = 1:endif
	gosub readbit:if checkbits!=0 then:  bit2 = 1:endif
	gosub readbit:if checkbits!=0 then:  bit1 = 1:endif
	gosub readbit:if checkbits!=0 then:  bit0 = 1:endif
	pauseus halfclock
	low portc DAT_PIN
	gosub datout
	gosub clocklow
	gosub datin
return

readbyte: 'inputs a byte to b0
	gosub datin
	transfer=0
	gosub readbit:if checkbits!=0 then:  bit7 = 1:endif
	gosub readbit:if checkbits!=0 then:  bit6 = 1:endif
	gosub readbit:if checkbits!=0 then:  bit5 = 1:endif
	gosub readbit:if checkbits!=0 then:  bit4 = 1:endif
	gosub readbit:if checkbits!=0 then:  bit3 = 1:endif
	gosub readbit:if checkbits!=0 then:  bit2 = 1:endif
	gosub readbit:if checkbits!=0 then:  bit1 = 1:endif
	gosub readbit:if checkbits!=0 then:  bit0 = 1:endif
	pauseus halfclock
	gosub clockhigh
return


clocklow:
	low portc SCK_PIN: low portc DAT_PIN ' set data low
	pauseus halfclock
	high portc SCK_PIN ' set clock high, data stays low
	pauseus fullclock
	low portc SCK_PIN: low portc DAT_PIN 'set clock low, data stays low
	pauseus halfclock
return

clockhigh:
	low portc SCK_PIN: high portc DAT_PIN ' set data high
	pauseus halfclock
	high portc SCK_PIN ' set clock high, data stays high
	pauseus fullclock
	low portc SCK_PIN: high portc DAT_PIN ' set clock low, data stays high
	pauseus halfclock
return

waitcomplete: 'put timeout code in here if required
	pause 20
	scratch=pins & NDAT
	if scratch !=0 then goto waitcomplete
return

measuretemp: ' read raw temperature data into tempraw
	gosub transstart
	transfer = t_in ' set measure temp command
	gosub writebyte
	gosub waitcomplete
	gosub readbyteack
	temprawhigh = transfer
	gosub readbyteack
	temprawlow = transfer
	gosub readbyte
	if tempraw> zero then
		tempraw=tempraw-zero
		wordcalc=tempraw//100
		wordcalc=wordcalc/10
		itemp=tempraw/100
	else
		tempraw=zero-tempraw
		wordcalc=tempraw//100
		wordcalc=wordcalc/10
		itemp=tempraw/100
	endif
return

measurehumi:'read raw humidity data into humiraw
	gosub transstart
	transfer = h_in
	gosub writebyte
	gosub waitcomplete
	gosub readbyteack
	humirawhigh = transfer
	gosub readbyteack
	humirawlow = transfer
	gosub readbyte
	for dloop=0 to 200 step 2 'lookup the raw humidity value in the table
		readtable dloop,wordhigh
		scratch=dloop+1
		readtable scratch,wordlow
		if wordcalc>=humiraw then: exit: endif	
	next dloop
	humidity=dloop/2
return
 

matherp

Senior Member
Yes this is the one but if you look on a certain auction site (UK) they are available for less than half that price. The unit can be soldered very carefully to a 8 pin soic to DIP adapter but make sure that any tracks on the adapter that go under the chip or close to the ends don't short out the little pcb. I've done 3 successfully with a normal soldering iron and standard solder (use leaded) I used 4K7 pullups to 5V on both the clock and data lines. The data line must connected to a pin on the picaxe that can be configured as both an input or output.
 

Attachments

Top