Tutorial Code Using ReadTemp12 & UHF Modules

rbright

Member
Recently I got some assistance from the Forum on getting a pair of UHF radio modules working with a DS18B20 using the ReadTemp12 instruction so I could send temperature readings to a theriotical 4 decimal places. Following is the code that eventuated with a lot of comments including how the DS18B20 is formatted.
DS18B20 info is a summary from the manufacture's datasheet
Obviously the code does not need to be used with UHF Modules but can be used with directly connected 08M PicAxe. The code is provided in 2 parts one for the sensor/transmitter & the other for the receiver
ENJOY


'******************************* UHF_TEMP_TRANSMIT *******************************************************
'Code for Picaxe-08M based upon 434MHz Tx/Rx data units. Ver 1.0 11/05 for Jan.'06 SiChip - THANKS STAN
' & a big thanks to HIPPY for helping with some of my early coding attempts as per earlier post
'
' FOLLOWING IS AN EXTRACT FROM DS18B20 DATA SHEET ON HOW TEMPERATURE DATA IS REPRESENTED
' The following text describes using the DS18B20 configured for 12 bits & will correspond to 0.0625 degree C per bit
' w1 equates to b3 & b2 where b3 = MSB & b2 = LSB Least Significant Byte
' think of w1 as a double byte (8 bits = byte) width 16 bit word commencing with the contents of b3 representing
' bits b15 - b8 or the Most Significant Byte (MSB) followed by contents of b2 representing bits b7 - b0 or LSB
' where b2 LSB = bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
' 2^3 2^2 2^1 2^0 2^-1 2^-2 2^-3 2^-4 where 2^3 = 2 raised to power of 3
' where b3 MSB = bit15 bit14 bit13 bit12 bit11 bit10 bit9 bit8
' S S S S S 2^6 2^5 2^4 where S = sign bits indicate if the
' temperature is pos or neg.
' for pos S = 0 & for neg S = 1
'
' Note in following examples first 5 bits from left in MSB (15-11) are sign bits
' so they don't get used in calculations below.
'
' Positive temperature examples
' Example: +125C = 0000 0111 1101 0000 (bits 10-0)=$07D0 = Decimal 2000 (2000 x 0.0625 degrees per bit = 125 degrees)
' Example: +25.0625C = 0000 0001 1001 0001 (bits 10-0)=$0191 = Decimal 401 (401 x 0.0625 degrees per bit = 25.0625)
' Negative temperature examples
' Example: -0.5C = 1111 1111 1111 1000 (bits 10-0)=$07F8 = Decimal 2040. Subtract from max value of 2048 - 2040 &
' multiply result by 0.0625 (8 x 0.0625 degrees per bit = 0.5 & because of sign bits = 1 value = -0.5 deg C)
' Example: -10.125C = 1111 1111 0101 1110 (bits 10-0)=$075E = Decimal 1886. Subtract from max value of 2048 - 1886 &
' multiply result by 0.0625 (162 x 0.0625 degrees per bit = 10.125 & because of sign bits = 1 value = -10.125
'
'
measure: ' 434MHz UHF transmitter control & DS18B20 temp reading routine
high 2 ' turn on DS18B20 temp sensor for sampling period - connected to I/O pin 2
pause 100 ' settling time pre temp reading
readtemp12 1,w1 ' direct Celsius value returned into 16 bits 0 - 15 as defined in text above

calculate_integer:
w2 = w1 / 16 ' strip out integer part of temperature reading leaving fraction behind

calculate_fraction: 'calc fraction from bits b0-3 in w1
w3 = w1 & %0001 MAX 1 * 0625 ' calc dec value of w1 bit 0
'i.e. if bit 0 = 1 then decimal value = .0625 degrees & store in w3
w3 = w1 & %0010 MAX 1 * 1250 + w3 ' calc dec value of w1 bit 1
'i.e. if bit 1 = 1 then decimal for this bit is 0.1250 so add this to w3
w3 = w1 & %0100 MAX 1 * 2500 + w3 ' calc dec value of w1 bit 2
' i.e. if bit 2 = 1 then decimal for this bit is 0.2500 so add this to w3
w3 = w1 & %1000 MAX 1 * 5000 + w3 ' calc dec value of w1 bit 3
'i.e. if bit 3 = 1 then decimal for this bit is 0.5000 so add this to w3

sertxd("Decimal value of raw data into w1 = ",#w1,CR,LF) ' for local diagnostic purposes - view in terminal mode

sertxd("Calculated Celcius Temp = ",#w2,".") ' send integer part followed by decimal point - display as text

IF w3>=1000 THEN NoLeadZero ' don't send leading zero after decimal point
sertxd("0") ' do if w3 < 1000 'Display as text
NoLeadZero:
sertxd(#w3,CR,LF) 'Display value after decimal point as ascii

serout 4,N300,(85,85,85,85,"ABC",b4,b5,b6,b7) 'Send as binary data with 85's to sync transmitter followed by a header ABC
' 'that will be looked for at the receiving device using the SerIn command
'
pulsout 0,200 ' red LED winks as data sent
low 2 ' turn off DS18B20 temp sensor to save power
sleep 1 ' power down (adjust to suit SLEEP units 2.3 sec) use 26 for 1 min sampling delay
goto measure ' loop to resample DS18B20 temperature & start again

'******************************* END OF - UHF_TEMP_TRANSMIT *********************************************


'******************************* UHF_TEMP_RECEIVE *******************************************************
rx: 'receiving routine
serin 3,N300,("ABC"),b4,b5,b6,b7 'receive raw binary data

pulsout 1,200 'LED winks if valid ABC header received

sertxd("SerIn Temp = ",#W2,".")

sertxd(#w3,CR,LF) 'Display as text

goto rx 'loop to await next data arrival

'******************************* END OF - UHF_TEMP_RECEIVE *******************************************************
 

manuka

Senior Member
Well done ! Thought- can you repost the listing using *code* in square brackets ] to help make the comments more lucid? Stan



Edited by - manuka on 27/05/2007 15:19:50
 
Top