PDA

View Full Version : Me again, but need REAL help now



gringobomba14
- 17th June 2008, 22:10
O.K. I Searched, found the code for the SHT71, thought I understood it, and am now more confused than a porcupine in a brush factory. I am trying to make a 16F877A have a generally good conversation with the SHT-71, and display the results on an LCD. I have mastered the LCD bit (really easy with picBasicPro), had an LM35 read an analogue temperature, but I just can't get my head around this SHT-71. is there anyone out there who has a few minutes to write an idiots guide??? Or has anyone got a finished full code so that I might learn from it??

Thanks in advance,

Gringo

skimask
- 18th June 2008, 03:37
What else have you built so far? Anything?

gringobomba14
- 18th June 2008, 07:52
Hi Ski,

Yes, I built a temp/humidity regulator, using the PIC16F877A, an LM35 temp sensor, and a non linear resistive humidity sensor, with 2X16 LCD, and a user menu to set the paramaters. Once the temp or humidity goes outside these paramaters, a relay is triggered (Through a ULN2003A) which will switch on either a humidifier, dehumidifier, heater, cooler. But the analogue stuff isn't very accurate which is why I'm searching like a maniac for help with this digital stuff.

Apart from that, various LED strobe kits for volunteer firefighters, again using the darlington pair chip, which can give you current output of 500mA per leg, or more if you run them in parallel.

But I'm still a newbie

Acetronics2
- 18th June 2008, 08:06
Hi,

Some ideas ( in english ! ) There :

http://www.mikroe.com/forum/viewtopic.php?t=10468&start=0&postdays=0&postorder=asc&highlight=sht11

Alain

skimask
- 18th June 2008, 14:21
Yes, I built a temp/humidity regulator, using the PIC16F877A, an LM35 temp sensor, and a non linear resistive humidity sensor, with 2X16 LCD, and a user menu to set the paramaters. Once the temp or humidity goes outside these paramaters, a relay is triggered (Through a ULN2003A) which will switch on either a humidifier, dehumidifier, heater, cooler. But the analogue stuff isn't very accurate which is why I'm searching like a maniac for help with this digital stuff.
Apart from that, various LED strobe kits for volunteer firefighters, again using the darlington pair chip, which can give you current output of 500mA per leg, or more if you run them in parallel.
But I'm still a newbie
Well, all that being said, that link posted above should give you one heck of a head start at getting that SHT71 working without much problem.

gringobomba14
- 18th June 2008, 18:07
Excellent guys, thanks. The conversion to PicBasic doesn't look too hard, so thats my project for tonight. Wish me luck!!!!!!

skimask
- 18th June 2008, 18:14
Excellent guys, thanks. The conversion to PicBasic doesn't look too hard, so thats my project for tonight. Wish me luck!!!!!!

Actually, I had my mind in another thread with that last comment.
The link above to mikro is good, but if you search HERE, on these forums, you'll find some code practically already done, waiting for your modifications.
Ya know...that search button, towards the top of the page, offset to the right a bit, little down arrow next to it, white letters, blue background...

gringobomba14
- 18th June 2008, 18:38
Done that a few days ago, and was lead to the SHT-71 code by cocacolaKid. the thing is, I don't understand the code well enough to make the correct mods. Unfortunately, maybe I'm not as well up on PicBasic as I could be, so I don't fully understand "shiftout DataPin,clk,1,[SensorCmd\8]" DataPin and CLK are pins on the SHT, so I define them, but SensorCMD/8????? No idea. Some hand-holding seems to be the only answer......

skimask
- 18th June 2008, 18:43
but SensorCMD/8????? No idea. Some hand-holding seems to be the only answer......

"SensorCMD\8" isn't in the book, but the definition of what those positions in the command "SHIFTOUT" are fully explained in the manual.

gringobomba14
- 18th June 2008, 19:04
RTFM springs to mind...lol. OK, so I'm closer, but now I have a question. What is the purpose of using this : "result.byte0"? It doesn't seemed to be liked by PB

gringobomba14
- 18th June 2008, 20:44
O.K., I worked that one out. Stay tuned, I may have this licked.......

