|
|
| Author |
Message |
mdown
Joined: 03 Feb 2006
Posts: 62
Location: Dallas, Texas
|
|
Posted: 08 February 2006, 16:15 PM Post subject: Sensirion SHT11 and the ZX |
|
|
I am using the SHT11 from Parallax to prototype a temperature sensor. I found the code below on Dr. Anderson's site for the BX24 and tried it, it doesn't seem to work correctly. Is there a timing issue because of the increased speed of the ZX vs the BX?
-Mike
| Code: |
' Program SHT71_2.Bas (BX24)
'
' Illustrates an interface with the Sensirion SHT1x/7x Relative Humidity
' and Temperature System.
'
' BX24 SHT1x/7x
'
' +5VDC
' |
' 4.7K
' |
' Term 14 ------------------ DATA
' Term 13 ------------------ SCK
'
' Note that terminal designations for the SHT1x differ from the SHT7x.
'
' Illustrates use of Soft Reset to set the SHT status register to the default
' values.
'
' Continually executes a sequence of measuring and calculating temperature and
' relative humidity. Linearizes relative humidity and performs a temperature
' adjustment. Calculates the dew point.
'
' Displays temperature, true relative humidity and dew point temperature.
'
' copyright, Peter H. Anderson, Baltimore, MD, Apr, 03
' Calculation of RH corrected, Sept 25, '03
const DATA as byte=14
const SCK as byte=13
const MeasTempCode as Byte = bx0000_0011
const MeasRHCode as Byte = bx0000_0101
const SoftReset as Byte = bx0001_1110
Sub Main()
Dim RHLinear as Single, RHTrue as Single, TC as Single, TF as Single
Dim SO_RH as Single, SO_Temp as Single
Dim EW_RH as Single, DewPointTC as Single, DewPointTF as Single
Dim H as Byte, L as Byte, CRC as Byte
Call Sleep(1.0) ' to avoid download problems
Debug.Print "..............." ' to see that something is happening
' be sure there is at least an 11 ms delay after the device is turned on.
' soft reset
Call SHT1xIdle()
Call SHT1xReset()
Call SHT1xTransmissionStart()
Call SHT1xOutByte(SoftReset)
Call SHT1xIdle()
Call Sleep(0.025) ' wait more than 11 ms for values to be written
' to status register
Do
' measure temperature
Call SHT1xIdle()
Call SHT1xReset()
Call SHT1xTransmissionStart()
Call SHT1xOutByte(MeasTempCode)
Call SHT1xWait(0.250)
H = SHT1xInByte(1) ' High Byte
L = SHT1xInByte(1) ' Low Byte
CRC = SHT1xInByte(0) ' CRC
Call SHT1xIdle()
' calculate the temperature
SO_Temp = CSng ((CInt(H) * 256) + CInt(L))
TC = -40.0 + 0.01 * SO_Temp
TF = 1.8 * TC + 32.0
Debug.Print "Temperature = "; CStr(TF); " "; CStr(TC); " "; CStr(H); " "; CStr(L); " "; CStr(CRC)
' relative humidity
Call SHT1xIdle()
Call SHT1xReset()
Call SHT1xTransmissionStart()
Call SHT1xOutByte(MeasRHCode)
Call SHT1xWait(0.60)
H = SHT1xInByte(1) ' High Byte
L = SHT1xInByte(1) ' Low Byte
CRC = SHT1xInByte(0) ' CRC
Call SHT1xIdle()
SO_RH = CSng ((CInt(H) * 256) + CInt(L))
' RHLinear = -4.0 + 0.0405 * SO_RH + 2.8e-6 * SO_RH * SO_RH
RHLinear = -4.0 + 0.0405 * SO_RH - 2.8e-6 * SO_RH * SO_RH ' corrected, Sept, 03
RHTrue = (TC - 25.0) * (0.01 + 0.00008 * SO_RH) + RHLinear
Debug.Print "RH = "; CStr(RHTrue); " "; CStr(H); " "; CStr(L); " "; CStr(CRC)
' calculate dew point
EW_RH = exp10(0.6607 + 7.5 * TC / (237.3 + TC)) * RHTrue / 100.0
DewPointTC = ((0.6607 - log10(EW_RH)) * 237.3) / (log10(EW_RH) - 8.16077)
DewPointTF = 1.8 * DewPointTC + 32.0
Debug.Print "Dew Point "; CStr(DewPointTF)
Call Sleep(1.0)
Loop
End Sub
Sub SHT1xIdle()
Call PutPin(SCK, 0) ' DATA high, clock low
Call PutPin(DATA, 2)
End Sub
Sub SHT1xReset() ' bring DATA high and provide 9 clock pulses
Dim N as Integer
Call PutPin(SCK, 0)
Call PutPin(DATA, 2)
For N = 1 to 9
Call PutPin(SCK, 1)
Call PutPin(SCK, 0)
Next
End Sub ' leave with DATA high, SCK low
Sub SHT1xTransmissionStart()
Call PutPin(DATA, 2)
Call PutPin(SCK, 1)
CALL PutPin(DATA, 0)
Call PutPin(SCK,0) ' negative clock pulse
Call PutPin(SCK, 1)
Call PutPin(DATA,2)
Call PutPin(SCK, 0) ' leave with DATA high and SCK low
End Sub
Sub SHT1xOutByte(ByVal X as Byte) ' SCK is low
Dim N as Integer
For N=1 to 8
If((X AND &H80) 0) Then
Call PutPin(DATA,2)
' Call PutByte(Asc("1")) ' used for debugging
Else
Call PutPin(DATA,0)
' Call PutByte(Asc("0")) ' used for debugging
End If
Call PutPin(SCK,1)
Call PutPin(SCK,0)
X = X*2
Next
' Call NewLine() ' used for debugging
Call PutPin(DATA,2)
Call PutPin(SCK,1)
Call PutPin(SCK,0)
Call PutPin(DATA,2)
End Sub
Sub SHT1xWait(ByVal X as Single)
Call Sleep(X)
End Sub
Function SHT1xInByte(ByVal Ack as Byte) as Byte
Dim N as Integer
Dim X as Byte
X=0
For N=1 to 8
Call PutPin(SCK,1)
If(GetPin(DATA)=1) Then
X = X * 2 +1
Else
X = X * 2
End If
Call PutPin(SCK, 0)
Next
If (Ack 0) Then
Call PutPin(DATA, 0)
Else
Call PutPin(DATA,2)
End If
Call PutPin(SCK,1)
Call PutPin(SCK,0)
Call PutPin(DATA,2) ' leave routine with DATA high and CLK low
SHT1xInByte = X
End Function
|
I get this output:
| Code: |
Temperature = -40.018 -40.01 255 255 255
RH = 13.59499 2 28 17
Dew Point -71.14272
|
|
|
| Back to top |
|
 |
