Name:
readtemp12
Syntax:

READTEMP12 pin,wordvariable

Pin - is the digital I/O pin to which the temperature sensor is attached.

Variable - receives the raw 12 bit data read.

Description:

Read 12 bit temperature data from a DS18B20 digital temperature sensor and store in a word variable.

The conversion can take up to 750ms. Both readtemp and readtemp12 take the same time to convert. This command is designed for more advanced users, for standard 'whole degree' data use the simpler readtemp command. The readtemp12 temperature is read back as the raw 12 bit data into a word variable (0.0625 degrees Celsius resolution). The user must interpret the data through mathematical manipulation. See the DS18B20 datasheet for more information on the 12 bit Temperature/Data relationship.

 

Note the readtemp12 command does not work directly the same with the older DS1820 or DS18S20 as they have a different internal resolution. However example 2 below shows a workaround for the DS18S20. The readtemp12 command is not designed to be used with parasitically powered DS18B20 sensors (DS18B20-PAR) as the 5V pin of the sensor must always be connected.

The readtemp12 command requires use of a digital I/O pin which is able to be used for both input and output. It cannot be used on the following pins due to silicon restrictions:

08M, 08M2 C.3 = fixed input, C.0 = fixed output
14M, 14M2 C.3 = fixed input, B.0 = fixed output
18M2 C.3 = fixed input, C.4, C.5 = fixed input
20M, 20M2, 20X2 C.6 = fixed input, A.0 = fixed output

Effect of increased clock speed

This command only functions at 4MHz. M2, X1 and X2 parts automatically use the internal 4MHz resonator for this command.

Applies To:
All (except 08, 18, 28)
See Also:
Related Create:
Share:
Print:

Read temperature

Read a 12-bit temperature value from a DS18B20 temperature sensor on pin B.1 and report the result

Code Example:
main:	readtemp12 B.1,w1	; read value into w1
	debug			; transmit to computer screen
	goto main
Copy Code Submit an Example

Read temperature from DS18S20

This code will allow a DS18S20 device to be read as if it were a 'readtemp12' from a DS18B20 device

Code Example:
	readtemp12 B.1, w0	; read DS18S20
	if w0 > 255 then	; convert to a reading as if from DS18B20
	  w0 = -w0 * 8		; when reading is negative
	  w0 = -w0
	else
	  w0 = w0 * 8		; when reading is positive
	end if
Copy Code Submit an Example

Displaying DS18S20 reading on an LCD

Einen 1-wire Temperaturfühler DS18S20 auslesen Den Wert am OLED anzeigen Positive und negative Temperaturen berücksichtigen

Code Example:
;=========================================
; PICAXE 20M2
;=========================================
; Posted by strippenstrolch
;
; Einen 1-wire Temperaturfühler DS18S20 auslesen
; Den Wert am OLED anzeigen
; Positive und negative Temperaturen berücksichtigen
;
; DS18S20 an Port c.7 --- AXE003Y an Port c.5
;=========================================

dirsb=%11111111 			; Port B auf Ausgang setzen
Pause 500 				; Display initialisieren
serout c.5,n2400,(254,1) : Pause 30 	; Display initialisieren
serout c.5,n2400,(254,128) 		; Gehe zu Zeile 1
serout c.5,N2400,("Temp.:")  		; Schreibe das "Gundgerüst" in die Zeile

do 					; Anfang der Endlosschleife
        

readtemp12 c.7, w0        		; read DS18S20
        
w0 = w0/2   				; Schiebe ein bit nach rechts
b2 = w0 				; Sichere das niederwertige Byte 

pinsb = b2 				; Variable b2 komplett am Port B ausgeben

serout c.5,N2400,(254,135) 		; Gehe an Position 7 (128 + 7) in Zeile 1

