PDA

View Full Version : Math for dunce...



fratello
- 25th April 2010, 07:49
I have three value : 25 ; 25.23 ; 26.02
I must do this : If 26.02 - (25/100) > 25.23 then do something...
Since the numbers are not integers, my results are wrongs. Please, a little help. Thanks in advance.

Darrel Taylor
- 25th April 2010, 07:55
Just multiply everything by 100.
Then it becomes ...

If (2602 - 25) > 2523 then do something...
<br>

fratello
- 25th April 2010, 08:23
Thank You, Mr.Darrel !
I have already do this, but...Maybe it's another mistake..So, I put the code.
I read the temperature :

OWOut DQ, 1, [$CC, $44]
OWOut DQ, 1, [$CC, $BE]
OWIn DQ, 2, [temperature.byte0, temperature.byte1]
SignBit = 0
if temperature.15 then
temperature = ~ temperature + 1
SignBit = 1
endif

Dummy = 625 * Temperature
TempC = DIV32 10
TempC = (Temperature & $7FF) >> 4
Float = ((Temperature.Lowbyte & $0F ) * 25 )>>2
Temperature=TempC*100 + Float
If SignBit then
V= 10000 - Temperature ' 25'C=12500 0'C=10000 -10'C=9000
SignBit = 0
else
V= 10000 + Temperature
EndIf

If V >= 10000 then ' Above 0 C.
Temperature=V-10000
else
Temperature=10000-V ' Below 0 C.
EndIf

GoSub SelectSign ' +/blank/- Sign

SelectSign:
If v = 10000 then ' Temperature = 0 C.
Sign=" " ' No sign
Else
If v < 10000 then ' <> 0
Sign="-" ' Temperature below 0 C.
Else
Sign="+" ' Temperature above 0 C.
EndIf
EndIf
Return
At the beginning I set the hysterezis for thermostat :

If Mode=1 then ' Set Hysteresis
LcdReg = %10000000 + 12 ' cursor X
call PrintCtrlLcd
LcdReg = %01000010 ' cursor Y
call PrintCtrlLcd
Char = 21
call PrintChar
Char = 19
call PrintChar
Char = 20
call PrintChar
Char = 15
call PrintChar
Gosub DTemp ' Display Hysteresis
If (PORTB.1=0) Or (PORTB.2=0) then ' Up or down button pushed

If PORTB.2=0 then ' Down button
if hyst > 5 then hyst=hyst-5
If Hyst < 5 then Hyst=5
Gosub Debounce
EndIf
If PORTB.1=0 then ' Up button
hyst=hyst+5
If Hyst > 50 then Hyst=50
Gosub Debounce
EndIf
Gosub DTemp ' Display Hysteresis
EndIf
EndIf
.....
DTemp :
LcdReg = %10100000 + 14 ' cursor X
call PrintCtrlLcd
LcdReg = %01000100 ' cursor Y
call PrintCtrlLcd

Char = (hyst dig 2) + 2
call PrintChar
Char = 14
call PrintChar
Char = (hyst dig 1) + 2
call PrintChar
Char = (hyst DIG 0) + 2
Call PrintChar
Return
and I try to do this :

check:
if temperature > (tref + hyst) then
tref=temperature
duratavar=(temperature-tref)/hyst
duratavar=duratavar*durata
if tipsun=2 then
sound PORTB.6, [varsnd, duratavar]
else
sound PORTB.6, [varsnd, 20]
endIf
endif

if temperature < (tref - hyst) then
tref=temperature
duratavar=(tref-temperature)/hyst
duratavar=duratavar*durata
if tipsun=2 then
sound PORTB.6, [varsnd2, duratavar]
else
sound portb.6, [varsnd2, 10]
endif
endif
Return
Everything works great, but this "if temperature > (tref + hyst) then" return wrong results.
Note : hyst, temperature, tref are var Word.
Hysterezis must be max 0.50 'C, with step of 0.05 'C. On display works fine, but inside the code I think I made mistake...Thank You !

Darrel Taylor
- 25th April 2010, 11:25
Not really sure what all is going on in your program fratello.
So let me just show what I would do.

First ... It takes 750mS from the time you start a conversion till you can read the results.
You can just put a pause after the Convert command [$CC, $44], to wait till it's done.
Or you can Poll the DS18B20's busy bit.

Once you have the reading from the DS18B20, it can be converted like this...
Sign = TempC.15
TempC = ABS(TempC)
TempC = ((TempC >> 4)*100) + ((TempC & $F)*100 >> 4)
IF Sign THEN TempC = -TempC

That gives you a Signed Word with 2 decimal places.
25.00 °C would have the value 2500.
-10.00 °C would be -1000.
It will work with the full temperature range of the sensor -55°C to +125°C.

You can display it on an LCD with this ...
DisplayTemp:
IF TempC.15 THEN LCDOUT "-"
LCDOUT DEC ABS TempC/100,".", DEC2 ABS TempC,13,10
RETURN


To compare the Temp with your hysteresis range, you just subtract the values.
If the value being subtracted is larger than the other one, the result will be negative.
So, just testing for the sign bit tells you if it's above or below the setpoints.

Compare:
Diff = (tref + Hyst) - TempC
Over = Diff.15

Diff = TempC - (tref - Hyst)
Under = Diff.15
RETURN
Over and Under are BIT vars, so you can test them later in the program to activate alarms or whatever.

Keep in mind that with 12-bit resolution, each step of the DS18B20's reading is 0.0625°C.
So incrementing the hyst. 0.05°C won't always give the expected Hyst. range.
It'll be close, but not perfect.

Test program attached.

fratello
- 25th April 2010, 11:37
As always, Mr.Darrel it's one fine Gentleman. Thank You ! I will test and I will post the results !

fratello
- 25th April 2010, 14:51
It's OK, of course !
BTW: Just for fun ; I redefine in my source "hyst" as byte (previous = word) and the results are similares !