mdown
Joined: 03 Feb 2006
Posts: 62
Location: Dallas, Texas
|
|
Posted: 08 February 2006, 16:27 PM Post subject: Code works on BX24 |
|
|
OK now I know for sure that it is a timing issue as I slapped an old bx24 into my project and it works correctly.
-Mike |
|
| Back to top |
|
 |
mikep
Joined: 24 Sep 2005
Posts: 765
Location: Austin, TX
|
|
Posted: 08 February 2006, 16:52 PM Post subject: |
|
|
My normal approach to this type of problem is to first isolate what is going wrong. It could be a timing problem or it could be something else. In the following code snippet: | Code: | H = SHT1xInByte(1) ' High Byte
L = SHT1xInByte(1) ' Low Byte | are the values of H and L as you expect and do they compare to the BX-24?
According to the datasheet the device allows up to 10MHz frequency for SCK (clock) so that is fine.
Also on one another note; I got my start looking at Dr. Andersons's BX page but his and his students coding techniques leave a lot to be desired. For example constants should be used for the calls to PutPin to replace the 0, 1, and 2. The interface to the device and low-level bit-banging is probably also better separated into a different module. I would rewrite it as basically 3 public functions called GetTemperature(), GetHumidity(), and GetDewpoint(). |
|
| Back to top |
|
 |