skimask
- 18th June 2008, 21:25
Stay tuned, I may have this licked.......
Well, that might just be a good test :D
Lick it and the humidity and temp should go up :D

gringobomba14
- 18th June 2008, 22:32
Can't seem to find the subroutine "ReceiveData" anywhere, so my program just does some kind of permanent loop. There must be a wall nearby I can bang my head off a few times....................

gringobomba14
- 19th June 2008, 00:31
This is the code I'm using, but it gets stuck at the WHILE/WEND bits in the ReadSensor subroutine. HELP!!!!!!!!!!!

INCLUDE "modedefs.bas"
CRC VAR word
tempc var word
signc var word
result var word
readhumidity var word
sensorcmd var word
readtemp var word
w var word
x var word
temp var word
rhtempcomp var word
rhlinear var word
byte0 var word
byte1 var word
databyte var word
databit var word
i var word

'include "16F877A"
' Define LCD pins
Define LCD_DREG PORTB
Define LCD_DBIT 4
Define LCD_RSREG PORTB
Define LCD_RSBIT 1
Define LCD_EREG PORTB
Define LCD_EBIT 3
Low PORTB.2 ' LCD R/W line low (W)


Pause 100 ' Wait for LCD to start
lcdout $fe,1, "Temperature and" ' show on LCD
lcdout $fe, $c0 ,"Humidity sensor." ' show on LCD
pause 2000 ' Wait 2 seconds
lcdout $fe,1 ' Clear screen
Goto mainloop ' Skip subroutines, start main program


mainloop:
lcdout $fe,1,"Temp ", # tempc

lcdout $fe,$c0,"Humidity ",#RHTempComp
gosub ReadSHT71
pause 100
goto mainloop
end




'read the sensor thingie
ReadSHT71:
Sensorcmd = ReadTemp
gosub readsensor
if result => 4000 then
tempC = result - 4000
SignC = 160
else
tempC = 4000 - result
SignC = "-"
endif
gosub Initialize
Sensorcmd = ReadHumidity
gosub readsensor

'--------------------- Calculate Relative Humidity -----------------------------

w = result * 405
w = div32 100
x = result * result
x = div32 1000
x = x * 28
x = div32 100
RHLinear = w - x - 400

'---------- Calculate Relative Humidity with Temperature Compensation ----------

w = (8 * result + 1000) / 10
if SignC = 160 then
if tempC > 2500 then
x = (tempC - 2500) * w
x = div32 100
RHTempComp = RHLinear + x / 100
else
x = (2500 - temp) * w
x = div32 100
RHTempComp = RHLinear - x / 100
endif
else
x = (2500 + tempC) * w
x = div32 10000
RHTempComp = RHLinear - x
endif
sleep 1
return

'---------------------------- Initialize the Sensor ----------------------------

Initialize:
high PORTD.0
low PORTD.1
for i = 1 to 10
high PORTD.1
pause 1
low PORTD.1
pause 1
next i
call TransferStart
return

'---------------------------- Get Data from Sensor -----------------------------

ReadSensor:
gosub TransferStart
gosub WaitSensor
shiftout PORTD.0,PORTD.1,1,[Sensorcmd\8] ' Send command byte
input PORTD.0 ' Wait for acknowledge
low PORTD.1
while PORTD.0 = 1
wend
pulsout PORTD.1,10 ' Send acknowledge
while PORTD.0 = 0
wend
while PORTD.0 = 1 ' Wait for conversion to complete
wend
low PORTD.1
shiftin PORTD.0,PORTD.1,0,[result.byte1\8] ' Get the first byte, 8 bits
low PORTD.0
pulsout PORTD.1,10 ' Send acknowledge
shiftin PORTD.0,PORTD.1,0,[result.byte0\8] ' Get the second byte, 8 bits
low PORTD.0
pulsout PORTD.1,10 ' Send acknowledge
shiftin PORTD.0,PORTD.1,0,[crc\8] ' Get third byte, 8 bits, CRC
high PORTD.0
pulsout PORTD.1,10 ' Send acknowledge
input PORTD.0 ' End of Transmission
input PORTD.1
return