if w0 > %00000000100000000 then		; Wenn Bit 9 gesetzt ist, ist der Wert negativ
        b3 = 255-b0						; Ziehe das niederwertige Byte von 255 ab
	serout c.5,N2400,("- ",#b3," ","C   ")			; Schreibe dort den Variableninhalt von B3 hin
	serout c.5,N2400,(254,192,"Achtung Frost !")		; Schreibe eine Frostwarnung
else
	serout c.5,N2400,(#b2," ","C   ")			; Schreibe dort den Variableninhalt von b2 hin
	serout c.5,N2400,(254,192,"                        ")	; Lösche die Frostwarnung
endif

loop				 	; Ende der Endlosschleife

End
Copy Code Submit an Example

7-Segment display of Fahrenheit temperature

Uses the DS18B20 to display the range 0.F Fahrenheit to 257.F Fahrenheit. If outside this range displays FFFF

Code Example:
#picaxe 20x2
'              -- --   -- --   -- --   -- --
'B.0-A              | |             | |             
'B.1-B	
'B.2-C              | |             | |
'B.3-D         -- --   -- --           -- --
'B.4-E        |             |       | |     
'B.5-F                                               
'B.6-G        |             |       | |     
'B.7-dp        -- --   -- --         o          
'
'Display         1       2       3       4             
'Common anode   C.3     C.2     C.1     c.0          
		
eeprom 0,(192,249,164,176,153,146,130,248,128,144,255)	; Display (0,1,2,3,4,5,6,7,8,9,blank)

main:
let dirsb = %11111111
let dirsc = %00111111

temp:	readtemp12 DS18B20, w1				; read in result  Ds18b20
                                                 
 	IF W1 > 2000 AND W1 < 65250 THEN temp		; skip if not within valid sensor range   0.F to 257.F
 	IF W1> 65251 THEN				; info 0.F - 32.F
 	  W1=-W1
 	  W1=W1*9/8-320
 	  W1=-W1					; info   ie  w1=10      Display   1.F
 	ELSE						; info 32.F - 257.F
 	  W1 = W1*9/8+320				; info   ie  w1=2570    Display 257.F
 	ENDIF
                                
	let dirsb = %11111111				; restore b pins after read
  	
display:
	
	for w15=1 to 400				; Temperature display time(6secs then updates)

	b0=10
	if w1 < 1000 then dig1				; Display1 zero blanking
	let b0 = w1 dig 3
Dig1:	read b0,b16	 	        
 	let pinsb = b16 : low c.3 : pause 1 : high c.3	; Display1 temp
 	  
	b0=10
	if w1 < 100  then dig2				; Display2 zero blanking
	let b0 = w1 dig 2	                                          
Dig2:	read b0,b17                           
	let pinsb = b17 : low c.2 : pause 1 : high c.2	; Display2 temp
	   
	let b0 = w1 dig 1	                                          
Dig3:	read b0,b18  let b18=b18-128			; Display3 d.p
	let pinsb = b18 : low c.1 : pause 1 : high c.1	; Display3 temp	  
			
Dig4:	let pinsb = 142 : low c.0 : pause 1 : high c.0	; Display4 F

	next w15
	goto temp
Copy Code Submit an Example

Show a readtemp12 to 0.625C resolution

This routine will output a signed temperature read from by a 'readtemp12' command to 0.625C resolution.

Code Example:
do
	readtemp12 C.1, w0
	gosub showsignedtemperature
	sertxd( "C", cr, lf )
	pause 1000
loop

showsignedtemperature:
	if w0 < $8000 then
		sertxd( "+" )
		w2 = w0
	else
		sertxd( "-" )
		w2 = -w0
	end if
	w3 = w2 & 15 * 625
	bintoascii w3, b15,b14,b13,b12,b11
	w3 = w2 / 16
	sertxd( #w3, ".", b14,b13,b12,b11 )
	return
  
Copy Code Submit an Example

Show a readtemp12 to 0.625F resolution

This routine will output a signed temperature read from by a 'readtemp12' command to 0.625F resolution. The 512 is equivalent to 32.0 with a 4-bit fractional.

Code Example:
do
	readtemp12 C.1, w0
	gosub convertcentigradetofahrenheit
	gosub showsignedtemperature
	sertxd( "C", cr, lf )
	pause 1000
loop

convertcentigradetofahrenheit:
	if w0 < $8000 then
		w0 = w0 * 9 / 5 + 512
	else
		w0 = -w0 * 9 / 5
		w0 = -w0 + 512
	end if
	return

showsignedtemperature:
	if w0 < $8000 then
		sertxd( "+" )
		w2 = w0
	else
		sertxd( "-" )
		w2 = -w0
	end if
	w3 = w2 & 15 * 625
	bintoascii w3, b15,b14,b13,b12,b11
	w3 = w2 / 16
	sertxd( #w3, ".", b14,b13,b12,b11 )
	return
Copy Code Submit an Example

Submit Your Own Code!

You must be logged in to submit code examples. Login now.

Comments

  • Login to leave a comment.