PDA

View Full Version : How do you store Fahrenheit temperature from the SHT as a variable?



Nerd2
05-29-2010, 04:07 AM
I have a SHT temperature sensor attached to my propeller. I have the propeller displaying the Fahrenheit temperature on a TV screen but I need the temperature also to be stored in a variable as a whole number no decimal. This is so I can do several If Then statements. How do I do this?

Sal Ammoniac
05-29-2010, 06:30 AM
Convert it to fixed point. Multiply the original value by 10 or 100 and truncate it down to an integer and store that.

Nerd2
05-29-2010, 07:10 AM
How do I do that?
Here is part of my code


pri currenttemp | rawTemp, rawHumidity, tempC, rh, dewC,msd

rawTemp := f.FFloat(sht.readTemperature)
tempC := celsius(rawTemp)
curtemp :=(fp.FloatToFormat(fahrenheit(tempC), 2, 0))
' curtemp :=(fp.FloatToFormat(fahrenheit(tempC), 5, 1))
str(curtemp)

'Current Temp Slider
if curtemp > 090
gr.box (80,-10,5,12)'(x, y, box_width, box_height)
return
if curtemp > 85
gr.box (65,-10,5,12)'(x, y, box_width, box_height)
return
if curtemp[0] >= 80.0
gr.box (50,-10,5,12)'(x, y, box_width, box_height)
return
if curtemp > 7500
gr.box (35,-10,5,12)'(x, y, box_width, box_height)
return
if curtemp > 7000
gr.box (20,-10,5,12)'(x, y, box_width, box_height)
return
if curtemp > 6500
gr.box (5,-10,5,12)'(x, y, box_width, box_height)
return
if curtemp > 6000
gr.box (-10,-10,5,12)'(x, y, box_width, box_height)
return
if curtemp > 5500
gr.box (-25,-10,5,12)'(x, y, box_width, box_height)
return
if curtemp > 5000
gr.box (-40,-10,5,12)'(x, y, box_width, box_height)
return
if curtemp > 4500
gr.box (-55,-10,5,12)'(x, y, box_width, box_height
return
if curtemp > 4000
gr.box (-70,-10,5,12)'(x, y, box_width, box_height)
return

Sal Ammoniac
05-29-2010, 07:41 AM
Here's my SHT15 code. Perhaps you can extract something useful from it.




'' SHT15 Driver
''
'' Pin assignments (relative to 'Pin' parameter passed to SHT15Init):
''
'' 0 SCK (output)
'' 1 Data (input/output, open drain)
''
''
'' Since the data line on an I2C device (SHT15 is I2C with one minor difference), and the Propeller doesn't
'' have true open drain I/O ports, we fake it as follows: leave the port set to '0' and toggle the state of
'' the direction register (dira). The combinations are:
''
'' dira[datapin] = 0 Input, high-Z, pulled high by pull-up resistor
'' dira[datapin] = 1 Output, pulled low

CON

_CLKMODE = XTAL1 + PLL16X
_XINFREQ = 5_000_000


SHT_TEMP = $3 ' read temperature command
SHT_HUMID = $5 ' read humidity command
SHT_PUTSTAT = $6 ' set status register command
SHT_GETSTAT = $7 ' read status register command


CR = 13 ' Carriage Return


VAR

long SCK, DATA
long status
long RH, RHraw, Temperature
long errcnt
long Stack[256]
byte Cog


PUB Start(PinStart) : okay

Stop
okay := (Cog := cognew(SHT15Init(PinStart), @Stack) + 1)

PUB Stop

if Cog
cogstop(Cog~ - 1)

PUB Active : YesNo

YesNo := Cog > 0


PUB SHT15Init(PinStart)

waitcnt(clkfreq/10 + cnt) ' wait 100 msec for SHT15 to initialize

SCK := PinStart ' save starting port pin number as SCK pin
DATA := SCK + 1 ' DATA pin is next port

outa[SCK..DATA]~ ' turn off both ports
dira[SCK..DATA] := %10 ' set port direction bits (SCK output, DATA input)
errcnt~

BusReset ' do a software reset of the sensor's bus
SHT15Loop ' kick off main loop


PUB SHT15Loop | SOrh, SOt, RHlinear, RHtrue, tempC, p

repeat
StartTransmission ' send transmission start sequence
Send(SHT_HUMID) ' send read humidity command
if(!WaitCompletion)
WaitMS(200) ' conversion failed; wait 200msec and try again
errcnt++
quit

SOrh := 0
SOrh := Read16 ' read raw humidity data

StartTransmission ' send transmission start sequence
Send(SHT_TEMP) ' send read temperature command
if(!WaitCompletion)
WaitMS(200) ' conversion failed; wait 200msec and try again
quit
SOt := Read16 ' read raw temperature data

tempC := -3970 + SOt ' in units of C x 100
RHlinear := -20468 + ((SOrh * 10_000) ** 157_625_300) - (((SOrh * SOrh * 1000) ** 6853) * 10)
RHtrue := (tempC * 100 - 250_000) * (100 + (SOrh * 10_000) ** 343_597) / 10_000 + RHlinear

