the simplest of things

davidwf

Senior Member
I am trying to readtemp of a DSB1820 connected to the PICAXE evaluation board and using an 18X using this simple code

main:
readtemp 0,w0
debug w0
pause 500
goto main

....but it doesn't echo anything back to the screen !

I have also tried sending it to the LCD display (serout) but that also does not register anything

I have tried READADC of the same input (with the temp sensor removed !) and connecting the input pin(17) to +5v gives 255, 0V gives 0 so it seems to be working OK but not with the DSB1820, I have tried 3 DSB's 2 of which were brand new .



Ideas please :confused:
 
Last edited:

hippy

Ex-Staff (retired)
Can't see anything wrong so I'd guess it's a hardware problem. You can put SERTXD's in the code to see where it's getting to -

#Picaxe 18X
#Terminal 4800
#No_Data
Pause 2000
Do
SerTxd( "About to read", CR, LF )
ReadTemp 0, w0
SerTxd( "Finished read, w0=", #w0, CR, LF )
Pause 500
Loop

That all runs as expected on my PICAXE AXE90 board.
 

westaust55

Moderator
Do you have a 4.7k Ohm resistor from the DS18B20 data line to Vcc (+5V).

Have a look at the diagram on page 155 of PICAXE Manual 2 (currently rev 6.9)
 

davidwf

Senior Member
Do you have a 4.7k Ohm resistor from the DS18B20 data line to Vcc (+5V).

Have a look at the diagram on page 155 of PICAXE Manual 2 (currently rev 6.9)
Ah good point.....possibly not.....I am using the Rev Ed tutorial starter pack board (AXE050U)....will check this tonight & report back.
Thanks
 

davidwf

Senior Member
Thanks westaust55......the pull up resistor was indeed not present :mad: ....but it is now and of course it works perfectly !
If only all things were that simple......:):):)

Here's a follow on question then....
If I use readtemp (or readtemp12) I get a numeric value returned, is there documented correlation of what this value represents in degrees C ??

Thanks
 
Last edited:

russbow

Senior Member
The value returned is the actual Centigrade value. Be careful though, negative values need to have 128 subtracted ( I think ) to get true value. Do a forum search.

R
 

benryves

Senior Member
readtemp returns the temperature rounded up to the nearest degree Celcius; watch out for negative values, as the PICAXE treats all values as unsigned (so -1 will be represented by 255, -2 by 254 etc). Edit: No they aren't, see westaust55's post. :|

readtemp12 returns the full twelve bits, where each unit of the result represents 0.0625 degrees Celcius - see the datasheet for more information. Effectively, you need to divide by sixteen to get the actual temperature in degrees Celcius.

A PICAXE code snippet that reads the thermometer and sends its reading in ASCII via the serial output pin follows:

Code:
Symbol Thermometer = 0

Do
	; Get the temperature.
	ReadTemp12 Thermometer, W0
	; Is it negative?
	If Bit15 = 0 Then
		SerTxd("+")
	Else
		SerTxd("-")
		W0 = -W0
	End If
	; Display the integer component.
	W1 = W0 / 16
	SerTxd(#W1)
	; Get the fractional component.
	W0 = W0 & $0F * 625
	; Convert to ASCII, then output.
	BinToAscii W0, B2, B3, B4, B5, B6
	SerTxd(".", B3, B4, B5, B6, CR, LF)
Loop
That code will output the temperature in Celcius with a leading - or + sign followed by four decimal places.
 
Last edited:

davidwf

Senior Member
I tried outputting the temp to the lcd display and using a freezer spray am getting some weird results as it warms up - silly figures like -10, -8, +90, +40, 5, 7, 9, etc.
Have added some delays into the lcd but it didn't make any difference...it displays correctly back to the PC though !
ideas ?

serout 7,n2400, (254,1) ' clear display
'pause 50

serout 7,n2400, (254, 128, "Fridge temp is ") ' on top line
'pause 50
main:

ReadTemp12 0, W0 ; Get the temperature & store in w0

If Bit15 = 0 Then ; Is it negative?
'sertxd ("+")
serout 7,N2400, (254, 192, "+") ' on bottom line (192)
'pause 50

Else
'sertxd ("-")
serout 7,N2400, (254, 192, "-")
'pause 50

W0 = -W0

End If

W1 = W0 / 16 ; Display the integer component.

'sertxd (#w1)
serout 7,N2400, (254, 194,#W1) ' on bottom line, 3rd character in (194)
'pause 50

W0 = W0 & $0F * 625 ; Get the fractional component.

BinToAscii W0, B2, B3, B4, B5, B6 ; Convert to ASCII, then output.
'sertxd (".", b3,cr,lf)
serout 7,N2400, (254, 196, ".", B3) ; only need 1st decimal place
pause 50
goto main
 

westaust55

Moderator
@David,

please read the PICAXE manual for the READTEMP command.

russbow is correct and the READTEMP command treats negative numbers as the value with bit 7 set.

so -1 ==> 128 + 1 = 129
-20 ==> 128 + 20 = 148

so with the readtemp command it is not two's complement as with "other" numbers including the READTEMP12 command.

thus in 8-bit temp mode your code needs to check bit 7 (that is if >127)
and if so make the sign "-" and subtract 128 to get the magnitude in degC.
 

westaust55

Moderator
I tried outputting the temp to the lcd display and using a freezer spray am getting some weird results as it warms up - silly figures like -10, -8, +90, +40, 5, 7, 9, etc.

main:

ReadTemp12 0, W0 ; Get the temperature & store in w0

If Bit15 = 0 Then ; Is it negative?
'sertxd ("+")
serout 7,N2400, (254, 192, "+") ' on bottom line (192)
'pause 50

Else
Your test in wrong. Should be:
If Bit15 = 1 Then​

furthermore, for the negative temps you do the 2's compliment and then add 1 for full accuracy.

for example -0.5 is read in as:
1111 1111 1111 1000

65535 - % 1111 1111 1111 1000 ==> 111 (= 7 dec)
but 7/16 is 0.4375

so add the 1 I mentioned and then:
(7 + 1) / 16 ==> 0.5000 which is correct :)

I have previously posted code for this if you wish to search further on the topic.
 
Last edited:

benryves

Senior Member
Sorry about that (my fault). :(
furthermore, for the negative temps you do the 2's compliment and then add 1 for full accuracy.
-1111111111111000 = 0000000000001000, according to Windows Calculator - that appears to be performing one's complement. (-x is 0-x, and 0 in this case would be 65536, not 65535 - otherwise -0 would become 65535). I could be wrong again, though...
 
Top