PDA

View Full Version : Negative Numbers/Virtual Ground



scottl
- 7th May 2006, 14:45
I am currently reading an AD595 thermocouple amplifier with a PC12F675 and everything works great! I now want to read below 32F or 0C. I have added a negative power supply to allow the negative readings from the AD595. I have also added a voltage divider to the ground reference to scale all readings by say 55mv +/-. I am using GPIO.0 and GPIO.1 to monitor both then in my code I subtract the virtual ground from the AD595 reading.

How do I handle the following in PBP for when I go below 0?

loop:
for sample = 1 to 20 ' 20 Samples then produce results
ADCON0.1 = 1
notdone:
IF ADCON0.1 = 1 Then notdone
adval.highbyte = ADRESH
adval.lowbyte = ADRESL
samples = samples + adval
next sample
adval = samples / 20

ADCIN 1, virtual_ground

if ((adval - virtual_ground) < 0) then
' Not Sure how to handle this
else
' Not Sure how to handle this
endif

Thanks for any help!

Scott

Darrel Taylor
- 7th May 2006, 21:35
Hi Scott,

If statements always assume that the number is positive. 0-65535 for words. &nbsp; So, &nbsp; "IF X < 0 then" &nbsp; will never return true.

There are several ways to deal with it.

One is to just see if virtual_ground is greater than adval. If it is then the subtraction will create a negative number so reverse the subtraction (virtual_ground - adval).

Another is to just go ahead and do the subtraction then test bit15 to see if it ended up being a negative number or not. If it did, use ABS() to get the absolute value.

1 more, subtract the two numbers and use the SDEC modifier for LCDOUT or SEROUT2 to display the negative number.

If you need to do any multiplication or division with the result, then it can't be negative. Only add and subtract will work with negative numbers.
<br>