Temperature := tempC
RH := RHtrue
RHraw := SOrh

WaitMS(500) ' update twice a second



PUB SHT15Temp
result := Temperature

PUB SHT15RH
result := RH

PUB SHT15RHAddr
result := @RH

PUB SHT15RHraw
result := RHraw

PRI SCK_HI
outa[SCK]~~

PRI SCK_LO
outa[SCK]~


PRI Read16 : rword

SCK_LO ' lower SCK
dira[DATA]~ ' set DATA as input

repeat 8
SCK_HI ' raise clock
rword := (rword << 1) | ina[DATA] ' accumulate MSB
SCK_LO ' drop clodk

dira[DATA]~~ ' pull DATA low (ACK)
SCK_HI ' raise clock (DATA will be
' dira[DATA]~ ' release DATA line (will float high)
SCK_LO ' drop clock
dira[DATA]~ ' release DATA line (will float high)

repeat 8
SCK_HI ' raise clock
rword := (rword << 1) | ina[DATA] ' accumulate LCB
SCK_LO ' drop clock

SCK_HI ' raise clock (false ACK to terminate measurement without CRC)
SCK_LO ' lower clock

PRI Read8(ack) : rbyte

SCK_LO ' lower clock
dira[DATA]~ ' set DATA as input

repeat 8
SCK_HI ' raise clock
rbyte := (rbyte << 1) | ina[DATA] ' accumulate data
SCK_LO ' drop clock

if(ACK)
dira[DATA]~~ ' pull DATA low (ACK)
SCK_HI ' raise clock
SCK_LO ' drop clock
dira[DATA]~ ' set DATA back to input


PRI Send(value) : rbyte | tmp

tmp := value >< 8 ' reverse order of bits
repeat 8
if tmp & 1
dira[DATA]~ ' output a '1' by floating pin and allowing pull-up to pull DATA to a '1'
else
dira[DATA]~~ ' pull data low to output a '0'
SCK_HI ' raise clock
SCK_LO ' lower clock
tmp >>= 1 ' shift byte

dira[DATA]~ ' set DATA to input
SCK_HI ' raise clock
rbyte := ina[DATA] ' read DATA line
SCK_LO ' drop clock

' return state of ACK bit


PRI WaitCompletion | i

dira[DATA]~ ' set DATA as input

repeat i from 1 to 255
if(ina[DATA] == 0)
quit ' exit loop if sensor pulls DATA low
WaitMS(10) ' wait 10msec

if(i == 255)
BusReset ' timed out waiting for completion; reset bus
return FALSE ' signal timeout to caller

return TRUE


PRI StartTransmission

dira[DATA]~ ' set DATA as input (hi-Z)

SCK_HI ' raise clock
dira[DATA]~~ ' pull DATA low
SCK_LO ' lower clock
SCK_HI ' raise clock
dira[DATA]~ ' release DATA (hi-Z)
SCK_LO ' lower clock


'' BusReset
''
'' Reset the SHT15's bus by toggle SCK nine times while DATA is high
''
PRI BusReset
dira[DATA]~ ' set DATA line as input (allowing it to be pulled high by pullup resistor
SCK_LO ' set SCK low

repeat 9
SCK_HI ' raise SCK
SCK_LO ' lower SCK


PRI WaitMS(ms)
waitcnt(clkfreq/1000*ms + cnt) ' wait for specifiec number of milliseconds

Nerd2
05-30-2010, 12:44 AM
I guess my problem is I have a string value of 85.0 I need this converted to a decimal value 85.

Ttailspin
05-30-2010, 09:30 AM
Maybe You Might have to "TRUNC" it.
This comes straight from the Prop Manual.
TRUNC ( FloatConstant )
Returns: Integer that is the given floating-point constant value truncated at the decimal point.
• FloatConstant is the floating-point constant expression to be truncated to an integer.
Explanation
TRUNC is one of three directives (FLOAT, ROUND and TRUNC) used for floating-point constant expressions.
TRUNC returns an integer constant that is the given floating-point constant expression with the fractional portion removed.

Idon't know if this is what you were thinking of, oris this way off base?

Zap-o
05-30-2010, 12:03 PM
Try using this line




pri currenttemp | rawTemp, rawHumidity, tempC, rh, dewC,msd
rawTemp := f.FFloat(sht.readTemperature)
rawtemp := f.round(rawtemp)




That should bring it to a decimal just remember that you cant use the expression floatotformat after that, at least for this variable

Nerd2
06-01-2010, 01:08 AM
I got it working thanks for all of your help here is my code if anyone wants to use it.


rawTemp := f.FFloat(sht.readTemperature)
tempC := celsius(rawTemp)
curtemp := f.FAdd(f.FMul(tempc, 1.8), 32.0)
curtemp := f.fround(curtemp)