Strange Math Problem (Resolved)
I found this code in another thread for Steinhart Hart
It always returns a negative number.
I put the above problem in mathcad(see attachment) and it calculates properly. Using the same numbers in the prop yields an answer of
-9.549217
Edit - Forgot to attach picture
Post Edited (Greg LaPolla) : 4/2/2009 2:54:06 AM GMT
PRI Calc_StHart (A,B,C,Resistance) : Kelvin | B_x_Rlog , C_x_Rlog , Rcubed, Rlog, Addit
G.start ' Start math32
Rlog := G.log(Resistance) ' Take the log of the Resistance
B_x_Rlog := G.FMul(B,Rlog) ' Multiply B to log of R
RCubed := G.FMul(Rlog,Rlog) ' Cube the log of R by R * R * R
RCubed := G.FMul(Rlog,RCubed) ' Finish the Cube
C_x_Rlog := G.FMul(RCubed,C) ' Multiply C to log the cubed log R
Addit := G.Fadd(C_x_Rlog,B_x_Rlog)
Addit := G.Fadd(Addit, A) ' Add all A,B,C together
Kelvin := G.Fdiv(one,addit) ' Kelvin = 1/ all added
G.stop
It always returns a negative number.
I put the above problem in mathcad(see attachment) and it calculates properly. Using the same numbers in the prop yields an answer of
-9.549217
Edit - Forgot to attach picture
Post Edited (Greg LaPolla) : 4/2/2009 2:54:06 AM GMT


Comments
CON Coeff1 = 0.0009461261 ' Steinhart Constant Coeff2 = 0.00022226704 ' Steinhart Constant Coeff3 = 0.000000105745286 ' Steinhart Constant R1 = 10000 repeat ohms := ((4095 * R1) / ADC.GetData(0)) - R1 temp := Calc_StHart(Coeff1,Coeff2,Coeff3,ohms) updateLcd(ohms, temp) waitcnt(clkfreq / 5 + cnt)R1_Float = 10000.0
R1_4095 = 4095.0 * R1_Float
and
ohms := FSub(FDiv(R1_4095,FFloat(ADC.GetData(0))),R1_Float)
Post Edited (Mike Green) : 4/1/2009 11:40:51 PM GMT
Thanks for the heads up!
Greg
PRI Calc_StHart (A,B,C,Resistance) : Fht | B_x_Rlog , C_x_Rlog , Rcubed, Rlog, Addit, R_Float, Kelvin, Celsius G.start ' Start math32 Rlog := G.log(G.FFloat(Resistance)) ' Take the log of the Resistance B_x_Rlog := G.FMul(B,Rlog) ' Multiply B to log of R RCubed := G.FMul(Rlog,Rlog) ' Cube the log of R by R * R * R RCubed := G.FMul(Rlog,RCubed) ' Finish the Cube C_x_Rlog := G.FMul(RCubed,C) ' Multiply C to log the cubed log R Addit := G.Fadd(C_x_Rlog,B_x_Rlog) Addit := G.Fadd(Addit, A) ' Add all A,B,C together Kelvin := G.Fdiv(one,addit) ' Kelvin = 1/ all added Celsius := G.Fsub(Kelvin,273.15) ' Convert to Celsius Fht := G.FMul(Celsius,1.8) ' Convert to Farenheight Fht := G.Fadd(Fht,32) G.stopThe last calculation ( Fht := G.Fadd(Fht,32)) will not run.
Cheers!
Paul Rowntree