mikep
Joined: 24 Sep 2005
Posts: 765
Location: Austin, TX
|
|
Posted: 08 February 2006, 16:59 PM Post subject: |
|
|
One other idea that may take a little more work is to replace the existing code with ShiftIn/Out and ShiftInEx/OutEx. These calls allow you to shift bits in and out and also control the timing by changing Register.TimerSpeed1.
I'm not sure why the original code didn't use ShiftIn/Out in the first place but you could try that on the BX-24 first. |
|
| Back to top |
|
 |
mdown
Joined: 03 Feb 2006
Posts: 62
Location: Dallas, Texas
|
|
Posted: 08 February 2006, 17:44 PM Post subject: |
|
|
| No it does work correctly on the BX24 it does not work on the ZX24. |
|
| Back to top |
|
 |
mikep
Joined: 24 Sep 2005
Posts: 765
Location: Austin, TX
|
|
Posted: 08 February 2006, 18:22 PM Post subject: |
|
|
| mdown wrote: | | No it does work correctly on the BX24 it does not work on the ZX24. |
I got that. Let me reiterate my points in priority order:
1. Isolate why it doesn't work on the ZX-24. Your conclusion of a timing problem may or may not be correct.
2. Try rewriting the code using ShitfIn and ShiftOut. If you do this first with the BX-24 then you can port it and play with the timing on the ZX-24.
3. Once the code is working, think about improving the readability and maintainability by using modules and more constants. |
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR
|
|
Posted: 08 February 2006, 18:35 PM Post subject: |
|
|
| Quote: | | mikep: I'm not sure why the original code didn't use ShiftIn/Out... |
It is probably because the data line is open drain: a logic zero is transmitted by setting the pin to output low state and a logic one is transmitted by setting the pin to an input state. ShiftOutEx() can transmit data using this regimen by asserting bit 3 of the 'flags' parameter.
| Quote: | | mdown: it does work correctly on the BX24 it does not work on the ZX24 |
There are several possible reason why this might be the case. So far, the data gathered is inconclusive. If it is true that the clock can be as fast as 10MHz it is it doesn't seem likely that bit timing is an issue but there still could be a problem with setup or hold time or the time between operations.
Other possible causes:
- the ZBasic compiler is not generating correct code
- the ZX virtual machine is not implementing the instructions correctly
- the slew rate of the signals is too fast (the datasheet alludes to some limit here but provides no specific parameters)
- the provided code unwittingly relies on undocumented behavior of the BX-24 that is not replicated by the ZX-24
As mikep suggested, more specific information is required to determine why the code does not work with the ZX-24. I don't have one of the devices on hand and it looks like they are limited in availability (Parallax has them back-ordered until March). If you can send me one for testing we can likely figure out what's happening and how to fix it. |
|
| Back to top |
|
 |
mdown
Joined: 03 Feb 2006
Posts: 62
Location: Dallas, Texas
|
|
Posted: 08 February 2006, 19:54 PM Post subject: |
|
|
Yes we know about them being back ordered, we were only able to acquire 4 though our local distributor. We have already assembled all of our boards with the 4 that we have. We will be moving to the SHT75 once we are done prototyping, the SHT71 the SHT11's cousin is available at http://www.newark.com/NewarkWebCommerce/newark/en_US/endecaSearch/partDetail.jsp;?SKU=07J2377&N=0 Dr. Anderson did note about the shorting out and that is why he didnt use shiftout, shiftin, only because he had the actual sensor and not the module from Parallax. Parallax adds a 4.7k resistor between vdd and the data line and a 300 ohm resistor between the data pin and the data line. I will play with using shift in/out and see if that helps. So I would gather that the "undocumented behavior" that Don is referring to could be the primary culprit.
-Mike |
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR
|
|
Posted: 08 February 2006, 21:00 PM Post subject: |
|
|
| Quote: | | So I would gather that the "undocumented behavior" that Don is referring to could be the primary culprit. |
That hypothesis is, as yet, unproven. There is no data to suggest that it is true or that it is not true. The other possible causes are equally likely at this point. |
|
| Back to top |
|
 |
|