Negative Number Variable

LouMar

Member
With what I am doing, negative numbers are possible. If I get a negative number, how do I store it in b0, b5, or varG?

readtemp B.0,b0
b5 = b0
(program code that is not listed here allows delay so that b0 is read again later)
readtemp B.0,b0
varP = b0 - b5
 

techElder

Well-known member
You have to do everything possible to NOT have negative numbers.

PICAXE basic is INTEGER basic with numbers ranging from 0 to 65535.
 

LouMar

Member
Tex,
Thank you for your response. I am reading temperature in Centigrade and there is a good chance that a temperature will be less than 0 degrees. I don't think there is a way to get around a less than 0 degree temperature reading. Also, even if b0 and b5 are both positive, if b5 is greater than b0, varP will be negative.
 

westaust55

Moderator
Where negative numbers cannot be avoided then the usual way is to use 2's compliment numbers.
In this case for a byte variable; values 0 to 127 represent positive numbers of the same value and
128 to 255 represents negative numbers where the negative number is represented by 256 - number (or 0 - number).
Note that the negative number bag the most significant bit at (=1)

Thus for example -1 ==> 256 - 1= 255 and -2 ==> 256 - 2 = 254.

However PICAXE does not know these are negative numbers and it is up to you to handle them correctly in your program.
 

AllyCat

Senior Member
Hi,

If, for example, you subtract 1 from zero, the byte value is $FF or %1111 1111 . The PICaxe always interprets that at 255 (decimal), but your program can display it differently, for example:
Code:
   If temperature > 127 then         ; It's really a negative number
     digits = 0 - temperature
     sertxd ("-",#digits, " degrees C")   ; Include a minus sign
   else
          sertxd (#temperature, " degrees C")
   endif
Generally, your program can still add and subtract these "negative" numbers, but multiplication, division and comparisons (IF statements), etc. will NOT work normally.

For temperatures, a useful method is for the program to work with "internal" values referenced to - 40 degrees. For example a temperature of 25 degrees C is stored internally as 65 (so you must add or subtract 40 before inputting or displaying the values). Apart from (usually) keeping all values positive, -40 is the same temperature in degrees C and F so it can be much easier to convert from one to the other.

Cheers, Alan.
 

LouMar

Member
Westaust55,
Thank you for your response. I understand and believe I can manipulate the 3 variables to positive numbers as you suggested in your examples so that PICAXE handles them correctly. However, in order to do so, I at least have to know the value of b0. By using readtemp B.0,b0 --- how will a negative temperature be recorded in b0?
 

LouMar

Member
Texasclodhopper,
Thank you. With the info in your link plus the posts from westaust55 and AllyCat, I think I can solve my problem.
Is the following correct:
When the actual temperature in the space is -5 degrees centigrade, b0 will be 251 (256-5) using readtemp B.0,b0?
 
Last edited:

premelec

Senior Member
From manual 2 P185:

"main:
readtemp C.1,b1 ; read value into b1
if b1 > 127 then neg ; test for negative
serout B.7,N2400,(#b1) ; transmit value to serial LCD
goto loop
neg:
let b1 = b1 - 128 ; adjust neg value
serout B.7,N2400,(“-”) ; transmit negative symbol
serout B.7,N2400,(#b1) ; transmit value to serial LCD
goto main"
 

westaust55

Moderator
From manual 2 P185:
to avoid confusion and errors lets correct this example:
Code:
"main:
readtemp C.1,b1 ; read value into b1
if b1 > 127 then neg ; test for negative
serout B.7,N2400,(#b1) ; transmit value to serial LCD
goto [COLOR="#FF0000"]main[/COLOR]
neg:
let b1 = b1 - 128 ; adjust neg value
serout B.7,N2400,(“-”) ; transmit negative symbol
serout B.7,N2400,(#b1) ; transmit value to serial LCD
goto main"

The on-line commands section has corrected that error:
http://www.picaxe.com/BASIC-Commands/Analogue-InputOutput/readtemp/

Also yes when using the DS18B20 and the 8-bit/integer READTEMP command the PICAXE firmware is doing a little bit of the math for the user to remove the 2's compliment but still using the most significant bit as a negative indicator.
With the READTEMP12 command you need to perform all the math.
 

LouMar

Member
Please understand that I am not very familiar with Picaxe or with programming in general. I do not understand 8-bit/integer, serout, 2's compliment, most significant bit, N2400, and most everything else. I am not trying to print out the temperature or to view it on a screen. All I want to do is compare b0 to b1 where b1 is a previous b0 value (b1=b0) and store the result in varP. Since varP must be positive number, I can do this by varP=100+b0-b1 or varP=1000+b0-b1.

The only thing I need to know is when using readtemp B.0,b0 what is the value of b0 as it is acquired from readtemp when the actual temperature being read is negative? For example, if the actual room temperature is -2, is 254 the value of b0? Please answer yes or no. If the answer is yes, my problem is resolved.
 

AllyCat

Senior Member
Hi Lou,

if you use readtemp -2 Celsius will equal 130
and -3 Celsius will equal 131
If you are ONLY interested in calculating temperature differences, then simply add 128 to ALL the values delivered by READTEMP (but you still need to understand that -1 is represented by 255, etc.) .

However, it would be better if you can learn to understand the significance of data-bits and twos-complement maths, etc..

Cheers, Alan.
 

LouMar

Member
marks,
You said, "if you use readtemp -2 Celsius will equal 130"
Does this mean that when using readtemp B.0,b0 ----- the number stored in b0 is 130?
If the answer is yes then can I make any negative b0 valve equal to 0 by the following?
readtemp B.0,b0
if b0>=128 then
b0=0
end if
This will limit my project to temperatures >= 0 which will be fine.
 
Last edited:

hippy

Technical Support
Staff member
This will get the temperature as a two's complement value ...

ReadTemp B.0, b0
b0 = bit7 * $7F ^ b0 + bit7

+1C = 1
+0C = 0
-1C = 255
-2C = 254
 

westaust55

Moderator
yes that's almost correct
readtemp B.0,b0
if b0 >127 then
b0=0
endif
In an integer math world,
>=128
Is the same as
>127

EDIT:
BUT then I see that LouMar
has edited his post 4 minutes after Marks post making Marks post look superfluous. :(
 
Last edited:

westaust55

Moderator
@LouMar,
Are you only trying to establish the magnitude of the temperature change or will you also seek to know whether the temperature is rising or falling in your calculations? :confused:
That will have a bearing on the calculations that you need to perform with the PICAXE.
 

LouMar

Member
Originally I wanted to establish the magnitude of the temperature change to know whether the temperature was rising or falling for negative and positive temperatures. I have changed my parameters such that all negative temperatures can be changed to 0C and my project will still work.
 

marks

Senior Member
Hi LouMar,
I don't use readtemp much ,so you got me there lol.
after later thinking about it ,your original code would have also been correct!

readtemp B.0,b0
if b0 >128 then
b0=0
endif

well interestingly in the maths 128 would also be 0
value 128 (bit7) only indicates negative

the highest Celsius value from our ds18b20 is 127
or using readtemp12 2047/16 equals 127.9375
 
Top