'---------------------------- Start Transfer -----------------------------------

TransferStart:
high PORTD.1
pause 1
low PORTD.0
pause 1
low PORTD.1
pause 1
high PORTD.1
pause 1
high PORTD.0
pause 1
low PORTD.1
return

'---------------------------- Wait for Sensor ----------------------------------

WaitSensor:
result = 4096

Loop:
result = result - 1
if PORTD.0 && result.bit11 then Loop
return

Archangel
- 19th June 2008, 01:39
Hello gringobomba14,
If you have any extra ports available you could put some code in those empty While Wend loops to light leds to tell you which loop is stalled, or add some serout commands to do the same. or put in an escape counter in the loops.

skimask
- 19th June 2008, 04:16
Try this:


Define LCD_DREG PORTB
Define LCD_DBIT 4
Define LCD_RSREG PORTB
Define LCD_RSBIT 1
Define LCD_EREG PORTB
Define LCD_EBIT 3
INCLUDE "modedefs.bas"
crc var word:tempc var word:signc var word:result var word
readhumidity var word:sensorcmd var word:readtemp var word:w var word
x var word:temp var word:rhtempcomp var word:rhlinear var word:byte0 var byte
byte1 var byte:databyte var byte:databit var bit:i var word:dpin var portd.0
cpin var portd.1:portb.2=0:output portb.2:Pause 1000
lcdout $fe,1, "Temperature and",$fe, $c0 ,"Humidity sensor.":pause 2000
lcdout $fe,1:Goto mainloop ' Skip subroutines, start main program
mainloop: lcdout $fe,1,"Temp ", #tempc,$fe,$c0,"Humidity ",#RHTempComp
gosub ReadSHT71:pause 100:goto mainloop
ReadSHT71: Sensorcmd = ReadTemp : gosub readsensor
if result => 4000 then
tempC = result - 4000 : SignC = 160
else
tempC = 4000 - result : SignC = "-"
endif
gosub Initialize : Sensorcmd = ReadHumidity : gosub readsensor
w = result * 405 : w = div32 100 : x = result * result : x = div32 1000
x = x * 28 : x = div32 100 : RHLinear = w - x - 400
w = ( (8 * result ) + 1000) / 10
if SignC = 160 then
if tempC > 2500 then
x = (tempC - 2500) * w : x = div32 100
RHTempComp = ( RHLinear + x ) / 100
else
x = (2500 - temp) * w : x = div32 100
RHTempComp = ( RHLinear - x ) / 100
endif
else
x = (2500 + tempC) * w : x = div32 10000
RHTempComp = RHLinear - x
endif
sleep 1 : return
Initialize: dpin=1:cpin=0:for i=1 to 10:cpin=1:pause 1:cpin=0:pause 1:next i
gosub TransferStart : return
ReadSensor: gosub TransferStart : gosub WaitSensor
shiftout dpin,cpin,0,[Sensorcmd\8]
input dpin : cpin = 0
while dpin = 1 : wend
cpin = 1 : pause 1 : cpin = 0
while dpin = 0 : wend
while dpin = 1 : wend
cpin = 0 : shiftin dpin,cpin,0,[result.byte1\8] : dpin = 0 : cpin = 1
pause 1 : cpin = 0 : shiftin dpin,cpin,0,[result.byte0\8] : dpin = 0
cpin = 1 : pause 1 : cpin = 0 : shiftin dpin,cpin,0,[crc\8] : dpin = 1
cpin = 1 : pause 1 : cpin = 0 : input dpin : input cpin : return
TransferStart: cpin = 1 : pause 1 : dpin = 0 : pause 1 : cpin = 0
pause 1 : cpin = 1 : pause 1 : dpin = 1 : pause 1 : cpin = 0 : pause 1
return
WaitSensor: result = 4096
Loop: result = result - 1 : if PORTD.0 & result.bit11 then Loop
return
END

Had a couple of subtle math errors, parenthesis type things..
Looks like the mode might've been wrong in a couple of places.
The END wasn't at the bottom...not that it makes much of a difference.
And it's been COLON-IZED! :D

