calculation problem in picbasic

Status
Not open for further replies.

amindzo

Member
in my program i should calculate this formula:
temperature=(temp*0.648)-(temp*temp*7.2e-4)-4
temp is an 8 bit number that can change from 0 to 255.(the output of the sensor)
for example i should do this:
temp*temp*72/100000
but 100000 is greater than a word and if i want to break it to 1000 and 100 like this:
(temp*temp*72/1000)/100 it won't give the correct answer
because every diviation"/" has a remian and also (temp*temp*72) is gerater than a word.

what should i do?
could you help me with this problem?
i use micro code studio picbasic pro2.41
 
16 bit math routines available

If you have 16 bit multiplication routine (resulting in 32 bit answer) your problem is solved.

For the first part of the equation (temp * 0.648) do a multiplaction (temp * 42467). The maximum value you get is 10829085 what fit into a 24 bit number.

For the second part of the equation (temp*temp*7.2e-4) do a multiplication (temp*temp*47). The maximum value you get is 3056175 what also fit into a 24 bit number.

Then substract both 24 bit numbers.

Then substract 262144 from previous result.

The temperature is located in the upper byte of the 24 bit answer.

42467= 0.648 * 2^16
47= 7.2e-4 * 2^16
262144= 4 * 2^16
 
Rescale your values so as not to require floating point, even if it's available it's slow and inaccurate - a little thought can often remove it's need!.
 
Hi,
could you explain more about this?
i couln't understamd it well.
how can i calculate the whole answer? because the answer is in two word.
how can i calculate it as a 32 bit number in decimal?

and i don't want to use a lookup table and also in picbasic we can multiply two 16 bit number but the answer is in two word and how can i calculate or show the whole number as a 32 bit number in decimal?
 
Last edited:
Nigel Goodwin said:
....it's slow...
Who cares? We're talking temperatures, so it won't change that fast.


Nigel Goodwin said:
....inaccurate...
Says who? I calculated an absolute error ranging from -0.01° to +0.18° for the complete "temp" range from 0 to 255. Isn't that accurate enough? If no use a DSP instead of µP!
 
As I said, scale the numbers - for a simple example, imagine you were doing sums with money? - instead of $123.78 you would use 12378 cents, same value, but an integer and not a float.

You are then stuck with the problem of only 16 bit numbers, you can overcome that by considering how much precision you really NEED. Your calculation is based on an 8 bit value, so the original source is only accurate to three digits (a bit better than 0.5%) - so it's pretty pointless trying to maintain 0.00001% (or whatever?) accuracy in your calculations.

What EXACTLY are you trying to calculate?, it should be possible to simplify the calculation without losing any significant accuracy.
 
First tell us what accuracy you want.
Is an absolute error between -0.01° and +0.18° enough or not?
If yes I need to tell you one more step needed in your calculation.

Do some math in Excel first so that you know what you can expect for the complete range 0 to 255.

You don't need a lookup table for this one, just rewrite you equation so that all data fits into 16 or 32 bit data.
 
Hi,
the error you said is enough.
in your method you said i should subtract two 24 bit numbers.how can i do this in picbasic?
in another stage i should put that answer in this formula for more accuracy:
Temerature(true)=(T0-25)*(t1+t2*temp)+temperature
temperature is the answer of last stage.
temp is the out put of the sendor and T0 is the number between 0 to 120.
temperature(true) is the correct answer.
t1=0.01 and t2=0.00008
what should i do for this?
should i do it as same as your method?
could you help me?

thank you
 
Last edited:
mcs51mc said:
Who cares? We're talking temperatures, so it won't change that fast.


Says who?

Says everyone! - it's using floating point maths that gives you electricity bills like £69.09875634426 - as back in the poor programmer days!.
 
Now I have to join Nigel when he wrote "What EXACTLY are you trying to calculate?"

You gave us a first equation and know you come up with another one? Now I'm

Basicly it's very easy:
first you have a sensor input to the PIC ("temp" in your first post)
then you need some math
finally you have a readout ("temperature" in your first post)

So tell us know:
How many digits after the decimal do you want?
According to you first post "temp" from 0 to 255 and the formula you wrote I expect a sensor temperature from -4.00° to 114.42°. Is this correct?
Why do you think you need another formula to improve accuracy? If the transfer function in your first post is correct you don't need nothing more.
 
i have a temperature and humidity sensor(a digitala one) and humidity in this sensor effect on temperature and with second formula we can reduce this effect.
i want two digit of float.
i also don't know how can i subtract two 24 bit number in picbasic as you said in first formula?
 
Any idea how big the humidity correction can be? Is it worth the effort?
What is T0 apart from a number between 0 and 120? Is it the humidity? In what unit? Bits or % or ...?

Like I already told you do some math simulation in Excel first. Once you know what you want to do start thinking about coding.

Personally I would melt to the formulas into one so that you have True Temperature = f(t sensor and h sensor) and program that formala in one step into the pic. Even if that T0 number is the result of a calculation, bring the formula into the temperature one, not the result. The more results you use, the less acuurate your result will be.

So do some calculations in Excel provide us ONE final formula and don't be shy to also tell us your way to do the math in the pic.

How to substract two 24 bits in picbasic? I don't know I never used pic's, always 8051 based µP!

See you
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…