Name:
readtemp
Syntax:

READTEMP pin,variable

Pin - is the input pin.

Variable - receives the data byte read.

Description:

Read temperature from a DS18B20 digital temperature sensor and store in variable.

The conversion takes up to 750ms. Readtemp carries out a full 12 bit conversion and then rounds the result to the nearest full degree Celsius (byte value). For the full 12 bit value use the readtemp12 command. The temperature is read back in whole degree steps, and the sensor operates from -55 to +125 degrees Celsius. Note that bit 7 is 0 for positive temperature values and 1 for negative values (ie negative values will appear as 128 + numeric value).

Note the readtemp command does not work directly with the older DS1820 or DS18S20 as they have a different internal resolution. However example 2 below shows a workaround for the DS18S20. The readtemp 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.

This command 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 output, 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 DS18B20 temperature sensor on pin C.1 and report the temperature

Code Example:
main:	readtemp C.1,b1			; read value into b1
	if b1 > 127 then negative	; test for negative
	serout B.7,N2400,(#b1)		; 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)		; transmit value to serial LCD
	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 'readtemp' from a DS18B20 device

Code Example:
	readtemp12 C.1, w0	; read DS18S20 as 12-bit
	if w0 > 255 then	; convert to a reading as if from DS18B20
	  w0 = -w0 / 2 or 128	; when reading is negative
	else
	  w0 = w0 / 2		; when reading is positive
	end if
	b1 = w0			; set as a byte result if required (optional)
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.