- Name:
- readtemp
- Syntax:
-
READTEMP pin,variable
Pin - is the digital I/O pin to which the temperature sensor is attached.
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.
The readtemp 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 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:
Read temperature
Read a DS18B20 temperature sensor on pin C.1 and report the temperature in degrees C
- Code Example:
-
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
Copy Code Submit an Example
Read temperature and convert to degrees F
Read a DS18B20 temperature sensor on pin C.1 and report the temperature in degrees C and degrees F
- Code Example:
-
symbol TempC = b0 symbol TempF = b1 symbol SignC = b2 symbol SignF = b3 Main: readtemp C.1,TempC if TempC > 145 then ; below 0F SignF = "-" TempF = TempC - 128 * 9 - 160 / 5 else if TempC > 128 then ; below 0C but above 0F SignF = "+" TempF = TempC - 128 * 9 TempF = 160 - TempF / 5 else ; above 0C SignF = "+" TempF = TempC * 9 / 5 + 32 end if if TempC > 127 then SignC = "-" ; below 0C TempC = TempC - 128 else SignC = "+" ; above 0C end if sertxd (SignC, #TempC,"C = ") sertxd (SignF, #TempF,"F", cr, lf) 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.