gringobomba14
- 19th June 2008, 07:21
Cheers Ski, looks more compact, and forgot about those colon type things to seperate code. AM I missing something else, because now I get this
ERROR: Macro AND?TTL not found in macro file.
I recall some kind of Math Macro...........

skimask
- 19th June 2008, 13:26
Cheers Ski, looks more compact, and forgot about those colon type things to seperate code. AM I missing something else, because now I get this
ERROR: Macro AND?TTL not found in macro file.
I recall some kind of Math Macro...........

Woof...sweet! Nice error!
What version of PBP are you running? And/Or do you have the patches installed?
And what is the 'full' verbage of the error? Can you track it down to which line is causing it?

Acetronics2
- 19th June 2008, 13:36
Hi, Ski

Here it is :




Executing: "C:\Program Files\Microchip\MPLAB IDE\PBP\PBPW.EXE" -ampasmwin -oq -z -v -p16F877 "Ski.bas"
PICBASIC PRO(TM) Compiler 2.50a, (c) 1998, 2007 microEngineering Labs, Inc.
All Rights Reserved.
Pass 1:
Pass 2:
Code Gen:
Macro Pass:
ERROR: Macro AND?TTL not found in macro file.
Halting build on first failure as requested.
BUILD FAILED: Thu Jun 19 14:33:50 2008



got that with all the very last releases ...

Alain

skimask
- 19th June 2008, 13:43
Here it is :
got that with all the very last releases ...
Alain
Let me re-phrase that last comment :D
Ya gotta page thru the resulting .asm and possibly .lst file to figure out where the problem is. Then trace it back to the source code lines.

Acetronics2
- 19th June 2008, 14:52
Hi,

May be




Loop:result = result - 1 :if PORTD.0 && result.11 then Loop'



instead of




Loop: result = result - 1 : if PORTD.0 & result.bit11 then Loop



... here it compiles !

may be you're not LOGICAL enough ... LoL !

Alain

skimask
- 19th June 2008, 15:00
... here it compiles !
may be you're not LOGICAL enough ... LoL !


Good one! :D

Yep, I finger-flubbed that one apparently...

gringobomba14
- 19th June 2008, 17:09
Excellent help guys. Compiled with the && so I learned something new today....... but now I just get 16 on Humidity and Temp. I have a 10K pull up resistor on the DATA pin of the SHT71, and the leads are about 5cm long. I found that the program stops here:

ReadSensor: gosub TransferStart : gosub WaitSensor
shiftout dpin,cpin,0,[Sensorcmd\8]
input dpin : cpin = 0
while dpin = 1 : wend
cpin = 1 : pause 1 : cpin = 0
while dpin = 0 : wend '<-----------------program stops here
so obviously the Data pin is not doing what its told to. Any more ideas out there???

skimask
- 19th June 2008, 17:11
I found that the program stops here:
ReadSensor: gosub TransferStart : gosub WaitSensor
shiftout dpin,cpin,0,[Sensorcmd\8]
input dpin : cpin = 0
while dpin = 1 : wend
cpin = 1 : pause 1 : cpin = 0
while dpin = 0 : wend '<-----------------program stops here
so obviously the Data pin is not doing what its told to. Any more ideas out there???
How do you know the data pin isn't doing what it's told to do?
How do you know that the sensor received the Sensorcmd correctly in the first place to trigger it to kick back some data at you? Do you even have to wait for the dpin to go low and/or high? How about just pausing for a bit longer and see what happens?

gringobomba14
- 19th June 2008, 17:19
All good questions............let me stick some LED's in there and see whats going on then

gringobomba14
- 19th June 2008, 17:47
This is now officially doing my head in.....pause 10 in place of WEND gives a temp of 4000 and a humidity of 65071. methinks this is not working for me, and my sanity quotient has dramatically decreased these last few days.

