PDA

View Full Version : Help!! Integer Math?



scottl
- 28th June 2006, 23:06
I have been racking my brain for the past two weeks with the following!

I have a PIC12F675 reading an AD595 thermocouple IC. The AD595 provides 10mV/C. In order to read the temp below 0 I have added a voltage divider to the ground pin of the AD595. I have also power the AD595 via a -5V to +5V power supply. I read the output of the AD595 on GPIO.0 and the Virtual Ground voltage via GPIO.1.

To read the temperature above 0C I basically subtract GPIO.1 from GPIO.0. I cannot get the math correct for any thing below 0C or 32F.

Here is some of the code:
'celsius
'farenheit
'adval = GPIO.0
'adval1 = GPIO.1

if adval > adval1 then 'this is correct down to 32F or 0C
celsius = adval - adval1
farenheit = (celsius * 18) / 10
farenheit = farenheit + 32
else
celsius = adval1 - adval 'Here is were my problems start
celsius = celsius - adval
farenheit = (celsius * 18) / 10
farenheit = farenheit + 32
endif

I will handle the sign output later!

Can anyone point me in the right direction with the math?

Thanks in advance,

Scott

Ingvar
- 29th June 2006, 09:14
Try removing the "celsius = celsius - adval" line.

Melanie
- 29th June 2006, 09:21
As I understand it...

Subtracting adval-adval1 gives you positive Celcius.

Therefore adval1-adval would give you negative Celcius. The math is the same. Conversion to Farenheit is more tricky. But stop and think before you go any further...

If you Reference everything to a single numeric value... say a value of 0 in a BYTE (or WORD) variable, equals the lowest Temperature you are likely to encounter (example -100C). Do your (positive or negative math) so you have ONE positive going linear scale, from that lowest Temperature reference point. Then it's a simple matter of calculating C or F using that one variable.
Example.

Say a count of ONE of (adval-adval1) equals 1C positive then.

So...

If adval>adval1 then
TEMP=100+(adval-adval1)
else
TEMP=100-(adval1-adval)
endif

Now we have a single variable TEMP which holds a linear representation of our whole Temperature range. Now from this variable we can calculate C or F easily.

In our above example a count of 100 is 0C. Anything over 100 is positive, and anything lower than 100 is negative.

If TEMP<100 then
TEMPB=100-TEMP
LCDOut "-",#TEMPB," C"
else
TEMPB=TEMP-100
LCDOut #TEMPB, "C"
endif

scottl
- 29th June 2006, 23:58
Hi Melanie,

I have been out of the office today! I was working on this late last night and come up with the following:

if adval > adval1 then
celsius = adval - adval1
farenheit = (celsius * 18)/10
farenheit = farenheit + 32
status = 0
else
celsius = adval1 - adval
farenheit = (celsius * 9)/5
farenheit = 32 - farenheit
status = 1
endif

if (status = 1) & (celsius > 32) then
sign = "-"
else
sign = "+"
endif

I will try your code tonight!

Thanks for your help,

Scott

psdayama
- 2nd July 2006, 14:49
I have been racking my brain for the past two weeks with the following!

I have a PIC12F675 reading an AD595 thermocouple IC. The AD595 provides 10mV/C. In order to read the temp below 0 I have added a voltage divider to the ground pin of the AD595. I have also power the AD595 via a -5V to +5V power supply. I read the output of the AD595 on GPIO.0 and the Virtual Ground voltage via GPIO.1.

To read the temperature above 0C I basically subtract GPIO.1 from GPIO.0. I cannot get the math correct for any thing below 0C or 32F.

I will handle the sign output later!

Can anyone point me in the right direction with the math?

Thanks in advance,

Scott


I saw the AD595 datasheet and for temp below 0 degrees Celecius the
output -ve. As U have stated that that U have added voltage devider to gnd
pin of ad595 but whether the input to PIC is above 0V or not? U must have
output of AD595 at 2.5V or half the reference supply of PIC. Then 0 degrees
would correspond to 128 in 8bit mode. this way may be Ur maths will work if U are gng to add - sign later