ds18b20 code

ewandingwall

New Member
good day all, I am building a temperature sensor alarm and i am having a look at the code in the manual where it states the code for reading temperature is:

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

can anyone help with what if b1 > 127 then negative means? im finding the coding slightly confusing and vague.
many thanks
 

hippy

Technical Support
Staff member
READTEMP returns an 8 bit result; a sign bit and a 7-bit value; %Snnnnnnn.

The sign bit indicates positive (0) or negative (1), so <= 127 is positive, >= 128 is negative.

The value part is 0 to 127 in both cases, so one needs to clear / remove the sign bit without affecting the value. Subtracting 128 ( %10000000 ) removes the sign bit if set -

%10000011 = 131 representing -3
%10000000 = 128 to be subtracted, leaves
%00000011 = 3
 

ewandingwall

New Member
thank you for your help. can you tell me why there is a percentage sign there please? and why is the value part from 0 to 127? thanks again
 

BESQUEUT

Senior Member
thank you for your help. can you tell me why there is a percentage sign there please? and why is the value part from 0 to 127? thanks again
% indicate a binary notation (IE only 0 or 1 are allowed...)
Each sign (0 or 1) is called a bit.
With 7 bits, you can write any number from 0 to 127 (decimal notation)
The 8nd bit is used for sign. His value is 128 (decimal notation)

bits are numbered from right to left.
The first bit (rightmost) value is 0 or 1
2nd bit value is 0 or 2
3nd bit value is 0 or 4
4nd bit value is 0 or 8
5nd bit value is 0 or 16
6nd bit value is 0 or 32
7nd bit value is 0 or 64

Looking as post #2 :
%10000011 can be interpreted as :128 +0+0+0+0+0+2+1=131
or : negative sign + 0+0+0+0+0+2+1=-3
 
Last edited:

ewandingwall

New Member
Thank you besqueut for explaining that, its a while since ive dealt with binary numbers. when writing the code would i have to write my High temperature number as a binary number or couldi just write it as normal number?
 

BESQUEUT

Senior Member
Thank you besqueut for explaining that, its a while since ive dealt with binary numbers. when writing the code would i have to write my High temperature number as a binary number or couldi just write it as normal number?
Posts #2 and #4 are for explanation only.
You can use "normal numbers" as used by Picaxes.
For example, from #1 :
negative: let b1 = b1 - 128

It's exactly the same thing to write :
negative: let b1 = b1 - %10000000

Be aware wen comparing two temperatures, or calculating b2-b1...
 

westaust55

Moderator
For nearly all computers and micocontrollers, numbers are held and used internally as binary numbers.
For the PICAXE the percent symbol is use as a prefix to indicate a binary number. ( some program languages use the letter "b" but for PICAXE this is reserved to indicate a byte variable).
Purely to help us humans understand the numbers earlier, the PICAXE firmware displays the numbers in decimal format (without a prefix).

You can enter values in binary by appending th "%" symbol or in hexadecimal by adding the "$" symbol as the prefix.
Using binary as it he input method can be handy when each bit in the byte/word variable is a flag/control/switch for a different purpose or IO pin.
Likewise hexadecimal notation, when understood, can also allow rapid interpretation of the values.bfor example $F0 is clearer than 240(decimal) at indicating the upper /most significant 4 bits are set.
 
Top