skimask
- 19th June 2008, 18:20
This is now officially doing my head in.....pause 10 in place of WEND gives a temp of 4000 and a humidity of 65071. methinks this is not working for me, and my sanity quotient has dramatically decreased these last few days.
Well, then get rid of all that crazy math (ok, it's really not that crazy, but it does increase the chance of a logical error of some sort) you've got going on, read raw values and see if anything at all makes sense.

Put the sensor in ice water, run the program, read the value.

Put the sensor in really hot water, run the program, read the value, see if the temp rises by something that's reasonable, like say you went from ice to boiling, the temp sensor might rise by 100 (or 212 depending on your units). And the humidity would surely rise a bunch too. Then maybe put the sensor in a bunch of dessicant, to dry it out really good and see what the humidity output reads.

That 4000 you've got might be 40.00C (doubt it, but anything's possible), that 65071 might be 65.071%RH (again, doubt it, but, you know...).

gringobomba14
- 19th June 2008, 18:51
Careful Ski, people might start accusing you of knowing things!!!!!! I think what the problem is, is the SHT is either not understanding the commands, or the program isn't complete (I got it off the forum). I wonder is there anyone out there who has experience at this sensor?

I have tried reading the raw data, and got, well, nothing. Or maybe less.

skimask
- 19th June 2008, 19:03
Careful Ski, people might start accusing you of knowing things!!!!!!
That's the last thing I want...and/or need...


I think what the problem is, is the SHT is either not understanding the commands, or the program isn't complete (I got it off the forum). I wonder is there anyone out there who has experience at this sensor?
That post I saw looked pretty complete to me.


I have tried reading the raw data, and got, well, nothing. Or maybe less.

Maybe you let all the smoke out of it?

I'd put it down, go out and mow the lawn or something, come back to it later.
But that's just me...and that's what I do when crap ain't working.

gringobomba14
- 19th June 2008, 19:12
Blue smoke genie is still in one piece, but just to make sure I changed PIC's and SHT's. (Maybe there is an "I" missing in that sensor code somewhere...........)
Here is a question. If I wanted to send say "00000101" from a pin on the PIC, how would I do that? SEROUT? and then to receive "01101010" would be SERIN right? Maybe I might have a go at some changes and see what kind of trouble I can get myself into

skimask
- 19th June 2008, 19:32
Here is a question. If I wanted to send say "00000101" from a pin on the PIC, how would I do that? SEROUT? and then to receive "01101010" would be SERIN right? Maybe I might have a go at some changes and see what kind of trouble I can get myself into

It's all in the PBP manual, check the modifiers.

gringobomba14
- 19th June 2008, 19:47
Ah yes, the standard RTFM answer. In the meantime, another search has produced results of some sort, which has the sensor doing SOMETHING, but I think now it's down to the crazy maths. Watching SCRUBS is a great way of clearing the mind and getting back to work.

skimask
- 19th June 2008, 20:49
Ah yes, the standard RTFM answer.
In this case, yes. 3 little letters might help BIN


In the meantime, another search has produced results of some sort, which has the sensor doing SOMETHING, but I think now it's down to the crazy maths
Don't forget about overflowing bytes and words...


Watching SCRUBS is a great way of clearing the mind and getting back to work.
Damn straight. Although, I'm not so sure it's so much that it clears the mind as it makes the mind DUMP...

gringobomba14
- 19th June 2008, 20:51
I found this code posted by NavMicroSystems, made some SLIGHT changes, and it works perfectly. Cheers to everyone. This works for both the SHT1x and the SHT7x sensors. I have pasted the code below for everyone to see, and play with should the need arise. Thanks again to everyone who spent time trying to make my little project work. Beers are on me (sorry, no delivery outside of my local area)


'** Pin-Assingments
DEFINE xtal 4
Define LCD_DREG PORTB
Define LCD_DBIT 4
Define LCD_RSREG PORTB
Define LCD_RSBIT 1
Define LCD_EREG PORTB
Define LCD_EBIT 3
portB.2=0
output portb.2
dta VAR PORTD.0 ' Sensor Data
clk VAR PORTD.1 ' 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 71 **"
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 RHlin/10,".",DEC1 RHLin'," ",DEC2 ABS DP/10,".",DEC1 ABS dp
pause 1000
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

</bt)+(bt>