SHT-11 with Picaxe-08M

CWells

New Member
I'm still working on it...I'm studying the SHT-11 docs & the BS2 code, which works, and now I can't figure out why. According to the doc, the SHT-11 defaults to a 12-bit measurement, and the BS2 code appears to assume 8-bit readings. I still haven't been able to get the Picaxe to get a coherent reading.
Here's the current version of the program, but I expect to be working on this till bedtime. (9:00 or so) Any help is vastly appreciated.



' Simple SHT-11 interface, BS2 to Picaxe-08M

symbol dat = pin1 ' bi-directional data
symbol sin = input1
symbol Clk = pin2

symbol MSBvalue = 128 ' MSBvalue (=128 for 8 bits, 512 for 10 bits, 2048 for 12 bits)

symbol ack = bit0
symbol timeout = bit1
symbol sorh = w2
symbol counter = b7 ' variable used during loop
symbol mask = w4 ' bit masking variable
symbol var_in = w5 ' data variable used durig shiftin
symbol var_out = w6 ' data variable used during shiftout


' Initialization

Initialize:
pause 1000
GOSUB SHT_Connection_Reset ' reset device connection
PAUSE 1000 ' let DEBUG window open


Main:


GOSUB SHT_Measure_Humidity
sertxd("Raw: ",#sorh, 13, 10)

PAUSE 2000
if timeout = 1 then goto ender ' minimum delay between readings
GOTO Main
ender:
end




' ------------------------------------------------------------------------------
' Subroutines
' ------------------------------------------------------------------------------



' connection reset: 9 clock cyles with ShtData high, then start sequence

SHT_Connection_Reset:

input 1
ack = dat
sertxd("rst",#ack,13,10)
for counter = 1 to 9
pulsout 2,1
pause 10
next counter

SHT_Start:


INPUT 1 ' let pull-up take line high
LOW 2
HIGH 2
LOW 1
LOW 2
high 2
INPUT 1
LOW 2
RETURN

' measure humidity

SHT_Measure_Humidity:

'sertxd("H:", 13, 10)
GOSUB SHT_Start ' alert device
var_out = %00000101 ' humidity command
GOSUB SHT_Write_Byte ' send command
GOSUB SHT_Wait ' wait until measurement done
Ack = 0 ' another read follows
GOSUB SHT_Read_Byte
b4 = var_in ' get MSB
ack = 1 ' last read
GOSUB SHT_Read_Byte ' get LSB
b5 = var_in
RETURN


SHT_Write_Byte:
gosub shiftout_MSB_First ' send byte
gosub getack
RETURN



SHT_Read_Byte:
sertxd("R",13,10)
gosub shiftin_MSB_Pre
gosub sendack
INPUT 1 ' release data line
RETURN


SHT_Wait:
ack = 0
timeout = 0
INPUT 1 ' data line is input
FOR counter = 1 TO 250 ' give ~1/4 second to finish
sertxd (#counter) ack = dat ' scan data line
IF ack = 0 THEN SHT_Wait_Done ' if low, we're done
PAUSE 1
NEXT counter
if counter > 249 then
timeout = 1
sertxd("X") ' Timeout indicator
endif

SHT_Wait_Done:
sertxd("O",13,10) ' Signal has been received
RETURN

shiftout_MSB_first:
for counter = 1 to 8
mask = var_out & MSBValue
low 1
if mask = 0 then skipMSB
input 1 ' data high
skipMSB:
pulsout 2,1 ' pulse clock for 10us
var_out = var_out * 2 ' shift variable left for MSB
next counter
return

#rem

shiftout_LSBFirst:
for counter = 1 to 9 ' number of bits
mask = var_out & 1 ' mask LSB
low 1 ' data low
if mask = 0 then skipLS
input 1 ' data high
skipLS:
pulsout 2,1 ' pulse clock for 10us
var_out = var_out / 2 ' shift variable right for LSB
next counter
return



shiftin_LSB_pre:
for counter = 1 to 8
mask = var_out & 1
low 1
if mask = 0 then skipLSB
input 1
skipLSB: pulsout 2,1
var_out = var_out / 2
next counter
RETURN

#endrem


shiftin_MSB_Pre:
let var_in = 0
for counter = 1 to 8 ' number of bits
var_in = var_in * 2 ' shift left as MSB first
if dat = 0 then skipMSBPre
var_in = var_in + 1 ' set LSB if serdata = 1
skipMSBPre:
pulsout 2,1 ' pulse clock to get next data bit
next counter
return



getack: 'Gets ack bit from SHT-11
input 1
do
if dat=0 then exit
loop

pulsout 2,1

return

sendack:

'Sends ack bit to SHT-11
if ack = 1 then
input 1

else
low 1
endif
pulsout 2,1
return
 

CWells

New Member
Update: I'm finally getting numbers that don't look like nonsense; I had the upper & lower bytes swapped. The Wait portion is operating strangely though; it's always timing out. Here's the current code:





' Simple SHT-11 interface, BS2 to Picaxe-08M

symbol dat = 1 ' bi-directional data
symbol sin = input1
symbol Clk = 2

symbol MSBvalue = 128 ' MSBvalue (=128 for 8 bits, 512 for 10 bits, 2048 for 12 bits)

symbol ack = bit0
symbol timeout = bit1
symbol sorh = w2
symbol counter = b7 ' variable used during loop
symbol mask = w4 ' bit masking variable
symbol var_in = w5 ' data variable used durig shiftin
symbol var_out = w6 ' data variable used during shiftout


' Initialization

Initialize:
pause 1000
GOSUB SHT_Connection_Reset ' reset device connection
PAUSE 1000 ' let DEBUG window open


Main:


GOSUB SHT_Measure_Humidity
sertxd("Raw: ",#sorh, 13, 10)

PAUSE 2000
if timeout = 1 then goto ender ' minimum delay between readings
GOTO Main
ender:
end




' ------------------------------------------------------------------------------
' Subroutines
' ------------------------------------------------------------------------------



' connection reset: 9 clock cyles with ShtData high, then start sequence

SHT_Connection_Reset:

input dat
ack = dat
sertxd("rst",#ack,13,10)
for counter = 1 to 9
pulsout clk,1
pause 10
next counter

SHT_Start:


INPUT dat ' let pull-up take line high
LOW clk
HIGH clk
LOW dat
LOW clk
high clk
INPUT dat
LOW clk
RETURN

' measure humidity

SHT_Measure_Humidity:

'sertxd("H:", 13, 10)
GOSUB SHT_Start ' alert device
var_out = %00000101 ' humidity command
GOSUB SHT_Write_Byte ' send command
GOSUB SHT_Wait ' wait until measurement done
Ack = 0 ' another read follows
GOSUB SHT_Read_Byte
b5 = var_in ' get MSB
ack = 1 ' last read
GOSUB SHT_Read_Byte ' get LSB
b4 = var_in
RETURN


SHT_Write_Byte:
gosub shiftout_MSB_First ' send byte
gosub getack
RETURN



SHT_Read_Byte:
sertxd("R",13,10)
gosub shiftin_MSB_Pre
gosub sendack
INPUT 1 ' release data line
RETURN


SHT_Wait:
ack = 0
timeout = 0
INPUT dat ' data line is input
FOR counter = 1 TO 250 ' give ~1/4 second to finish
ack = dat ' scan data line
sertxd (#counter,#ack,13,10)
IF ack = 0 THEN SHT_Wait_Done ' if low, we're done
PAUSE 1
NEXT counter
if counter > 249 then
timeout = 1
sertxd("X") ' Timeout indicator
endif

SHT_Wait_Done:
sertxd("O",13,10) ' Signal has been received
RETURN

shiftout_MSB_first:
for counter = 1 to 8
mask = var_out & MSBValue
low dat
if mask = 0 then skipMSB
input dat ' data high
skipMSB:
pulsout clk,1 ' pulse clock for 10us
var_out = var_out * 2 ' shift variable left for MSB
next counter
return


shiftin_MSB_Pre:
let var_in = 0
for counter = 1 to 8 ' number of bits
var_in = var_in * 2 ' shift left as MSB first
if sin = 0 then skipMSBPre
var_in = var_in + 1 ' set LSB if serdata = 1
skipMSBPre:
pulsout clk,1 ' pulse clock to get next data bit
next counter
return



getack: 'Gets ack bit from SHT-11
input dat
do
if sin=0 then exit
loop

pulsout clk,1

return

sendack:

'Sends ack bit to SHT-11
if ack = 1 then
input dat

else
low dat
endif
pulsout clk,1
return




 

CWells

New Member
I finally got it working! I had changed the pin symbols and had the ack bit set to dat instead of sin...thanks for all the help, and for putting up with my newbie nonsense.
 

erdc

Member
Excelent!

I was trying to find my code on how I worked with the sht11, but did not/can not locate it.

Could you please post the final working code for this? I would like to see how yours is differnet then mine (when/if I find it again)
 
Top